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