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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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. if pull.IsFilesConflicted() {
  298. ctx.Data["IsPullFilesConflicted"] = true
  299. ctx.Data["ConflictedFiles"] = pull.ConflictedFiles
  300. }
  301. ctx.Data["NumCommits"] = prInfo.Commits.Len()
  302. ctx.Data["NumFiles"] = prInfo.NumFiles
  303. return prInfo
  304. }
  305. // ViewPullCommits show commits for a pull request
  306. func ViewPullCommits(ctx *context.Context) {
  307. ctx.Data["PageIsPullList"] = true
  308. ctx.Data["PageIsPullCommits"] = true
  309. issue := checkPullInfo(ctx)
  310. if ctx.Written() {
  311. return
  312. }
  313. pull := issue.PullRequest
  314. var commits *list.List
  315. if pull.HasMerged {
  316. prInfo := PrepareMergedViewPullInfo(ctx, issue)
  317. if ctx.Written() {
  318. return
  319. } else if prInfo == nil {
  320. ctx.NotFound("ViewPullCommits", nil)
  321. return
  322. }
  323. ctx.Data["Username"] = ctx.Repo.Owner.Name
  324. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  325. commits = prInfo.Commits
  326. } else {
  327. prInfo := PrepareViewPullInfo(ctx, issue)
  328. if ctx.Written() {
  329. return
  330. } else if prInfo == nil {
  331. ctx.NotFound("ViewPullCommits", nil)
  332. return
  333. }
  334. ctx.Data["Username"] = pull.HeadUserName
  335. ctx.Data["Reponame"] = pull.HeadRepo.Name
  336. commits = prInfo.Commits
  337. }
  338. commits = models.ValidateCommitsWithEmails(commits)
  339. commits = models.ParseCommitsWithSignature(commits)
  340. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  341. ctx.Data["Commits"] = commits
  342. ctx.Data["CommitCount"] = commits.Len()
  343. ctx.HTML(200, tplPullCommits)
  344. }
  345. // ViewPullFiles render pull request changed files list page
  346. func ViewPullFiles(ctx *context.Context) {
  347. ctx.Data["PageIsPullList"] = true
  348. ctx.Data["PageIsPullFiles"] = true
  349. issue := checkPullInfo(ctx)
  350. if ctx.Written() {
  351. return
  352. }
  353. pull := issue.PullRequest
  354. whitespaceFlags := map[string]string{
  355. "ignore-all": "-w",
  356. "ignore-change": "-b",
  357. "ignore-eol": "--ignore-space-at-eol",
  358. "": ""}
  359. var (
  360. diffRepoPath string
  361. startCommitID string
  362. endCommitID string
  363. gitRepo *git.Repository
  364. )
  365. var headTarget string
  366. if pull.HasMerged {
  367. prInfo := PrepareMergedViewPullInfo(ctx, issue)
  368. if ctx.Written() {
  369. return
  370. } else if prInfo == nil {
  371. ctx.NotFound("ViewPullFiles", nil)
  372. return
  373. }
  374. diffRepoPath = ctx.Repo.GitRepo.Path
  375. gitRepo = ctx.Repo.GitRepo
  376. headCommitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName())
  377. if err != nil {
  378. ctx.ServerError("GetRefCommitID", err)
  379. return
  380. }
  381. startCommitID = prInfo.MergeBase
  382. endCommitID = headCommitID
  383. headTarget = path.Join(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  384. ctx.Data["Username"] = ctx.Repo.Owner.Name
  385. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  386. } else {
  387. prInfo := PrepareViewPullInfo(ctx, issue)
  388. if ctx.Written() {
  389. return
  390. } else if prInfo == nil {
  391. ctx.NotFound("ViewPullFiles", nil)
  392. return
  393. }
  394. headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
  395. headGitRepo, err := git.OpenRepository(headRepoPath)
  396. if err != nil {
  397. ctx.ServerError("OpenRepository", err)
  398. return
  399. }
  400. headCommitID, err := headGitRepo.GetBranchCommitID(pull.HeadBranch)
  401. if err != nil {
  402. ctx.ServerError("GetBranchCommitID", err)
  403. return
  404. }
  405. diffRepoPath = headRepoPath
  406. startCommitID = prInfo.MergeBase
  407. endCommitID = headCommitID
  408. gitRepo = headGitRepo
  409. headTarget = path.Join(pull.HeadUserName, pull.HeadRepo.Name)
  410. ctx.Data["Username"] = pull.HeadUserName
  411. ctx.Data["Reponame"] = pull.HeadRepo.Name
  412. }
  413. diff, err := models.GetDiffRangeWithWhitespaceBehavior(diffRepoPath,
  414. startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
  415. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles,
  416. whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)])
  417. if err != nil {
  418. ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err)
  419. return
  420. }
  421. if err = diff.LoadComments(issue, ctx.User); err != nil {
  422. ctx.ServerError("LoadComments", err)
  423. return
  424. }
  425. ctx.Data["Diff"] = diff
  426. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  427. commit, err := gitRepo.GetCommit(endCommitID)
  428. if err != nil {
  429. ctx.ServerError("GetCommit", err)
  430. return
  431. }
  432. ctx.Data["IsImageFile"] = commit.IsImageFile
  433. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", endCommitID)
  434. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", startCommitID)
  435. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", "commit", endCommitID)
  436. ctx.Data["RequireHighlightJS"] = true
  437. ctx.Data["RequireTribute"] = true
  438. if ctx.Data["Assignees"], err = ctx.Repo.Repository.GetAssignees(); err != nil {
  439. ctx.ServerError("GetAssignees", err)
  440. return
  441. }
  442. ctx.Data["CurrentReview"], err = models.GetCurrentReview(ctx.User, issue)
  443. if err != nil && !models.IsErrReviewNotExist(err) {
  444. ctx.ServerError("GetCurrentReview", err)
  445. return
  446. }
  447. ctx.HTML(200, tplPullFiles)
  448. }
  449. // MergePullRequest response for merging pull request
  450. func MergePullRequest(ctx *context.Context, form auth.MergePullRequestForm) {
  451. issue := checkPullInfo(ctx)
  452. if ctx.Written() {
  453. return
  454. }
  455. if issue.IsClosed {
  456. ctx.NotFound("MergePullRequest", nil)
  457. return
  458. }
  459. pr := issue.PullRequest
  460. if !pr.CanAutoMerge() || pr.HasMerged {
  461. ctx.NotFound("MergePullRequest", nil)
  462. return
  463. }
  464. if pr.IsWorkInProgress() {
  465. ctx.Flash.Error(ctx.Tr("repo.pulls.no_merge_wip"))
  466. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  467. return
  468. }
  469. if ctx.HasError() {
  470. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  471. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  472. return
  473. }
  474. message := strings.TrimSpace(form.MergeTitleField)
  475. if len(message) == 0 {
  476. if models.MergeStyle(form.Do) == models.MergeStyleMerge {
  477. message = pr.GetDefaultMergeMessage()
  478. }
  479. if models.MergeStyle(form.Do) == models.MergeStyleRebaseMerge {
  480. message = pr.GetDefaultMergeMessage()
  481. }
  482. if models.MergeStyle(form.Do) == models.MergeStyleSquash {
  483. message = pr.GetDefaultSquashMessage()
  484. }
  485. }
  486. form.MergeMessageField = strings.TrimSpace(form.MergeMessageField)
  487. if len(form.MergeMessageField) > 0 {
  488. message += "\n\n" + form.MergeMessageField
  489. }
  490. pr.Issue = issue
  491. pr.Issue.Repo = ctx.Repo.Repository
  492. noDeps, err := models.IssueNoDependenciesLeft(issue)
  493. if err != nil {
  494. return
  495. }
  496. if !noDeps {
  497. ctx.Flash.Error(ctx.Tr("repo.issues.dependency.pr_close_blocked"))
  498. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  499. return
  500. }
  501. if err = pr.Merge(ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil {
  502. if models.IsErrInvalidMergeStyle(err) {
  503. ctx.Flash.Error(ctx.Tr("repo.pulls.invalid_merge_option"))
  504. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  505. return
  506. }
  507. ctx.ServerError("Merge", err)
  508. return
  509. }
  510. if err := stopTimerIfAvailable(ctx.User, issue); err != nil {
  511. ctx.ServerError("CreateOrStopIssueStopwatch", err)
  512. return
  513. }
  514. notification.NotifyMergePullRequest(pr, ctx.User, ctx.Repo.GitRepo)
  515. log.Trace("Pull request merged: %d", pr.ID)
  516. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  517. }
  518. func stopTimerIfAvailable(user *models.User, issue *models.Issue) error {
  519. if models.StopwatchExists(user.ID, issue.ID) {
  520. if err := models.CreateOrStopIssueStopwatch(user, issue); err != nil {
  521. return err
  522. }
  523. }
  524. return nil
  525. }
  526. // ParseCompareInfo parse compare info between two commit for preparing pull request
  527. func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
  528. baseRepo := ctx.Repo.Repository
  529. // Get compared branches information
  530. // format: <base branch>...[<head repo>:]<head branch>
  531. // base<-head: master...head:feature
  532. // same repo: master...feature
  533. var (
  534. headUser *models.User
  535. headBranch string
  536. isSameRepo bool
  537. infoPath string
  538. err error
  539. )
  540. infoPath, err = url.QueryUnescape(ctx.Params("*"))
  541. if err != nil {
  542. ctx.NotFound("QueryUnescape", err)
  543. }
  544. infos := strings.Split(infoPath, "...")
  545. if len(infos) != 2 {
  546. log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)
  547. ctx.NotFound("CompareAndPullRequest", nil)
  548. return nil, nil, nil, nil, "", ""
  549. }
  550. baseBranch := infos[0]
  551. ctx.Data["BaseBranch"] = baseBranch
  552. // If there is no head repository, it means pull request between same repository.
  553. headInfos := strings.Split(infos[1], ":")
  554. if len(headInfos) == 1 {
  555. isSameRepo = true
  556. headUser = ctx.Repo.Owner
  557. headBranch = headInfos[0]
  558. } else if len(headInfos) == 2 {
  559. headUser, err = models.GetUserByName(headInfos[0])
  560. if err != nil {
  561. if models.IsErrUserNotExist(err) {
  562. ctx.NotFound("GetUserByName", nil)
  563. } else {
  564. ctx.ServerError("GetUserByName", err)
  565. }
  566. return nil, nil, nil, nil, "", ""
  567. }
  568. headBranch = headInfos[1]
  569. isSameRepo = headUser.ID == ctx.Repo.Owner.ID
  570. } else {
  571. ctx.NotFound("CompareAndPullRequest", nil)
  572. return nil, nil, nil, nil, "", ""
  573. }
  574. ctx.Data["HeadUser"] = headUser
  575. ctx.Data["HeadBranch"] = headBranch
  576. ctx.Repo.PullRequest.SameRepo = isSameRepo
  577. // Check if base branch is valid.
  578. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  579. ctx.NotFound("IsBranchExist", nil)
  580. return nil, nil, nil, nil, "", ""
  581. }
  582. // Check if current user has fork of repository or in the same repository.
  583. headRepo, has := models.HasForkedRepo(headUser.ID, baseRepo.ID)
  584. if !has && !isSameRepo {
  585. log.Trace("ParseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
  586. ctx.NotFound("ParseCompareInfo", nil)
  587. return nil, nil, nil, nil, "", ""
  588. }
  589. var headGitRepo *git.Repository
  590. if isSameRepo {
  591. headRepo = ctx.Repo.Repository
  592. headGitRepo = ctx.Repo.GitRepo
  593. ctx.Data["BaseName"] = headUser.Name
  594. } else {
  595. headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  596. ctx.Data["BaseName"] = baseRepo.OwnerName
  597. if err != nil {
  598. ctx.ServerError("OpenRepository", err)
  599. return nil, nil, nil, nil, "", ""
  600. }
  601. }
  602. perm, err := models.GetUserRepoPermission(headRepo, ctx.User)
  603. if err != nil {
  604. ctx.ServerError("GetUserRepoPermission", err)
  605. return nil, nil, nil, nil, "", ""
  606. }
  607. if !perm.CanReadIssuesOrPulls(true) {
  608. log.Trace("ParseCompareInfo[%d]: cannot create/read pull requests", baseRepo.ID)
  609. ctx.NotFound("ParseCompareInfo", nil)
  610. return nil, nil, nil, nil, "", ""
  611. }
  612. // Check if head branch is valid.
  613. if !headGitRepo.IsBranchExist(headBranch) {
  614. ctx.NotFound("IsBranchExist", nil)
  615. return nil, nil, nil, nil, "", ""
  616. }
  617. headBranches, err := headGitRepo.GetBranches()
  618. if err != nil {
  619. ctx.ServerError("GetBranches", err)
  620. return nil, nil, nil, nil, "", ""
  621. }
  622. ctx.Data["HeadBranches"] = headBranches
  623. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
  624. if err != nil {
  625. ctx.ServerError("GetPullRequestInfo", err)
  626. return nil, nil, nil, nil, "", ""
  627. }
  628. ctx.Data["BeforeCommitID"] = prInfo.MergeBase
  629. return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
  630. }
  631. // PrepareCompareDiff render pull request preview diff page
  632. func PrepareCompareDiff(
  633. ctx *context.Context,
  634. headUser *models.User,
  635. headRepo *models.Repository,
  636. headGitRepo *git.Repository,
  637. prInfo *git.PullRequestInfo,
  638. baseBranch, headBranch string) bool {
  639. var (
  640. repo = ctx.Repo.Repository
  641. err error
  642. title string
  643. )
  644. // Get diff information.
  645. ctx.Data["CommitRepoLink"] = headRepo.Link()
  646. headCommitID, err := headGitRepo.GetBranchCommitID(headBranch)
  647. if err != nil {
  648. ctx.ServerError("GetBranchCommitID", err)
  649. return false
  650. }
  651. ctx.Data["AfterCommitID"] = headCommitID
  652. if headCommitID == prInfo.MergeBase {
  653. ctx.Data["IsNothingToCompare"] = true
  654. return true
  655. }
  656. diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
  657. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
  658. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  659. if err != nil {
  660. ctx.ServerError("GetDiffRange", err)
  661. return false
  662. }
  663. ctx.Data["Diff"] = diff
  664. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  665. headCommit, err := headGitRepo.GetCommit(headCommitID)
  666. if err != nil {
  667. ctx.ServerError("GetCommit", err)
  668. return false
  669. }
  670. prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
  671. prInfo.Commits = models.ParseCommitsWithSignature(prInfo.Commits)
  672. prInfo.Commits = models.ParseCommitsWithStatus(prInfo.Commits, headRepo)
  673. ctx.Data["Commits"] = prInfo.Commits
  674. ctx.Data["CommitCount"] = prInfo.Commits.Len()
  675. if prInfo.Commits.Len() == 1 {
  676. c := prInfo.Commits.Front().Value.(models.SignCommitWithStatuses)
  677. title = strings.TrimSpace(c.UserCommit.Summary())
  678. body := strings.Split(strings.TrimSpace(c.UserCommit.Message()), "\n")
  679. if len(body) > 1 {
  680. ctx.Data["content"] = strings.Join(body[1:], "\n")
  681. }
  682. } else {
  683. title = headBranch
  684. }
  685. ctx.Data["title"] = title
  686. ctx.Data["Username"] = headUser.Name
  687. ctx.Data["Reponame"] = headRepo.Name
  688. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  689. headTarget := path.Join(headUser.Name, repo.Name)
  690. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", headCommitID)
  691. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", prInfo.MergeBase)
  692. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", "commit", headCommitID)
  693. return false
  694. }
  695. // CompareAndPullRequest render pull request preview page
  696. func CompareAndPullRequest(ctx *context.Context) {
  697. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  698. ctx.Data["PageIsComparePull"] = true
  699. ctx.Data["IsDiffCompare"] = true
  700. ctx.Data["RequireHighlightJS"] = true
  701. ctx.Data["RequireTribute"] = true
  702. ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
  703. setTemplateIfExists(ctx, pullRequestTemplateKey, pullRequestTemplateCandidates)
  704. renderAttachmentSettings(ctx)
  705. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  706. if ctx.Written() {
  707. return
  708. }
  709. pr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
  710. if err != nil {
  711. if !models.IsErrPullRequestNotExist(err) {
  712. ctx.ServerError("GetUnmergedPullRequest", err)
  713. return
  714. }
  715. } else {
  716. ctx.Data["HasPullRequest"] = true
  717. ctx.Data["PullRequest"] = pr
  718. ctx.HTML(200, tplComparePull)
  719. return
  720. }
  721. nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  722. if ctx.Written() {
  723. return
  724. }
  725. if !nothingToCompare {
  726. // Setup information for new form.
  727. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  728. if ctx.Written() {
  729. return
  730. }
  731. }
  732. ctx.HTML(200, tplComparePull)
  733. }
  734. // CompareAndPullRequestPost response for creating pull request
  735. func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm) {
  736. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  737. ctx.Data["PageIsComparePull"] = true
  738. ctx.Data["IsDiffCompare"] = true
  739. ctx.Data["RequireHighlightJS"] = true
  740. ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
  741. renderAttachmentSettings(ctx)
  742. var (
  743. repo = ctx.Repo.Repository
  744. attachments []string
  745. )
  746. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  747. if ctx.Written() {
  748. return
  749. }
  750. labelIDs, assigneeIDs, milestoneID := ValidateRepoMetas(ctx, form, true)
  751. if ctx.Written() {
  752. return
  753. }
  754. if setting.AttachmentEnabled {
  755. attachments = form.Files
  756. }
  757. if ctx.HasError() {
  758. auth.AssignForm(form, ctx.Data)
  759. // This stage is already stop creating new pull request, so it does not matter if it has
  760. // something to compare or not.
  761. PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  762. if ctx.Written() {
  763. return
  764. }
  765. ctx.HTML(200, tplComparePull)
  766. return
  767. }
  768. if util.IsEmptyString(form.Title) {
  769. PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  770. if ctx.Written() {
  771. return
  772. }
  773. ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplComparePull, form)
  774. return
  775. }
  776. patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
  777. if err != nil {
  778. ctx.ServerError("GetPatch", err)
  779. return
  780. }
  781. pullIssue := &models.Issue{
  782. RepoID: repo.ID,
  783. Index: repo.NextIssueIndex(),
  784. Title: form.Title,
  785. PosterID: ctx.User.ID,
  786. Poster: ctx.User,
  787. MilestoneID: milestoneID,
  788. IsPull: true,
  789. Content: form.Content,
  790. }
  791. pullRequest := &models.PullRequest{
  792. HeadRepoID: headRepo.ID,
  793. BaseRepoID: repo.ID,
  794. HeadUserName: headUser.Name,
  795. HeadBranch: headBranch,
  796. BaseBranch: baseBranch,
  797. HeadRepo: headRepo,
  798. BaseRepo: repo,
  799. MergeBase: prInfo.MergeBase,
  800. Type: models.PullRequestGitea,
  801. }
  802. // FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
  803. // instead of 500.
  804. if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch, assigneeIDs); err != nil {
  805. if models.IsErrUserDoesNotHaveAccessToRepo(err) {
  806. ctx.Error(400, "UserDoesNotHaveAccessToRepo", err.Error())
  807. return
  808. }
  809. ctx.ServerError("NewPullRequest", err)
  810. return
  811. } else if err := pullRequest.PushToBaseRepo(); err != nil {
  812. ctx.ServerError("PushToBaseRepo", err)
  813. return
  814. }
  815. notification.NotifyNewPullRequest(pullRequest)
  816. log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
  817. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
  818. }
  819. // TriggerTask response for a trigger task request
  820. func TriggerTask(ctx *context.Context) {
  821. pusherID := ctx.QueryInt64("pusher")
  822. branch := ctx.Query("branch")
  823. secret := ctx.Query("secret")
  824. if len(branch) == 0 || len(secret) == 0 || pusherID <= 0 {
  825. ctx.Error(404)
  826. log.Trace("TriggerTask: branch or secret is empty, or pusher ID is not valid")
  827. return
  828. }
  829. owner, repo := parseOwnerAndRepo(ctx)
  830. if ctx.Written() {
  831. return
  832. }
  833. if secret != base.EncodeMD5(owner.Salt) {
  834. ctx.Error(404)
  835. log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
  836. return
  837. }
  838. pusher, err := models.GetUserByID(pusherID)
  839. if err != nil {
  840. if models.IsErrUserNotExist(err) {
  841. ctx.Error(404)
  842. } else {
  843. ctx.ServerError("GetUserByID", err)
  844. }
  845. return
  846. }
  847. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  848. go models.HookQueue.Add(repo.ID)
  849. go models.AddTestPullRequestTask(pusher, repo.ID, branch, true)
  850. ctx.Status(202)
  851. }
  852. // CleanUpPullRequest responses for delete merged branch when PR has been merged
  853. func CleanUpPullRequest(ctx *context.Context) {
  854. issue := checkPullInfo(ctx)
  855. if ctx.Written() {
  856. return
  857. }
  858. pr := issue.PullRequest
  859. // Allow cleanup only for merged PR
  860. if !pr.HasMerged {
  861. ctx.NotFound("CleanUpPullRequest", nil)
  862. return
  863. }
  864. if err := pr.GetHeadRepo(); err != nil {
  865. ctx.ServerError("GetHeadRepo", err)
  866. return
  867. } else if pr.HeadRepo == nil {
  868. // Forked repository has already been deleted
  869. ctx.NotFound("CleanUpPullRequest", nil)
  870. return
  871. } else if pr.GetBaseRepo(); err != nil {
  872. ctx.ServerError("GetBaseRepo", err)
  873. return
  874. } else if pr.HeadRepo.GetOwner(); err != nil {
  875. ctx.ServerError("HeadRepo.GetOwner", err)
  876. return
  877. }
  878. perm, err := models.GetUserRepoPermission(pr.HeadRepo, ctx.User)
  879. if err != nil {
  880. ctx.ServerError("GetUserRepoPermission", err)
  881. return
  882. }
  883. if !perm.CanWrite(models.UnitTypeCode) {
  884. ctx.NotFound("CleanUpPullRequest", nil)
  885. return
  886. }
  887. fullBranchName := pr.HeadRepo.Owner.Name + "/" + pr.HeadBranch
  888. gitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
  889. if err != nil {
  890. ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.RepoPath()), err)
  891. return
  892. }
  893. gitBaseRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
  894. if err != nil {
  895. ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.BaseRepo.RepoPath()), err)
  896. return
  897. }
  898. defer func() {
  899. ctx.JSON(200, map[string]interface{}{
  900. "redirect": pr.BaseRepo.Link() + "/pulls/" + com.ToStr(issue.Index),
  901. })
  902. }()
  903. if pr.HeadBranch == pr.HeadRepo.DefaultBranch || !gitRepo.IsBranchExist(pr.HeadBranch) {
  904. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  905. return
  906. }
  907. // Check if branch is not protected
  908. if protected, err := pr.HeadRepo.IsProtectedBranch(pr.HeadBranch, ctx.User); err != nil || protected {
  909. if err != nil {
  910. log.Error(4, "HeadRepo.IsProtectedBranch: %v", err)
  911. }
  912. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  913. return
  914. }
  915. // Check if branch has no new commits
  916. headCommitID, err := gitBaseRepo.GetRefCommitID(pr.GetGitRefName())
  917. if err != nil {
  918. log.Error(4, "GetRefCommitID: %v", err)
  919. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  920. return
  921. }
  922. branchCommitID, err := gitRepo.GetBranchCommitID(pr.HeadBranch)
  923. if err != nil {
  924. log.Error(4, "GetBranchCommitID: %v", err)
  925. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  926. return
  927. }
  928. if headCommitID != branchCommitID {
  929. ctx.Flash.Error(ctx.Tr("repo.branch.delete_branch_has_new_commits", fullBranchName))
  930. return
  931. }
  932. if err := gitRepo.DeleteBranch(pr.HeadBranch, git.DeleteBranchOptions{
  933. Force: true,
  934. }); err != nil {
  935. log.Error(4, "DeleteBranch: %v", err)
  936. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  937. return
  938. }
  939. if err := models.AddDeletePRBranchComment(ctx.User, pr.BaseRepo, issue.ID, pr.HeadBranch); err != nil {
  940. // Do not fail here as branch has already been deleted
  941. log.Error(4, "DeleteBranch: %v", err)
  942. }
  943. ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", fullBranchName))
  944. }
  945. // DownloadPullDiff render a pull's raw diff
  946. func DownloadPullDiff(ctx *context.Context) {
  947. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  948. if err != nil {
  949. if models.IsErrIssueNotExist(err) {
  950. ctx.NotFound("GetIssueByIndex", err)
  951. } else {
  952. ctx.ServerError("GetIssueByIndex", err)
  953. }
  954. return
  955. }
  956. // Return not found if it's not a pull request
  957. if !issue.IsPull {
  958. ctx.NotFound("DownloadPullDiff",
  959. fmt.Errorf("Issue is not a pull request"))
  960. return
  961. }
  962. if err = issue.LoadPullRequest(); err != nil {
  963. ctx.ServerError("LoadPullRequest", err)
  964. return
  965. }
  966. pr := issue.PullRequest
  967. if err = pr.GetBaseRepo(); err != nil {
  968. ctx.ServerError("GetBaseRepo", err)
  969. return
  970. }
  971. patch, err := pr.BaseRepo.PatchPath(pr.Index)
  972. if err != nil {
  973. ctx.ServerError("PatchPath", err)
  974. return
  975. }
  976. ctx.ServeFileContent(patch)
  977. }
  978. // DownloadPullPatch render a pull's raw patch
  979. func DownloadPullPatch(ctx *context.Context) {
  980. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  981. if err != nil {
  982. if models.IsErrIssueNotExist(err) {
  983. ctx.NotFound("GetIssueByIndex", err)
  984. } else {
  985. ctx.ServerError("GetIssueByIndex", err)
  986. }
  987. return
  988. }
  989. // Return not found if it's not a pull request
  990. if !issue.IsPull {
  991. ctx.NotFound("DownloadPullDiff",
  992. fmt.Errorf("Issue is not a pull request"))
  993. return
  994. }
  995. if err = issue.LoadPullRequest(); err != nil {
  996. ctx.ServerError("LoadPullRequest", err)
  997. return
  998. }
  999. pr := issue.PullRequest
  1000. if err = pr.GetHeadRepo(); err != nil {
  1001. ctx.ServerError("GetHeadRepo", err)
  1002. return
  1003. }
  1004. headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
  1005. if err != nil {
  1006. ctx.ServerError("OpenRepository", err)
  1007. return
  1008. }
  1009. patch, err := headGitRepo.GetFormatPatch(pr.MergeBase, pr.HeadBranch)
  1010. if err != nil {
  1011. ctx.ServerError("GetFormatPatch", err)
  1012. return
  1013. }
  1014. _, err = io.Copy(ctx, patch)
  1015. if err != nil {
  1016. ctx.ServerError("io.Copy", err)
  1017. return
  1018. }
  1019. }