2016-12-02 16:40:39 +05:30
// 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-18 02:53:58 +05:30
"net/http"
2016-12-02 16:40:39 +05:30
"strings"
2019-11-03 20:16:32 +05:30
"time"
2016-12-02 16:40:39 +05:30
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
2020-01-10 13:23:53 +05:30
"code.gitea.io/gitea/modules/convert"
2021-01-26 21:06:53 +05:30
auth "code.gitea.io/gitea/modules/forms"
2019-03-27 15:03:00 +05:30
"code.gitea.io/gitea/modules/git"
2016-12-02 16:40:39 +05:30
"code.gitea.io/gitea/modules/log"
2020-05-17 02:35:19 +05:30
"code.gitea.io/gitea/modules/notification"
2019-05-11 15:51:34 +05:30
api "code.gitea.io/gitea/modules/structs"
2019-08-15 20:16:21 +05:30
"code.gitea.io/gitea/modules/timeutil"
2021-01-26 21:06:53 +05:30
"code.gitea.io/gitea/modules/web"
2020-01-25 00:30:29 +05:30
"code.gitea.io/gitea/routers/api/v1/utils"
2019-10-25 20:16:37 +05:30
issue_service "code.gitea.io/gitea/services/issue"
2019-09-27 05:52:36 +05:30
pull_service "code.gitea.io/gitea/services/pull"
2016-12-02 16:40:39 +05:30
)
// ListPullRequests returns a list of all PRs
2021-01-26 21:06:53 +05:30
func ListPullRequests ( ctx * context . APIContext ) {
2017-11-13 12:32:25 +05:30
// 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-21 09:10:42 +05:30
// - 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
2020-01-25 00:30:29 +05:30
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// - name: limit
// in: query
2020-06-09 10:27:38 +05:30
// description: page size of results
2020-01-25 00:30:29 +05:30
// type: integer
2017-11-13 12:32:25 +05:30
// responses:
// "200":
// "$ref": "#/responses/PullRequestList"
2019-12-20 22:37:12 +05:30
2020-01-25 00:30:29 +05:30
listOptions := utils . GetListOptions ( ctx )
2016-12-02 16:40:39 +05:30
prs , maxResults , err := models . PullRequests ( ctx . Repo . Repository . ID , & models . PullRequestsOptions {
2020-01-25 00:30:29 +05:30
ListOptions : listOptions ,
2016-12-02 16:40:39 +05:30
State : ctx . QueryTrim ( "state" ) ,
SortType : ctx . QueryTrim ( "sort" ) ,
Labels : ctx . QueryStrings ( "labels" ) ,
MilestoneID : ctx . QueryInt64 ( "milestone" ) ,
} )
if err != nil {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "PullRequests" , err )
2016-12-02 16:40:39 +05:30
return
}
apiPrs := make ( [ ] * api . PullRequest , len ( prs ) )
for i := range prs {
2017-11-04 23:40:01 +05:30
if err = prs [ i ] . LoadIssue ( ) ; err != nil {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "LoadIssue" , err )
2017-11-04 23:40:01 +05:30
return
}
if err = prs [ i ] . LoadAttributes ( ) ; err != nil {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "LoadAttributes" , err )
2017-11-04 23:40:01 +05:30
return
}
2020-03-03 04:01:55 +05:30
if err = prs [ i ] . LoadBaseRepo ( ) ; err != nil {
ctx . Error ( http . StatusInternalServerError , "LoadBaseRepo" , err )
2017-11-04 23:40:01 +05:30
return
}
2020-03-03 04:01:55 +05:30
if err = prs [ i ] . LoadHeadRepo ( ) ; err != nil {
ctx . Error ( http . StatusInternalServerError , "LoadHeadRepo" , err )
2017-11-04 23:40:01 +05:30
return
}
2020-01-10 13:23:53 +05:30
apiPrs [ i ] = convert . ToAPIPullRequest ( prs [ i ] )
2016-12-02 16:40:39 +05:30
}
2020-01-25 00:30:29 +05:30
ctx . SetLinkHeader ( int ( maxResults ) , listOptions . PageSize )
2020-04-18 12:23:56 +05:30
ctx . Header ( ) . Set ( "X-Total-Count" , fmt . Sprintf ( "%d" , maxResults ) )
2020-08-13 22:48:18 +05:30
ctx . Header ( ) . Set ( "Access-Control-Expose-Headers" , "X-Total-Count, Link" )
2019-12-20 22:37:12 +05:30
ctx . JSON ( http . StatusOK , & apiPrs )
2016-12-02 16:40:39 +05:30
}
// GetPullRequest returns a single PR based on index
func GetPullRequest ( ctx * context . APIContext ) {
2017-11-13 12:32:25 +05:30
// 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-21 09:10:42 +05:30
// format: int64
2017-11-13 12:32:25 +05:30
// required: true
// responses:
// "200":
// "$ref": "#/responses/PullRequest"
2020-01-09 17:26:32 +05:30
// "404":
// "$ref": "#/responses/notFound"
2019-12-20 22:37:12 +05:30
2016-12-02 16:40:39 +05:30
pr , err := models . GetPullRequestByIndex ( ctx . Repo . Repository . ID , ctx . ParamsInt64 ( ":index" ) )
if err != nil {
if models . IsErrPullRequestNotExist ( err ) {
2019-03-19 07:59:43 +05:30
ctx . NotFound ( )
2016-12-02 16:40:39 +05:30
} else {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "GetPullRequestByIndex" , err )
2016-12-02 16:40:39 +05:30
}
return
}
2020-03-03 04:01:55 +05:30
if err = pr . LoadBaseRepo ( ) ; err != nil {
ctx . Error ( http . StatusInternalServerError , "LoadBaseRepo" , err )
2017-11-04 23:40:01 +05:30
return
}
2020-03-03 04:01:55 +05:30
if err = pr . LoadHeadRepo ( ) ; err != nil {
ctx . Error ( http . StatusInternalServerError , "LoadHeadRepo" , err )
2017-11-04 23:40:01 +05:30
return
}
2020-01-10 13:23:53 +05:30
ctx . JSON ( http . StatusOK , convert . ToAPIPullRequest ( pr ) )
2016-12-02 16:40:39 +05:30
}
2020-06-05 16:33:12 +05:30
// DownloadPullDiff render a pull's raw diff
func DownloadPullDiff ( ctx * context . APIContext ) {
// swagger:operation GET /repos/{owner}/{repo}/pulls/{index}.diff repository repoDownloadPullDiff
// ---
// summary: Get a pull request diff
// produces:
// - text/plain
// 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
// format: int64
// required: true
// responses:
// "200":
// "$ref": "#/responses/string"
// "404":
// "$ref": "#/responses/notFound"
DownloadPullDiffOrPatch ( ctx , false )
}
// DownloadPullPatch render a pull's raw patch
func DownloadPullPatch ( ctx * context . APIContext ) {
// swagger:operation GET /repos/{owner}/{repo}/pulls/{index}.patch repository repoDownloadPullPatch
// ---
// summary: Get a pull request patch file
// produces:
// - text/plain
// 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
// format: int64
// required: true
// responses:
// "200":
// "$ref": "#/responses/string"
// "404":
// "$ref": "#/responses/notFound"
DownloadPullDiffOrPatch ( ctx , true )
}
// DownloadPullDiffOrPatch render a pull's raw diff or patch
func DownloadPullDiffOrPatch ( ctx * context . APIContext , patch bool ) {
pr , err := models . GetPullRequestByIndex ( ctx . Repo . Repository . ID , ctx . ParamsInt64 ( ":index" ) )
if err != nil {
if models . IsErrPullRequestNotExist ( err ) {
ctx . NotFound ( )
} else {
ctx . InternalServerError ( err )
}
return
}
if err := pull_service . DownloadDiffOrPatch ( pr , ctx , patch ) ; err != nil {
ctx . InternalServerError ( err )
return
}
}
2016-12-02 16:40:39 +05:30
// CreatePullRequest does what it says
2021-01-26 21:06:53 +05:30
func CreatePullRequest ( ctx * context . APIContext ) {
2017-11-13 12:32:25 +05:30
// 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"
2019-12-20 22:37:12 +05:30
// "409":
// "$ref": "#/responses/error"
// "422":
// "$ref": "#/responses/validationError"
2021-01-26 21:06:53 +05:30
form := * web . GetForm ( ctx ) . ( * api . CreatePullRequestOption )
2020-10-26 14:35:27 +05:30
if form . Head == form . Base {
ctx . Error ( http . StatusUnprocessableEntity , "BaseHeadSame" ,
"Invalid PullRequest: There are no changes between the head and the base" )
return
}
2016-12-02 16:40:39 +05:30
var (
repo = ctx . Repo . Repository
labelIDs [ ] int64
milestoneID int64
)
// Get repo/branch information
2019-10-18 16:43:31 +05:30
_ , headRepo , headGitRepo , compareInfo , baseBranch , headBranch := parseCompareInfo ( ctx , form )
2016-12-02 16:40:39 +05:30
if ctx . Written ( ) {
return
}
2019-11-13 12:31:19 +05:30
defer headGitRepo . Close ( )
2016-12-02 16:40:39 +05:30
// 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 ) {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "GetUnmergedPullRequest" , err )
2016-12-02 16:40:39 +05:30
return
}
} else {
err = models . ErrPullRequestAlreadyExists {
ID : existingPr . ID ,
IssueID : existingPr . Index ,
HeadRepoID : existingPr . HeadRepoID ,
BaseRepoID : existingPr . BaseRepoID ,
HeadBranch : existingPr . HeadBranch ,
BaseBranch : existingPr . BaseBranch ,
}
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusConflict , "GetUnmergedPullRequest" , err )
2016-12-02 16:40:39 +05:30
return
}
if len ( form . Labels ) > 0 {
labels , err := models . GetLabelsInRepoByIDs ( ctx . Repo . Repository . ID , form . Labels )
if err != nil {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "GetLabelsInRepoByIDs" , err )
2016-12-02 16:40:39 +05:30
return
}
2020-04-20 18:40:45 +05:30
labelIDs = make ( [ ] int64 , len ( form . Labels ) )
orgLabelIDs := make ( [ ] int64 , len ( form . Labels ) )
2016-12-02 16:40:39 +05:30
for i := range labels {
labelIDs [ i ] = labels [ i ] . ID
}
2020-04-20 18:40:45 +05:30
if ctx . Repo . Owner . IsOrganization ( ) {
orgLabels , err := models . GetLabelsInOrgByIDs ( ctx . Repo . Owner . ID , form . Labels )
if err != nil {
ctx . Error ( http . StatusInternalServerError , "GetLabelsInOrgByIDs" , err )
return
}
for i := range orgLabels {
orgLabelIDs [ i ] = orgLabels [ i ] . ID
}
}
labelIDs = append ( labelIDs , orgLabelIDs ... )
2016-12-02 16:40:39 +05:30
}
if form . Milestone > 0 {
2021-03-13 23:36:52 +05:30
milestone , err := models . GetMilestoneByRepoID ( ctx . Repo . Repository . ID , form . Milestone )
2016-12-02 16:40:39 +05:30
if err != nil {
if models . IsErrMilestoneNotExist ( err ) {
2019-03-19 07:59:43 +05:30
ctx . NotFound ( )
2016-12-02 16:40:39 +05:30
} else {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "GetMilestoneByRepoID" , err )
2016-12-02 16:40:39 +05:30
}
return
}
milestoneID = milestone . ID
}
2019-08-15 20:16:21 +05:30
var deadlineUnix timeutil . TimeStamp
2018-05-02 00:35:28 +05:30
if form . Deadline != nil {
2019-08-15 20:16:21 +05:30
deadlineUnix = timeutil . TimeStamp ( form . Deadline . Unix ( ) )
2018-05-02 00:35:28 +05:30
}
2016-12-02 16:40:39 +05:30
prIssue := & models . Issue {
2018-05-02 00:35:28 +05:30
RepoID : repo . ID ,
Title : form . Title ,
PosterID : ctx . User . ID ,
Poster : ctx . User ,
MilestoneID : milestoneID ,
IsPull : true ,
Content : form . Body ,
DeadlineUnix : deadlineUnix ,
2016-12-02 16:40:39 +05:30
}
pr := & models . PullRequest {
2019-10-18 16:43:31 +05:30
HeadRepoID : headRepo . ID ,
BaseRepoID : repo . ID ,
HeadBranch : headBranch ,
BaseBranch : baseBranch ,
HeadRepo : headRepo ,
BaseRepo : repo ,
MergeBase : compareInfo . MergeBase ,
Type : models . PullRequestGitea ,
2016-12-02 16:40:39 +05:30
}
2018-05-09 21:59:04 +05:30
// Get all assignee IDs
assigneeIDs , err := models . MakeIDsFromAPIAssigneesToAdd ( form . Assignee , form . Assignees )
if err != nil {
if models . IsErrUserNotExist ( err ) {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusUnprocessableEntity , "" , fmt . Sprintf ( "Assignee does not exist: [name: %s]" , err ) )
2018-05-09 21:59:04 +05:30
} else {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "AddAssigneeByName" , err )
2018-05-09 21:59:04 +05:30
}
return
}
2019-10-25 20:16:37 +05:30
// Check if the passed assignees is assignable
for _ , aID := range assigneeIDs {
assignee , err := models . GetUserByID ( aID )
if err != nil {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "GetUserByID" , err )
2019-10-25 20:16:37 +05:30
return
}
2018-05-09 21:59:04 +05:30
2019-10-25 20:16:37 +05:30
valid , err := models . CanBeAssigned ( assignee , repo , true )
if err != nil {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "canBeAssigned" , err )
2019-10-25 20:16:37 +05:30
return
}
if ! valid {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusUnprocessableEntity , "canBeAssigned" , models . ErrUserDoesNotHaveAccessToRepo { UserID : aID , RepoName : repo . Name } )
2019-10-25 20:16:37 +05:30
return
}
}
2019-12-14 03:51:06 +05:30
if err := pull_service . NewPullRequest ( repo , prIssue , labelIDs , [ ] string { } , pr , assigneeIDs ) ; err != nil {
2018-05-09 21:59:04 +05:30
if models . IsErrUserDoesNotHaveAccessToRepo ( err ) {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusBadRequest , "UserDoesNotHaveAccessToRepo" , err )
2018-05-09 21:59:04 +05:30
return
}
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "NewPullRequest" , err )
2016-12-02 16:40:39 +05:30
return
}
log . Trace ( "Pull request created: %d/%d" , repo . ID , prIssue . ID )
2020-01-10 13:23:53 +05:30
ctx . JSON ( http . StatusCreated , convert . ToAPIPullRequest ( pr ) )
2016-12-02 16:40:39 +05:30
}
// EditPullRequest does what it says
2021-01-26 21:06:53 +05:30
func EditPullRequest ( ctx * context . APIContext ) {
2017-11-13 12:32:25 +05:30
// swagger:operation PATCH /repos/{owner}/{repo}/pulls/{index} repository repoEditPullRequest
// ---
2019-11-03 20:16:32 +05:30
// summary: Update a pull request. If using deadline only the date will be taken into account, and time of day ignored.
2017-11-13 12:32:25 +05:30
// 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-21 09:10:42 +05:30
// format: int64
2017-11-13 12:32:25 +05:30
// required: true
// - name: body
// in: body
// schema:
// "$ref": "#/definitions/EditPullRequestOption"
// responses:
// "201":
// "$ref": "#/responses/PullRequest"
2019-12-20 22:37:12 +05:30
// "403":
// "$ref": "#/responses/forbidden"
2020-06-08 00:43:40 +05:30
// "409":
// "$ref": "#/responses/error"
2019-12-20 22:37:12 +05:30
// "412":
// "$ref": "#/responses/error"
// "422":
// "$ref": "#/responses/validationError"
2021-01-26 21:06:53 +05:30
form := web . GetForm ( ctx ) . ( * api . EditPullRequestOption )
2016-12-02 16:40:39 +05:30
pr , err := models . GetPullRequestByIndex ( ctx . Repo . Repository . ID , ctx . ParamsInt64 ( ":index" ) )
if err != nil {
if models . IsErrPullRequestNotExist ( err ) {
2019-03-19 07:59:43 +05:30
ctx . NotFound ( )
2016-12-02 16:40:39 +05:30
} else {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "GetPullRequestByIndex" , err )
2016-12-02 16:40:39 +05:30
}
return
}
2019-06-13 01:11:28 +05:30
err = pr . LoadIssue ( )
if err != nil {
ctx . Error ( http . StatusInternalServerError , "LoadIssue" , err )
return
}
2016-12-02 16:40:39 +05:30
issue := pr . Issue
2018-12-13 21:25:43 +05:30
issue . Repo = ctx . Repo . Repository
2016-12-02 16:40:39 +05:30
2018-11-28 16:56:14 +05:30
if ! issue . IsPoster ( ctx . User . ID ) && ! ctx . Repo . CanWrite ( models . UnitTypePullRequests ) {
2019-12-20 22:37:12 +05:30
ctx . Status ( http . StatusForbidden )
2016-12-02 16:40:39 +05:30
return
}
2020-05-17 02:35:19 +05:30
oldTitle := issue . Title
2016-12-02 16:40:39 +05:30
if len ( form . Title ) > 0 {
issue . Title = form . Title
}
if len ( form . Body ) > 0 {
issue . Content = form . Body
}
2019-11-03 20:16:32 +05:30
// 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-28 05:05:20 +05:30
if err := models . UpdateIssueDeadline ( issue , deadlineUnix , ctx . User ) ; err != nil {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "UpdateIssueDeadline" , err )
2019-10-28 05:05:20 +05:30
return
}
issue . DeadlineUnix = deadlineUnix
2018-05-02 00:35:28 +05:30
}
2018-05-09 21:59:04 +05:30
// Add/delete assignees
2016-12-02 16:40:39 +05:30
2019-03-10 02:45:45 +05:30
// Deleting is done the GitHub way (quote from their api documentation):
2018-05-09 21:59:04 +05:30
// 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 16:56:14 +05:30
if ctx . Repo . CanWrite ( models . UnitTypePullRequests ) && ( form . Assignees != nil || len ( form . Assignee ) > 0 ) {
2019-10-25 20:16:37 +05:30
err = issue_service . UpdateAssignees ( issue , form . Assignee , form . Assignees , ctx . User )
2018-05-09 21:59:04 +05:30
if err != nil {
if models . IsErrUserNotExist ( err ) {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusUnprocessableEntity , "" , fmt . Sprintf ( "Assignee does not exist: [name: %s]" , err ) )
2018-05-09 21:59:04 +05:30
} else {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "UpdateAssignees" , err )
2018-05-09 21:59:04 +05:30
}
2016-12-02 16:40:39 +05:30
return
}
}
2018-05-09 21:59:04 +05:30
2018-11-28 16:56:14 +05:30
if ctx . Repo . CanWrite ( models . UnitTypePullRequests ) && form . Milestone != 0 &&
2016-12-02 16:40:39 +05:30
issue . MilestoneID != form . Milestone {
oldMilestoneID := issue . MilestoneID
issue . MilestoneID = form . Milestone
2019-11-02 09:03:20 +05:30
if err = issue_service . ChangeMilestoneAssign ( issue , ctx . User , oldMilestoneID ) ; err != nil {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "ChangeMilestoneAssign" , err )
2016-12-02 16:40:39 +05:30
return
}
}
2018-12-01 11:35:57 +05:30
if ctx . Repo . CanWrite ( models . UnitTypePullRequests ) && form . Labels != nil {
2018-11-16 16:42:44 +05:30
labels , err := models . GetLabelsInRepoByIDs ( ctx . Repo . Repository . ID , form . Labels )
if err != nil {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "GetLabelsInRepoByIDsError" , err )
2018-11-16 16:42:44 +05:30
return
}
2020-04-20 18:40:45 +05:30
if ctx . Repo . Owner . IsOrganization ( ) {
orgLabels , err := models . GetLabelsInOrgByIDs ( ctx . Repo . Owner . ID , form . Labels )
if err != nil {
ctx . Error ( http . StatusInternalServerError , "GetLabelsInOrgByIDs" , err )
return
}
labels = append ( labels , orgLabels ... )
}
2018-11-16 16:42:44 +05:30
if err = issue . ReplaceLabels ( labels , ctx . User ) ; err != nil {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "ReplaceLabelsError" , err )
2018-11-16 16:42:44 +05:30
return
}
}
2016-12-02 16:40:39 +05:30
if form . State != nil {
2020-05-17 02:35:19 +05:30
issue . IsClosed = ( api . StateClosed == api . StateType ( * form . State ) )
}
statusChangeComment , titleChanged , err := models . UpdateIssueByAPI ( issue , ctx . User )
if err != nil {
if models . IsErrDependenciesLeft ( err ) {
ctx . Error ( http . StatusPreconditionFailed , "DependenciesLeft" , "cannot close this pull request because it still has open dependencies" )
2016-12-02 16:40:39 +05:30
return
}
2020-05-17 02:35:19 +05:30
ctx . Error ( http . StatusInternalServerError , "UpdateIssueByAPI" , err )
return
}
if titleChanged {
notification . NotifyIssueChangeTitle ( ctx . User , issue , oldTitle )
}
if statusChangeComment != nil {
notification . NotifyIssueChangeStatus ( ctx . User , issue , statusChangeComment , issue . IsClosed )
2016-12-02 16:40:39 +05:30
}
2020-06-08 00:43:40 +05:30
// change pull target branch
if len ( form . Base ) != 0 && form . Base != pr . BaseBranch {
if ! ctx . Repo . GitRepo . IsBranchExist ( form . Base ) {
ctx . Error ( http . StatusNotFound , "NewBaseBranchNotExist" , fmt . Errorf ( "new base '%s' not exist" , form . Base ) )
return
}
if err := pull_service . ChangeTargetBranch ( pr , ctx . User , form . Base ) ; err != nil {
if models . IsErrPullRequestAlreadyExists ( err ) {
ctx . Error ( http . StatusConflict , "IsErrPullRequestAlreadyExists" , err )
return
} else if models . IsErrIssueIsClosed ( err ) {
ctx . Error ( http . StatusUnprocessableEntity , "IsErrIssueIsClosed" , err )
return
} else if models . IsErrPullRequestHasMerged ( err ) {
ctx . Error ( http . StatusConflict , "IsErrPullRequestHasMerged" , err )
return
} else {
ctx . InternalServerError ( err )
}
return
}
notification . NotifyPullRequestChangeTargetBranch ( ctx . User , pr , form . Base )
}
2016-12-02 16:40:39 +05:30
// Refetch from database
pr , err = models . GetPullRequestByIndex ( ctx . Repo . Repository . ID , pr . Index )
if err != nil {
if models . IsErrPullRequestNotExist ( err ) {
2019-03-19 07:59:43 +05:30
ctx . NotFound ( )
2016-12-02 16:40:39 +05:30
} else {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "GetPullRequestByIndex" , err )
2016-12-02 16:40:39 +05:30
}
return
}
2017-11-13 12:32:25 +05:30
// TODO this should be 200, not 201
2020-01-10 13:23:53 +05:30
ctx . JSON ( http . StatusCreated , convert . ToAPIPullRequest ( pr ) )
2016-12-02 16:40:39 +05:30
}
// IsPullRequestMerged checks if a PR exists given an index
func IsPullRequestMerged ( ctx * context . APIContext ) {
2017-11-13 12:32:25 +05:30
// 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-21 09:10:42 +05:30
// format: int64
2017-11-13 12:32:25 +05:30
// required: true
// responses:
// "204":
// description: pull request has been merged
// "404":
// description: pull request has not been merged
2019-12-20 22:37:12 +05:30
2016-12-02 16:40:39 +05:30
pr , err := models . GetPullRequestByIndex ( ctx . Repo . Repository . ID , ctx . ParamsInt64 ( ":index" ) )
if err != nil {
if models . IsErrPullRequestNotExist ( err ) {
2019-03-19 07:59:43 +05:30
ctx . NotFound ( )
2016-12-02 16:40:39 +05:30
} else {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "GetPullRequestByIndex" , err )
2016-12-02 16:40:39 +05:30
}
return
}
if pr . HasMerged {
2019-12-20 22:37:12 +05:30
ctx . Status ( http . StatusNoContent )
2016-12-02 16:40:39 +05:30
}
2019-03-19 07:59:43 +05:30
ctx . NotFound ( )
2016-12-02 16:40:39 +05:30
}
// MergePullRequest merges a PR given an index
2021-01-26 21:06:53 +05:30
func MergePullRequest ( ctx * context . APIContext ) {
2017-11-13 12:32:25 +05:30
// 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-21 09:10:42 +05:30
// format: int64
2017-11-13 12:32:25 +05:30
// required: true
2019-02-08 13:38:38 +05:30
// - name: body
// in: body
// schema:
// $ref: "#/definitions/MergePullRequestOption"
2017-11-13 12:32:25 +05:30
// responses:
// "200":
// "$ref": "#/responses/empty"
// "405":
// "$ref": "#/responses/empty"
2019-12-20 22:37:12 +05:30
// "409":
// "$ref": "#/responses/error"
2021-01-26 21:06:53 +05:30
form := web . GetForm ( ctx ) . ( * auth . MergePullRequestForm )
2016-12-02 16:40:39 +05:30
pr , err := models . GetPullRequestByIndex ( ctx . Repo . Repository . ID , ctx . ParamsInt64 ( ":index" ) )
if err != nil {
if models . IsErrPullRequestNotExist ( err ) {
2018-01-11 03:04:17 +05:30
ctx . NotFound ( "GetPullRequestByIndex" , err )
2016-12-02 16:40:39 +05:30
} else {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "GetPullRequestByIndex" , err )
2016-12-02 16:40:39 +05:30
}
return
}
2020-03-03 04:01:55 +05:30
if err = pr . LoadHeadRepo ( ) ; err != nil {
2020-09-21 01:50:14 +05:30
ctx . Error ( http . StatusInternalServerError , "LoadHeadRepo" , err )
2016-12-02 16:40:39 +05:30
return
}
2019-06-13 01:11:28 +05:30
err = pr . LoadIssue ( )
if err != nil {
ctx . Error ( http . StatusInternalServerError , "LoadIssue" , err )
return
}
2016-12-02 16:40:39 +05:30
pr . Issue . Repo = ctx . Repo . Repository
if ctx . IsSigned {
// Update issue-user.
if err = pr . Issue . ReadBy ( ctx . User . ID ) ; err != nil {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "ReadBy" , err )
2016-12-02 16:40:39 +05:30
return
}
}
if pr . Issue . IsClosed {
2019-03-19 07:59:43 +05:30
ctx . NotFound ( )
2016-12-02 16:40:39 +05:30
return
}
2020-01-16 19:48:30 +05:30
allowedMerge , err := pull_service . IsUserAllowedToMerge ( pr , ctx . Repo . Permission , ctx . User )
2019-09-18 11:09:45 +05:30
if err != nil {
2020-01-11 12:59:34 +05:30
ctx . Error ( http . StatusInternalServerError , "IsUSerAllowedToMerge" , err )
return
}
if ! allowedMerge {
ctx . Error ( http . StatusMethodNotAllowed , "Merge" , "User not allowed to merge PR" )
2019-09-18 11:09:45 +05:30
return
}
2021-03-04 09:11:23 +05:30
if pr . HasMerged {
ctx . Error ( http . StatusMethodNotAllowed , "PR already merged" , "" )
2020-08-20 03:05:06 +05:30
return
}
2021-03-04 09:11:23 +05:30
// handle manually-merged mark
if models . MergeStyle ( form . Do ) == models . MergeStyleManuallyMerged {
if err = pull_service . MergedManually ( pr , ctx . User , ctx . Repo . GitRepo , form . MergeCommitID ) ; err != nil {
if models . IsErrInvalidMergeStyle ( err ) {
ctx . Error ( http . StatusMethodNotAllowed , "Invalid merge style" , fmt . Errorf ( "%s is not allowed an allowed merge style for this repository" , models . MergeStyle ( form . Do ) ) )
return
}
if strings . Contains ( err . Error ( ) , "Wrong commit ID" ) {
ctx . JSON ( http . StatusConflict , err )
return
}
ctx . Error ( http . StatusInternalServerError , "Manually-Merged" , err )
return
}
ctx . Status ( http . StatusOK )
return
}
if ! pr . CanAutoMerge ( ) {
ctx . Error ( http . StatusMethodNotAllowed , "PR not in mergeable state" , "Please try again later" )
2020-08-20 03:05:06 +05:30
return
}
if pr . IsWorkInProgress ( ) {
ctx . Error ( http . StatusMethodNotAllowed , "PR is a work in progress" , "Work in progress PRs cannot be merged" )
2019-09-18 11:09:45 +05:30
return
}
2020-10-14 00:20:57 +05:30
if err := pull_service . CheckPRReadyToMerge ( pr , false ) ; err != nil {
2020-01-11 12:59:34 +05:30
if ! models . IsErrNotAllowedToMerge ( err ) {
ctx . Error ( http . StatusInternalServerError , "CheckPRReadyToMerge" , err )
return
}
if form . ForceMerge != nil && * form . ForceMerge {
if isRepoAdmin , err := models . IsUserRepoAdmin ( pr . BaseRepo , ctx . User ) ; err != nil {
ctx . Error ( http . StatusInternalServerError , "IsUserRepoAdmin" , err )
return
} else if ! isRepoAdmin {
ctx . Error ( http . StatusMethodNotAllowed , "Merge" , "Only repository admin can merge if not all checks are ok (force merge)" )
}
} else {
ctx . Error ( http . StatusMethodNotAllowed , "PR is not ready to be merged" , err )
return
}
}
2020-01-15 14:02:57 +05:30
if _ , err := pull_service . IsSignedIfRequired ( pr , ctx . User ) ; err != nil {
if ! models . IsErrWontSign ( err ) {
ctx . Error ( http . StatusInternalServerError , "IsSignedIfRequired" , err )
return
}
ctx . Error ( http . StatusMethodNotAllowed , fmt . Sprintf ( "Protected branch %s requires signed commits but this merge would not be signed" , pr . BaseBranch ) , err )
return
}
2018-01-06 00:26:50 +05:30
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-27 05:52:36 +05:30
if err := pull_service . Merge ( pr , ctx . User , ctx . Repo . GitRepo , models . MergeStyle ( form . Do ) , message ) ; err != nil {
2018-01-06 00:26:50 +05:30
if models . IsErrInvalidMergeStyle ( err ) {
2020-08-20 03:05:06 +05:30
ctx . Error ( http . StatusMethodNotAllowed , "Invalid merge style" , fmt . Errorf ( "%s is not allowed an allowed merge style for this repository" , models . MergeStyle ( form . Do ) ) )
2018-01-06 00:26:50 +05:30
return
2019-11-10 14:12:51 +05:30
} 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 )
2020-03-28 09:43:18 +05:30
} else if git . IsErrPushOutOfDate ( err ) {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusConflict , "Merge" , "merge push out of date" )
2019-11-10 14:12:51 +05:30
return
2020-03-28 09:43:18 +05:30
} else if git . IsErrPushRejected ( err ) {
errPushRej := err . ( * git . ErrPushRejected )
2020-02-22 18:38:48 +05:30
if len ( errPushRej . Message ) == 0 {
ctx . Error ( http . StatusConflict , "Merge" , "PushRejected without remote error message" )
return
}
ctx . Error ( http . StatusConflict , "Merge" , "PushRejected with remote message: " + errPushRej . Message )
return
2018-01-06 00:26:50 +05:30
}
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "Merge" , err )
2016-12-02 16:40:39 +05:30
return
}
log . Trace ( "Pull request merged: %d" , pr . ID )
2019-12-20 22:37:12 +05:30
ctx . Status ( http . StatusOK )
2016-12-02 16:40:39 +05:30
}
2019-06-08 01:59:29 +05:30
func parseCompareInfo ( ctx * context . APIContext , form api . CreatePullRequestOption ) ( * models . User , * models . Repository , * git . Repository , * git . CompareInfo , string , string ) {
2016-12-02 16:40:39 +05:30
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 22:50:23 +05:30
ctx . NotFound ( "GetUserByName" )
2016-12-02 16:40:39 +05:30
} else {
2020-09-21 01:50:14 +05:30
ctx . Error ( http . StatusInternalServerError , "GetUserByName" , err )
2016-12-02 16:40:39 +05:30
}
return nil , nil , nil , nil , "" , ""
}
headBranch = headInfos [ 1 ]
} else {
2019-03-19 07:59:43 +05:30
ctx . NotFound ( )
2016-12-02 16:40:39 +05:30
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 22:50:23 +05:30
ctx . NotFound ( "IsBranchExist" )
2016-12-02 16:40:39 +05:30
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 22:50:23 +05:30
ctx . NotFound ( "HasForkedRepo" )
2016-12-02 16:40:39 +05:30
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 {
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "OpenRepository" , err )
2016-12-02 16:40:39 +05:30
return nil , nil , nil , nil , "" , ""
}
}
2019-05-07 22:50:23 +05:30
// user should have permission to read baseRepo's codes and pulls, NOT headRepo's
permBase , err := models . GetUserRepoPermission ( baseRepo , ctx . User )
2018-11-28 16:56:14 +05:30
if err != nil {
2019-11-13 12:31:19 +05:30
headGitRepo . Close ( )
2020-09-21 01:50:14 +05:30
ctx . Error ( http . StatusInternalServerError , "GetUserRepoPermission" , err )
2018-11-28 16:56:14 +05:30
return nil , nil , nil , nil , "" , ""
}
2019-05-07 22:50:23 +05:30
if ! permBase . CanReadIssuesOrPulls ( true ) || ! permBase . CanRead ( models . UnitTypeCode ) {
2019-04-23 02:10:51 +05:30
if log . IsTrace ( ) {
2019-05-07 22:50:23 +05:30
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 12:31:19 +05:30
headGitRepo . Close ( )
2019-05-07 22:50:23 +05:30
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 12:31:19 +05:30
headGitRepo . Close ( )
2020-09-21 01:50:14 +05:30
ctx . Error ( http . StatusInternalServerError , "GetUserRepoPermission" , err )
2019-05-07 22:50:23 +05:30
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-23 02:10:51 +05:30
ctx . User ,
headRepo ,
2019-05-07 22:50:23 +05:30
permHead )
2019-04-23 02:10:51 +05:30
}
2019-11-13 12:31:19 +05:30
headGitRepo . Close ( )
2019-05-07 22:50:23 +05:30
ctx . NotFound ( "Can't read headRepo UnitTypeCode" )
2016-12-02 16:40:39 +05:30
return nil , nil , nil , nil , "" , ""
}
// Check if head branch is valid.
if ! headGitRepo . IsBranchExist ( headBranch ) {
2019-11-13 12:31:19 +05:30
headGitRepo . Close ( )
2019-03-19 07:59:43 +05:30
ctx . NotFound ( )
2016-12-02 16:40:39 +05:30
return nil , nil , nil , nil , "" , ""
}
2019-06-08 01:59:29 +05:30
compareInfo , err := headGitRepo . GetCompareInfo ( models . RepoPath ( baseRepo . Owner . Name , baseRepo . Name ) , baseBranch , headBranch )
2016-12-02 16:40:39 +05:30
if err != nil {
2019-11-13 12:31:19 +05:30
headGitRepo . Close ( )
2019-12-20 22:37:12 +05:30
ctx . Error ( http . StatusInternalServerError , "GetCompareInfo" , err )
2016-12-02 16:40:39 +05:30
return nil , nil , nil , nil , "" , ""
}
2019-06-08 01:59:29 +05:30
return headUser , headRepo , headGitRepo , compareInfo , baseBranch , headBranch
2016-12-02 16:40:39 +05:30
}
2020-08-05 02:25:22 +05:30
// UpdatePullRequest merge PR's baseBranch into headBranch
func UpdatePullRequest ( ctx * context . APIContext ) {
// swagger:operation POST /repos/{owner}/{repo}/pulls/{index}/update repository repoUpdatePullRequest
// ---
// summary: Merge PR's baseBranch into headBranch
// 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
// format: int64
// required: true
// responses:
// "200":
// "$ref": "#/responses/empty"
// "403":
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
// "409":
// "$ref": "#/responses/error"
// "422":
// "$ref": "#/responses/validationError"
pr , err := models . GetPullRequestByIndex ( ctx . Repo . Repository . ID , ctx . ParamsInt64 ( ":index" ) )
if err != nil {
if models . IsErrPullRequestNotExist ( err ) {
ctx . NotFound ( )
} else {
ctx . Error ( http . StatusInternalServerError , "GetPullRequestByIndex" , err )
}
return
}
if pr . HasMerged {
ctx . Error ( http . StatusUnprocessableEntity , "UpdatePullRequest" , err )
return
}
if err = pr . LoadIssue ( ) ; err != nil {
ctx . Error ( http . StatusInternalServerError , "LoadIssue" , err )
return
}
if pr . Issue . IsClosed {
ctx . Error ( http . StatusUnprocessableEntity , "UpdatePullRequest" , err )
return
}
if err = pr . LoadBaseRepo ( ) ; err != nil {
ctx . Error ( http . StatusInternalServerError , "LoadBaseRepo" , err )
return
}
if err = pr . LoadHeadRepo ( ) ; err != nil {
ctx . Error ( http . StatusInternalServerError , "LoadHeadRepo" , err )
return
}
allowedUpdate , err := pull_service . IsUserAllowedToUpdate ( pr , ctx . User )
if err != nil {
ctx . Error ( http . StatusInternalServerError , "IsUserAllowedToMerge" , err )
return
}
if ! allowedUpdate {
ctx . Status ( http . StatusForbidden )
return
}
// default merge commit message
message := fmt . Sprintf ( "Merge branch '%s' into %s" , pr . BaseBranch , pr . HeadBranch )
if err = pull_service . Update ( pr , ctx . User , message ) ; err != nil {
if models . IsErrMergeConflicts ( err ) {
ctx . Error ( http . StatusConflict , "Update" , "merge failed because of conflict" )
return
}
ctx . Error ( http . StatusInternalServerError , "pull_service.Update" , err )
return
}
ctx . Status ( http . StatusOK )
}