|
| 1 | +// Copyright 2018 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package private |
| 6 | + |
| 7 | +import ( |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/models" |
| 12 | + |
| 13 | + macaron "gopkg.in/macaron.v1" |
| 14 | +) |
| 15 | + |
| 16 | +// GetRepository return the default branch of a repository |
| 17 | +func GetRepository(ctx *macaron.Context) { |
| 18 | + repoID := ctx.ParamsInt64(":rid") |
| 19 | + repository, err := models.GetRepositoryByID(repoID) |
| 20 | + repository.MustOwnerName() |
| 21 | + allowPulls := repository.AllowsPulls() |
| 22 | + // put it back to nil because json unmarshal can't unmarshal it |
| 23 | + repository.Units = nil |
| 24 | + |
| 25 | + if err != nil { |
| 26 | + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ |
| 27 | + "err": err.Error(), |
| 28 | + }) |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + if repository.IsFork { |
| 33 | + repository.GetBaseRepo() |
| 34 | + if err != nil { |
| 35 | + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ |
| 36 | + "err": err.Error(), |
| 37 | + }) |
| 38 | + return |
| 39 | + } |
| 40 | + repository.BaseRepo.MustOwnerName() |
| 41 | + allowPulls = repository.BaseRepo.AllowsPulls() |
| 42 | + // put it back to nil because json unmarshal can't unmarshal it |
| 43 | + repository.BaseRepo.Units = nil |
| 44 | + } |
| 45 | + |
| 46 | + ctx.JSON(http.StatusOK, struct { |
| 47 | + Repository *models.Repository |
| 48 | + AllowPullRequest bool |
| 49 | + }{ |
| 50 | + Repository: repository, |
| 51 | + AllowPullRequest: allowPulls, |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +// GetActivePullRequest return an active pull request when it exists or an empty object |
| 56 | +func GetActivePullRequest(ctx *macaron.Context) { |
| 57 | + baseRepoID := ctx.QueryInt64("baseRepoID") |
| 58 | + headRepoID := ctx.QueryInt64("headRepoID") |
| 59 | + baseBranch, err := url.QueryUnescape(ctx.QueryTrim("baseBranch")) |
| 60 | + if err != nil { |
| 61 | + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ |
| 62 | + "err": err.Error(), |
| 63 | + }) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + headBranch, err := url.QueryUnescape(ctx.QueryTrim("headBranch")) |
| 68 | + if err != nil { |
| 69 | + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ |
| 70 | + "err": err.Error(), |
| 71 | + }) |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + pr, err := models.GetUnmergedPullRequest(headRepoID, baseRepoID, headBranch, baseBranch) |
| 76 | + if err != nil && !models.IsErrPullRequestNotExist(err) { |
| 77 | + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ |
| 78 | + "err": err.Error(), |
| 79 | + }) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + ctx.JSON(http.StatusOK, pr) |
| 84 | +} |
0 commit comments