2016-12-02 06:10:39 -05:00
|
|
|
// Copyright 2016 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 repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-07-17 17:23:58 -04:00
|
|
|
"net/http"
|
2016-12-02 06:10:39 -05:00
|
|
|
"strings"
|
2019-11-03 09:46:32 -05:00
|
|
|
"time"
|
2016-12-02 06:10:39 -05:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2018-01-05 13:56:50 -05:00
|
|
|
"code.gitea.io/gitea/modules/auth"
|
2016-12-02 06:10:39 -05:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2019-03-27 05:33:00 -04:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2016-12-02 06:10:39 -05:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2018-10-18 07:23:05 -04:00
|
|
|
"code.gitea.io/gitea/modules/notification"
|
2019-05-11 06:21:34 -04:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2019-08-15 10:46:21 -04:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2019-10-25 10:46:37 -04:00
|
|
|
issue_service "code.gitea.io/gitea/services/issue"
|
2019-09-26 20:22:36 -04:00
|
|
|
pull_service "code.gitea.io/gitea/services/pull"
|
2016-12-02 06:10:39 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ListPullRequests returns a list of all PRs
|
|
|
|
func ListPullRequests(ctx *context.APIContext, form api.ListPullRequestsOptions) {
|
2017-11-13 02:02:25 -05:00
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/pulls repository repoListPullRequests
|
|
|
|
// ---
|
|
|
|
// summary: List a repo's pull requests
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
2018-10-20 23:40:42 -04:00
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: Page number
|
|
|
|
// type: integer
|
|
|
|
// - name: state
|
|
|
|
// in: query
|
|
|
|
// description: "State of pull request: open or closed (optional)"
|
|
|
|
// type: string
|
|
|
|
// enum: [closed, open, all]
|
|
|
|
// - name: sort
|
|
|
|
// in: query
|
|
|
|
// description: "Type of sort"
|
|
|
|
// type: string
|
|
|
|
// enum: [oldest, recentupdate, leastupdate, mostcomment, leastcomment, priority]
|
|
|
|
// - name: milestone
|
|
|
|
// in: query
|
|
|
|
// description: "ID of the milestone"
|
|
|
|
// type: integer
|
|
|
|
// format: int64
|
|
|
|
// - name: labels
|
|
|
|
// in: query
|
|
|
|
// description: "Label IDs"
|
|
|
|
// type: array
|
|
|
|
// collectionFormat: multi
|
|
|
|
// items:
|
|
|
|
// type: integer
|
|
|
|
// format: int64
|
2017-11-13 02:02:25 -05:00
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/PullRequestList"
|
2016-12-02 06:10:39 -05:00
|
|
|
prs, maxResults, err := models.PullRequests(ctx.Repo.Repository.ID, &models.PullRequestsOptions{
|
|
|
|
Page: ctx.QueryInt("page"),
|
|
|
|
State: ctx.QueryTrim("state"),
|
|
|
|
SortType: ctx.QueryTrim("sort"),
|
|
|
|
Labels: ctx.QueryStrings("labels"),
|
|
|
|
MilestoneID: ctx.QueryInt64("milestone"),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(500, "PullRequests", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
apiPrs := make([]*api.PullRequest, len(prs))
|
|
|
|
for i := range prs {
|
2017-11-04 14:10:01 -04:00
|
|
|
if err = prs[i].LoadIssue(); err != nil {
|
|
|
|
ctx.Error(500, "LoadIssue", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = prs[i].LoadAttributes(); err != nil {
|
|
|
|
ctx.Error(500, "LoadAttributes", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = prs[i].GetBaseRepo(); err != nil {
|
|
|
|
ctx.Error(500, "GetBaseRepo", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = prs[i].GetHeadRepo(); err != nil {
|
|
|
|
ctx.Error(500, "GetHeadRepo", err)
|
|
|
|
return
|
|
|
|
}
|
2016-12-02 06:10:39 -05:00
|
|
|
apiPrs[i] = prs[i].APIFormat()
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.SetLinkHeader(int(maxResults), models.ItemsPerPage)
|
|
|
|
ctx.JSON(200, &apiPrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPullRequest returns a single PR based on index
|
|
|
|
func GetPullRequest(ctx *context.APIContext) {
|
2017-11-13 02:02:25 -05:00
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/pulls/{index} repository repoGetPullRequest
|
|
|
|
// ---
|
|
|
|
// summary: Get a pull request
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: index
|
|
|
|
// in: path
|
|
|
|
// description: index of the pull request to get
|
|
|
|
// type: integer
|
2018-10-20 23:40:42 -04:00
|
|
|
// format: int64
|
2017-11-13 02:02:25 -05:00
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/PullRequest"
|
2016-12-02 06:10:39 -05:00
|
|
|
pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrPullRequestNotExist(err) {
|
2019-03-18 22:29:43 -04:00
|
|
|
ctx.NotFound()
|
2016-12-02 06:10:39 -05:00
|
|
|
} else {
|
|
|
|
ctx.Error(500, "GetPullRequestByIndex", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-04 14:10:01 -04:00
|
|
|
if err = pr.GetBaseRepo(); err != nil {
|
|
|
|
ctx.Error(500, "GetBaseRepo", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = pr.GetHeadRepo(); err != nil {
|
|
|
|
ctx.Error(500, "GetHeadRepo", err)
|
|
|
|
return
|
|
|
|
}
|
2016-12-02 06:10:39 -05:00
|
|
|
ctx.JSON(200, pr.APIFormat())
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreatePullRequest does what it says
|
|
|
|
func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption) {
|
2017-11-13 02:02:25 -05:00
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/pulls repository repoCreatePullRequest
|
|
|
|
// ---
|
|
|
|
// summary: Create a pull request
|
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: body
|
|
|
|
// in: body
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/CreatePullRequestOption"
|
|
|
|
// responses:
|
|
|
|
// "201":
|
|
|
|
// "$ref": "#/responses/PullRequest"
|
2016-12-02 06:10:39 -05:00
|
|
|
var (
|
|
|
|
repo = ctx.Repo.Repository
|
|
|
|
labelIDs []int64
|
|
|
|
assigneeID int64
|
|
|
|
milestoneID int64
|
|
|
|
)
|
|
|
|
|
|
|
|
// Get repo/branch information
|
2019-10-18 07:13:31 -04:00
|
|
|
_, headRepo, headGitRepo, compareInfo, baseBranch, headBranch := parseCompareInfo(ctx, form)
|
2016-12-02 06:10:39 -05:00
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2019-11-13 02:01:19 -05:00
|
|
|
defer headGitRepo.Close()
|
2016-12-02 06:10:39 -05:00
|
|
|
|
|
|
|
// Check if another PR exists with the same targets
|
|
|
|
existingPr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
|
|
|
|
if err != nil {
|
|
|
|
if !models.IsErrPullRequestNotExist(err) {
|
|
|
|
ctx.Error(500, "GetUnmergedPullRequest", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = models.ErrPullRequestAlreadyExists{
|
|
|
|
ID: existingPr.ID,
|
|
|
|
IssueID: existingPr.Index,
|
|
|
|
HeadRepoID: existingPr.HeadRepoID,
|
|
|
|
BaseRepoID: existingPr.BaseRepoID,
|
|
|
|
HeadBranch: existingPr.HeadBranch,
|
|
|
|
BaseBranch: existingPr.BaseBranch,
|
|
|
|
}
|
|
|
|
ctx.Error(409, "GetUnmergedPullRequest", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(form.Labels) > 0 {
|
|
|
|
labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(500, "GetLabelsInRepoByIDs", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
labelIDs = make([]int64, len(labels))
|
|
|
|
for i := range labels {
|
|
|
|
labelIDs[i] = labels[i].ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.Milestone > 0 {
|
|
|
|
milestone, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, milestoneID)
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrMilestoneNotExist(err) {
|
2019-03-18 22:29:43 -04:00
|
|
|
ctx.NotFound()
|
2016-12-02 06:10:39 -05:00
|
|
|
} else {
|
|
|
|
ctx.Error(500, "GetMilestoneByRepoID", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
milestoneID = milestone.ID
|
|
|
|
}
|
|
|
|
|
2019-06-07 16:29:29 -04:00
|
|
|
patch, err := headGitRepo.GetPatch(compareInfo.MergeBase, headBranch)
|
2016-12-02 06:10:39 -05:00
|
|
|
if err != nil {
|
|
|
|
ctx.Error(500, "GetPatch", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-15 10:46:21 -04:00
|
|
|
var deadlineUnix timeutil.TimeStamp
|
2018-05-01 15:05:28 -04:00
|
|
|
if form.Deadline != nil {
|
2019-08-15 10:46:21 -04:00
|
|
|
deadlineUnix = timeutil.TimeStamp(form.Deadline.Unix())
|
2018-05-01 15:05:28 -04:00
|
|
|
}
|
|
|
|
|
2016-12-02 06:10:39 -05:00
|
|
|
prIssue := &models.Issue{
|
2018-05-01 15:05:28 -04:00
|
|
|
RepoID: repo.ID,
|
|
|
|
Title: form.Title,
|
|
|
|
PosterID: ctx.User.ID,
|
|
|
|
Poster: ctx.User,
|
|
|
|
MilestoneID: milestoneID,
|
|
|
|
AssigneeID: assigneeID,
|
|
|
|
IsPull: true,
|
|
|
|
Content: form.Body,
|
|
|
|
DeadlineUnix: deadlineUnix,
|
2016-12-02 06:10:39 -05:00
|
|
|
}
|
|
|
|
pr := &models.PullRequest{
|
2019-10-18 07:13:31 -04:00
|
|
|
HeadRepoID: headRepo.ID,
|
|
|
|
BaseRepoID: repo.ID,
|
|
|
|
HeadBranch: headBranch,
|
|
|
|
BaseBranch: baseBranch,
|
|
|
|
HeadRepo: headRepo,
|
|
|
|
BaseRepo: repo,
|
|
|
|
MergeBase: compareInfo.MergeBase,
|
|
|
|
Type: models.PullRequestGitea,
|
2016-12-02 06:10:39 -05:00
|
|
|
}
|
|
|
|
|
2018-05-09 12:29:04 -04:00
|
|
|
// Get all assignee IDs
|
|
|
|
assigneeIDs, err := models.MakeIDsFromAPIAssigneesToAdd(form.Assignee, form.Assignees)
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrUserNotExist(err) {
|
|
|
|
ctx.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err))
|
|
|
|
} else {
|
|
|
|
ctx.Error(500, "AddAssigneeByName", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-10-25 10:46:37 -04:00
|
|
|
// Check if the passed assignees is assignable
|
|
|
|
for _, aID := range assigneeIDs {
|
|
|
|
assignee, err := models.GetUserByID(aID)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(500, "GetUserByID", err)
|
|
|
|
return
|
|
|
|
}
|
2018-05-09 12:29:04 -04:00
|
|
|
|
2019-10-25 10:46:37 -04:00
|
|
|
valid, err := models.CanBeAssigned(assignee, repo, true)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(500, "canBeAssigned", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !valid {
|
|
|
|
ctx.Error(422, "canBeAssigned", models.ErrUserDoesNotHaveAccessToRepo{UserID: aID, RepoName: repo.Name})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 12:45:43 -04:00
|
|
|
if err := pull_service.NewPullRequest(repo, prIssue, labelIDs, []string{}, pr, patch, assigneeIDs); err != nil {
|
2018-05-09 12:29:04 -04:00
|
|
|
if models.IsErrUserDoesNotHaveAccessToRepo(err) {
|
|
|
|
ctx.Error(400, "UserDoesNotHaveAccessToRepo", err)
|
|
|
|
return
|
|
|
|
}
|
2016-12-02 06:10:39 -05:00
|
|
|
ctx.Error(500, "NewPullRequest", err)
|
|
|
|
return
|
|
|
|
} else if err := pr.PushToBaseRepo(); err != nil {
|
|
|
|
ctx.Error(500, "PushToBaseRepo", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:23:05 -04:00
|
|
|
notification.NotifyNewPullRequest(pr)
|
|
|
|
|
2016-12-02 06:10:39 -05:00
|
|
|
log.Trace("Pull request created: %d/%d", repo.ID, prIssue.ID)
|
|
|
|
ctx.JSON(201, pr.APIFormat())
|
|
|
|
}
|
|
|
|
|
|
|
|
// EditPullRequest does what it says
|
|
|
|
func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
|
2017-11-13 02:02:25 -05:00
|
|
|
// swagger:operation PATCH /repos/{owner}/{repo}/pulls/{index} repository repoEditPullRequest
|
|
|
|
// ---
|
2019-11-03 09:46:32 -05:00
|
|
|
// summary: Update a pull request. If using deadline only the date will be taken into account, and time of day ignored.
|
2017-11-13 02:02:25 -05:00
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: index
|
|
|
|
// in: path
|
|
|
|
// description: index of the pull request to edit
|
|
|
|
// type: integer
|
2018-10-20 23:40:42 -04:00
|
|
|
// format: int64
|
2017-11-13 02:02:25 -05:00
|
|
|
// required: true
|
|
|
|
// - name: body
|
|
|
|
// in: body
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/EditPullRequestOption"
|
|
|
|
// responses:
|
|
|
|
// "201":
|
|
|
|
// "$ref": "#/responses/PullRequest"
|
2016-12-02 06:10:39 -05:00
|
|
|
pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrPullRequestNotExist(err) {
|
2019-03-18 22:29:43 -04:00
|
|
|
ctx.NotFound()
|
2016-12-02 06:10:39 -05:00
|
|
|
} else {
|
|
|
|
ctx.Error(500, "GetPullRequestByIndex", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-12 15:41:28 -04:00
|
|
|
err = pr.LoadIssue()
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "LoadIssue", err)
|
|
|
|
return
|
|
|
|
}
|
2016-12-02 06:10:39 -05:00
|
|
|
issue := pr.Issue
|
2018-12-13 10:55:43 -05:00
|
|
|
issue.Repo = ctx.Repo.Repository
|
2016-12-02 06:10:39 -05:00
|
|
|
|
2018-11-28 06:26:14 -05:00
|
|
|
if !issue.IsPoster(ctx.User.ID) && !ctx.Repo.CanWrite(models.UnitTypePullRequests) {
|
2016-12-02 06:10:39 -05:00
|
|
|
ctx.Status(403)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(form.Title) > 0 {
|
|
|
|
issue.Title = form.Title
|
|
|
|
}
|
|
|
|
if len(form.Body) > 0 {
|
|
|
|
issue.Content = form.Body
|
|
|
|
}
|
|
|
|
|
2019-11-03 09:46:32 -05:00
|
|
|
// Update or remove deadline if set
|
|
|
|
if form.Deadline != nil || form.RemoveDeadline != nil {
|
|
|
|
var deadlineUnix timeutil.TimeStamp
|
|
|
|
if (form.RemoveDeadline == nil || !*form.RemoveDeadline) && !form.Deadline.IsZero() {
|
|
|
|
deadline := time.Date(form.Deadline.Year(), form.Deadline.Month(), form.Deadline.Day(),
|
|
|
|
23, 59, 59, 0, form.Deadline.Location())
|
|
|
|
deadlineUnix = timeutil.TimeStamp(deadline.Unix())
|
|
|
|
}
|
|
|
|
|
2019-10-27 19:35:20 -04:00
|
|
|
if err := models.UpdateIssueDeadline(issue, deadlineUnix, ctx.User); err != nil {
|
|
|
|
ctx.Error(500, "UpdateIssueDeadline", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
issue.DeadlineUnix = deadlineUnix
|
2018-05-01 15:05:28 -04:00
|
|
|
}
|
|
|
|
|
2018-05-09 12:29:04 -04:00
|
|
|
// Add/delete assignees
|
2016-12-02 06:10:39 -05:00
|
|
|
|
2019-03-09 16:15:45 -05:00
|
|
|
// Deleting is done the GitHub way (quote from their api documentation):
|
2018-05-09 12:29:04 -04:00
|
|
|
// https://developer.github.com/v3/issues/#edit-an-issue
|
|
|
|
// "assignees" (array): Logins for Users to assign to this issue.
|
|
|
|
// Pass one or more user logins to replace the set of assignees on this Issue.
|
|
|
|
// Send an empty array ([]) to clear all assignees from the Issue.
|
|
|
|
|
2018-11-28 06:26:14 -05:00
|
|
|
if ctx.Repo.CanWrite(models.UnitTypePullRequests) && (form.Assignees != nil || len(form.Assignee) > 0) {
|
2019-10-25 10:46:37 -04:00
|
|
|
err = issue_service.UpdateAssignees(issue, form.Assignee, form.Assignees, ctx.User)
|
2018-05-09 12:29:04 -04:00
|
|
|
if err != nil {
|
|
|
|
if models.IsErrUserNotExist(err) {
|
|
|
|
ctx.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err))
|
|
|
|
} else {
|
2019-10-25 10:46:37 -04:00
|
|
|
ctx.Error(500, "UpdateAssignees", err)
|
2018-05-09 12:29:04 -04:00
|
|
|
}
|
2016-12-02 06:10:39 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-05-09 12:29:04 -04:00
|
|
|
|
2018-11-28 06:26:14 -05:00
|
|
|
if ctx.Repo.CanWrite(models.UnitTypePullRequests) && form.Milestone != 0 &&
|
2016-12-02 06:10:39 -05:00
|
|
|
issue.MilestoneID != form.Milestone {
|
|
|
|
oldMilestoneID := issue.MilestoneID
|
|
|
|
issue.MilestoneID = form.Milestone
|
2019-11-01 23:33:20 -04:00
|
|
|
if err = issue_service.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
|
2016-12-02 06:10:39 -05:00
|
|
|
ctx.Error(500, "ChangeMilestoneAssign", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-01 01:05:57 -05:00
|
|
|
if ctx.Repo.CanWrite(models.UnitTypePullRequests) && form.Labels != nil {
|
2018-11-16 06:12:44 -05:00
|
|
|
labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(500, "GetLabelsInRepoByIDsError", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = issue.ReplaceLabels(labels, ctx.User); err != nil {
|
|
|
|
ctx.Error(500, "ReplaceLabelsError", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-02 06:10:39 -05:00
|
|
|
if err = models.UpdateIssue(issue); err != nil {
|
|
|
|
ctx.Error(500, "UpdateIssue", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if form.State != nil {
|
2019-10-28 01:26:46 -04:00
|
|
|
if err = issue_service.ChangeStatus(issue, ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
|
2018-07-17 17:23:58 -04:00
|
|
|
if models.IsErrDependenciesLeft(err) {
|
|
|
|
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this pull request because it still has open dependencies")
|
|
|
|
return
|
|
|
|
}
|
2016-12-02 06:10:39 -05:00
|
|
|
ctx.Error(500, "ChangeStatus", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Refetch from database
|
|
|
|
pr, err = models.GetPullRequestByIndex(ctx.Repo.Repository.ID, pr.Index)
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrPullRequestNotExist(err) {
|
2019-03-18 22:29:43 -04:00
|
|
|
ctx.NotFound()
|
2016-12-02 06:10:39 -05:00
|
|
|
} else {
|
|
|
|
ctx.Error(500, "GetPullRequestByIndex", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-13 02:02:25 -05:00
|
|
|
// TODO this should be 200, not 201
|
2016-12-02 06:10:39 -05:00
|
|
|
ctx.JSON(201, pr.APIFormat())
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsPullRequestMerged checks if a PR exists given an index
|
|
|
|
func IsPullRequestMerged(ctx *context.APIContext) {
|
2017-11-13 02:02:25 -05:00
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/pulls/{index}/merge repository repoPullRequestIsMerged
|
|
|
|
// ---
|
|
|
|
// summary: Check if a pull request has been merged
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: index
|
|
|
|
// in: path
|
|
|
|
// description: index of the pull request
|
|
|
|
// type: integer
|
2018-10-20 23:40:42 -04:00
|
|
|
// format: int64
|
2017-11-13 02:02:25 -05:00
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// description: pull request has been merged
|
|
|
|
// "404":
|
|
|
|
// description: pull request has not been merged
|
2016-12-02 06:10:39 -05:00
|
|
|
pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrPullRequestNotExist(err) {
|
2019-03-18 22:29:43 -04:00
|
|
|
ctx.NotFound()
|
2016-12-02 06:10:39 -05:00
|
|
|
} else {
|
|
|
|
ctx.Error(500, "GetPullRequestByIndex", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if pr.HasMerged {
|
|
|
|
ctx.Status(204)
|
|
|
|
}
|
2019-03-18 22:29:43 -04:00
|
|
|
ctx.NotFound()
|
2016-12-02 06:10:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// MergePullRequest merges a PR given an index
|
2018-01-05 13:56:50 -05:00
|
|
|
func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
|
2017-11-13 02:02:25 -05:00
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/pulls/{index}/merge repository repoMergePullRequest
|
|
|
|
// ---
|
|
|
|
// summary: Merge a pull request
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: index
|
|
|
|
// in: path
|
|
|
|
// description: index of the pull request to merge
|
|
|
|
// type: integer
|
2018-10-20 23:40:42 -04:00
|
|
|
// format: int64
|
2017-11-13 02:02:25 -05:00
|
|
|
// required: true
|
2019-02-08 03:08:38 -05:00
|
|
|
// - name: body
|
|
|
|
// in: body
|
|
|
|
// schema:
|
|
|
|
// $ref: "#/definitions/MergePullRequestOption"
|
2017-11-13 02:02:25 -05:00
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/empty"
|
|
|
|
// "405":
|
|
|
|
// "$ref": "#/responses/empty"
|
2016-12-02 06:10:39 -05:00
|
|
|
pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrPullRequestNotExist(err) {
|
2018-01-10 16:34:17 -05:00
|
|
|
ctx.NotFound("GetPullRequestByIndex", err)
|
2016-12-02 06:10:39 -05:00
|
|
|
} else {
|
|
|
|
ctx.Error(500, "GetPullRequestByIndex", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = pr.GetHeadRepo(); err != nil {
|
2018-01-10 16:34:17 -05:00
|
|
|
ctx.ServerError("GetHeadRepo", err)
|
2016-12-02 06:10:39 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-12 15:41:28 -04:00
|
|
|
err = pr.LoadIssue()
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "LoadIssue", err)
|
|
|
|
return
|
|
|
|
}
|
2016-12-02 06:10:39 -05:00
|
|
|
pr.Issue.Repo = ctx.Repo.Repository
|
|
|
|
|
|
|
|
if ctx.IsSigned {
|
|
|
|
// Update issue-user.
|
|
|
|
if err = pr.Issue.ReadBy(ctx.User.ID); err != nil {
|
|
|
|
ctx.Error(500, "ReadBy", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if pr.Issue.IsClosed {
|
2019-03-18 22:29:43 -04:00
|
|
|
ctx.NotFound()
|
2016-12-02 06:10:39 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-13 15:04:39 -04:00
|
|
|
if !pr.CanAutoMerge() || pr.HasMerged || pr.IsWorkInProgress() {
|
2016-12-02 06:10:39 -05:00
|
|
|
ctx.Status(405)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-18 01:39:45 -04:00
|
|
|
isPass, err := pull_service.IsPullCommitStatusPass(pr)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(500, "IsPullCommitStatusPass", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isPass && !ctx.IsUserRepoAdmin() {
|
|
|
|
ctx.Status(405)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-05 13:56:50 -05:00
|
|
|
if len(form.Do) == 0 {
|
|
|
|
form.Do = string(models.MergeStyleMerge)
|
|
|
|
}
|
|
|
|
|
|
|
|
message := strings.TrimSpace(form.MergeTitleField)
|
|
|
|
if len(message) == 0 {
|
|
|
|
if models.MergeStyle(form.Do) == models.MergeStyleMerge {
|
|
|
|
message = pr.GetDefaultMergeMessage()
|
|
|
|
}
|
|
|
|
if models.MergeStyle(form.Do) == models.MergeStyleSquash {
|
|
|
|
message = pr.GetDefaultSquashMessage()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
form.MergeMessageField = strings.TrimSpace(form.MergeMessageField)
|
|
|
|
if len(form.MergeMessageField) > 0 {
|
|
|
|
message += "\n\n" + form.MergeMessageField
|
|
|
|
}
|
|
|
|
|
2019-09-26 20:22:36 -04:00
|
|
|
if err := pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil {
|
2018-01-05 13:56:50 -05:00
|
|
|
if models.IsErrInvalidMergeStyle(err) {
|
|
|
|
ctx.Status(405)
|
|
|
|
return
|
2019-11-10 03:42:51 -05:00
|
|
|
} else if models.IsErrMergeConflicts(err) {
|
|
|
|
conflictError := err.(models.ErrMergeConflicts)
|
|
|
|
ctx.JSON(http.StatusConflict, conflictError)
|
|
|
|
} else if models.IsErrRebaseConflicts(err) {
|
|
|
|
conflictError := err.(models.ErrRebaseConflicts)
|
|
|
|
ctx.JSON(http.StatusConflict, conflictError)
|
|
|
|
} else if models.IsErrMergeUnrelatedHistories(err) {
|
|
|
|
conflictError := err.(models.ErrMergeUnrelatedHistories)
|
|
|
|
ctx.JSON(http.StatusConflict, conflictError)
|
|
|
|
} else if models.IsErrMergePushOutOfDate(err) {
|
|
|
|
ctx.Status(http.StatusConflict)
|
|
|
|
return
|
2018-01-05 13:56:50 -05:00
|
|
|
}
|
2016-12-02 06:10:39 -05:00
|
|
|
ctx.Error(500, "Merge", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("Pull request merged: %d", pr.ID)
|
|
|
|
ctx.Status(200)
|
|
|
|
}
|
|
|
|
|
2019-06-07 16:29:29 -04:00
|
|
|
func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) (*models.User, *models.Repository, *git.Repository, *git.CompareInfo, string, string) {
|
2016-12-02 06:10:39 -05:00
|
|
|
baseRepo := ctx.Repo.Repository
|
|
|
|
|
|
|
|
// Get compared branches information
|
|
|
|
// format: <base branch>...[<head repo>:]<head branch>
|
|
|
|
// base<-head: master...head:feature
|
|
|
|
// same repo: master...feature
|
|
|
|
|
|
|
|
// TODO: Validate form first?
|
|
|
|
|
|
|
|
baseBranch := form.Base
|
|
|
|
|
|
|
|
var (
|
|
|
|
headUser *models.User
|
|
|
|
headBranch string
|
|
|
|
isSameRepo bool
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
// If there is no head repository, it means pull request between same repository.
|
|
|
|
headInfos := strings.Split(form.Head, ":")
|
|
|
|
if len(headInfos) == 1 {
|
|
|
|
isSameRepo = true
|
|
|
|
headUser = ctx.Repo.Owner
|
|
|
|
headBranch = headInfos[0]
|
|
|
|
|
|
|
|
} else if len(headInfos) == 2 {
|
|
|
|
headUser, err = models.GetUserByName(headInfos[0])
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrUserNotExist(err) {
|
2019-05-07 13:20:23 -04:00
|
|
|
ctx.NotFound("GetUserByName")
|
2016-12-02 06:10:39 -05:00
|
|
|
} else {
|
2018-01-10 16:34:17 -05:00
|
|
|
ctx.ServerError("GetUserByName", err)
|
2016-12-02 06:10:39 -05:00
|
|
|
}
|
|
|
|
return nil, nil, nil, nil, "", ""
|
|
|
|
}
|
|
|
|
headBranch = headInfos[1]
|
|
|
|
|
|
|
|
} else {
|
2019-03-18 22:29:43 -04:00
|
|
|
ctx.NotFound()
|
2016-12-02 06:10:39 -05:00
|
|
|
return nil, nil, nil, nil, "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Repo.PullRequest.SameRepo = isSameRepo
|
|
|
|
log.Info("Base branch: %s", baseBranch)
|
|
|
|
log.Info("Repo path: %s", ctx.Repo.GitRepo.Path)
|
|
|
|
// Check if base branch is valid.
|
|
|
|
if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
|
2019-05-07 13:20:23 -04:00
|
|
|
ctx.NotFound("IsBranchExist")
|
2016-12-02 06:10:39 -05:00
|
|
|
return nil, nil, nil, nil, "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if current user has fork of repository or in the same repository.
|
|
|
|
headRepo, has := models.HasForkedRepo(headUser.ID, baseRepo.ID)
|
|
|
|
if !has && !isSameRepo {
|
|
|
|
log.Trace("parseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
|
2019-05-07 13:20:23 -04:00
|
|
|
ctx.NotFound("HasForkedRepo")
|
2016-12-02 06:10:39 -05:00
|
|
|
return nil, nil, nil, nil, "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var headGitRepo *git.Repository
|
|
|
|
if isSameRepo {
|
|
|
|
headRepo = ctx.Repo.Repository
|
|
|
|
headGitRepo = ctx.Repo.GitRepo
|
|
|
|
} else {
|
|
|
|
headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(500, "OpenRepository", err)
|
|
|
|
return nil, nil, nil, nil, "", ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 13:20:23 -04:00
|
|
|
// user should have permission to read baseRepo's codes and pulls, NOT headRepo's
|
|
|
|
permBase, err := models.GetUserRepoPermission(baseRepo, ctx.User)
|
2018-11-28 06:26:14 -05:00
|
|
|
if err != nil {
|
2019-11-13 02:01:19 -05:00
|
|
|
headGitRepo.Close()
|
2018-11-28 06:26:14 -05:00
|
|
|
ctx.ServerError("GetUserRepoPermission", err)
|
|
|
|
return nil, nil, nil, nil, "", ""
|
|
|
|
}
|
2019-05-07 13:20:23 -04:00
|
|
|
if !permBase.CanReadIssuesOrPulls(true) || !permBase.CanRead(models.UnitTypeCode) {
|
2019-04-22 16:40:51 -04:00
|
|
|
if log.IsTrace() {
|
2019-05-07 13:20:23 -04:00
|
|
|
log.Trace("Permission Denied: User %-v cannot create/read pull requests or cannot read code in Repo %-v\nUser in baseRepo has Permissions: %-+v",
|
|
|
|
ctx.User,
|
|
|
|
baseRepo,
|
|
|
|
permBase)
|
|
|
|
}
|
2019-11-13 02:01:19 -05:00
|
|
|
headGitRepo.Close()
|
2019-05-07 13:20:23 -04:00
|
|
|
ctx.NotFound("Can't read pulls or can't read UnitTypeCode")
|
|
|
|
return nil, nil, nil, nil, "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// user should have permission to read headrepo's codes
|
|
|
|
permHead, err := models.GetUserRepoPermission(headRepo, ctx.User)
|
|
|
|
if err != nil {
|
2019-11-13 02:01:19 -05:00
|
|
|
headGitRepo.Close()
|
2019-05-07 13:20:23 -04:00
|
|
|
ctx.ServerError("GetUserRepoPermission", err)
|
|
|
|
return nil, nil, nil, nil, "", ""
|
|
|
|
}
|
|
|
|
if !permHead.CanRead(models.UnitTypeCode) {
|
|
|
|
if log.IsTrace() {
|
|
|
|
log.Trace("Permission Denied: User: %-v cannot read code in Repo: %-v\nUser in headRepo has Permissions: %-+v",
|
2019-04-22 16:40:51 -04:00
|
|
|
ctx.User,
|
|
|
|
headRepo,
|
2019-05-07 13:20:23 -04:00
|
|
|
permHead)
|
2019-04-22 16:40:51 -04:00
|
|
|
}
|
2019-11-13 02:01:19 -05:00
|
|
|
headGitRepo.Close()
|
2019-05-07 13:20:23 -04:00
|
|
|
ctx.NotFound("Can't read headRepo UnitTypeCode")
|
2016-12-02 06:10:39 -05:00
|
|
|
return nil, nil, nil, nil, "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if head branch is valid.
|
|
|
|
if !headGitRepo.IsBranchExist(headBranch) {
|
2019-11-13 02:01:19 -05:00
|
|
|
headGitRepo.Close()
|
2019-03-18 22:29:43 -04:00
|
|
|
ctx.NotFound()
|
2016-12-02 06:10:39 -05:00
|
|
|
return nil, nil, nil, nil, "", ""
|
|
|
|
}
|
|
|
|
|
2019-06-07 16:29:29 -04:00
|
|
|
compareInfo, err := headGitRepo.GetCompareInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
|
2016-12-02 06:10:39 -05:00
|
|
|
if err != nil {
|
2019-11-13 02:01:19 -05:00
|
|
|
headGitRepo.Close()
|
2019-06-07 16:29:29 -04:00
|
|
|
ctx.Error(500, "GetCompareInfo", err)
|
2016-12-02 06:10:39 -05:00
|
|
|
return nil, nil, nil, nil, "", ""
|
|
|
|
}
|
|
|
|
|
2019-06-07 16:29:29 -04:00
|
|
|
return headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch
|
2016-12-02 06:10:39 -05:00
|
|
|
}
|