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 32 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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. "code.gitea.io/gitea/modules/util"
  23. "github.com/Unknwon/com"
  24. )
  25. const (
  26. tplFork base.TplName = "repo/pulls/fork"
  27. tplComparePull base.TplName = "repo/pulls/compare"
  28. tplPullCommits base.TplName = "repo/pulls/commits"
  29. tplPullFiles base.TplName = "repo/pulls/files"
  30. pullRequestTemplateKey = "PullRequestTemplate"
  31. )
  32. var (
  33. pullRequestTemplateCandidates = []string{
  34. "PULL_REQUEST_TEMPLATE.md",
  35. "pull_request_template.md",
  36. ".gitea/PULL_REQUEST_TEMPLATE.md",
  37. ".gitea/pull_request_template.md",
  38. ".github/PULL_REQUEST_TEMPLATE.md",
  39. ".github/pull_request_template.md",
  40. }
  41. )
  42. func getForkRepository(ctx *context.Context) *models.Repository {
  43. forkRepo, err := models.GetRepositoryByID(ctx.ParamsInt64(":repoid"))
  44. if err != nil {
  45. if models.IsErrRepoNotExist(err) {
  46. ctx.NotFound("GetRepositoryByID", nil)
  47. } else {
  48. ctx.ServerError("GetRepositoryByID", err)
  49. }
  50. return nil
  51. }
  52. perm, err := models.GetUserRepoPermission(forkRepo, ctx.User)
  53. if err != nil {
  54. ctx.ServerError("GetUserRepoPermission", err)
  55. return nil
  56. }
  57. if forkRepo.IsEmpty || !perm.CanRead(models.UnitTypeCode) {
  58. ctx.NotFound("getForkRepository", nil)
  59. return nil
  60. }
  61. ctx.Data["repo_name"] = forkRepo.Name
  62. ctx.Data["description"] = forkRepo.Description
  63. ctx.Data["IsPrivate"] = forkRepo.IsPrivate
  64. canForkToUser := forkRepo.OwnerID != ctx.User.ID && !ctx.User.HasForkedRepo(forkRepo.ID)
  65. if err = forkRepo.GetOwner(); err != nil {
  66. ctx.ServerError("GetOwner", err)
  67. return nil
  68. }
  69. ctx.Data["ForkFrom"] = forkRepo.Owner.Name + "/" + forkRepo.Name
  70. ctx.Data["ForkFromOwnerID"] = forkRepo.Owner.ID
  71. if err := ctx.User.GetOwnedOrganizations(); err != nil {
  72. ctx.ServerError("GetOwnedOrganizations", err)
  73. return nil
  74. }
  75. var orgs []*models.User
  76. for _, org := range ctx.User.OwnedOrgs {
  77. if forkRepo.OwnerID != org.ID && !org.HasForkedRepo(forkRepo.ID) {
  78. orgs = append(orgs, org)
  79. }
  80. }
  81. var traverseParentRepo = forkRepo
  82. for {
  83. if ctx.User.ID == traverseParentRepo.OwnerID {
  84. canForkToUser = false
  85. } else {
  86. for i, org := range orgs {
  87. if org.ID == traverseParentRepo.OwnerID {
  88. orgs = append(orgs[:i], orgs[i+1:]...)
  89. break
  90. }
  91. }
  92. }
  93. if !traverseParentRepo.IsFork {
  94. break
  95. }
  96. traverseParentRepo, err = models.GetRepositoryByID(traverseParentRepo.ForkID)
  97. if err != nil {
  98. ctx.ServerError("GetRepositoryByID", err)
  99. return nil
  100. }
  101. }
  102. ctx.Data["CanForkToUser"] = canForkToUser
  103. ctx.Data["Orgs"] = orgs
  104. if canForkToUser {
  105. ctx.Data["ContextUser"] = ctx.User
  106. } else if len(orgs) > 0 {
  107. ctx.Data["ContextUser"] = orgs[0]
  108. }
  109. return forkRepo
  110. }
  111. // Fork render repository fork page
  112. func Fork(ctx *context.Context) {
  113. ctx.Data["Title"] = ctx.Tr("new_fork")
  114. getForkRepository(ctx)
  115. if ctx.Written() {
  116. return
  117. }
  118. ctx.HTML(200, tplFork)
  119. }
  120. // ForkPost response for forking a repository
  121. func ForkPost(ctx *context.Context, form auth.CreateRepoForm) {
  122. ctx.Data["Title"] = ctx.Tr("new_fork")
  123. ctxUser := checkContextUser(ctx, form.UID)
  124. if ctx.Written() {
  125. return
  126. }
  127. forkRepo := getForkRepository(ctx)
  128. if ctx.Written() {
  129. return
  130. }
  131. ctx.Data["ContextUser"] = ctxUser
  132. if ctx.HasError() {
  133. ctx.HTML(200, tplFork)
  134. return
  135. }
  136. var err error
  137. var traverseParentRepo = forkRepo
  138. for {
  139. if ctxUser.ID == traverseParentRepo.OwnerID {
  140. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplFork, &form)
  141. return
  142. }
  143. repo, has := models.HasForkedRepo(ctxUser.ID, traverseParentRepo.ID)
  144. if has {
  145. ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + repo.Name)
  146. return
  147. }
  148. if !traverseParentRepo.IsFork {
  149. break
  150. }
  151. traverseParentRepo, err = models.GetRepositoryByID(traverseParentRepo.ForkID)
  152. if err != nil {
  153. ctx.ServerError("GetRepositoryByID", err)
  154. return
  155. }
  156. }
  157. // Check ownership of organization.
  158. if ctxUser.IsOrganization() {
  159. isOwner, err := ctxUser.IsOwnedBy(ctx.User.ID)
  160. if err != nil {
  161. ctx.ServerError("IsOwnedBy", err)
  162. return
  163. } else if !isOwner {
  164. ctx.Error(403)
  165. return
  166. }
  167. }
  168. repo, err := models.ForkRepository(ctx.User, ctxUser, forkRepo, form.RepoName, form.Description)
  169. if err != nil {
  170. ctx.Data["Err_RepoName"] = true
  171. switch {
  172. case models.IsErrRepoAlreadyExist(err):
  173. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplFork, &form)
  174. case models.IsErrNameReserved(err):
  175. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), tplFork, &form)
  176. case models.IsErrNamePatternNotAllowed(err):
  177. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplFork, &form)
  178. default:
  179. ctx.ServerError("ForkPost", err)
  180. }
  181. return
  182. }
  183. log.Trace("Repository forked[%d]: %s/%s", forkRepo.ID, ctxUser.Name, repo.Name)
  184. ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + repo.Name)
  185. }
  186. func checkPullInfo(ctx *context.Context) *models.Issue {
  187. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  188. if err != nil {
  189. if models.IsErrIssueNotExist(err) {
  190. ctx.NotFound("GetIssueByIndex", err)
  191. } else {
  192. ctx.ServerError("GetIssueByIndex", err)
  193. }
  194. return nil
  195. }
  196. if err = issue.LoadPoster(); err != nil {
  197. ctx.ServerError("LoadPoster", err)
  198. return nil
  199. }
  200. ctx.Data["Title"] = fmt.Sprintf("#%d - %s", issue.Index, issue.Title)
  201. ctx.Data["Issue"] = issue
  202. if !issue.IsPull {
  203. ctx.NotFound("ViewPullCommits", nil)
  204. return nil
  205. }
  206. if err = issue.LoadPullRequest(); err != nil {
  207. ctx.ServerError("LoadPullRequest", err)
  208. return nil
  209. }
  210. if err = issue.PullRequest.GetHeadRepo(); err != nil {
  211. ctx.ServerError("GetHeadRepo", err)
  212. return nil
  213. }
  214. if ctx.IsSigned {
  215. // Update issue-user.
  216. if err = issue.ReadBy(ctx.User.ID); err != nil {
  217. ctx.ServerError("ReadBy", err)
  218. return nil
  219. }
  220. }
  221. return issue
  222. }
  223. func setMergeTarget(ctx *context.Context, pull *models.PullRequest) {
  224. if ctx.Repo.Owner.Name == pull.HeadUserName {
  225. ctx.Data["HeadTarget"] = pull.HeadBranch
  226. } else if pull.HeadRepo == nil {
  227. ctx.Data["HeadTarget"] = pull.HeadUserName + ":" + pull.HeadBranch
  228. } else {
  229. ctx.Data["HeadTarget"] = pull.HeadUserName + "/" + pull.HeadRepo.Name + ":" + pull.HeadBranch
  230. }
  231. ctx.Data["BaseTarget"] = pull.BaseBranch
  232. }
  233. // PrepareMergedViewPullInfo show meta information for a merged pull request view page
  234. func PrepareMergedViewPullInfo(ctx *context.Context, issue *models.Issue) *git.PullRequestInfo {
  235. pull := issue.PullRequest
  236. setMergeTarget(ctx, pull)
  237. ctx.Data["HasMerged"] = true
  238. prInfo, err := ctx.Repo.GitRepo.GetPullRequestInfo(ctx.Repo.Repository.RepoPath(),
  239. pull.MergeBase, pull.GetGitRefName())
  240. if err != nil {
  241. if strings.Contains(err.Error(), "fatal: Not a valid object name") {
  242. ctx.Data["IsPullRequestBroken"] = true
  243. ctx.Data["BaseTarget"] = "deleted"
  244. ctx.Data["NumCommits"] = 0
  245. ctx.Data["NumFiles"] = 0
  246. return nil
  247. }
  248. ctx.ServerError("GetPullRequestInfo", err)
  249. return nil
  250. }
  251. ctx.Data["NumCommits"] = prInfo.Commits.Len()
  252. ctx.Data["NumFiles"] = prInfo.NumFiles
  253. return prInfo
  254. }
  255. // PrepareViewPullInfo show meta information for a pull request preview page
  256. func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.PullRequestInfo {
  257. repo := ctx.Repo.Repository
  258. pull := issue.PullRequest
  259. var err error
  260. if err = pull.GetHeadRepo(); err != nil {
  261. ctx.ServerError("GetHeadRepo", err)
  262. return nil
  263. }
  264. setMergeTarget(ctx, pull)
  265. var headGitRepo *git.Repository
  266. if pull.HeadRepo != nil {
  267. headGitRepo, err = git.OpenRepository(pull.HeadRepo.RepoPath())
  268. if err != nil {
  269. ctx.ServerError("OpenRepository", err)
  270. return nil
  271. }
  272. }
  273. if pull.HeadRepo == nil || !headGitRepo.IsBranchExist(pull.HeadBranch) {
  274. ctx.Data["IsPullRequestBroken"] = true
  275. ctx.Data["HeadTarget"] = "deleted"
  276. ctx.Data["NumCommits"] = 0
  277. ctx.Data["NumFiles"] = 0
  278. return nil
  279. }
  280. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name),
  281. pull.BaseBranch, pull.HeadBranch)
  282. if err != nil {
  283. if strings.Contains(err.Error(), "fatal: Not a valid object name") {
  284. ctx.Data["IsPullRequestBroken"] = true
  285. ctx.Data["BaseTarget"] = "deleted"
  286. ctx.Data["NumCommits"] = 0
  287. ctx.Data["NumFiles"] = 0
  288. return nil
  289. }
  290. ctx.ServerError("GetPullRequestInfo", err)
  291. return nil
  292. }
  293. if pull.IsWorkInProgress() {
  294. ctx.Data["IsPullWorkInProgress"] = true
  295. ctx.Data["WorkInProgressPrefix"] = pull.GetWorkInProgressPrefix()
  296. }
  297. ctx.Data["NumCommits"] = prInfo.Commits.Len()
  298. ctx.Data["NumFiles"] = prInfo.NumFiles
  299. return prInfo
  300. }
  301. // ViewPullCommits show commits for a pull request
  302. func ViewPullCommits(ctx *context.Context) {
  303. ctx.Data["PageIsPullList"] = true
  304. ctx.Data["PageIsPullCommits"] = true
  305. issue := checkPullInfo(ctx)
  306. if ctx.Written() {
  307. return
  308. }
  309. pull := issue.PullRequest
  310. var commits *list.List
  311. if pull.HasMerged {
  312. prInfo := PrepareMergedViewPullInfo(ctx, issue)
  313. if ctx.Written() {
  314. return
  315. } else if prInfo == nil {
  316. ctx.NotFound("ViewPullCommits", nil)
  317. return
  318. }
  319. ctx.Data["Username"] = ctx.Repo.Owner.Name
  320. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  321. commits = prInfo.Commits
  322. } else {
  323. prInfo := PrepareViewPullInfo(ctx, issue)
  324. if ctx.Written() {
  325. return
  326. } else if prInfo == nil {
  327. ctx.NotFound("ViewPullCommits", nil)
  328. return
  329. }
  330. ctx.Data["Username"] = pull.HeadUserName
  331. ctx.Data["Reponame"] = pull.HeadRepo.Name
  332. commits = prInfo.Commits
  333. }
  334. commits = models.ValidateCommitsWithEmails(commits)
  335. commits = models.ParseCommitsWithSignature(commits)
  336. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  337. ctx.Data["Commits"] = commits
  338. ctx.Data["CommitCount"] = commits.Len()
  339. ctx.HTML(200, tplPullCommits)
  340. }
  341. // ViewPullFiles render pull request changed files list page
  342. func ViewPullFiles(ctx *context.Context) {
  343. ctx.Data["PageIsPullList"] = true
  344. ctx.Data["PageIsPullFiles"] = true
  345. issue := checkPullInfo(ctx)
  346. if ctx.Written() {
  347. return
  348. }
  349. pull := issue.PullRequest
  350. whitespaceFlags := map[string]string{
  351. "ignore-all": "-w",
  352. "ignore-change": "-b",
  353. "ignore-eol": "--ignore-space-at-eol",
  354. "": ""}
  355. var (
  356. diffRepoPath string
  357. startCommitID string
  358. endCommitID string
  359. gitRepo *git.Repository
  360. )
  361. var headTarget string
  362. if pull.HasMerged {
  363. prInfo := PrepareMergedViewPullInfo(ctx, issue)
  364. if ctx.Written() {
  365. return
  366. } else if prInfo == nil {
  367. ctx.NotFound("ViewPullFiles", nil)
  368. return
  369. }
  370. diffRepoPath = ctx.Repo.GitRepo.Path
  371. gitRepo = ctx.Repo.GitRepo
  372. headCommitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName())
  373. if err != nil {
  374. ctx.ServerError("GetRefCommitID", err)
  375. return
  376. }
  377. startCommitID = prInfo.MergeBase
  378. endCommitID = headCommitID
  379. headTarget = path.Join(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  380. ctx.Data["Username"] = ctx.Repo.Owner.Name
  381. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  382. } else {
  383. prInfo := PrepareViewPullInfo(ctx, issue)
  384. if ctx.Written() {
  385. return
  386. } else if prInfo == nil {
  387. ctx.NotFound("ViewPullFiles", nil)
  388. return
  389. }
  390. headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
  391. headGitRepo, err := git.OpenRepository(headRepoPath)
  392. if err != nil {
  393. ctx.ServerError("OpenRepository", err)
  394. return
  395. }
  396. headCommitID, err := headGitRepo.GetBranchCommitID(pull.HeadBranch)
  397. if err != nil {
  398. ctx.ServerError("GetBranchCommitID", err)
  399. return
  400. }
  401. diffRepoPath = headRepoPath
  402. startCommitID = prInfo.MergeBase
  403. endCommitID = headCommitID
  404. gitRepo = headGitRepo
  405. headTarget = path.Join(pull.HeadUserName, pull.HeadRepo.Name)
  406. ctx.Data["Username"] = pull.HeadUserName
  407. ctx.Data["Reponame"] = pull.HeadRepo.Name
  408. }
  409. diff, err := models.GetDiffRangeWithWhitespaceBehavior(diffRepoPath,
  410. startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
  411. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles,
  412. whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)])
  413. if err != nil {
  414. ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err)
  415. return
  416. }
  417. if err = diff.LoadComments(issue, ctx.User); err != nil {
  418. ctx.ServerError("LoadComments", err)
  419. return
  420. }
  421. ctx.Data["Diff"] = diff
  422. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  423. commit, err := gitRepo.GetCommit(endCommitID)
  424. if err != nil {
  425. ctx.ServerError("GetCommit", err)
  426. return
  427. }
  428. ctx.Data["IsImageFile"] = commit.IsImageFile
  429. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", endCommitID)
  430. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", startCommitID)
  431. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", "commit", endCommitID)
  432. ctx.Data["RequireHighlightJS"] = true
  433. ctx.Data["RequireTribute"] = true
  434. if ctx.Data["Assignees"], err = ctx.Repo.Repository.GetAssignees(); err != nil {
  435. ctx.ServerError("GetAssignees", err)
  436. return
  437. }
  438. ctx.Data["CurrentReview"], err = models.GetCurrentReview(ctx.User, issue)
  439. if err != nil && !models.IsErrReviewNotExist(err) {
  440. ctx.ServerError("GetCurrentReview", err)
  441. return
  442. }
  443. ctx.HTML(200, tplPullFiles)
  444. }
  445. // MergePullRequest response for merging pull request
  446. func MergePullRequest(ctx *context.Context, form auth.MergePullRequestForm) {
  447. issue := checkPullInfo(ctx)
  448. if ctx.Written() {
  449. return
  450. }
  451. if issue.IsClosed {
  452. ctx.NotFound("MergePullRequest", nil)
  453. return
  454. }
  455. pr := issue.PullRequest
  456. if !pr.CanAutoMerge() || pr.HasMerged {
  457. ctx.NotFound("MergePullRequest", nil)
  458. return
  459. }
  460. if pr.IsWorkInProgress() {
  461. ctx.Flash.Error(ctx.Tr("repo.pulls.no_merge_wip"))
  462. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  463. return
  464. }
  465. if ctx.HasError() {
  466. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  467. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  468. return
  469. }
  470. message := strings.TrimSpace(form.MergeTitleField)
  471. if len(message) == 0 {
  472. if models.MergeStyle(form.Do) == models.MergeStyleMerge {
  473. message = pr.GetDefaultMergeMessage()
  474. }
  475. if models.MergeStyle(form.Do) == models.MergeStyleRebaseMerge {
  476. message = pr.GetDefaultMergeMessage()
  477. }
  478. if models.MergeStyle(form.Do) == models.MergeStyleSquash {
  479. message = pr.GetDefaultSquashMessage()
  480. }
  481. }
  482. form.MergeMessageField = strings.TrimSpace(form.MergeMessageField)
  483. if len(form.MergeMessageField) > 0 {
  484. message += "\n\n" + form.MergeMessageField
  485. }
  486. pr.Issue = issue
  487. pr.Issue.Repo = ctx.Repo.Repository
  488. noDeps, err := models.IssueNoDependenciesLeft(issue)
  489. if err != nil {
  490. return
  491. }
  492. if !noDeps {
  493. ctx.Flash.Error(ctx.Tr("repo.issues.dependency.pr_close_blocked"))
  494. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  495. return
  496. }
  497. if err = pr.Merge(ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil {
  498. if models.IsErrInvalidMergeStyle(err) {
  499. ctx.Flash.Error(ctx.Tr("repo.pulls.invalid_merge_option"))
  500. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  501. return
  502. }
  503. ctx.ServerError("Merge", err)
  504. return
  505. }
  506. notification.NotifyMergePullRequest(pr, ctx.User, ctx.Repo.GitRepo)
  507. log.Trace("Pull request merged: %d", pr.ID)
  508. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  509. }
  510. // ParseCompareInfo parse compare info between two commit for preparing pull request
  511. func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
  512. baseRepo := ctx.Repo.Repository
  513. // Get compared branches information
  514. // format: <base branch>...[<head repo>:]<head branch>
  515. // base<-head: master...head:feature
  516. // same repo: master...feature
  517. var (
  518. headUser *models.User
  519. headBranch string
  520. isSameRepo bool
  521. infoPath string
  522. err error
  523. )
  524. infoPath, err = url.QueryUnescape(ctx.Params("*"))
  525. if err != nil {
  526. ctx.NotFound("QueryUnescape", err)
  527. }
  528. infos := strings.Split(infoPath, "...")
  529. if len(infos) != 2 {
  530. log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)
  531. ctx.NotFound("CompareAndPullRequest", nil)
  532. return nil, nil, nil, nil, "", ""
  533. }
  534. baseBranch := infos[0]
  535. ctx.Data["BaseBranch"] = baseBranch
  536. // If there is no head repository, it means pull request between same repository.
  537. headInfos := strings.Split(infos[1], ":")
  538. if len(headInfos) == 1 {
  539. isSameRepo = true
  540. headUser = ctx.Repo.Owner
  541. headBranch = headInfos[0]
  542. } else if len(headInfos) == 2 {
  543. headUser, err = models.GetUserByName(headInfos[0])
  544. if err != nil {
  545. if models.IsErrUserNotExist(err) {
  546. ctx.NotFound("GetUserByName", nil)
  547. } else {
  548. ctx.ServerError("GetUserByName", err)
  549. }
  550. return nil, nil, nil, nil, "", ""
  551. }
  552. headBranch = headInfos[1]
  553. isSameRepo = headUser.ID == ctx.Repo.Owner.ID
  554. } else {
  555. ctx.NotFound("CompareAndPullRequest", nil)
  556. return nil, nil, nil, nil, "", ""
  557. }
  558. ctx.Data["HeadUser"] = headUser
  559. ctx.Data["HeadBranch"] = headBranch
  560. ctx.Repo.PullRequest.SameRepo = isSameRepo
  561. // Check if base branch is valid.
  562. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  563. ctx.NotFound("IsBranchExist", nil)
  564. return nil, nil, nil, nil, "", ""
  565. }
  566. // Check if current user has fork of repository or in the same repository.
  567. headRepo, has := models.HasForkedRepo(headUser.ID, baseRepo.ID)
  568. if !has && !isSameRepo {
  569. log.Trace("ParseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
  570. ctx.NotFound("ParseCompareInfo", nil)
  571. return nil, nil, nil, nil, "", ""
  572. }
  573. var headGitRepo *git.Repository
  574. if isSameRepo {
  575. headRepo = ctx.Repo.Repository
  576. headGitRepo = ctx.Repo.GitRepo
  577. } else {
  578. headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  579. if err != nil {
  580. ctx.ServerError("OpenRepository", err)
  581. return nil, nil, nil, nil, "", ""
  582. }
  583. }
  584. perm, err := models.GetUserRepoPermission(headRepo, ctx.User)
  585. if err != nil {
  586. ctx.ServerError("GetUserRepoPermission", err)
  587. return nil, nil, nil, nil, "", ""
  588. }
  589. if !perm.CanWrite(models.UnitTypeCode) {
  590. log.Trace("ParseCompareInfo[%d]: does not have write access or site admin", baseRepo.ID)
  591. ctx.NotFound("ParseCompareInfo", nil)
  592. return nil, nil, nil, nil, "", ""
  593. }
  594. // Check if head branch is valid.
  595. if !headGitRepo.IsBranchExist(headBranch) {
  596. ctx.NotFound("IsBranchExist", nil)
  597. return nil, nil, nil, nil, "", ""
  598. }
  599. headBranches, err := headGitRepo.GetBranches()
  600. if err != nil {
  601. ctx.ServerError("GetBranches", err)
  602. return nil, nil, nil, nil, "", ""
  603. }
  604. ctx.Data["HeadBranches"] = headBranches
  605. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
  606. if err != nil {
  607. ctx.ServerError("GetPullRequestInfo", err)
  608. return nil, nil, nil, nil, "", ""
  609. }
  610. ctx.Data["BeforeCommitID"] = prInfo.MergeBase
  611. return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
  612. }
  613. // PrepareCompareDiff render pull request preview diff page
  614. func PrepareCompareDiff(
  615. ctx *context.Context,
  616. headUser *models.User,
  617. headRepo *models.Repository,
  618. headGitRepo *git.Repository,
  619. prInfo *git.PullRequestInfo,
  620. baseBranch, headBranch string) bool {
  621. var (
  622. repo = ctx.Repo.Repository
  623. err error
  624. title string
  625. )
  626. // Get diff information.
  627. ctx.Data["CommitRepoLink"] = headRepo.Link()
  628. headCommitID, err := headGitRepo.GetBranchCommitID(headBranch)
  629. if err != nil {
  630. ctx.ServerError("GetBranchCommitID", err)
  631. return false
  632. }
  633. ctx.Data["AfterCommitID"] = headCommitID
  634. if headCommitID == prInfo.MergeBase {
  635. ctx.Data["IsNothingToCompare"] = true
  636. return true
  637. }
  638. diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
  639. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
  640. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  641. if err != nil {
  642. ctx.ServerError("GetDiffRange", err)
  643. return false
  644. }
  645. ctx.Data["Diff"] = diff
  646. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  647. headCommit, err := headGitRepo.GetCommit(headCommitID)
  648. if err != nil {
  649. ctx.ServerError("GetCommit", err)
  650. return false
  651. }
  652. prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
  653. prInfo.Commits = models.ParseCommitsWithSignature(prInfo.Commits)
  654. prInfo.Commits = models.ParseCommitsWithStatus(prInfo.Commits, headRepo)
  655. ctx.Data["Commits"] = prInfo.Commits
  656. ctx.Data["CommitCount"] = prInfo.Commits.Len()
  657. if prInfo.Commits.Len() == 1 {
  658. c := prInfo.Commits.Front().Value.(models.SignCommitWithStatuses)
  659. title = strings.TrimSpace(c.UserCommit.Summary())
  660. body := strings.Split(strings.TrimSpace(c.UserCommit.Message()), "\n")
  661. if len(body) > 1 {
  662. ctx.Data["content"] = strings.Join(body[1:], "\n")
  663. }
  664. } else {
  665. title = headBranch
  666. }
  667. ctx.Data["title"] = title
  668. ctx.Data["Username"] = headUser.Name
  669. ctx.Data["Reponame"] = headRepo.Name
  670. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  671. headTarget := path.Join(headUser.Name, repo.Name)
  672. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", headCommitID)
  673. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", prInfo.MergeBase)
  674. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", "commit", headCommitID)
  675. return false
  676. }
  677. // CompareAndPullRequest render pull request preview page
  678. func CompareAndPullRequest(ctx *context.Context) {
  679. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  680. ctx.Data["PageIsComparePull"] = true
  681. ctx.Data["IsDiffCompare"] = true
  682. ctx.Data["RequireHighlightJS"] = true
  683. ctx.Data["RequireTribute"] = true
  684. ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
  685. setTemplateIfExists(ctx, pullRequestTemplateKey, pullRequestTemplateCandidates)
  686. renderAttachmentSettings(ctx)
  687. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  688. if ctx.Written() {
  689. return
  690. }
  691. pr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
  692. if err != nil {
  693. if !models.IsErrPullRequestNotExist(err) {
  694. ctx.ServerError("GetUnmergedPullRequest", err)
  695. return
  696. }
  697. } else {
  698. ctx.Data["HasPullRequest"] = true
  699. ctx.Data["PullRequest"] = pr
  700. ctx.HTML(200, tplComparePull)
  701. return
  702. }
  703. nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  704. if ctx.Written() {
  705. return
  706. }
  707. if !nothingToCompare {
  708. // Setup information for new form.
  709. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  710. if ctx.Written() {
  711. return
  712. }
  713. }
  714. ctx.HTML(200, tplComparePull)
  715. }
  716. // CompareAndPullRequestPost response for creating pull request
  717. func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm) {
  718. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  719. ctx.Data["PageIsComparePull"] = true
  720. ctx.Data["IsDiffCompare"] = true
  721. ctx.Data["RequireHighlightJS"] = true
  722. ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
  723. renderAttachmentSettings(ctx)
  724. var (
  725. repo = ctx.Repo.Repository
  726. attachments []string
  727. )
  728. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  729. if ctx.Written() {
  730. return
  731. }
  732. labelIDs, assigneeIDs, milestoneID := ValidateRepoMetas(ctx, form, true)
  733. if ctx.Written() {
  734. return
  735. }
  736. if setting.AttachmentEnabled {
  737. attachments = form.Files
  738. }
  739. if ctx.HasError() {
  740. auth.AssignForm(form, ctx.Data)
  741. // This stage is already stop creating new pull request, so it does not matter if it has
  742. // something to compare or not.
  743. PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  744. if ctx.Written() {
  745. return
  746. }
  747. ctx.HTML(200, tplComparePull)
  748. return
  749. }
  750. if util.IsEmptyString(form.Title) {
  751. PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  752. if ctx.Written() {
  753. return
  754. }
  755. ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplComparePull, form)
  756. return
  757. }
  758. patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
  759. if err != nil {
  760. ctx.ServerError("GetPatch", err)
  761. return
  762. }
  763. pullIssue := &models.Issue{
  764. RepoID: repo.ID,
  765. Index: repo.NextIssueIndex(),
  766. Title: form.Title,
  767. PosterID: ctx.User.ID,
  768. Poster: ctx.User,
  769. MilestoneID: milestoneID,
  770. IsPull: true,
  771. Content: form.Content,
  772. }
  773. pullRequest := &models.PullRequest{
  774. HeadRepoID: headRepo.ID,
  775. BaseRepoID: repo.ID,
  776. HeadUserName: headUser.Name,
  777. HeadBranch: headBranch,
  778. BaseBranch: baseBranch,
  779. HeadRepo: headRepo,
  780. BaseRepo: repo,
  781. MergeBase: prInfo.MergeBase,
  782. Type: models.PullRequestGitea,
  783. }
  784. // FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
  785. // instead of 500.
  786. if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch, assigneeIDs); err != nil {
  787. if models.IsErrUserDoesNotHaveAccessToRepo(err) {
  788. ctx.Error(400, "UserDoesNotHaveAccessToRepo", err.Error())
  789. return
  790. }
  791. ctx.ServerError("NewPullRequest", err)
  792. return
  793. } else if err := pullRequest.PushToBaseRepo(); err != nil {
  794. ctx.ServerError("PushToBaseRepo", err)
  795. return
  796. }
  797. notification.NotifyNewPullRequest(pullRequest)
  798. log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
  799. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
  800. }
  801. // TriggerTask response for a trigger task request
  802. func TriggerTask(ctx *context.Context) {
  803. pusherID := ctx.QueryInt64("pusher")
  804. branch := ctx.Query("branch")
  805. secret := ctx.Query("secret")
  806. if len(branch) == 0 || len(secret) == 0 || pusherID <= 0 {
  807. ctx.Error(404)
  808. log.Trace("TriggerTask: branch or secret is empty, or pusher ID is not valid")
  809. return
  810. }
  811. owner, repo := parseOwnerAndRepo(ctx)
  812. if ctx.Written() {
  813. return
  814. }
  815. if secret != base.EncodeMD5(owner.Salt) {
  816. ctx.Error(404)
  817. log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
  818. return
  819. }
  820. pusher, err := models.GetUserByID(pusherID)
  821. if err != nil {
  822. if models.IsErrUserNotExist(err) {
  823. ctx.Error(404)
  824. } else {
  825. ctx.ServerError("GetUserByID", err)
  826. }
  827. return
  828. }
  829. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  830. go models.HookQueue.Add(repo.ID)
  831. go models.AddTestPullRequestTask(pusher, repo.ID, branch, true)
  832. ctx.Status(202)
  833. }
  834. // CleanUpPullRequest responses for delete merged branch when PR has been merged
  835. func CleanUpPullRequest(ctx *context.Context) {
  836. issue := checkPullInfo(ctx)
  837. if ctx.Written() {
  838. return
  839. }
  840. pr := issue.PullRequest
  841. // Allow cleanup only for merged PR
  842. if !pr.HasMerged {
  843. ctx.NotFound("CleanUpPullRequest", nil)
  844. return
  845. }
  846. if err := pr.GetHeadRepo(); err != nil {
  847. ctx.ServerError("GetHeadRepo", err)
  848. return
  849. } else if pr.HeadRepo == nil {
  850. // Forked repository has already been deleted
  851. ctx.NotFound("CleanUpPullRequest", nil)
  852. return
  853. } else if pr.GetBaseRepo(); err != nil {
  854. ctx.ServerError("GetBaseRepo", err)
  855. return
  856. } else if pr.HeadRepo.GetOwner(); err != nil {
  857. ctx.ServerError("HeadRepo.GetOwner", err)
  858. return
  859. }
  860. perm, err := models.GetUserRepoPermission(pr.HeadRepo, ctx.User)
  861. if err != nil {
  862. ctx.ServerError("GetUserRepoPermission", err)
  863. return
  864. }
  865. if !perm.CanWrite(models.UnitTypeCode) {
  866. ctx.NotFound("CleanUpPullRequest", nil)
  867. return
  868. }
  869. fullBranchName := pr.HeadRepo.Owner.Name + "/" + pr.HeadBranch
  870. gitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
  871. if err != nil {
  872. ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.RepoPath()), err)
  873. return
  874. }
  875. gitBaseRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
  876. if err != nil {
  877. ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.BaseRepo.RepoPath()), err)
  878. return
  879. }
  880. defer func() {
  881. ctx.JSON(200, map[string]interface{}{
  882. "redirect": pr.BaseRepo.Link() + "/pulls/" + com.ToStr(issue.Index),
  883. })
  884. }()
  885. if pr.HeadBranch == pr.HeadRepo.DefaultBranch || !gitRepo.IsBranchExist(pr.HeadBranch) {
  886. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  887. return
  888. }
  889. // Check if branch is not protected
  890. if protected, err := pr.HeadRepo.IsProtectedBranch(pr.HeadBranch, ctx.User); err != nil || protected {
  891. if err != nil {
  892. log.Error(4, "HeadRepo.IsProtectedBranch: %v", err)
  893. }
  894. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  895. return
  896. }
  897. // Check if branch has no new commits
  898. headCommitID, err := gitBaseRepo.GetRefCommitID(pr.GetGitRefName())
  899. if err != nil {
  900. log.Error(4, "GetRefCommitID: %v", err)
  901. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  902. return
  903. }
  904. branchCommitID, err := gitRepo.GetBranchCommitID(pr.HeadBranch)
  905. if err != nil {
  906. log.Error(4, "GetBranchCommitID: %v", err)
  907. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  908. return
  909. }
  910. if headCommitID != branchCommitID {
  911. ctx.Flash.Error(ctx.Tr("repo.branch.delete_branch_has_new_commits", fullBranchName))
  912. return
  913. }
  914. if err := gitRepo.DeleteBranch(pr.HeadBranch, git.DeleteBranchOptions{
  915. Force: true,
  916. }); err != nil {
  917. log.Error(4, "DeleteBranch: %v", err)
  918. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  919. return
  920. }
  921. if err := models.AddDeletePRBranchComment(ctx.User, pr.BaseRepo, issue.ID, pr.HeadBranch); err != nil {
  922. // Do not fail here as branch has already been deleted
  923. log.Error(4, "DeleteBranch: %v", err)
  924. }
  925. ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", fullBranchName))
  926. }
  927. // DownloadPullDiff render a pull's raw diff
  928. func DownloadPullDiff(ctx *context.Context) {
  929. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  930. if err != nil {
  931. if models.IsErrIssueNotExist(err) {
  932. ctx.NotFound("GetIssueByIndex", err)
  933. } else {
  934. ctx.ServerError("GetIssueByIndex", err)
  935. }
  936. return
  937. }
  938. // Return not found if it's not a pull request
  939. if !issue.IsPull {
  940. ctx.NotFound("DownloadPullDiff",
  941. fmt.Errorf("Issue is not a pull request"))
  942. return
  943. }
  944. if err = issue.LoadPullRequest(); err != nil {
  945. ctx.ServerError("LoadPullRequest", err)
  946. return
  947. }
  948. pr := issue.PullRequest
  949. if err = pr.GetBaseRepo(); err != nil {
  950. ctx.ServerError("GetBaseRepo", err)
  951. return
  952. }
  953. patch, err := pr.BaseRepo.PatchPath(pr.Index)
  954. if err != nil {
  955. ctx.ServerError("PatchPath", err)
  956. return
  957. }
  958. ctx.ServeFileContent(patch)
  959. }
  960. // DownloadPullPatch render a pull's raw patch
  961. func DownloadPullPatch(ctx *context.Context) {
  962. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  963. if err != nil {
  964. if models.IsErrIssueNotExist(err) {
  965. ctx.NotFound("GetIssueByIndex", err)
  966. } else {
  967. ctx.ServerError("GetIssueByIndex", err)
  968. }
  969. return
  970. }
  971. // Return not found if it's not a pull request
  972. if !issue.IsPull {
  973. ctx.NotFound("DownloadPullDiff",
  974. fmt.Errorf("Issue is not a pull request"))
  975. return
  976. }
  977. if err = issue.LoadPullRequest(); err != nil {
  978. ctx.ServerError("LoadPullRequest", err)
  979. return
  980. }
  981. pr := issue.PullRequest
  982. if err = pr.GetHeadRepo(); err != nil {
  983. ctx.ServerError("GetHeadRepo", err)
  984. return
  985. }
  986. headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
  987. if err != nil {
  988. ctx.ServerError("OpenRepository", err)
  989. return
  990. }
  991. patch, err := headGitRepo.GetFormatPatch(pr.MergeBase, pr.HeadBranch)
  992. if err != nil {
  993. ctx.ServerError("GetFormatPatch", err)
  994. return
  995. }
  996. _, err = io.Copy(ctx, patch)
  997. if err != nil {
  998. ctx.ServerError("io.Copy", err)
  999. return
  1000. }
  1001. }