diff options
author | Julien Tant <julien@craftyx.fr> | 2018-10-20 08:59:06 +0200 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2018-10-20 09:59:06 +0300 |
commit | dea3d849e1e21ccda6f272591cadcb6d1d338b56 (patch) | |
tree | 14302df694d167582f19fa0d0a1ae5a65cfbab98 /routers/private | |
parent | cabdf84f1f5e55e8b449bbd3b4d4ba0c69a87c87 (diff) | |
download | gitea-dea3d849e1e21ccda6f272591cadcb6d1d338b56.tar.gz gitea-dea3d849e1e21ccda6f272591cadcb6d1d338b56.zip |
Give user a link to create PR after push (#4716)
* Give user a link to create PR after push
* Forks now create PR in the base repository + make sure PR creation is allowed
* fix code style
Diffstat (limited to 'routers/private')
-rw-r--r-- | routers/private/internal.go | 2 | ||||
-rw-r--r-- | routers/private/repository.go | 84 |
2 files changed, 86 insertions, 0 deletions
diff --git a/routers/private/internal.go b/routers/private/internal.go index b69411dd06..96021d8feb 100644 --- a/routers/private/internal.go +++ b/routers/private/internal.go @@ -44,5 +44,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/push/update", PushUpdate) m.Get("/protectedbranch/:pbid/:userid", CanUserPush) m.Get("/branch/:id/*", GetProtectedBranchBy) + m.Get("/repository/:rid", GetRepository) + m.Get("/active-pull-request", GetActivePullRequest) }, CheckInternalToken) } diff --git a/routers/private/repository.go b/routers/private/repository.go new file mode 100644 index 0000000000..0769e1f250 --- /dev/null +++ b/routers/private/repository.go @@ -0,0 +1,84 @@ +// 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" + "net/url" + + "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, err := url.QueryUnescape(ctx.QueryTrim("baseBranch")) + if err != nil { + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ + "err": err.Error(), + }) + return + } + + headBranch, err := url.QueryUnescape(ctx.QueryTrim("headBranch")) + if err != nil { + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ + "err": err.Error(), + }) + 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) +} |