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 31 kB

11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
Pull request review/approval and comment on code (#3748) * Initial ui components for pull request review * Add Review Add IssueComment types Signed-off-by: Jonas Franz <info@jonasfranz.software> (cherry picked from commit 2b4daab) Signed-off-by: Jonas Franz <info@jonasfranz.software> * Replace ReviewComment with Content Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add load functions Add ReviewID to findComments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add create review comment implementation Add migration for review Other small changes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Simplified create and find functions for review Signed-off-by: Jonas Franz <info@jonasfranz.software> * Moved "Pending" to first position Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add GetCurrentReview to simplify fetching current review Signed-off-by: Jonas Franz <info@jonasfranz.software> * Preview for listing comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Move new comment form to its own file Signed-off-by: Jonas Franz <info@jonasfranz.software> * Implement Review form Show Review comments on comment stream Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for single comments Showing buttons in context Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add pending tag to pending review comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add unit tests for Review Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fetch all review ids at once Add unit tests Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Improved comment rendering in "Files" view by adding Comments to DiffLine Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for invalidating comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Switched back to code.gitea.io/git Signed-off-by: Jonas Franz <info@jonasfranz.software> * Moved review migration from v64 to v65 Signed-off-by: Jonas Franz <info@jonasfranz.software> * Rebuild css Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Improve translations Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests by updating fixtures and updating outdated test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Comments will be shown at the right place now Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for deleting CodeComments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix problems caused by files in subdirectories Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for showing code comments of reviews in conversation Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for "Show/Hide outdated" Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update code.gitea.io/git Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for new webhooks Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update comparison Signed-off-by: Jonas Franz <info@jonasfranz.software> * Resolve conflicts Signed-off-by: Jonas Franz <info@jonasfranz.software> * Minor UI improvements * update code.gitea.io/git * Fix ui bug reported by @lunny causing wrong position of add button Add functionality to "Cancel" button Add scale effects to add button Hide "Cancel" button for existing comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Prepare solving conflicts Signed-off-by: Jonas Franz <info@jonasfranz.software> * Show add button only if no comments already exist for the line Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing vendor files Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if reviewer is nil Signed-off-by: Jonas Franz <info@jonasfranz.software> * Show forms only to users who are logged in Signed-off-by: Jonas Franz <info@jonasfranz.software> * Revert "Show forms only to users who are logged in" This reverts commit c083682 Signed-off-by: Jonas Franz <info@jonasfranz.software> * Save patch in comment Render patch for code comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add link to comment in code Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add reply form to comment list Show forms only to signed in users Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add 'Reply' as translatable Add CODE_COMMENT_LINES setting Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix problems introduced by checking for singed in user Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add v70 Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update generated stylesheet Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix preview Beginn with new review comment patch system Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add new algo to generate diff for line range Remove old algo used for cutting big diffs (it was very buggy) * Add documentation and example for CutDiffAroundLine * Fix example of CutDiffAroundLine * Fix some comment UI rendering bugs * Add code comment edit mode * Send notifications / actions to users until review gets published Fix diff generation bug Fix wrong hashtag * Fix vet errors * Send notifications also for single comments * Fix some notification bugs, fix link * Fix: add comment icon is only shown on code lines * Add lint comment * Add unit tests for git diff * Add more error messages * Regenerated css Signed-off-by: Jonas Franz <info@jonasfranz.software> * fmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Regenerated CSS with latest less version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by updating comment type to new ID Signed-off-by: Jonas Franz <info@jonasfranz.software> * Introducing CodeComments as type for map[string]map[int64][]*Comment Other minor code improvements Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix data-tab issues Signed-off-by: Jonas Franz <info@jonasfranz.software> * Remove unnecessary change Signed-off-by: Jonas Franz <info@jonasfranz.software> * refactored checkForInvalidation Signed-off-by: Jonas Franz <info@jonasfranz.software> * Append comments instead of setting Signed-off-by: Jonas Franz <info@jonasfranz.software> * Use HeadRepo instead of BaseRepo Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update migration Signed-off-by: Jonas Franz <info@jonasfranz.de> * Regenerated CSS Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add copyright Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update index.css Signed-off-by: Jonas Franz <info@jonasfranz.software>
7 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
Pull request review/approval and comment on code (#3748) * Initial ui components for pull request review * Add Review Add IssueComment types Signed-off-by: Jonas Franz <info@jonasfranz.software> (cherry picked from commit 2b4daab) Signed-off-by: Jonas Franz <info@jonasfranz.software> * Replace ReviewComment with Content Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add load functions Add ReviewID to findComments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add create review comment implementation Add migration for review Other small changes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Simplified create and find functions for review Signed-off-by: Jonas Franz <info@jonasfranz.software> * Moved "Pending" to first position Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add GetCurrentReview to simplify fetching current review Signed-off-by: Jonas Franz <info@jonasfranz.software> * Preview for listing comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Move new comment form to its own file Signed-off-by: Jonas Franz <info@jonasfranz.software> * Implement Review form Show Review comments on comment stream Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for single comments Showing buttons in context Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add pending tag to pending review comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add unit tests for Review Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fetch all review ids at once Add unit tests Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Improved comment rendering in "Files" view by adding Comments to DiffLine Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for invalidating comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Switched back to code.gitea.io/git Signed-off-by: Jonas Franz <info@jonasfranz.software> * Moved review migration from v64 to v65 Signed-off-by: Jonas Franz <info@jonasfranz.software> * Rebuild css Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Improve translations Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests by updating fixtures and updating outdated test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Comments will be shown at the right place now Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for deleting CodeComments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix problems caused by files in subdirectories Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for showing code comments of reviews in conversation Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for "Show/Hide outdated" Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update code.gitea.io/git Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for new webhooks Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update comparison Signed-off-by: Jonas Franz <info@jonasfranz.software> * Resolve conflicts Signed-off-by: Jonas Franz <info@jonasfranz.software> * Minor UI improvements * update code.gitea.io/git * Fix ui bug reported by @lunny causing wrong position of add button Add functionality to "Cancel" button Add scale effects to add button Hide "Cancel" button for existing comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Prepare solving conflicts Signed-off-by: Jonas Franz <info@jonasfranz.software> * Show add button only if no comments already exist for the line Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing vendor files Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if reviewer is nil Signed-off-by: Jonas Franz <info@jonasfranz.software> * Show forms only to users who are logged in Signed-off-by: Jonas Franz <info@jonasfranz.software> * Revert "Show forms only to users who are logged in" This reverts commit c083682 Signed-off-by: Jonas Franz <info@jonasfranz.software> * Save patch in comment Render patch for code comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add link to comment in code Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add reply form to comment list Show forms only to signed in users Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add 'Reply' as translatable Add CODE_COMMENT_LINES setting Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix problems introduced by checking for singed in user Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add v70 Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update generated stylesheet Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix preview Beginn with new review comment patch system Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add new algo to generate diff for line range Remove old algo used for cutting big diffs (it was very buggy) * Add documentation and example for CutDiffAroundLine * Fix example of CutDiffAroundLine * Fix some comment UI rendering bugs * Add code comment edit mode * Send notifications / actions to users until review gets published Fix diff generation bug Fix wrong hashtag * Fix vet errors * Send notifications also for single comments * Fix some notification bugs, fix link * Fix: add comment icon is only shown on code lines * Add lint comment * Add unit tests for git diff * Add more error messages * Regenerated css Signed-off-by: Jonas Franz <info@jonasfranz.software> * fmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Regenerated CSS with latest less version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by updating comment type to new ID Signed-off-by: Jonas Franz <info@jonasfranz.software> * Introducing CodeComments as type for map[string]map[int64][]*Comment Other minor code improvements Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix data-tab issues Signed-off-by: Jonas Franz <info@jonasfranz.software> * Remove unnecessary change Signed-off-by: Jonas Franz <info@jonasfranz.software> * refactored checkForInvalidation Signed-off-by: Jonas Franz <info@jonasfranz.software> * Append comments instead of setting Signed-off-by: Jonas Franz <info@jonasfranz.software> * Use HeadRepo instead of BaseRepo Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update migration Signed-off-by: Jonas Franz <info@jonasfranz.de> * Regenerated CSS Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add copyright Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update index.css Signed-off-by: Jonas Franz <info@jonasfranz.software>
7 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
Squashed commit of the following: commit 0afcb843d7ffd596991c4885cab768273a6eb42c Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 17:13:29 2016 -0600 Removed Upload stats as the upload table is just a temporary table commit 7ecd73ff5535612d79d471409173ee7f1fcfa157 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:42:41 2016 -0600 Fix for CodeMirror mode commit c29b9ab531e2e7af0fb5db24dc17e51027dd1174 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:03:33 2016 -0600 Made tabbing in editor use spaces commit 23af384c53206a8a40e11e45bf49d7a149c4adcd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:56:46 2016 -0600 Fix for data-url commit cfb8a97591cb6fc0a92e49563b7b764c524db0e9 Merge: 7fc8a89 991ce42 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:42:53 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit 7fc8a89cb495478225b02d613e647f99a1489634 Merge: fd3d86c c03d040 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:40:00 2016 -0600 Merge branch 'feature-create-and-edit-repo-file' of github.com:richmahn/gogs into feature-create-and-edit-repo-file commit fd3d86ca6bbc02cfda566a504ffd6b03db4f75ef Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:39:44 2016 -0600 Code cleanup commit c03d0401c1049eeeccc32ab1f9c3303c130be5ee Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 29 15:38:23 2016 -0600 Code cleanup commit 98e1206ccf9f9a4503c020e3a7830cf9f861dfae Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:36:01 2016 -0600 Code cleanup and fixes commit c2895dc742f25f8412879c9fa15e18f27f42f194 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:24:04 2016 -0600 Fixes per Unknwon's requests commit 6aa7e46b21ad4c96e562daa2eac26a8fb408f8ef Merge: 889e9fa ad7ea88 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 17:13:43 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go modules/setting/setting.go commit 889e9faf1bd8559a4979c8f46005d488c1a234d4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:09:18 2016 -0600 Fix in gogs.js commit 47603edf223f147b114be65f3bd27bc1e88827a5 Merge: bb57912 cf85e9e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:07:36 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit bb5791255867a71c11a77b639db050ad09c597a4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:02:18 2016 -0600 Update for using CodeMirror mode addon commit d10d128c51039be19e2af9c66c63db66a9f2ec6d Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 16:12:57 2016 -0600 Update for Edit commit 34a34982025144e3225e389f7849eb6273c1d576 Merge: fa1b752 1c7dcdd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 11:52:02 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go commit fa1b752be29cd455c5184ddac2ffe80b3489763e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 15 18:35:42 2016 -0600 Feature for editing, creating, uploading and deleting files
9 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. // Copyright 2018 The Gitea Authors.
  2. // Copyright 2014 The Gogs Authors.
  3. // All rights reserved.
  4. // Use of this source code is governed by a MIT-style
  5. // license that can be found in the LICENSE file.
  6. package repo
  7. import (
  8. "container/list"
  9. "fmt"
  10. "io"
  11. "net/url"
  12. "path"
  13. "strings"
  14. "code.gitea.io/git"
  15. "code.gitea.io/gitea/models"
  16. "code.gitea.io/gitea/modules/auth"
  17. "code.gitea.io/gitea/modules/base"
  18. "code.gitea.io/gitea/modules/context"
  19. "code.gitea.io/gitea/modules/log"
  20. "code.gitea.io/gitea/modules/notification"
  21. "code.gitea.io/gitea/modules/setting"
  22. "github.com/Unknwon/com"
  23. )
  24. const (
  25. tplFork base.TplName = "repo/pulls/fork"
  26. tplComparePull base.TplName = "repo/pulls/compare"
  27. tplPullCommits base.TplName = "repo/pulls/commits"
  28. tplPullFiles base.TplName = "repo/pulls/files"
  29. pullRequestTemplateKey = "PullRequestTemplate"
  30. )
  31. var (
  32. pullRequestTemplateCandidates = []string{
  33. "PULL_REQUEST_TEMPLATE.md",
  34. "pull_request_template.md",
  35. ".gitea/PULL_REQUEST_TEMPLATE.md",
  36. ".gitea/pull_request_template.md",
  37. ".github/PULL_REQUEST_TEMPLATE.md",
  38. ".github/pull_request_template.md",
  39. }
  40. )
  41. func getForkRepository(ctx *context.Context) *models.Repository {
  42. forkRepo, err := models.GetRepositoryByID(ctx.ParamsInt64(":repoid"))
  43. if err != nil {
  44. if models.IsErrRepoNotExist(err) {
  45. ctx.NotFound("GetRepositoryByID", nil)
  46. } else {
  47. ctx.ServerError("GetRepositoryByID", err)
  48. }
  49. return nil
  50. }
  51. if !forkRepo.CanBeForked() || !forkRepo.HasAccess(ctx.User) {
  52. ctx.NotFound("getForkRepository", nil)
  53. return nil
  54. }
  55. ctx.Data["repo_name"] = forkRepo.Name
  56. ctx.Data["description"] = forkRepo.Description
  57. ctx.Data["IsPrivate"] = forkRepo.IsPrivate
  58. canForkToUser := forkRepo.OwnerID != ctx.User.ID && !ctx.User.HasForkedRepo(forkRepo.ID)
  59. if err = forkRepo.GetOwner(); err != nil {
  60. ctx.ServerError("GetOwner", err)
  61. return nil
  62. }
  63. ctx.Data["ForkFrom"] = forkRepo.Owner.Name + "/" + forkRepo.Name
  64. ctx.Data["ForkFromOwnerID"] = forkRepo.Owner.ID
  65. if err := ctx.User.GetOwnedOrganizations(); err != nil {
  66. ctx.ServerError("GetOwnedOrganizations", err)
  67. return nil
  68. }
  69. var orgs []*models.User
  70. for _, org := range ctx.User.OwnedOrgs {
  71. if forkRepo.OwnerID != org.ID && !org.HasForkedRepo(forkRepo.ID) {
  72. orgs = append(orgs, org)
  73. }
  74. }
  75. var traverseParentRepo = forkRepo
  76. for {
  77. if ctx.User.ID == traverseParentRepo.OwnerID {
  78. canForkToUser = false
  79. } else {
  80. for i, org := range orgs {
  81. if org.ID == traverseParentRepo.OwnerID {
  82. orgs = append(orgs[:i], orgs[i+1:]...)
  83. break
  84. }
  85. }
  86. }
  87. if !traverseParentRepo.IsFork {
  88. break
  89. }
  90. traverseParentRepo, err = models.GetRepositoryByID(traverseParentRepo.ForkID)
  91. if err != nil {
  92. ctx.ServerError("GetRepositoryByID", err)
  93. return nil
  94. }
  95. }
  96. ctx.Data["CanForkToUser"] = canForkToUser
  97. ctx.Data["Orgs"] = orgs
  98. if canForkToUser {
  99. ctx.Data["ContextUser"] = ctx.User
  100. } else if len(orgs) > 0 {
  101. ctx.Data["ContextUser"] = orgs[0]
  102. }
  103. return forkRepo
  104. }
  105. // Fork render repository fork page
  106. func Fork(ctx *context.Context) {
  107. ctx.Data["Title"] = ctx.Tr("new_fork")
  108. getForkRepository(ctx)
  109. if ctx.Written() {
  110. return
  111. }
  112. ctx.HTML(200, tplFork)
  113. }
  114. // ForkPost response for forking a repository
  115. func ForkPost(ctx *context.Context, form auth.CreateRepoForm) {
  116. ctx.Data["Title"] = ctx.Tr("new_fork")
  117. ctxUser := checkContextUser(ctx, form.UID)
  118. if ctx.Written() {
  119. return
  120. }
  121. forkRepo := getForkRepository(ctx)
  122. if ctx.Written() {
  123. return
  124. }
  125. ctx.Data["ContextUser"] = ctxUser
  126. if ctx.HasError() {
  127. ctx.HTML(200, tplFork)
  128. return
  129. }
  130. var err error
  131. var traverseParentRepo = forkRepo
  132. for {
  133. if ctxUser.ID == traverseParentRepo.OwnerID {
  134. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplFork, &form)
  135. return
  136. }
  137. repo, has := models.HasForkedRepo(ctxUser.ID, traverseParentRepo.ID)
  138. if has {
  139. ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + repo.Name)
  140. return
  141. }
  142. if !traverseParentRepo.IsFork {
  143. break
  144. }
  145. traverseParentRepo, err = models.GetRepositoryByID(traverseParentRepo.ForkID)
  146. if err != nil {
  147. ctx.ServerError("GetRepositoryByID", err)
  148. return
  149. }
  150. }
  151. // Check ownership of organization.
  152. if ctxUser.IsOrganization() {
  153. isOwner, err := ctxUser.IsOwnedBy(ctx.User.ID)
  154. if err != nil {
  155. ctx.ServerError("IsOwnedBy", err)
  156. return
  157. } else if !isOwner {
  158. ctx.Error(403)
  159. return
  160. }
  161. }
  162. repo, err := models.ForkRepository(ctx.User, ctxUser, forkRepo, form.RepoName, form.Description)
  163. if err != nil {
  164. ctx.Data["Err_RepoName"] = true
  165. switch {
  166. case models.IsErrRepoAlreadyExist(err):
  167. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplFork, &form)
  168. case models.IsErrNameReserved(err):
  169. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), tplFork, &form)
  170. case models.IsErrNamePatternNotAllowed(err):
  171. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplFork, &form)
  172. default:
  173. ctx.ServerError("ForkPost", err)
  174. }
  175. return
  176. }
  177. log.Trace("Repository forked[%d]: %s/%s", forkRepo.ID, ctxUser.Name, repo.Name)
  178. ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + repo.Name)
  179. }
  180. func checkPullInfo(ctx *context.Context) *models.Issue {
  181. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  182. if err != nil {
  183. if models.IsErrIssueNotExist(err) {
  184. ctx.NotFound("GetIssueByIndex", err)
  185. } else {
  186. ctx.ServerError("GetIssueByIndex", err)
  187. }
  188. return nil
  189. }
  190. ctx.Data["Title"] = fmt.Sprintf("#%d - %s", issue.Index, issue.Title)
  191. ctx.Data["Issue"] = issue
  192. if !issue.IsPull {
  193. ctx.NotFound("ViewPullCommits", nil)
  194. return nil
  195. }
  196. if err = issue.PullRequest.GetHeadRepo(); err != nil {
  197. ctx.ServerError("GetHeadRepo", err)
  198. return nil
  199. }
  200. if ctx.IsSigned {
  201. // Update issue-user.
  202. if err = issue.ReadBy(ctx.User.ID); err != nil {
  203. ctx.ServerError("ReadBy", err)
  204. return nil
  205. }
  206. }
  207. return issue
  208. }
  209. func setMergeTarget(ctx *context.Context, pull *models.PullRequest) {
  210. if ctx.Repo.Owner.Name == pull.HeadUserName {
  211. ctx.Data["HeadTarget"] = pull.HeadBranch
  212. } else if pull.HeadRepo == nil {
  213. ctx.Data["HeadTarget"] = pull.HeadUserName + ":" + pull.HeadBranch
  214. } else {
  215. ctx.Data["HeadTarget"] = pull.HeadUserName + "/" + pull.HeadRepo.Name + ":" + pull.HeadBranch
  216. }
  217. ctx.Data["BaseTarget"] = pull.BaseBranch
  218. }
  219. // PrepareMergedViewPullInfo show meta information for a merged pull request view page
  220. func PrepareMergedViewPullInfo(ctx *context.Context, issue *models.Issue) *git.PullRequestInfo {
  221. pull := issue.PullRequest
  222. setMergeTarget(ctx, pull)
  223. ctx.Data["HasMerged"] = true
  224. prInfo, err := ctx.Repo.GitRepo.GetPullRequestInfo(ctx.Repo.Repository.RepoPath(),
  225. pull.MergeBase, pull.GetGitRefName())
  226. if err != nil {
  227. if strings.Contains(err.Error(), "fatal: Not a valid object name") {
  228. ctx.Data["IsPullRequestBroken"] = true
  229. ctx.Data["BaseTarget"] = "deleted"
  230. ctx.Data["NumCommits"] = 0
  231. ctx.Data["NumFiles"] = 0
  232. return nil
  233. }
  234. ctx.ServerError("GetPullRequestInfo", err)
  235. return nil
  236. }
  237. ctx.Data["NumCommits"] = prInfo.Commits.Len()
  238. ctx.Data["NumFiles"] = prInfo.NumFiles
  239. return prInfo
  240. }
  241. // PrepareViewPullInfo show meta information for a pull request preview page
  242. func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.PullRequestInfo {
  243. repo := ctx.Repo.Repository
  244. pull := issue.PullRequest
  245. var err error
  246. if err = pull.GetHeadRepo(); err != nil {
  247. ctx.ServerError("GetHeadRepo", err)
  248. return nil
  249. }
  250. setMergeTarget(ctx, pull)
  251. var headGitRepo *git.Repository
  252. if pull.HeadRepo != nil {
  253. headGitRepo, err = git.OpenRepository(pull.HeadRepo.RepoPath())
  254. if err != nil {
  255. ctx.ServerError("OpenRepository", err)
  256. return nil
  257. }
  258. }
  259. if pull.HeadRepo == nil || !headGitRepo.IsBranchExist(pull.HeadBranch) {
  260. ctx.Data["IsPullRequestBroken"] = true
  261. ctx.Data["HeadTarget"] = "deleted"
  262. ctx.Data["NumCommits"] = 0
  263. ctx.Data["NumFiles"] = 0
  264. return nil
  265. }
  266. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name),
  267. pull.BaseBranch, pull.HeadBranch)
  268. if err != nil {
  269. if strings.Contains(err.Error(), "fatal: Not a valid object name") {
  270. ctx.Data["IsPullRequestBroken"] = true
  271. ctx.Data["BaseTarget"] = "deleted"
  272. ctx.Data["NumCommits"] = 0
  273. ctx.Data["NumFiles"] = 0
  274. return nil
  275. }
  276. ctx.ServerError("GetPullRequestInfo", err)
  277. return nil
  278. }
  279. if pull.IsWorkInProgress() {
  280. ctx.Data["IsPullWorkInProgress"] = true
  281. ctx.Data["WorkInProgressPrefix"] = pull.GetWorkInProgressPrefix()
  282. }
  283. ctx.Data["NumCommits"] = prInfo.Commits.Len()
  284. ctx.Data["NumFiles"] = prInfo.NumFiles
  285. return prInfo
  286. }
  287. // ViewPullCommits show commits for a pull request
  288. func ViewPullCommits(ctx *context.Context) {
  289. ctx.Data["PageIsPullList"] = true
  290. ctx.Data["PageIsPullCommits"] = true
  291. issue := checkPullInfo(ctx)
  292. if ctx.Written() {
  293. return
  294. }
  295. pull := issue.PullRequest
  296. var commits *list.List
  297. if pull.HasMerged {
  298. prInfo := PrepareMergedViewPullInfo(ctx, issue)
  299. if ctx.Written() {
  300. return
  301. } else if prInfo == nil {
  302. ctx.NotFound("ViewPullCommits", nil)
  303. return
  304. }
  305. ctx.Data["Username"] = ctx.Repo.Owner.Name
  306. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  307. commits = prInfo.Commits
  308. } else {
  309. prInfo := PrepareViewPullInfo(ctx, issue)
  310. if ctx.Written() {
  311. return
  312. } else if prInfo == nil {
  313. ctx.NotFound("ViewPullCommits", nil)
  314. return
  315. }
  316. ctx.Data["Username"] = pull.HeadUserName
  317. ctx.Data["Reponame"] = pull.HeadRepo.Name
  318. commits = prInfo.Commits
  319. }
  320. commits = models.ValidateCommitsWithEmails(commits)
  321. commits = models.ParseCommitsWithSignature(commits)
  322. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  323. ctx.Data["Commits"] = commits
  324. ctx.Data["CommitCount"] = commits.Len()
  325. ctx.HTML(200, tplPullCommits)
  326. }
  327. // ViewPullFiles render pull request changed files list page
  328. func ViewPullFiles(ctx *context.Context) {
  329. ctx.Data["PageIsPullList"] = true
  330. ctx.Data["PageIsPullFiles"] = true
  331. issue := checkPullInfo(ctx)
  332. if ctx.Written() {
  333. return
  334. }
  335. pull := issue.PullRequest
  336. whitespaceFlags := map[string]string{
  337. "ignore-all": "-w",
  338. "ignore-change": "-b",
  339. "ignore-eol": "--ignore-space-at-eol",
  340. "": ""}
  341. var (
  342. diffRepoPath string
  343. startCommitID string
  344. endCommitID string
  345. gitRepo *git.Repository
  346. )
  347. var headTarget string
  348. if pull.HasMerged {
  349. prInfo := PrepareMergedViewPullInfo(ctx, issue)
  350. if ctx.Written() {
  351. return
  352. } else if prInfo == nil {
  353. ctx.NotFound("ViewPullFiles", nil)
  354. return
  355. }
  356. diffRepoPath = ctx.Repo.GitRepo.Path
  357. gitRepo = ctx.Repo.GitRepo
  358. headCommitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName())
  359. if err != nil {
  360. ctx.ServerError("GetRefCommitID", err)
  361. return
  362. }
  363. startCommitID = prInfo.MergeBase
  364. endCommitID = headCommitID
  365. headTarget = path.Join(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  366. ctx.Data["Username"] = ctx.Repo.Owner.Name
  367. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  368. } else {
  369. prInfo := PrepareViewPullInfo(ctx, issue)
  370. if ctx.Written() {
  371. return
  372. } else if prInfo == nil {
  373. ctx.NotFound("ViewPullFiles", nil)
  374. return
  375. }
  376. headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
  377. headGitRepo, err := git.OpenRepository(headRepoPath)
  378. if err != nil {
  379. ctx.ServerError("OpenRepository", err)
  380. return
  381. }
  382. headCommitID, err := headGitRepo.GetBranchCommitID(pull.HeadBranch)
  383. if err != nil {
  384. ctx.ServerError("GetBranchCommitID", err)
  385. return
  386. }
  387. diffRepoPath = headRepoPath
  388. startCommitID = prInfo.MergeBase
  389. endCommitID = headCommitID
  390. gitRepo = headGitRepo
  391. headTarget = path.Join(pull.HeadUserName, pull.HeadRepo.Name)
  392. ctx.Data["Username"] = pull.HeadUserName
  393. ctx.Data["Reponame"] = pull.HeadRepo.Name
  394. }
  395. diff, err := models.GetDiffRangeWithWhitespaceBehavior(diffRepoPath,
  396. startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
  397. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles,
  398. whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)])
  399. if err != nil {
  400. ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err)
  401. return
  402. }
  403. if err = diff.LoadComments(issue, ctx.User); err != nil {
  404. ctx.ServerError("LoadComments", err)
  405. return
  406. }
  407. ctx.Data["Diff"] = diff
  408. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  409. commit, err := gitRepo.GetCommit(endCommitID)
  410. if err != nil {
  411. ctx.ServerError("GetCommit", err)
  412. return
  413. }
  414. ctx.Data["IsImageFile"] = commit.IsImageFile
  415. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", endCommitID)
  416. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", startCommitID)
  417. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", "commit", endCommitID)
  418. ctx.Data["RequireHighlightJS"] = true
  419. ctx.Data["RequireTribute"] = true
  420. if ctx.Data["Assignees"], err = ctx.Repo.Repository.GetAssignees(); err != nil {
  421. ctx.ServerError("GetAssignees", err)
  422. return
  423. }
  424. ctx.Data["CurrentReview"], err = models.GetCurrentReview(ctx.User, issue)
  425. if err != nil && !models.IsErrReviewNotExist(err) {
  426. ctx.ServerError("GetCurrentReview", err)
  427. return
  428. }
  429. ctx.HTML(200, tplPullFiles)
  430. }
  431. // MergePullRequest response for merging pull request
  432. func MergePullRequest(ctx *context.Context, form auth.MergePullRequestForm) {
  433. issue := checkPullInfo(ctx)
  434. if ctx.Written() {
  435. return
  436. }
  437. if issue.IsClosed {
  438. ctx.NotFound("MergePullRequest", nil)
  439. return
  440. }
  441. pr, err := models.GetPullRequestByIssueID(issue.ID)
  442. if err != nil {
  443. if models.IsErrPullRequestNotExist(err) {
  444. ctx.NotFound("GetPullRequestByIssueID", nil)
  445. } else {
  446. ctx.ServerError("GetPullRequestByIssueID", err)
  447. }
  448. return
  449. }
  450. pr.Issue = issue
  451. if !pr.CanAutoMerge() || pr.HasMerged {
  452. ctx.NotFound("MergePullRequest", nil)
  453. return
  454. }
  455. if pr.IsWorkInProgress() {
  456. ctx.Flash.Error(ctx.Tr("repo.pulls.no_merge_wip"))
  457. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  458. return
  459. }
  460. if ctx.HasError() {
  461. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  462. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  463. return
  464. }
  465. message := strings.TrimSpace(form.MergeTitleField)
  466. if len(message) == 0 {
  467. if models.MergeStyle(form.Do) == models.MergeStyleMerge {
  468. message = pr.GetDefaultMergeMessage()
  469. }
  470. if models.MergeStyle(form.Do) == models.MergeStyleSquash {
  471. message = pr.GetDefaultSquashMessage()
  472. }
  473. }
  474. form.MergeMessageField = strings.TrimSpace(form.MergeMessageField)
  475. if len(form.MergeMessageField) > 0 {
  476. message += "\n\n" + form.MergeMessageField
  477. }
  478. pr.Issue = issue
  479. pr.Issue.Repo = ctx.Repo.Repository
  480. noDeps, err := models.IssueNoDependenciesLeft(issue)
  481. if err != nil {
  482. return
  483. }
  484. if !noDeps {
  485. ctx.Flash.Error(ctx.Tr("repo.issues.dependency.pr_close_blocked"))
  486. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  487. return
  488. }
  489. if err = pr.Merge(ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil {
  490. if models.IsErrInvalidMergeStyle(err) {
  491. ctx.Flash.Error(ctx.Tr("repo.pulls.invalid_merge_option"))
  492. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  493. return
  494. }
  495. ctx.ServerError("Merge", err)
  496. return
  497. }
  498. notification.NotifyMergePullRequest(pr, ctx.User, ctx.Repo.GitRepo)
  499. log.Trace("Pull request merged: %d", pr.ID)
  500. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  501. }
  502. // ParseCompareInfo parse compare info between two commit for preparing pull request
  503. func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
  504. baseRepo := ctx.Repo.Repository
  505. // Get compared branches information
  506. // format: <base branch>...[<head repo>:]<head branch>
  507. // base<-head: master...head:feature
  508. // same repo: master...feature
  509. var (
  510. headUser *models.User
  511. headBranch string
  512. isSameRepo bool
  513. infoPath string
  514. err error
  515. )
  516. infoPath, err = url.QueryUnescape(ctx.Params("*"))
  517. if err != nil {
  518. ctx.NotFound("QueryUnescape", err)
  519. }
  520. infos := strings.Split(infoPath, "...")
  521. if len(infos) != 2 {
  522. log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)
  523. ctx.NotFound("CompareAndPullRequest", nil)
  524. return nil, nil, nil, nil, "", ""
  525. }
  526. baseBranch := infos[0]
  527. ctx.Data["BaseBranch"] = baseBranch
  528. // If there is no head repository, it means pull request between same repository.
  529. headInfos := strings.Split(infos[1], ":")
  530. if len(headInfos) == 1 {
  531. isSameRepo = true
  532. headUser = ctx.Repo.Owner
  533. headBranch = headInfos[0]
  534. } else if len(headInfos) == 2 {
  535. headUser, err = models.GetUserByName(headInfos[0])
  536. if err != nil {
  537. if models.IsErrUserNotExist(err) {
  538. ctx.NotFound("GetUserByName", nil)
  539. } else {
  540. ctx.ServerError("GetUserByName", err)
  541. }
  542. return nil, nil, nil, nil, "", ""
  543. }
  544. headBranch = headInfos[1]
  545. isSameRepo = headUser.ID == ctx.Repo.Owner.ID
  546. } else {
  547. ctx.NotFound("CompareAndPullRequest", nil)
  548. return nil, nil, nil, nil, "", ""
  549. }
  550. ctx.Data["HeadUser"] = headUser
  551. ctx.Data["HeadBranch"] = headBranch
  552. ctx.Repo.PullRequest.SameRepo = isSameRepo
  553. // Check if base branch is valid.
  554. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  555. ctx.NotFound("IsBranchExist", nil)
  556. return nil, nil, nil, nil, "", ""
  557. }
  558. // Check if current user has fork of repository or in the same repository.
  559. headRepo, has := models.HasForkedRepo(headUser.ID, baseRepo.ID)
  560. if !has && !isSameRepo {
  561. log.Trace("ParseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
  562. ctx.NotFound("ParseCompareInfo", nil)
  563. return nil, nil, nil, nil, "", ""
  564. }
  565. var headGitRepo *git.Repository
  566. if isSameRepo {
  567. headRepo = ctx.Repo.Repository
  568. headGitRepo = ctx.Repo.GitRepo
  569. } else {
  570. headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  571. if err != nil {
  572. ctx.ServerError("OpenRepository", err)
  573. return nil, nil, nil, nil, "", ""
  574. }
  575. }
  576. if !ctx.User.IsWriterOfRepo(headRepo) && !ctx.User.IsAdmin {
  577. log.Trace("ParseCompareInfo[%d]: does not have write access or site admin", baseRepo.ID)
  578. ctx.NotFound("ParseCompareInfo", nil)
  579. return nil, nil, nil, nil, "", ""
  580. }
  581. // Check if head branch is valid.
  582. if !headGitRepo.IsBranchExist(headBranch) {
  583. ctx.NotFound("IsBranchExist", nil)
  584. return nil, nil, nil, nil, "", ""
  585. }
  586. headBranches, err := headGitRepo.GetBranches()
  587. if err != nil {
  588. ctx.ServerError("GetBranches", err)
  589. return nil, nil, nil, nil, "", ""
  590. }
  591. ctx.Data["HeadBranches"] = headBranches
  592. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
  593. if err != nil {
  594. ctx.ServerError("GetPullRequestInfo", err)
  595. return nil, nil, nil, nil, "", ""
  596. }
  597. ctx.Data["BeforeCommitID"] = prInfo.MergeBase
  598. return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
  599. }
  600. // PrepareCompareDiff render pull request preview diff page
  601. func PrepareCompareDiff(
  602. ctx *context.Context,
  603. headUser *models.User,
  604. headRepo *models.Repository,
  605. headGitRepo *git.Repository,
  606. prInfo *git.PullRequestInfo,
  607. baseBranch, headBranch string) bool {
  608. var (
  609. repo = ctx.Repo.Repository
  610. err error
  611. )
  612. // Get diff information.
  613. ctx.Data["CommitRepoLink"] = headRepo.Link()
  614. headCommitID, err := headGitRepo.GetBranchCommitID(headBranch)
  615. if err != nil {
  616. ctx.ServerError("GetBranchCommitID", err)
  617. return false
  618. }
  619. ctx.Data["AfterCommitID"] = headCommitID
  620. if headCommitID == prInfo.MergeBase {
  621. ctx.Data["IsNothingToCompare"] = true
  622. return true
  623. }
  624. diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
  625. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
  626. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  627. if err != nil {
  628. ctx.ServerError("GetDiffRange", err)
  629. return false
  630. }
  631. ctx.Data["Diff"] = diff
  632. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  633. headCommit, err := headGitRepo.GetCommit(headCommitID)
  634. if err != nil {
  635. ctx.ServerError("GetCommit", err)
  636. return false
  637. }
  638. prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
  639. prInfo.Commits = models.ParseCommitsWithSignature(prInfo.Commits)
  640. prInfo.Commits = models.ParseCommitsWithStatus(prInfo.Commits, headRepo)
  641. ctx.Data["Commits"] = prInfo.Commits
  642. ctx.Data["CommitCount"] = prInfo.Commits.Len()
  643. ctx.Data["Username"] = headUser.Name
  644. ctx.Data["Reponame"] = headRepo.Name
  645. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  646. headTarget := path.Join(headUser.Name, repo.Name)
  647. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", headCommitID)
  648. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", prInfo.MergeBase)
  649. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", "commit", headCommitID)
  650. return false
  651. }
  652. // CompareAndPullRequest render pull request preview page
  653. func CompareAndPullRequest(ctx *context.Context) {
  654. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  655. ctx.Data["PageIsComparePull"] = true
  656. ctx.Data["IsDiffCompare"] = true
  657. ctx.Data["RequireHighlightJS"] = true
  658. ctx.Data["RequireTribute"] = true
  659. ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
  660. setTemplateIfExists(ctx, pullRequestTemplateKey, pullRequestTemplateCandidates)
  661. renderAttachmentSettings(ctx)
  662. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  663. if ctx.Written() {
  664. return
  665. }
  666. pr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
  667. if err != nil {
  668. if !models.IsErrPullRequestNotExist(err) {
  669. ctx.ServerError("GetUnmergedPullRequest", err)
  670. return
  671. }
  672. } else {
  673. ctx.Data["HasPullRequest"] = true
  674. ctx.Data["PullRequest"] = pr
  675. ctx.HTML(200, tplComparePull)
  676. return
  677. }
  678. nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  679. if ctx.Written() {
  680. return
  681. }
  682. if !nothingToCompare {
  683. // Setup information for new form.
  684. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  685. if ctx.Written() {
  686. return
  687. }
  688. }
  689. ctx.HTML(200, tplComparePull)
  690. }
  691. // CompareAndPullRequestPost response for creating pull request
  692. func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm) {
  693. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  694. ctx.Data["PageIsComparePull"] = true
  695. ctx.Data["IsDiffCompare"] = true
  696. ctx.Data["RequireHighlightJS"] = true
  697. ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
  698. renderAttachmentSettings(ctx)
  699. var (
  700. repo = ctx.Repo.Repository
  701. attachments []string
  702. )
  703. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  704. if ctx.Written() {
  705. return
  706. }
  707. labelIDs, assigneeIDs, milestoneID := ValidateRepoMetas(ctx, form)
  708. if ctx.Written() {
  709. return
  710. }
  711. if setting.AttachmentEnabled {
  712. attachments = form.Files
  713. }
  714. if ctx.HasError() {
  715. auth.AssignForm(form, ctx.Data)
  716. // This stage is already stop creating new pull request, so it does not matter if it has
  717. // something to compare or not.
  718. PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  719. if ctx.Written() {
  720. return
  721. }
  722. ctx.HTML(200, tplComparePull)
  723. return
  724. }
  725. patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
  726. if err != nil {
  727. ctx.ServerError("GetPatch", err)
  728. return
  729. }
  730. pullIssue := &models.Issue{
  731. RepoID: repo.ID,
  732. Index: repo.NextIssueIndex(),
  733. Title: form.Title,
  734. PosterID: ctx.User.ID,
  735. Poster: ctx.User,
  736. MilestoneID: milestoneID,
  737. IsPull: true,
  738. Content: form.Content,
  739. }
  740. pullRequest := &models.PullRequest{
  741. HeadRepoID: headRepo.ID,
  742. BaseRepoID: repo.ID,
  743. HeadUserName: headUser.Name,
  744. HeadBranch: headBranch,
  745. BaseBranch: baseBranch,
  746. HeadRepo: headRepo,
  747. BaseRepo: repo,
  748. MergeBase: prInfo.MergeBase,
  749. Type: models.PullRequestGitea,
  750. }
  751. // FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
  752. // instead of 500.
  753. if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch, assigneeIDs); err != nil {
  754. if models.IsErrUserDoesNotHaveAccessToRepo(err) {
  755. ctx.Error(400, "UserDoesNotHaveAccessToRepo", err.Error())
  756. return
  757. }
  758. ctx.ServerError("NewPullRequest", err)
  759. return
  760. } else if err := pullRequest.PushToBaseRepo(); err != nil {
  761. ctx.ServerError("PushToBaseRepo", err)
  762. return
  763. }
  764. notification.NotifyNewPullRequest(pullRequest)
  765. log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
  766. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
  767. }
  768. // TriggerTask response for a trigger task request
  769. func TriggerTask(ctx *context.Context) {
  770. pusherID := ctx.QueryInt64("pusher")
  771. branch := ctx.Query("branch")
  772. secret := ctx.Query("secret")
  773. if len(branch) == 0 || len(secret) == 0 || pusherID <= 0 {
  774. ctx.Error(404)
  775. log.Trace("TriggerTask: branch or secret is empty, or pusher ID is not valid")
  776. return
  777. }
  778. owner, repo := parseOwnerAndRepo(ctx)
  779. if ctx.Written() {
  780. return
  781. }
  782. if secret != base.EncodeMD5(owner.Salt) {
  783. ctx.Error(404)
  784. log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
  785. return
  786. }
  787. pusher, err := models.GetUserByID(pusherID)
  788. if err != nil {
  789. if models.IsErrUserNotExist(err) {
  790. ctx.Error(404)
  791. } else {
  792. ctx.ServerError("GetUserByID", err)
  793. }
  794. return
  795. }
  796. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  797. go models.HookQueue.Add(repo.ID)
  798. go models.AddTestPullRequestTask(pusher, repo.ID, branch, true)
  799. ctx.Status(202)
  800. }
  801. // CleanUpPullRequest responses for delete merged branch when PR has been merged
  802. func CleanUpPullRequest(ctx *context.Context) {
  803. issue := checkPullInfo(ctx)
  804. if ctx.Written() {
  805. return
  806. }
  807. pr, err := models.GetPullRequestByIssueID(issue.ID)
  808. if err != nil {
  809. if models.IsErrPullRequestNotExist(err) {
  810. ctx.NotFound("GetPullRequestByIssueID", nil)
  811. } else {
  812. ctx.ServerError("GetPullRequestByIssueID", err)
  813. }
  814. return
  815. }
  816. // Allow cleanup only for merged PR
  817. if !pr.HasMerged {
  818. ctx.NotFound("CleanUpPullRequest", nil)
  819. return
  820. }
  821. if err = pr.GetHeadRepo(); err != nil {
  822. ctx.ServerError("GetHeadRepo", err)
  823. return
  824. } else if pr.HeadRepo == nil {
  825. // Forked repository has already been deleted
  826. ctx.NotFound("CleanUpPullRequest", nil)
  827. return
  828. } else if pr.GetBaseRepo(); err != nil {
  829. ctx.ServerError("GetBaseRepo", err)
  830. return
  831. } else if pr.HeadRepo.GetOwner(); err != nil {
  832. ctx.ServerError("HeadRepo.GetOwner", err)
  833. return
  834. }
  835. if !ctx.User.IsWriterOfRepo(pr.HeadRepo) {
  836. ctx.NotFound("CleanUpPullRequest", nil)
  837. return
  838. }
  839. fullBranchName := pr.HeadRepo.Owner.Name + "/" + pr.HeadBranch
  840. gitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
  841. if err != nil {
  842. ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.RepoPath()), err)
  843. return
  844. }
  845. gitBaseRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
  846. if err != nil {
  847. ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.BaseRepo.RepoPath()), err)
  848. return
  849. }
  850. defer func() {
  851. ctx.JSON(200, map[string]interface{}{
  852. "redirect": pr.BaseRepo.Link() + "/pulls/" + com.ToStr(issue.Index),
  853. })
  854. }()
  855. if pr.HeadBranch == pr.HeadRepo.DefaultBranch || !gitRepo.IsBranchExist(pr.HeadBranch) {
  856. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  857. return
  858. }
  859. // Check if branch is not protected
  860. if protected, err := pr.HeadRepo.IsProtectedBranch(pr.HeadBranch, ctx.User); err != nil || protected {
  861. if err != nil {
  862. log.Error(4, "HeadRepo.IsProtectedBranch: %v", err)
  863. }
  864. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  865. return
  866. }
  867. // Check if branch has no new commits
  868. headCommitID, err := gitBaseRepo.GetRefCommitID(pr.GetGitRefName())
  869. if err != nil {
  870. log.Error(4, "GetRefCommitID: %v", err)
  871. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  872. return
  873. }
  874. branchCommitID, err := gitRepo.GetBranchCommitID(pr.HeadBranch)
  875. if err != nil {
  876. log.Error(4, "GetBranchCommitID: %v", err)
  877. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  878. return
  879. }
  880. if headCommitID != branchCommitID {
  881. ctx.Flash.Error(ctx.Tr("repo.branch.delete_branch_has_new_commits", fullBranchName))
  882. return
  883. }
  884. if err := gitRepo.DeleteBranch(pr.HeadBranch, git.DeleteBranchOptions{
  885. Force: true,
  886. }); err != nil {
  887. log.Error(4, "DeleteBranch: %v", err)
  888. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  889. return
  890. }
  891. if err := models.AddDeletePRBranchComment(ctx.User, pr.BaseRepo, issue.ID, pr.HeadBranch); err != nil {
  892. // Do not fail here as branch has already been deleted
  893. log.Error(4, "DeleteBranch: %v", err)
  894. }
  895. ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", fullBranchName))
  896. }
  897. // DownloadPullDiff render a pull's raw diff
  898. func DownloadPullDiff(ctx *context.Context) {
  899. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  900. if err != nil {
  901. if models.IsErrIssueNotExist(err) {
  902. ctx.NotFound("GetIssueByIndex", err)
  903. } else {
  904. ctx.ServerError("GetIssueByIndex", err)
  905. }
  906. return
  907. }
  908. // Return not found if it's not a pull request
  909. if !issue.IsPull {
  910. ctx.NotFound("DownloadPullDiff",
  911. fmt.Errorf("Issue is not a pull request"))
  912. return
  913. }
  914. pr := issue.PullRequest
  915. if err = pr.GetBaseRepo(); err != nil {
  916. ctx.ServerError("GetBaseRepo", err)
  917. return
  918. }
  919. patch, err := pr.BaseRepo.PatchPath(pr.Index)
  920. if err != nil {
  921. ctx.ServerError("PatchPath", err)
  922. return
  923. }
  924. ctx.ServeFileContent(patch)
  925. }
  926. // DownloadPullPatch render a pull's raw patch
  927. func DownloadPullPatch(ctx *context.Context) {
  928. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  929. if err != nil {
  930. if models.IsErrIssueNotExist(err) {
  931. ctx.NotFound("GetIssueByIndex", err)
  932. } else {
  933. ctx.ServerError("GetIssueByIndex", err)
  934. }
  935. return
  936. }
  937. // Return not found if it's not a pull request
  938. if !issue.IsPull {
  939. ctx.NotFound("DownloadPullDiff",
  940. fmt.Errorf("Issue is not a pull request"))
  941. return
  942. }
  943. pr := issue.PullRequest
  944. if err = pr.GetHeadRepo(); err != nil {
  945. ctx.ServerError("GetHeadRepo", err)
  946. return
  947. }
  948. headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
  949. if err != nil {
  950. ctx.ServerError("OpenRepository", err)
  951. return
  952. }
  953. patch, err := headGitRepo.GetFormatPatch(pr.MergeBase, pr.HeadBranch)
  954. if err != nil {
  955. ctx.ServerError("GetFormatPatch", err)
  956. return
  957. }
  958. _, err = io.Copy(ctx, patch)
  959. if err != nil {
  960. ctx.ServerError("io.Copy", err)
  961. return
  962. }
  963. }