summaryrefslogtreecommitdiffstats
path: root/routers/private/repository.go
blob: 9f451bcf1dbb057744502931ae73d95be48a7491 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2018 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package private

import (
	"net/http"

	"code.gitea.io/gitea/models"

	macaron "gopkg.in/macaron.v1"
)

// GetRepository return the default branch of a repository
func GetRepository(ctx *macaron.Context) {
	repoID := ctx.ParamsInt64(":rid")
	repository, err := models.GetRepositoryByID(repoID)
	repository.MustOwnerName()
	allowPulls := repository.AllowsPulls()
	// put it back to nil because json unmarshal can't unmarshal it
	repository.Units = nil

	if err != nil {
		ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
			"err": err.Error(),
		})
		return
	}

	if repository.IsFork {
		repository.GetBaseRepo()
		if err != nil {
			ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
				"err": err.Error(),
			})
			return
		}
		repository.BaseRepo.MustOwnerName()
		allowPulls = repository.BaseRepo.AllowsPulls()
		// put it back to nil because json unmarshal can't unmarshal it
		repository.BaseRepo.Units = nil
	}

	ctx.JSON(http.StatusOK, struct {
		Repository       *models.Repository
		AllowPullRequest bool
	}{
		Repository:       repository,
		AllowPullRequest: allowPulls,
	})
}

// GetActivePullRequest return an active pull request when it exists or an empty object
func GetActivePullRequest(ctx *macaron.Context) {
	baseRepoID := ctx.QueryInt64("baseRepoID")
	headRepoID := ctx.QueryInt64("headRepoID")
	baseBranch := ctx.QueryTrim("baseBranch")
	if len(baseBranch) == 0 {
		ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
			"err": "QueryTrim failed",
		})
		return
	}

	headBranch := ctx.QueryTrim("headBranch")
	if len(headBranch) == 0 {
		ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
			"err": "QueryTrim failed",
		})
		return
	}

	pr, err := models.GetUnmergedPullRequest(headRepoID, baseRepoID, headBranch, baseBranch)
	if err != nil && !models.IsErrPullRequestNotExist(err) {
		ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
			"err": err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, pr)
}