You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pull.go 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. // Copyright 2016 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. package repo
  5. import (
  6. "fmt"
  7. "net/http"
  8. "strings"
  9. "time"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/auth"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/git"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/notification"
  16. api "code.gitea.io/gitea/modules/structs"
  17. "code.gitea.io/gitea/modules/timeutil"
  18. issue_service "code.gitea.io/gitea/services/issue"
  19. pull_service "code.gitea.io/gitea/services/pull"
  20. )
  21. // ListPullRequests returns a list of all PRs
  22. func ListPullRequests(ctx *context.APIContext, form api.ListPullRequestsOptions) {
  23. // swagger:operation GET /repos/{owner}/{repo}/pulls repository repoListPullRequests
  24. // ---
  25. // summary: List a repo's pull requests
  26. // produces:
  27. // - application/json
  28. // parameters:
  29. // - name: owner
  30. // in: path
  31. // description: owner of the repo
  32. // type: string
  33. // required: true
  34. // - name: repo
  35. // in: path
  36. // description: name of the repo
  37. // type: string
  38. // required: true
  39. // - name: page
  40. // in: query
  41. // description: Page number
  42. // type: integer
  43. // - name: state
  44. // in: query
  45. // description: "State of pull request: open or closed (optional)"
  46. // type: string
  47. // enum: [closed, open, all]
  48. // - name: sort
  49. // in: query
  50. // description: "Type of sort"
  51. // type: string
  52. // enum: [oldest, recentupdate, leastupdate, mostcomment, leastcomment, priority]
  53. // - name: milestone
  54. // in: query
  55. // description: "ID of the milestone"
  56. // type: integer
  57. // format: int64
  58. // - name: labels
  59. // in: query
  60. // description: "Label IDs"
  61. // type: array
  62. // collectionFormat: multi
  63. // items:
  64. // type: integer
  65. // format: int64
  66. // responses:
  67. // "200":
  68. // "$ref": "#/responses/PullRequestList"
  69. prs, maxResults, err := models.PullRequests(ctx.Repo.Repository.ID, &models.PullRequestsOptions{
  70. Page: ctx.QueryInt("page"),
  71. State: ctx.QueryTrim("state"),
  72. SortType: ctx.QueryTrim("sort"),
  73. Labels: ctx.QueryStrings("labels"),
  74. MilestoneID: ctx.QueryInt64("milestone"),
  75. })
  76. if err != nil {
  77. ctx.Error(500, "PullRequests", err)
  78. return
  79. }
  80. apiPrs := make([]*api.PullRequest, len(prs))
  81. for i := range prs {
  82. if err = prs[i].LoadIssue(); err != nil {
  83. ctx.Error(500, "LoadIssue", err)
  84. return
  85. }
  86. if err = prs[i].LoadAttributes(); err != nil {
  87. ctx.Error(500, "LoadAttributes", err)
  88. return
  89. }
  90. if err = prs[i].GetBaseRepo(); err != nil {
  91. ctx.Error(500, "GetBaseRepo", err)
  92. return
  93. }
  94. if err = prs[i].GetHeadRepo(); err != nil {
  95. ctx.Error(500, "GetHeadRepo", err)
  96. return
  97. }
  98. apiPrs[i] = prs[i].APIFormat()
  99. }
  100. ctx.SetLinkHeader(int(maxResults), models.ItemsPerPage)
  101. ctx.JSON(200, &apiPrs)
  102. }
  103. // GetPullRequest returns a single PR based on index
  104. func GetPullRequest(ctx *context.APIContext) {
  105. // swagger:operation GET /repos/{owner}/{repo}/pulls/{index} repository repoGetPullRequest
  106. // ---
  107. // summary: Get a pull request
  108. // produces:
  109. // - application/json
  110. // parameters:
  111. // - name: owner
  112. // in: path
  113. // description: owner of the repo
  114. // type: string
  115. // required: true
  116. // - name: repo
  117. // in: path
  118. // description: name of the repo
  119. // type: string
  120. // required: true
  121. // - name: index
  122. // in: path
  123. // description: index of the pull request to get
  124. // type: integer
  125. // format: int64
  126. // required: true
  127. // responses:
  128. // "200":
  129. // "$ref": "#/responses/PullRequest"
  130. pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  131. if err != nil {
  132. if models.IsErrPullRequestNotExist(err) {
  133. ctx.NotFound()
  134. } else {
  135. ctx.Error(500, "GetPullRequestByIndex", err)
  136. }
  137. return
  138. }
  139. if err = pr.GetBaseRepo(); err != nil {
  140. ctx.Error(500, "GetBaseRepo", err)
  141. return
  142. }
  143. if err = pr.GetHeadRepo(); err != nil {
  144. ctx.Error(500, "GetHeadRepo", err)
  145. return
  146. }
  147. ctx.JSON(200, pr.APIFormat())
  148. }
  149. // CreatePullRequest does what it says
  150. func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption) {
  151. // swagger:operation POST /repos/{owner}/{repo}/pulls repository repoCreatePullRequest
  152. // ---
  153. // summary: Create a pull request
  154. // consumes:
  155. // - application/json
  156. // produces:
  157. // - application/json
  158. // parameters:
  159. // - name: owner
  160. // in: path
  161. // description: owner of the repo
  162. // type: string
  163. // required: true
  164. // - name: repo
  165. // in: path
  166. // description: name of the repo
  167. // type: string
  168. // required: true
  169. // - name: body
  170. // in: body
  171. // schema:
  172. // "$ref": "#/definitions/CreatePullRequestOption"
  173. // responses:
  174. // "201":
  175. // "$ref": "#/responses/PullRequest"
  176. var (
  177. repo = ctx.Repo.Repository
  178. labelIDs []int64
  179. assigneeID int64
  180. milestoneID int64
  181. )
  182. // Get repo/branch information
  183. _, headRepo, headGitRepo, compareInfo, baseBranch, headBranch := parseCompareInfo(ctx, form)
  184. if ctx.Written() {
  185. return
  186. }
  187. // Check if another PR exists with the same targets
  188. existingPr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
  189. if err != nil {
  190. if !models.IsErrPullRequestNotExist(err) {
  191. ctx.Error(500, "GetUnmergedPullRequest", err)
  192. return
  193. }
  194. } else {
  195. err = models.ErrPullRequestAlreadyExists{
  196. ID: existingPr.ID,
  197. IssueID: existingPr.Index,
  198. HeadRepoID: existingPr.HeadRepoID,
  199. BaseRepoID: existingPr.BaseRepoID,
  200. HeadBranch: existingPr.HeadBranch,
  201. BaseBranch: existingPr.BaseBranch,
  202. }
  203. ctx.Error(409, "GetUnmergedPullRequest", err)
  204. return
  205. }
  206. if len(form.Labels) > 0 {
  207. labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
  208. if err != nil {
  209. ctx.Error(500, "GetLabelsInRepoByIDs", err)
  210. return
  211. }
  212. labelIDs = make([]int64, len(labels))
  213. for i := range labels {
  214. labelIDs[i] = labels[i].ID
  215. }
  216. }
  217. if form.Milestone > 0 {
  218. milestone, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, milestoneID)
  219. if err != nil {
  220. if models.IsErrMilestoneNotExist(err) {
  221. ctx.NotFound()
  222. } else {
  223. ctx.Error(500, "GetMilestoneByRepoID", err)
  224. }
  225. return
  226. }
  227. milestoneID = milestone.ID
  228. }
  229. patch, err := headGitRepo.GetPatch(compareInfo.MergeBase, headBranch)
  230. if err != nil {
  231. ctx.Error(500, "GetPatch", err)
  232. return
  233. }
  234. var deadlineUnix timeutil.TimeStamp
  235. if form.Deadline != nil {
  236. deadlineUnix = timeutil.TimeStamp(form.Deadline.Unix())
  237. }
  238. prIssue := &models.Issue{
  239. RepoID: repo.ID,
  240. Title: form.Title,
  241. PosterID: ctx.User.ID,
  242. Poster: ctx.User,
  243. MilestoneID: milestoneID,
  244. AssigneeID: assigneeID,
  245. IsPull: true,
  246. Content: form.Body,
  247. DeadlineUnix: deadlineUnix,
  248. }
  249. pr := &models.PullRequest{
  250. HeadRepoID: headRepo.ID,
  251. BaseRepoID: repo.ID,
  252. HeadBranch: headBranch,
  253. BaseBranch: baseBranch,
  254. HeadRepo: headRepo,
  255. BaseRepo: repo,
  256. MergeBase: compareInfo.MergeBase,
  257. Type: models.PullRequestGitea,
  258. }
  259. // Get all assignee IDs
  260. assigneeIDs, err := models.MakeIDsFromAPIAssigneesToAdd(form.Assignee, form.Assignees)
  261. if err != nil {
  262. if models.IsErrUserNotExist(err) {
  263. ctx.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err))
  264. } else {
  265. ctx.Error(500, "AddAssigneeByName", err)
  266. }
  267. return
  268. }
  269. if assigneeIDs != nil {
  270. for _, aID := range assigneeIDs {
  271. assignee, err := models.GetUserByID(aID)
  272. if err != nil {
  273. ctx.Error(500, "GetUserByID", err)
  274. return
  275. }
  276. // Check if the passed assignees is assignable
  277. valid, err := models.CanBeAssigned(assignee, repo, true)
  278. if err != nil {
  279. ctx.Error(500, "canBeAssigned", err)
  280. return
  281. }
  282. if !valid {
  283. ctx.Error(422, "canBeAssigned", models.ErrUserDoesNotHaveAccessToRepo{UserID: aID, RepoName: repo.Name})
  284. return
  285. }
  286. }
  287. }
  288. if err := pull_service.NewPullRequest(repo, prIssue, labelIDs, []string{}, pr, patch, assigneeIDs); err != nil {
  289. if models.IsErrUserDoesNotHaveAccessToRepo(err) {
  290. ctx.Error(400, "UserDoesNotHaveAccessToRepo", err)
  291. return
  292. }
  293. ctx.Error(500, "NewPullRequest", err)
  294. return
  295. } else if err := pr.PushToBaseRepo(); err != nil {
  296. ctx.Error(500, "PushToBaseRepo", err)
  297. return
  298. }
  299. notification.NotifyNewPullRequest(pr)
  300. log.Trace("Pull request created: %d/%d", repo.ID, prIssue.ID)
  301. ctx.JSON(201, pr.APIFormat())
  302. }
  303. // EditPullRequest does what it says
  304. func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
  305. // swagger:operation PATCH /repos/{owner}/{repo}/pulls/{index} repository repoEditPullRequest
  306. // ---
  307. // summary: Update a pull request. If using deadline only the date will be taken into account, and time of day ignored.
  308. // consumes:
  309. // - application/json
  310. // produces:
  311. // - application/json
  312. // parameters:
  313. // - name: owner
  314. // in: path
  315. // description: owner of the repo
  316. // type: string
  317. // required: true
  318. // - name: repo
  319. // in: path
  320. // description: name of the repo
  321. // type: string
  322. // required: true
  323. // - name: index
  324. // in: path
  325. // description: index of the pull request to edit
  326. // type: integer
  327. // format: int64
  328. // required: true
  329. // - name: body
  330. // in: body
  331. // schema:
  332. // "$ref": "#/definitions/EditPullRequestOption"
  333. // responses:
  334. // "201":
  335. // "$ref": "#/responses/PullRequest"
  336. pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  337. if err != nil {
  338. if models.IsErrPullRequestNotExist(err) {
  339. ctx.NotFound()
  340. } else {
  341. ctx.Error(500, "GetPullRequestByIndex", err)
  342. }
  343. return
  344. }
  345. err = pr.LoadIssue()
  346. if err != nil {
  347. ctx.Error(http.StatusInternalServerError, "LoadIssue", err)
  348. return
  349. }
  350. issue := pr.Issue
  351. issue.Repo = ctx.Repo.Repository
  352. if !issue.IsPoster(ctx.User.ID) && !ctx.Repo.CanWrite(models.UnitTypePullRequests) {
  353. ctx.Status(403)
  354. return
  355. }
  356. if len(form.Title) > 0 {
  357. issue.Title = form.Title
  358. }
  359. if len(form.Body) > 0 {
  360. issue.Content = form.Body
  361. }
  362. // Update or remove deadline if set
  363. if form.Deadline != nil || form.RemoveDeadline != nil {
  364. var deadlineUnix timeutil.TimeStamp
  365. if (form.RemoveDeadline == nil || !*form.RemoveDeadline) && !form.Deadline.IsZero() {
  366. deadline := time.Date(form.Deadline.Year(), form.Deadline.Month(), form.Deadline.Day(),
  367. 23, 59, 59, 0, form.Deadline.Location())
  368. deadlineUnix = timeutil.TimeStamp(deadline.Unix())
  369. }
  370. if err := models.UpdateIssueDeadline(issue, deadlineUnix, ctx.User); err != nil {
  371. ctx.Error(500, "UpdateIssueDeadline", err)
  372. return
  373. }
  374. issue.DeadlineUnix = deadlineUnix
  375. }
  376. // Add/delete assignees
  377. // Deleting is done the GitHub way (quote from their api documentation):
  378. // https://developer.github.com/v3/issues/#edit-an-issue
  379. // "assignees" (array): Logins for Users to assign to this issue.
  380. // Pass one or more user logins to replace the set of assignees on this Issue.
  381. // Send an empty array ([]) to clear all assignees from the Issue.
  382. if ctx.Repo.CanWrite(models.UnitTypePullRequests) && (form.Assignees != nil || len(form.Assignee) > 0) {
  383. err = issue_service.UpdateAssignees(issue, form.Assignee, form.Assignees, ctx.User)
  384. if err != nil {
  385. if models.IsErrUserNotExist(err) {
  386. ctx.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err))
  387. } else {
  388. ctx.Error(500, "UpdateAssignees", err)
  389. }
  390. return
  391. }
  392. }
  393. if ctx.Repo.CanWrite(models.UnitTypePullRequests) && form.Milestone != 0 &&
  394. issue.MilestoneID != form.Milestone {
  395. oldMilestoneID := issue.MilestoneID
  396. issue.MilestoneID = form.Milestone
  397. if err = issue_service.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
  398. ctx.Error(500, "ChangeMilestoneAssign", err)
  399. return
  400. }
  401. }
  402. if ctx.Repo.CanWrite(models.UnitTypePullRequests) && form.Labels != nil {
  403. labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
  404. if err != nil {
  405. ctx.Error(500, "GetLabelsInRepoByIDsError", err)
  406. return
  407. }
  408. if err = issue.ReplaceLabels(labels, ctx.User); err != nil {
  409. ctx.Error(500, "ReplaceLabelsError", err)
  410. return
  411. }
  412. }
  413. if err = models.UpdateIssue(issue); err != nil {
  414. ctx.Error(500, "UpdateIssue", err)
  415. return
  416. }
  417. if form.State != nil {
  418. if err = issue_service.ChangeStatus(issue, ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
  419. if models.IsErrDependenciesLeft(err) {
  420. ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this pull request because it still has open dependencies")
  421. return
  422. }
  423. ctx.Error(500, "ChangeStatus", err)
  424. return
  425. }
  426. }
  427. // Refetch from database
  428. pr, err = models.GetPullRequestByIndex(ctx.Repo.Repository.ID, pr.Index)
  429. if err != nil {
  430. if models.IsErrPullRequestNotExist(err) {
  431. ctx.NotFound()
  432. } else {
  433. ctx.Error(500, "GetPullRequestByIndex", err)
  434. }
  435. return
  436. }
  437. // TODO this should be 200, not 201
  438. ctx.JSON(201, pr.APIFormat())
  439. }
  440. // IsPullRequestMerged checks if a PR exists given an index
  441. func IsPullRequestMerged(ctx *context.APIContext) {
  442. // swagger:operation GET /repos/{owner}/{repo}/pulls/{index}/merge repository repoPullRequestIsMerged
  443. // ---
  444. // summary: Check if a pull request has been merged
  445. // produces:
  446. // - application/json
  447. // parameters:
  448. // - name: owner
  449. // in: path
  450. // description: owner of the repo
  451. // type: string
  452. // required: true
  453. // - name: repo
  454. // in: path
  455. // description: name of the repo
  456. // type: string
  457. // required: true
  458. // - name: index
  459. // in: path
  460. // description: index of the pull request
  461. // type: integer
  462. // format: int64
  463. // required: true
  464. // responses:
  465. // "204":
  466. // description: pull request has been merged
  467. // "404":
  468. // description: pull request has not been merged
  469. pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  470. if err != nil {
  471. if models.IsErrPullRequestNotExist(err) {
  472. ctx.NotFound()
  473. } else {
  474. ctx.Error(500, "GetPullRequestByIndex", err)
  475. }
  476. return
  477. }
  478. if pr.HasMerged {
  479. ctx.Status(204)
  480. }
  481. ctx.NotFound()
  482. }
  483. // MergePullRequest merges a PR given an index
  484. func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
  485. // swagger:operation POST /repos/{owner}/{repo}/pulls/{index}/merge repository repoMergePullRequest
  486. // ---
  487. // summary: Merge a pull request
  488. // produces:
  489. // - application/json
  490. // parameters:
  491. // - name: owner
  492. // in: path
  493. // description: owner of the repo
  494. // type: string
  495. // required: true
  496. // - name: repo
  497. // in: path
  498. // description: name of the repo
  499. // type: string
  500. // required: true
  501. // - name: index
  502. // in: path
  503. // description: index of the pull request to merge
  504. // type: integer
  505. // format: int64
  506. // required: true
  507. // - name: body
  508. // in: body
  509. // schema:
  510. // $ref: "#/definitions/MergePullRequestOption"
  511. // responses:
  512. // "200":
  513. // "$ref": "#/responses/empty"
  514. // "405":
  515. // "$ref": "#/responses/empty"
  516. pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  517. if err != nil {
  518. if models.IsErrPullRequestNotExist(err) {
  519. ctx.NotFound("GetPullRequestByIndex", err)
  520. } else {
  521. ctx.Error(500, "GetPullRequestByIndex", err)
  522. }
  523. return
  524. }
  525. if err = pr.GetHeadRepo(); err != nil {
  526. ctx.ServerError("GetHeadRepo", err)
  527. return
  528. }
  529. err = pr.LoadIssue()
  530. if err != nil {
  531. ctx.Error(http.StatusInternalServerError, "LoadIssue", err)
  532. return
  533. }
  534. pr.Issue.Repo = ctx.Repo.Repository
  535. if ctx.IsSigned {
  536. // Update issue-user.
  537. if err = pr.Issue.ReadBy(ctx.User.ID); err != nil {
  538. ctx.Error(500, "ReadBy", err)
  539. return
  540. }
  541. }
  542. if pr.Issue.IsClosed {
  543. ctx.NotFound()
  544. return
  545. }
  546. if !pr.CanAutoMerge() || pr.HasMerged || pr.IsWorkInProgress() {
  547. ctx.Status(405)
  548. return
  549. }
  550. isPass, err := pull_service.IsPullCommitStatusPass(pr)
  551. if err != nil {
  552. ctx.Error(500, "IsPullCommitStatusPass", err)
  553. return
  554. }
  555. if !isPass && !ctx.IsUserRepoAdmin() {
  556. ctx.Status(405)
  557. return
  558. }
  559. if len(form.Do) == 0 {
  560. form.Do = string(models.MergeStyleMerge)
  561. }
  562. message := strings.TrimSpace(form.MergeTitleField)
  563. if len(message) == 0 {
  564. if models.MergeStyle(form.Do) == models.MergeStyleMerge {
  565. message = pr.GetDefaultMergeMessage()
  566. }
  567. if models.MergeStyle(form.Do) == models.MergeStyleSquash {
  568. message = pr.GetDefaultSquashMessage()
  569. }
  570. }
  571. form.MergeMessageField = strings.TrimSpace(form.MergeMessageField)
  572. if len(form.MergeMessageField) > 0 {
  573. message += "\n\n" + form.MergeMessageField
  574. }
  575. if err := pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil {
  576. if models.IsErrInvalidMergeStyle(err) {
  577. ctx.Status(405)
  578. return
  579. }
  580. ctx.Error(500, "Merge", err)
  581. return
  582. }
  583. log.Trace("Pull request merged: %d", pr.ID)
  584. ctx.Status(200)
  585. }
  586. func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) (*models.User, *models.Repository, *git.Repository, *git.CompareInfo, string, string) {
  587. baseRepo := ctx.Repo.Repository
  588. // Get compared branches information
  589. // format: <base branch>...[<head repo>:]<head branch>
  590. // base<-head: master...head:feature
  591. // same repo: master...feature
  592. // TODO: Validate form first?
  593. baseBranch := form.Base
  594. var (
  595. headUser *models.User
  596. headBranch string
  597. isSameRepo bool
  598. err error
  599. )
  600. // If there is no head repository, it means pull request between same repository.
  601. headInfos := strings.Split(form.Head, ":")
  602. if len(headInfos) == 1 {
  603. isSameRepo = true
  604. headUser = ctx.Repo.Owner
  605. headBranch = headInfos[0]
  606. } else if len(headInfos) == 2 {
  607. headUser, err = models.GetUserByName(headInfos[0])
  608. if err != nil {
  609. if models.IsErrUserNotExist(err) {
  610. ctx.NotFound("GetUserByName")
  611. } else {
  612. ctx.ServerError("GetUserByName", err)
  613. }
  614. return nil, nil, nil, nil, "", ""
  615. }
  616. headBranch = headInfos[1]
  617. } else {
  618. ctx.NotFound()
  619. return nil, nil, nil, nil, "", ""
  620. }
  621. ctx.Repo.PullRequest.SameRepo = isSameRepo
  622. log.Info("Base branch: %s", baseBranch)
  623. log.Info("Repo path: %s", ctx.Repo.GitRepo.Path)
  624. // Check if base branch is valid.
  625. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  626. ctx.NotFound("IsBranchExist")
  627. return nil, nil, nil, nil, "", ""
  628. }
  629. // Check if current user has fork of repository or in the same repository.
  630. headRepo, has := models.HasForkedRepo(headUser.ID, baseRepo.ID)
  631. if !has && !isSameRepo {
  632. log.Trace("parseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
  633. ctx.NotFound("HasForkedRepo")
  634. return nil, nil, nil, nil, "", ""
  635. }
  636. var headGitRepo *git.Repository
  637. if isSameRepo {
  638. headRepo = ctx.Repo.Repository
  639. headGitRepo = ctx.Repo.GitRepo
  640. } else {
  641. headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  642. if err != nil {
  643. ctx.Error(500, "OpenRepository", err)
  644. return nil, nil, nil, nil, "", ""
  645. }
  646. }
  647. // user should have permission to read baseRepo's codes and pulls, NOT headRepo's
  648. permBase, err := models.GetUserRepoPermission(baseRepo, ctx.User)
  649. if err != nil {
  650. ctx.ServerError("GetUserRepoPermission", err)
  651. return nil, nil, nil, nil, "", ""
  652. }
  653. if !permBase.CanReadIssuesOrPulls(true) || !permBase.CanRead(models.UnitTypeCode) {
  654. if log.IsTrace() {
  655. log.Trace("Permission Denied: User %-v cannot create/read pull requests or cannot read code in Repo %-v\nUser in baseRepo has Permissions: %-+v",
  656. ctx.User,
  657. baseRepo,
  658. permBase)
  659. }
  660. ctx.NotFound("Can't read pulls or can't read UnitTypeCode")
  661. return nil, nil, nil, nil, "", ""
  662. }
  663. // user should have permission to read headrepo's codes
  664. permHead, err := models.GetUserRepoPermission(headRepo, ctx.User)
  665. if err != nil {
  666. ctx.ServerError("GetUserRepoPermission", err)
  667. return nil, nil, nil, nil, "", ""
  668. }
  669. if !permHead.CanRead(models.UnitTypeCode) {
  670. if log.IsTrace() {
  671. log.Trace("Permission Denied: User: %-v cannot read code in Repo: %-v\nUser in headRepo has Permissions: %-+v",
  672. ctx.User,
  673. headRepo,
  674. permHead)
  675. }
  676. ctx.NotFound("Can't read headRepo UnitTypeCode")
  677. return nil, nil, nil, nil, "", ""
  678. }
  679. // Check if head branch is valid.
  680. if !headGitRepo.IsBranchExist(headBranch) {
  681. ctx.NotFound()
  682. return nil, nil, nil, nil, "", ""
  683. }
  684. compareInfo, err := headGitRepo.GetCompareInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
  685. if err != nil {
  686. ctx.Error(500, "GetCompareInfo", err)
  687. return nil, nil, nil, nil, "", ""
  688. }
  689. return headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch
  690. }