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

pull.go 20 kB

Compare branches, commits and tags with each other (#6991) * Supports tags when comparing commits or branches Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Hide headline when only comparing and don't load unused data Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Merges compare logics to allow comparing branches, commits and tags with eachother Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Display branch or tag instead of commit when used for comparing Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show pull request form after click on button Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Transfers relevant pull.go changes from master to compare.go Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Fixes error when comparing forks against a commit or tag Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes console.log from JavaScript file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show icon next to commit reference when comparing branch or tag Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Updates css file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Fixes import order * Renames template variable * Update routers/repo/compare.go Co-Authored-By: zeripath <art27@cantab.net> * Update from master Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Allow short-shas in compare * Renames prInfo to compareInfo Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Check PR permissions only if compare is pull request Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adjusts comment Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use compareInfo instead of prInfo
6 years ago
Compare branches, commits and tags with each other (#6991) * Supports tags when comparing commits or branches Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Hide headline when only comparing and don't load unused data Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Merges compare logics to allow comparing branches, commits and tags with eachother Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Display branch or tag instead of commit when used for comparing Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show pull request form after click on button Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Transfers relevant pull.go changes from master to compare.go Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Fixes error when comparing forks against a commit or tag Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes console.log from JavaScript file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show icon next to commit reference when comparing branch or tag Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Updates css file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Fixes import order * Renames template variable * Update routers/repo/compare.go Co-Authored-By: zeripath <art27@cantab.net> * Update from master Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Allow short-shas in compare * Renames prInfo to compareInfo Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Check PR permissions only if compare is pull request Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adjusts comment Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use compareInfo instead of prInfo
6 years ago
Issue due date (#3794) * Started adding deadline to ui * Implemented basic issue due date managing * Improved UI for due date managing * Added at least write access to the repo in order to modify issue due dates * Ui improvements * Added issue comments creation when adding/modifying/removing a due date * Show due date in issue list * Added api support for issue due dates * Fixed lint suggestions * Added deadline to sdk * Updated css * Added support for adding/modifiying deadlines for pull requests via api * Fixed comments not created when updating or removing a deadline * update sdk (will do properly once go-gitea/go-sdk#103 is merged) * enhanced updateIssueDeadline * Removed unnessecary Issue.DeadlineString * UI improvements * Small improvments to comment creation + ui & validation improvements * Check if an issue is overdue is now a seperate function * Updated go-sdk with govendor as it was merged * Simplified isOverdue method * removed unessecary deadline to 0 set * Update swagger definitions * Added missing return * Added an explanary comment * Improved updateIssueDeadline method so it'll only update `deadline_unix` * Small changes and improvements * no need to explicitly load the issue when updating a deadline, just use whats already there * small optimisations * Added check if a deadline was modified before updating it * Moved comment creating logic into its own function * Code cleanup for creating deadline comment * locale improvement * When modifying a deadline, the old deadline is saved with the comment * small improvments to xorm session handling when updating an issue deadline + style nitpicks * style nitpicks * Moved checking for if the user has write acces to middleware
7 years ago
Issue due date (#3794) * Started adding deadline to ui * Implemented basic issue due date managing * Improved UI for due date managing * Added at least write access to the repo in order to modify issue due dates * Ui improvements * Added issue comments creation when adding/modifying/removing a due date * Show due date in issue list * Added api support for issue due dates * Fixed lint suggestions * Added deadline to sdk * Updated css * Added support for adding/modifiying deadlines for pull requests via api * Fixed comments not created when updating or removing a deadline * update sdk (will do properly once go-gitea/go-sdk#103 is merged) * enhanced updateIssueDeadline * Removed unnessecary Issue.DeadlineString * UI improvements * Small improvments to comment creation + ui & validation improvements * Check if an issue is overdue is now a seperate function * Updated go-sdk with govendor as it was merged * Simplified isOverdue method * removed unessecary deadline to 0 set * Update swagger definitions * Added missing return * Added an explanary comment * Improved updateIssueDeadline method so it'll only update `deadline_unix` * Small changes and improvements * no need to explicitly load the issue when updating a deadline, just use whats already there * small optimisations * Added check if a deadline was modified before updating it * Moved comment creating logic into its own function * Code cleanup for creating deadline comment * locale improvement * When modifying a deadline, the old deadline is saved with the comment * small improvments to xorm session handling when updating an issue deadline + style nitpicks * style nitpicks * Moved checking for if the user has write acces to middleware
7 years ago
Issue due date (#3794) * Started adding deadline to ui * Implemented basic issue due date managing * Improved UI for due date managing * Added at least write access to the repo in order to modify issue due dates * Ui improvements * Added issue comments creation when adding/modifying/removing a due date * Show due date in issue list * Added api support for issue due dates * Fixed lint suggestions * Added deadline to sdk * Updated css * Added support for adding/modifiying deadlines for pull requests via api * Fixed comments not created when updating or removing a deadline * update sdk (will do properly once go-gitea/go-sdk#103 is merged) * enhanced updateIssueDeadline * Removed unnessecary Issue.DeadlineString * UI improvements * Small improvments to comment creation + ui & validation improvements * Check if an issue is overdue is now a seperate function * Updated go-sdk with govendor as it was merged * Simplified isOverdue method * removed unessecary deadline to 0 set * Update swagger definitions * Added missing return * Added an explanary comment * Improved updateIssueDeadline method so it'll only update `deadline_unix` * Small changes and improvements * no need to explicitly load the issue when updating a deadline, just use whats already there * small optimisations * Added check if a deadline was modified before updating it * Moved comment creating logic into its own function * Code cleanup for creating deadline comment * locale improvement * When modifying a deadline, the old deadline is saved with the comment * small improvments to xorm session handling when updating an issue deadline + style nitpicks * style nitpicks * Moved checking for if the user has write acces to middleware
7 years ago
Compare branches, commits and tags with each other (#6991) * Supports tags when comparing commits or branches Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Hide headline when only comparing and don't load unused data Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Merges compare logics to allow comparing branches, commits and tags with eachother Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Display branch or tag instead of commit when used for comparing Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show pull request form after click on button Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Transfers relevant pull.go changes from master to compare.go Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Fixes error when comparing forks against a commit or tag Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes console.log from JavaScript file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show icon next to commit reference when comparing branch or tag Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Updates css file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Fixes import order * Renames template variable * Update routers/repo/compare.go Co-Authored-By: zeripath <art27@cantab.net> * Update from master Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Allow short-shas in compare * Renames prInfo to compareInfo Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Check PR permissions only if compare is pull request Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adjusts comment Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use compareInfo instead of prInfo
6 years ago
Issue due date (#3794) * Started adding deadline to ui * Implemented basic issue due date managing * Improved UI for due date managing * Added at least write access to the repo in order to modify issue due dates * Ui improvements * Added issue comments creation when adding/modifying/removing a due date * Show due date in issue list * Added api support for issue due dates * Fixed lint suggestions * Added deadline to sdk * Updated css * Added support for adding/modifiying deadlines for pull requests via api * Fixed comments not created when updating or removing a deadline * update sdk (will do properly once go-gitea/go-sdk#103 is merged) * enhanced updateIssueDeadline * Removed unnessecary Issue.DeadlineString * UI improvements * Small improvments to comment creation + ui & validation improvements * Check if an issue is overdue is now a seperate function * Updated go-sdk with govendor as it was merged * Simplified isOverdue method * removed unessecary deadline to 0 set * Update swagger definitions * Added missing return * Added an explanary comment * Improved updateIssueDeadline method so it'll only update `deadline_unix` * Small changes and improvements * no need to explicitly load the issue when updating a deadline, just use whats already there * small optimisations * Added check if a deadline was modified before updating it * Moved comment creating logic into its own function * Code cleanup for creating deadline comment * locale improvement * When modifying a deadline, the old deadline is saved with the comment * small improvments to xorm session handling when updating an issue deadline + style nitpicks * style nitpicks * Moved checking for if the user has write acces to middleware
7 years ago
Issue due date (#3794) * Started adding deadline to ui * Implemented basic issue due date managing * Improved UI for due date managing * Added at least write access to the repo in order to modify issue due dates * Ui improvements * Added issue comments creation when adding/modifying/removing a due date * Show due date in issue list * Added api support for issue due dates * Fixed lint suggestions * Added deadline to sdk * Updated css * Added support for adding/modifiying deadlines for pull requests via api * Fixed comments not created when updating or removing a deadline * update sdk (will do properly once go-gitea/go-sdk#103 is merged) * enhanced updateIssueDeadline * Removed unnessecary Issue.DeadlineString * UI improvements * Small improvments to comment creation + ui & validation improvements * Check if an issue is overdue is now a seperate function * Updated go-sdk with govendor as it was merged * Simplified isOverdue method * removed unessecary deadline to 0 set * Update swagger definitions * Added missing return * Added an explanary comment * Improved updateIssueDeadline method so it'll only update `deadline_unix` * Small changes and improvements * no need to explicitly load the issue when updating a deadline, just use whats already there * small optimisations * Added check if a deadline was modified before updating it * Moved comment creating logic into its own function * Code cleanup for creating deadline comment * locale improvement * When modifying a deadline, the old deadline is saved with the comment * small improvments to xorm session handling when updating an issue deadline + style nitpicks * style nitpicks * Moved checking for if the user has write acces to middleware
7 years ago
Compare branches, commits and tags with each other (#6991) * Supports tags when comparing commits or branches Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Hide headline when only comparing and don't load unused data Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Merges compare logics to allow comparing branches, commits and tags with eachother Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Display branch or tag instead of commit when used for comparing Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show pull request form after click on button Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Transfers relevant pull.go changes from master to compare.go Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Fixes error when comparing forks against a commit or tag Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes console.log from JavaScript file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show icon next to commit reference when comparing branch or tag Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Updates css file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Fixes import order * Renames template variable * Update routers/repo/compare.go Co-Authored-By: zeripath <art27@cantab.net> * Update from master Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Allow short-shas in compare * Renames prInfo to compareInfo Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Check PR permissions only if compare is pull request Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adjusts comment Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use compareInfo instead of prInfo
6 years ago
Compare branches, commits and tags with each other (#6991) * Supports tags when comparing commits or branches Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Hide headline when only comparing and don't load unused data Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Merges compare logics to allow comparing branches, commits and tags with eachother Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Display branch or tag instead of commit when used for comparing Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show pull request form after click on button Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Transfers relevant pull.go changes from master to compare.go Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Fixes error when comparing forks against a commit or tag Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes console.log from JavaScript file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show icon next to commit reference when comparing branch or tag Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Updates css file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Fixes import order * Renames template variable * Update routers/repo/compare.go Co-Authored-By: zeripath <art27@cantab.net> * Update from master Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Allow short-shas in compare * Renames prInfo to compareInfo Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Check PR permissions only if compare is pull request Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adjusts comment Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use compareInfo instead of prInfo
6 years ago
Compare branches, commits and tags with each other (#6991) * Supports tags when comparing commits or branches Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Hide headline when only comparing and don't load unused data Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Merges compare logics to allow comparing branches, commits and tags with eachother Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Display branch or tag instead of commit when used for comparing Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show pull request form after click on button Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Transfers relevant pull.go changes from master to compare.go Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Fixes error when comparing forks against a commit or tag Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes console.log from JavaScript file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show icon next to commit reference when comparing branch or tag Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Updates css file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Fixes import order * Renames template variable * Update routers/repo/compare.go Co-Authored-By: zeripath <art27@cantab.net> * Update from master Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Allow short-shas in compare * Renames prInfo to compareInfo Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Check PR permissions only if compare is pull request Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adjusts comment Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use compareInfo instead of prInfo
6 years ago
Compare branches, commits and tags with each other (#6991) * Supports tags when comparing commits or branches Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Hide headline when only comparing and don't load unused data Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Merges compare logics to allow comparing branches, commits and tags with eachother Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Display branch or tag instead of commit when used for comparing Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show pull request form after click on button Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Transfers relevant pull.go changes from master to compare.go Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Fixes error when comparing forks against a commit or tag Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes console.log from JavaScript file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show icon next to commit reference when comparing branch or tag Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Updates css file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Fixes import order * Renames template variable * Update routers/repo/compare.go Co-Authored-By: zeripath <art27@cantab.net> * Update from master Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Allow short-shas in compare * Renames prInfo to compareInfo Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Check PR permissions only if compare is pull request Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adjusts comment Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use compareInfo instead of prInfo
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/auth"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/notification"
  15. "code.gitea.io/gitea/modules/pull"
  16. pull_service "code.gitea.io/gitea/modules/pull"
  17. api "code.gitea.io/gitea/modules/structs"
  18. "code.gitea.io/gitea/modules/timeutil"
  19. milestone_service "code.gitea.io/gitea/services/milestone"
  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. headUser, 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. HeadUserName: headUser.Name,
  253. HeadBranch: headBranch,
  254. BaseBranch: baseBranch,
  255. HeadRepo: headRepo,
  256. BaseRepo: repo,
  257. MergeBase: compareInfo.MergeBase,
  258. Type: models.PullRequestGitea,
  259. }
  260. // Get all assignee IDs
  261. assigneeIDs, err := models.MakeIDsFromAPIAssigneesToAdd(form.Assignee, form.Assignees)
  262. if err != nil {
  263. if models.IsErrUserNotExist(err) {
  264. ctx.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err))
  265. } else {
  266. ctx.Error(500, "AddAssigneeByName", err)
  267. }
  268. return
  269. }
  270. if err := models.NewPullRequest(repo, prIssue, labelIDs, []string{}, pr, patch, assigneeIDs); err != nil {
  271. if models.IsErrUserDoesNotHaveAccessToRepo(err) {
  272. ctx.Error(400, "UserDoesNotHaveAccessToRepo", err)
  273. return
  274. }
  275. ctx.Error(500, "NewPullRequest", err)
  276. return
  277. } else if err := pr.PushToBaseRepo(); err != nil {
  278. ctx.Error(500, "PushToBaseRepo", err)
  279. return
  280. }
  281. notification.NotifyNewPullRequest(pr)
  282. log.Trace("Pull request created: %d/%d", repo.ID, prIssue.ID)
  283. ctx.JSON(201, pr.APIFormat())
  284. }
  285. // EditPullRequest does what it says
  286. func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
  287. // swagger:operation PATCH /repos/{owner}/{repo}/pulls/{index} repository repoEditPullRequest
  288. // ---
  289. // summary: Update a pull request
  290. // consumes:
  291. // - application/json
  292. // produces:
  293. // - application/json
  294. // parameters:
  295. // - name: owner
  296. // in: path
  297. // description: owner of the repo
  298. // type: string
  299. // required: true
  300. // - name: repo
  301. // in: path
  302. // description: name of the repo
  303. // type: string
  304. // required: true
  305. // - name: index
  306. // in: path
  307. // description: index of the pull request to edit
  308. // type: integer
  309. // format: int64
  310. // required: true
  311. // - name: body
  312. // in: body
  313. // schema:
  314. // "$ref": "#/definitions/EditPullRequestOption"
  315. // responses:
  316. // "201":
  317. // "$ref": "#/responses/PullRequest"
  318. pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  319. if err != nil {
  320. if models.IsErrPullRequestNotExist(err) {
  321. ctx.NotFound()
  322. } else {
  323. ctx.Error(500, "GetPullRequestByIndex", err)
  324. }
  325. return
  326. }
  327. err = pr.LoadIssue()
  328. if err != nil {
  329. ctx.Error(http.StatusInternalServerError, "LoadIssue", err)
  330. return
  331. }
  332. issue := pr.Issue
  333. issue.Repo = ctx.Repo.Repository
  334. if !issue.IsPoster(ctx.User.ID) && !ctx.Repo.CanWrite(models.UnitTypePullRequests) {
  335. ctx.Status(403)
  336. return
  337. }
  338. if len(form.Title) > 0 {
  339. issue.Title = form.Title
  340. }
  341. if len(form.Body) > 0 {
  342. issue.Content = form.Body
  343. }
  344. // Update Deadline
  345. var deadlineUnix timeutil.TimeStamp
  346. if form.Deadline != nil && !form.Deadline.IsZero() {
  347. deadlineUnix = timeutil.TimeStamp(form.Deadline.Unix())
  348. }
  349. if err := models.UpdateIssueDeadline(issue, deadlineUnix, ctx.User); err != nil {
  350. ctx.Error(500, "UpdateIssueDeadline", err)
  351. return
  352. }
  353. // Add/delete assignees
  354. // Deleting is done the GitHub way (quote from their api documentation):
  355. // https://developer.github.com/v3/issues/#edit-an-issue
  356. // "assignees" (array): Logins for Users to assign to this issue.
  357. // Pass one or more user logins to replace the set of assignees on this Issue.
  358. // Send an empty array ([]) to clear all assignees from the Issue.
  359. if ctx.Repo.CanWrite(models.UnitTypePullRequests) && (form.Assignees != nil || len(form.Assignee) > 0) {
  360. err = models.UpdateAPIAssignee(issue, form.Assignee, form.Assignees, ctx.User)
  361. if err != nil {
  362. if models.IsErrUserNotExist(err) {
  363. ctx.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err))
  364. } else {
  365. ctx.Error(500, "UpdateAPIAssignee", err)
  366. }
  367. return
  368. }
  369. }
  370. if ctx.Repo.CanWrite(models.UnitTypePullRequests) && form.Milestone != 0 &&
  371. issue.MilestoneID != form.Milestone {
  372. oldMilestoneID := issue.MilestoneID
  373. issue.MilestoneID = form.Milestone
  374. if err = milestone_service.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
  375. ctx.Error(500, "ChangeMilestoneAssign", err)
  376. return
  377. }
  378. }
  379. if ctx.Repo.CanWrite(models.UnitTypePullRequests) && form.Labels != nil {
  380. labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
  381. if err != nil {
  382. ctx.Error(500, "GetLabelsInRepoByIDsError", err)
  383. return
  384. }
  385. if err = issue.ReplaceLabels(labels, ctx.User); err != nil {
  386. ctx.Error(500, "ReplaceLabelsError", err)
  387. return
  388. }
  389. }
  390. if err = models.UpdateIssue(issue); err != nil {
  391. ctx.Error(500, "UpdateIssue", err)
  392. return
  393. }
  394. if form.State != nil {
  395. if err = issue.ChangeStatus(ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
  396. if models.IsErrDependenciesLeft(err) {
  397. ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this pull request because it still has open dependencies")
  398. return
  399. }
  400. ctx.Error(500, "ChangeStatus", err)
  401. return
  402. }
  403. notification.NotifyIssueChangeStatus(ctx.User, issue, api.StateClosed == api.StateType(*form.State))
  404. }
  405. // Refetch from database
  406. pr, err = models.GetPullRequestByIndex(ctx.Repo.Repository.ID, pr.Index)
  407. if err != nil {
  408. if models.IsErrPullRequestNotExist(err) {
  409. ctx.NotFound()
  410. } else {
  411. ctx.Error(500, "GetPullRequestByIndex", err)
  412. }
  413. return
  414. }
  415. // TODO this should be 200, not 201
  416. ctx.JSON(201, pr.APIFormat())
  417. }
  418. // IsPullRequestMerged checks if a PR exists given an index
  419. func IsPullRequestMerged(ctx *context.APIContext) {
  420. // swagger:operation GET /repos/{owner}/{repo}/pulls/{index}/merge repository repoPullRequestIsMerged
  421. // ---
  422. // summary: Check if a pull request has been merged
  423. // produces:
  424. // - application/json
  425. // parameters:
  426. // - name: owner
  427. // in: path
  428. // description: owner of the repo
  429. // type: string
  430. // required: true
  431. // - name: repo
  432. // in: path
  433. // description: name of the repo
  434. // type: string
  435. // required: true
  436. // - name: index
  437. // in: path
  438. // description: index of the pull request
  439. // type: integer
  440. // format: int64
  441. // required: true
  442. // responses:
  443. // "204":
  444. // description: pull request has been merged
  445. // "404":
  446. // description: pull request has not been merged
  447. pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  448. if err != nil {
  449. if models.IsErrPullRequestNotExist(err) {
  450. ctx.NotFound()
  451. } else {
  452. ctx.Error(500, "GetPullRequestByIndex", err)
  453. }
  454. return
  455. }
  456. if pr.HasMerged {
  457. ctx.Status(204)
  458. }
  459. ctx.NotFound()
  460. }
  461. // MergePullRequest merges a PR given an index
  462. func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
  463. // swagger:operation POST /repos/{owner}/{repo}/pulls/{index}/merge repository repoMergePullRequest
  464. // ---
  465. // summary: Merge a pull request
  466. // produces:
  467. // - application/json
  468. // parameters:
  469. // - name: owner
  470. // in: path
  471. // description: owner of the repo
  472. // type: string
  473. // required: true
  474. // - name: repo
  475. // in: path
  476. // description: name of the repo
  477. // type: string
  478. // required: true
  479. // - name: index
  480. // in: path
  481. // description: index of the pull request to merge
  482. // type: integer
  483. // format: int64
  484. // required: true
  485. // - name: body
  486. // in: body
  487. // schema:
  488. // $ref: "#/definitions/MergePullRequestOption"
  489. // responses:
  490. // "200":
  491. // "$ref": "#/responses/empty"
  492. // "405":
  493. // "$ref": "#/responses/empty"
  494. pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  495. if err != nil {
  496. if models.IsErrPullRequestNotExist(err) {
  497. ctx.NotFound("GetPullRequestByIndex", err)
  498. } else {
  499. ctx.Error(500, "GetPullRequestByIndex", err)
  500. }
  501. return
  502. }
  503. if err = pr.GetHeadRepo(); err != nil {
  504. ctx.ServerError("GetHeadRepo", err)
  505. return
  506. }
  507. err = pr.LoadIssue()
  508. if err != nil {
  509. ctx.Error(http.StatusInternalServerError, "LoadIssue", err)
  510. return
  511. }
  512. pr.Issue.Repo = ctx.Repo.Repository
  513. if ctx.IsSigned {
  514. // Update issue-user.
  515. if err = pr.Issue.ReadBy(ctx.User.ID); err != nil {
  516. ctx.Error(500, "ReadBy", err)
  517. return
  518. }
  519. }
  520. if pr.Issue.IsClosed {
  521. ctx.NotFound()
  522. return
  523. }
  524. if !pr.CanAutoMerge() || pr.HasMerged || pr.IsWorkInProgress() {
  525. ctx.Status(405)
  526. return
  527. }
  528. isPass, err := pull_service.IsPullCommitStatusPass(pr)
  529. if err != nil {
  530. ctx.Error(500, "IsPullCommitStatusPass", err)
  531. return
  532. }
  533. if !isPass && !ctx.IsUserRepoAdmin() {
  534. ctx.Status(405)
  535. return
  536. }
  537. if len(form.Do) == 0 {
  538. form.Do = string(models.MergeStyleMerge)
  539. }
  540. message := strings.TrimSpace(form.MergeTitleField)
  541. if len(message) == 0 {
  542. if models.MergeStyle(form.Do) == models.MergeStyleMerge {
  543. message = pr.GetDefaultMergeMessage()
  544. }
  545. if models.MergeStyle(form.Do) == models.MergeStyleSquash {
  546. message = pr.GetDefaultSquashMessage()
  547. }
  548. }
  549. form.MergeMessageField = strings.TrimSpace(form.MergeMessageField)
  550. if len(form.MergeMessageField) > 0 {
  551. message += "\n\n" + form.MergeMessageField
  552. }
  553. if err := pull.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil {
  554. if models.IsErrInvalidMergeStyle(err) {
  555. ctx.Status(405)
  556. return
  557. }
  558. ctx.Error(500, "Merge", err)
  559. return
  560. }
  561. log.Trace("Pull request merged: %d", pr.ID)
  562. ctx.Status(200)
  563. }
  564. func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) (*models.User, *models.Repository, *git.Repository, *git.CompareInfo, string, string) {
  565. baseRepo := ctx.Repo.Repository
  566. // Get compared branches information
  567. // format: <base branch>...[<head repo>:]<head branch>
  568. // base<-head: master...head:feature
  569. // same repo: master...feature
  570. // TODO: Validate form first?
  571. baseBranch := form.Base
  572. var (
  573. headUser *models.User
  574. headBranch string
  575. isSameRepo bool
  576. err error
  577. )
  578. // If there is no head repository, it means pull request between same repository.
  579. headInfos := strings.Split(form.Head, ":")
  580. if len(headInfos) == 1 {
  581. isSameRepo = true
  582. headUser = ctx.Repo.Owner
  583. headBranch = headInfos[0]
  584. } else if len(headInfos) == 2 {
  585. headUser, err = models.GetUserByName(headInfos[0])
  586. if err != nil {
  587. if models.IsErrUserNotExist(err) {
  588. ctx.NotFound("GetUserByName")
  589. } else {
  590. ctx.ServerError("GetUserByName", err)
  591. }
  592. return nil, nil, nil, nil, "", ""
  593. }
  594. headBranch = headInfos[1]
  595. } else {
  596. ctx.NotFound()
  597. return nil, nil, nil, nil, "", ""
  598. }
  599. ctx.Repo.PullRequest.SameRepo = isSameRepo
  600. log.Info("Base branch: %s", baseBranch)
  601. log.Info("Repo path: %s", ctx.Repo.GitRepo.Path)
  602. // Check if base branch is valid.
  603. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  604. ctx.NotFound("IsBranchExist")
  605. return nil, nil, nil, nil, "", ""
  606. }
  607. // Check if current user has fork of repository or in the same repository.
  608. headRepo, has := models.HasForkedRepo(headUser.ID, baseRepo.ID)
  609. if !has && !isSameRepo {
  610. log.Trace("parseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
  611. ctx.NotFound("HasForkedRepo")
  612. return nil, nil, nil, nil, "", ""
  613. }
  614. var headGitRepo *git.Repository
  615. if isSameRepo {
  616. headRepo = ctx.Repo.Repository
  617. headGitRepo = ctx.Repo.GitRepo
  618. } else {
  619. headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  620. if err != nil {
  621. ctx.Error(500, "OpenRepository", err)
  622. return nil, nil, nil, nil, "", ""
  623. }
  624. }
  625. // user should have permission to read baseRepo's codes and pulls, NOT headRepo's
  626. permBase, err := models.GetUserRepoPermission(baseRepo, ctx.User)
  627. if err != nil {
  628. ctx.ServerError("GetUserRepoPermission", err)
  629. return nil, nil, nil, nil, "", ""
  630. }
  631. if !permBase.CanReadIssuesOrPulls(true) || !permBase.CanRead(models.UnitTypeCode) {
  632. if log.IsTrace() {
  633. log.Trace("Permission Denied: User %-v cannot create/read pull requests or cannot read code in Repo %-v\nUser in baseRepo has Permissions: %-+v",
  634. ctx.User,
  635. baseRepo,
  636. permBase)
  637. }
  638. ctx.NotFound("Can't read pulls or can't read UnitTypeCode")
  639. return nil, nil, nil, nil, "", ""
  640. }
  641. // user should have permission to read headrepo's codes
  642. permHead, err := models.GetUserRepoPermission(headRepo, ctx.User)
  643. if err != nil {
  644. ctx.ServerError("GetUserRepoPermission", err)
  645. return nil, nil, nil, nil, "", ""
  646. }
  647. if !permHead.CanRead(models.UnitTypeCode) {
  648. if log.IsTrace() {
  649. log.Trace("Permission Denied: User: %-v cannot read code in Repo: %-v\nUser in headRepo has Permissions: %-+v",
  650. ctx.User,
  651. headRepo,
  652. permHead)
  653. }
  654. ctx.NotFound("Can't read headRepo UnitTypeCode")
  655. return nil, nil, nil, nil, "", ""
  656. }
  657. // Check if head branch is valid.
  658. if !headGitRepo.IsBranchExist(headBranch) {
  659. ctx.NotFound()
  660. return nil, nil, nil, nil, "", ""
  661. }
  662. compareInfo, err := headGitRepo.GetCompareInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
  663. if err != nil {
  664. ctx.Error(500, "GetCompareInfo", err)
  665. return nil, nil, nil, nil, "", ""
  666. }
  667. return headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch
  668. }