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 25 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "container/list"
  7. "fmt"
  8. "path"
  9. "strings"
  10. "code.gitea.io/git"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/auth"
  13. "code.gitea.io/gitea/modules/base"
  14. "code.gitea.io/gitea/modules/context"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/notification"
  17. "code.gitea.io/gitea/modules/setting"
  18. "github.com/Unknwon/com"
  19. )
  20. const (
  21. tplFork base.TplName = "repo/pulls/fork"
  22. tplComparePull base.TplName = "repo/pulls/compare"
  23. tplPullCommits base.TplName = "repo/pulls/commits"
  24. tplPullFiles base.TplName = "repo/pulls/files"
  25. pullRequestTemplateKey = "PullRequestTemplate"
  26. )
  27. var (
  28. pullRequestTemplateCandidates = []string{
  29. "PULL_REQUEST_TEMPLATE.md",
  30. "pull_request_template.md",
  31. ".gitea/PULL_REQUEST_TEMPLATE.md",
  32. ".gitea/pull_request_template.md",
  33. ".github/PULL_REQUEST_TEMPLATE.md",
  34. ".github/pull_request_template.md",
  35. }
  36. )
  37. func getForkRepository(ctx *context.Context) *models.Repository {
  38. forkRepo, err := models.GetRepositoryByID(ctx.ParamsInt64(":repoid"))
  39. if err != nil {
  40. if models.IsErrRepoNotExist(err) {
  41. ctx.Handle(404, "GetRepositoryByID", nil)
  42. } else {
  43. ctx.Handle(500, "GetRepositoryByID", err)
  44. }
  45. return nil
  46. }
  47. if !forkRepo.CanBeForked() || !forkRepo.HasAccess(ctx.User) {
  48. ctx.Handle(404, "getForkRepository", nil)
  49. return nil
  50. }
  51. ctx.Data["repo_name"] = forkRepo.Name
  52. ctx.Data["description"] = forkRepo.Description
  53. ctx.Data["IsPrivate"] = forkRepo.IsPrivate
  54. if err = forkRepo.GetOwner(); err != nil {
  55. ctx.Handle(500, "GetOwner", err)
  56. return nil
  57. }
  58. ctx.Data["ForkFrom"] = forkRepo.Owner.Name + "/" + forkRepo.Name
  59. ctx.Data["ForkFromOwnerID"] = forkRepo.Owner.ID
  60. if err := ctx.User.GetOrganizations(true); err != nil {
  61. ctx.Handle(500, "GetOrganizations", err)
  62. return nil
  63. }
  64. ctx.Data["Orgs"] = ctx.User.Orgs
  65. return forkRepo
  66. }
  67. // Fork render repository fork page
  68. func Fork(ctx *context.Context) {
  69. ctx.Data["Title"] = ctx.Tr("new_fork")
  70. getForkRepository(ctx)
  71. if ctx.Written() {
  72. return
  73. }
  74. ctx.Data["ContextUser"] = ctx.User
  75. ctx.HTML(200, tplFork)
  76. }
  77. // ForkPost response for forking a repository
  78. func ForkPost(ctx *context.Context, form auth.CreateRepoForm) {
  79. ctx.Data["Title"] = ctx.Tr("new_fork")
  80. forkRepo := getForkRepository(ctx)
  81. if ctx.Written() {
  82. return
  83. }
  84. ctxUser := checkContextUser(ctx, form.UID)
  85. if ctx.Written() {
  86. return
  87. }
  88. ctx.Data["ContextUser"] = ctxUser
  89. if ctx.HasError() {
  90. ctx.HTML(200, tplFork)
  91. return
  92. }
  93. // Check ownership of organization.
  94. if ctxUser.IsOrganization() {
  95. if !ctxUser.IsOwnedBy(ctx.User.ID) {
  96. ctx.Error(403)
  97. return
  98. }
  99. }
  100. repo, err := models.ForkRepository(ctxUser, forkRepo, form.RepoName, form.Description)
  101. if err != nil {
  102. ctx.Data["Err_RepoName"] = true
  103. switch {
  104. case models.IsErrRepoAlreadyExist(err):
  105. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplFork, &form)
  106. case models.IsErrNameReserved(err):
  107. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), tplFork, &form)
  108. case models.IsErrNamePatternNotAllowed(err):
  109. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplFork, &form)
  110. default:
  111. ctx.Handle(500, "ForkPost", err)
  112. }
  113. return
  114. }
  115. log.Trace("Repository forked[%d]: %s/%s", forkRepo.ID, ctxUser.Name, repo.Name)
  116. ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + repo.Name)
  117. }
  118. func checkPullInfo(ctx *context.Context) *models.Issue {
  119. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  120. if err != nil {
  121. if models.IsErrIssueNotExist(err) {
  122. ctx.Handle(404, "GetIssueByIndex", err)
  123. } else {
  124. ctx.Handle(500, "GetIssueByIndex", err)
  125. }
  126. return nil
  127. }
  128. ctx.Data["Title"] = fmt.Sprintf("#%d - %s", issue.Index, issue.Title)
  129. ctx.Data["Issue"] = issue
  130. if !issue.IsPull {
  131. ctx.Handle(404, "ViewPullCommits", nil)
  132. return nil
  133. }
  134. if err = issue.PullRequest.GetHeadRepo(); err != nil {
  135. ctx.Handle(500, "GetHeadRepo", err)
  136. return nil
  137. }
  138. if ctx.IsSigned {
  139. // Update issue-user.
  140. if err = issue.ReadBy(ctx.User.ID); err != nil {
  141. ctx.Handle(500, "ReadBy", err)
  142. return nil
  143. }
  144. }
  145. return issue
  146. }
  147. // PrepareMergedViewPullInfo show meta information for a merged pull request view page
  148. func PrepareMergedViewPullInfo(ctx *context.Context, issue *models.Issue) {
  149. pull := issue.PullRequest
  150. ctx.Data["HasMerged"] = true
  151. ctx.Data["HeadTarget"] = issue.PullRequest.HeadUserName + "/" + pull.HeadBranch
  152. ctx.Data["BaseTarget"] = ctx.Repo.Owner.Name + "/" + pull.BaseBranch
  153. var err error
  154. ctx.Data["NumCommits"], err = ctx.Repo.GitRepo.CommitsCountBetween(pull.MergeBase, pull.MergedCommitID)
  155. if err != nil {
  156. ctx.Handle(500, "Repo.GitRepo.CommitsCountBetween", err)
  157. return
  158. }
  159. ctx.Data["NumFiles"], err = ctx.Repo.GitRepo.FilesCountBetween(pull.MergeBase, pull.MergedCommitID)
  160. if err != nil {
  161. ctx.Handle(500, "Repo.GitRepo.FilesCountBetween", err)
  162. return
  163. }
  164. }
  165. // PrepareViewPullInfo show meta information for a pull request preview page
  166. func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.PullRequestInfo {
  167. repo := ctx.Repo.Repository
  168. pull := issue.PullRequest
  169. ctx.Data["HeadTarget"] = pull.HeadUserName + "/" + pull.HeadBranch
  170. ctx.Data["BaseTarget"] = ctx.Repo.Owner.Name + "/" + pull.BaseBranch
  171. var (
  172. headGitRepo *git.Repository
  173. err error
  174. )
  175. if err = pull.GetHeadRepo(); err != nil {
  176. ctx.Handle(500, "GetHeadRepo", err)
  177. return nil
  178. }
  179. if pull.HeadRepo != nil {
  180. headGitRepo, err = git.OpenRepository(pull.HeadRepo.RepoPath())
  181. if err != nil {
  182. ctx.Handle(500, "OpenRepository", err)
  183. return nil
  184. }
  185. }
  186. if pull.HeadRepo == nil || !headGitRepo.IsBranchExist(pull.HeadBranch) {
  187. ctx.Data["IsPullReuqestBroken"] = true
  188. ctx.Data["HeadTarget"] = "deleted"
  189. ctx.Data["NumCommits"] = 0
  190. ctx.Data["NumFiles"] = 0
  191. return nil
  192. }
  193. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name),
  194. pull.BaseBranch, pull.HeadBranch)
  195. if err != nil {
  196. if strings.Contains(err.Error(), "fatal: Not a valid object name") {
  197. ctx.Data["IsPullReuqestBroken"] = true
  198. ctx.Data["BaseTarget"] = "deleted"
  199. ctx.Data["NumCommits"] = 0
  200. ctx.Data["NumFiles"] = 0
  201. return nil
  202. }
  203. ctx.Handle(500, "GetPullRequestInfo", err)
  204. return nil
  205. }
  206. ctx.Data["NumCommits"] = prInfo.Commits.Len()
  207. ctx.Data["NumFiles"] = prInfo.NumFiles
  208. return prInfo
  209. }
  210. // ViewPullCommits show commits for a pull request
  211. func ViewPullCommits(ctx *context.Context) {
  212. ctx.Data["PageIsPullList"] = true
  213. ctx.Data["PageIsPullCommits"] = true
  214. issue := checkPullInfo(ctx)
  215. if ctx.Written() {
  216. return
  217. }
  218. pull := issue.PullRequest
  219. var commits *list.List
  220. if pull.HasMerged {
  221. PrepareMergedViewPullInfo(ctx, issue)
  222. if ctx.Written() {
  223. return
  224. }
  225. ctx.Data["Username"] = ctx.Repo.Owner.Name
  226. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  227. startCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergeBase)
  228. if err != nil {
  229. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  230. return
  231. }
  232. endCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergedCommitID)
  233. if err != nil {
  234. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  235. return
  236. }
  237. commits, err = ctx.Repo.GitRepo.CommitsBetween(endCommit, startCommit)
  238. if err != nil {
  239. ctx.Handle(500, "Repo.GitRepo.CommitsBetween", err)
  240. return
  241. }
  242. } else {
  243. prInfo := PrepareViewPullInfo(ctx, issue)
  244. if ctx.Written() {
  245. return
  246. } else if prInfo == nil {
  247. ctx.Handle(404, "ViewPullCommits", nil)
  248. return
  249. }
  250. ctx.Data["Username"] = pull.HeadUserName
  251. ctx.Data["Reponame"] = pull.HeadRepo.Name
  252. commits = prInfo.Commits
  253. }
  254. commits = models.ValidateCommitsWithEmails(commits)
  255. commits = models.ParseCommitsWithSignature(commits)
  256. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  257. ctx.Data["Commits"] = commits
  258. ctx.Data["CommitCount"] = commits.Len()
  259. ctx.HTML(200, tplPullCommits)
  260. }
  261. // ViewPullFiles render pull request changed files list page
  262. func ViewPullFiles(ctx *context.Context) {
  263. ctx.Data["PageIsPullList"] = true
  264. ctx.Data["PageIsPullFiles"] = true
  265. issue := checkPullInfo(ctx)
  266. if ctx.Written() {
  267. return
  268. }
  269. pull := issue.PullRequest
  270. var (
  271. diffRepoPath string
  272. startCommitID string
  273. endCommitID string
  274. gitRepo *git.Repository
  275. )
  276. var headTarget string
  277. if pull.HasMerged {
  278. PrepareMergedViewPullInfo(ctx, issue)
  279. if ctx.Written() {
  280. return
  281. }
  282. diffRepoPath = ctx.Repo.GitRepo.Path
  283. startCommitID = pull.MergeBase
  284. endCommitID = pull.MergedCommitID
  285. gitRepo = ctx.Repo.GitRepo
  286. headTarget = path.Join(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  287. ctx.Data["Username"] = ctx.Repo.Owner.Name
  288. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  289. } else {
  290. prInfo := PrepareViewPullInfo(ctx, issue)
  291. if ctx.Written() {
  292. return
  293. } else if prInfo == nil {
  294. ctx.Handle(404, "ViewPullFiles", nil)
  295. return
  296. }
  297. headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
  298. headGitRepo, err := git.OpenRepository(headRepoPath)
  299. if err != nil {
  300. ctx.Handle(500, "OpenRepository", err)
  301. return
  302. }
  303. headCommitID, err := headGitRepo.GetBranchCommitID(pull.HeadBranch)
  304. if err != nil {
  305. ctx.Handle(500, "GetBranchCommitID", err)
  306. return
  307. }
  308. diffRepoPath = headRepoPath
  309. startCommitID = prInfo.MergeBase
  310. endCommitID = headCommitID
  311. gitRepo = headGitRepo
  312. headTarget = path.Join(pull.HeadUserName, pull.HeadRepo.Name)
  313. ctx.Data["Username"] = pull.HeadUserName
  314. ctx.Data["Reponame"] = pull.HeadRepo.Name
  315. }
  316. diff, err := models.GetDiffRange(diffRepoPath,
  317. startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
  318. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  319. if err != nil {
  320. ctx.Handle(500, "GetDiffRange", err)
  321. return
  322. }
  323. ctx.Data["Diff"] = diff
  324. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  325. commit, err := gitRepo.GetCommit(endCommitID)
  326. if err != nil {
  327. ctx.Handle(500, "GetCommit", err)
  328. return
  329. }
  330. ctx.Data["IsImageFile"] = commit.IsImageFile
  331. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", endCommitID)
  332. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", startCommitID)
  333. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", endCommitID)
  334. ctx.Data["RequireHighlightJS"] = true
  335. ctx.HTML(200, tplPullFiles)
  336. }
  337. // MergePullRequest response for merging pull request
  338. func MergePullRequest(ctx *context.Context) {
  339. issue := checkPullInfo(ctx)
  340. if ctx.Written() {
  341. return
  342. }
  343. if issue.IsClosed {
  344. ctx.Handle(404, "MergePullRequest", nil)
  345. return
  346. }
  347. pr, err := models.GetPullRequestByIssueID(issue.ID)
  348. if err != nil {
  349. if models.IsErrPullRequestNotExist(err) {
  350. ctx.Handle(404, "GetPullRequestByIssueID", nil)
  351. } else {
  352. ctx.Handle(500, "GetPullRequestByIssueID", err)
  353. }
  354. return
  355. }
  356. if !pr.CanAutoMerge() || pr.HasMerged {
  357. ctx.Handle(404, "MergePullRequest", nil)
  358. return
  359. }
  360. pr.Issue = issue
  361. pr.Issue.Repo = ctx.Repo.Repository
  362. if err = pr.Merge(ctx.User, ctx.Repo.GitRepo); err != nil {
  363. ctx.Handle(500, "Merge", err)
  364. return
  365. }
  366. notification.Service.NotifyIssue(pr.Issue, ctx.User.ID)
  367. log.Trace("Pull request merged: %d", pr.ID)
  368. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  369. }
  370. // ParseCompareInfo parse compare info between two commit for preparing pull request
  371. func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
  372. baseRepo := ctx.Repo.Repository
  373. // Get compared branches information
  374. // format: <base branch>...[<head repo>:]<head branch>
  375. // base<-head: master...head:feature
  376. // same repo: master...feature
  377. infos := strings.Split(ctx.Params("*"), "...")
  378. if len(infos) != 2 {
  379. log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)
  380. ctx.Handle(404, "CompareAndPullRequest", nil)
  381. return nil, nil, nil, nil, "", ""
  382. }
  383. baseBranch := infos[0]
  384. ctx.Data["BaseBranch"] = baseBranch
  385. var (
  386. headUser *models.User
  387. headBranch string
  388. isSameRepo bool
  389. err error
  390. )
  391. // If there is no head repository, it means pull request between same repository.
  392. headInfos := strings.Split(infos[1], ":")
  393. if len(headInfos) == 1 {
  394. isSameRepo = true
  395. headUser = ctx.Repo.Owner
  396. headBranch = headInfos[0]
  397. } else if len(headInfos) == 2 {
  398. headUser, err = models.GetUserByName(headInfos[0])
  399. if err != nil {
  400. if models.IsErrUserNotExist(err) {
  401. ctx.Handle(404, "GetUserByName", nil)
  402. } else {
  403. ctx.Handle(500, "GetUserByName", err)
  404. }
  405. return nil, nil, nil, nil, "", ""
  406. }
  407. headBranch = headInfos[1]
  408. isSameRepo = headUser.ID == ctx.Repo.Owner.ID
  409. } else {
  410. ctx.Handle(404, "CompareAndPullRequest", nil)
  411. return nil, nil, nil, nil, "", ""
  412. }
  413. ctx.Data["HeadUser"] = headUser
  414. ctx.Data["HeadBranch"] = headBranch
  415. ctx.Repo.PullRequest.SameRepo = isSameRepo
  416. // Check if base branch is valid.
  417. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  418. ctx.Handle(404, "IsBranchExist", nil)
  419. return nil, nil, nil, nil, "", ""
  420. }
  421. // Check if current user has fork of repository or in the same repository.
  422. headRepo, has := models.HasForkedRepo(headUser.ID, baseRepo.ID)
  423. if !has && !isSameRepo {
  424. log.Trace("ParseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
  425. ctx.Handle(404, "ParseCompareInfo", nil)
  426. return nil, nil, nil, nil, "", ""
  427. }
  428. var headGitRepo *git.Repository
  429. if isSameRepo {
  430. headRepo = ctx.Repo.Repository
  431. headGitRepo = ctx.Repo.GitRepo
  432. } else {
  433. headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  434. if err != nil {
  435. ctx.Handle(500, "OpenRepository", err)
  436. return nil, nil, nil, nil, "", ""
  437. }
  438. }
  439. if !ctx.User.IsWriterOfRepo(headRepo) && !ctx.User.IsAdmin {
  440. log.Trace("ParseCompareInfo[%d]: does not have write access or site admin", baseRepo.ID)
  441. ctx.Handle(404, "ParseCompareInfo", nil)
  442. return nil, nil, nil, nil, "", ""
  443. }
  444. // Check if head branch is valid.
  445. if !headGitRepo.IsBranchExist(headBranch) {
  446. ctx.Handle(404, "IsBranchExist", nil)
  447. return nil, nil, nil, nil, "", ""
  448. }
  449. headBranches, err := headGitRepo.GetBranches()
  450. if err != nil {
  451. ctx.Handle(500, "GetBranches", err)
  452. return nil, nil, nil, nil, "", ""
  453. }
  454. ctx.Data["HeadBranches"] = headBranches
  455. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
  456. if err != nil {
  457. ctx.Handle(500, "GetPullRequestInfo", err)
  458. return nil, nil, nil, nil, "", ""
  459. }
  460. ctx.Data["BeforeCommitID"] = prInfo.MergeBase
  461. return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
  462. }
  463. // PrepareCompareDiff render pull request preview diff page
  464. func PrepareCompareDiff(
  465. ctx *context.Context,
  466. headUser *models.User,
  467. headRepo *models.Repository,
  468. headGitRepo *git.Repository,
  469. prInfo *git.PullRequestInfo,
  470. baseBranch, headBranch string) bool {
  471. var (
  472. repo = ctx.Repo.Repository
  473. err error
  474. )
  475. // Get diff information.
  476. ctx.Data["CommitRepoLink"] = headRepo.Link()
  477. headCommitID, err := headGitRepo.GetBranchCommitID(headBranch)
  478. if err != nil {
  479. ctx.Handle(500, "GetBranchCommitID", err)
  480. return false
  481. }
  482. ctx.Data["AfterCommitID"] = headCommitID
  483. if headCommitID == prInfo.MergeBase {
  484. ctx.Data["IsNothingToCompare"] = true
  485. return true
  486. }
  487. diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
  488. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
  489. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  490. if err != nil {
  491. ctx.Handle(500, "GetDiffRange", err)
  492. return false
  493. }
  494. ctx.Data["Diff"] = diff
  495. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  496. headCommit, err := headGitRepo.GetCommit(headCommitID)
  497. if err != nil {
  498. ctx.Handle(500, "GetCommit", err)
  499. return false
  500. }
  501. prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
  502. prInfo.Commits = models.ParseCommitsWithSignature(prInfo.Commits)
  503. prInfo.Commits = models.ParseCommitsWithStatus(prInfo.Commits, headRepo)
  504. ctx.Data["Commits"] = prInfo.Commits
  505. ctx.Data["CommitCount"] = prInfo.Commits.Len()
  506. ctx.Data["Username"] = headUser.Name
  507. ctx.Data["Reponame"] = headRepo.Name
  508. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  509. headTarget := path.Join(headUser.Name, repo.Name)
  510. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", headCommitID)
  511. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", prInfo.MergeBase)
  512. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", headCommitID)
  513. return false
  514. }
  515. // CompareAndPullRequest render pull request preview page
  516. func CompareAndPullRequest(ctx *context.Context) {
  517. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  518. ctx.Data["PageIsComparePull"] = true
  519. ctx.Data["IsDiffCompare"] = true
  520. ctx.Data["RequireHighlightJS"] = true
  521. setTemplateIfExists(ctx, pullRequestTemplateKey, pullRequestTemplateCandidates)
  522. renderAttachmentSettings(ctx)
  523. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  524. if ctx.Written() {
  525. return
  526. }
  527. pr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
  528. if err != nil {
  529. if !models.IsErrPullRequestNotExist(err) {
  530. ctx.Handle(500, "GetUnmergedPullRequest", err)
  531. return
  532. }
  533. } else {
  534. ctx.Data["HasPullRequest"] = true
  535. ctx.Data["PullRequest"] = pr
  536. ctx.HTML(200, tplComparePull)
  537. return
  538. }
  539. nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  540. if ctx.Written() {
  541. return
  542. }
  543. if !nothingToCompare {
  544. // Setup information for new form.
  545. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  546. if ctx.Written() {
  547. return
  548. }
  549. }
  550. ctx.HTML(200, tplComparePull)
  551. }
  552. // CompareAndPullRequestPost response for creating pull request
  553. func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm) {
  554. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  555. ctx.Data["PageIsComparePull"] = true
  556. ctx.Data["IsDiffCompare"] = true
  557. ctx.Data["RequireHighlightJS"] = true
  558. renderAttachmentSettings(ctx)
  559. var (
  560. repo = ctx.Repo.Repository
  561. attachments []string
  562. )
  563. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  564. if ctx.Written() {
  565. return
  566. }
  567. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, form)
  568. if ctx.Written() {
  569. return
  570. }
  571. if setting.AttachmentEnabled {
  572. attachments = form.Files
  573. }
  574. if ctx.HasError() {
  575. auth.AssignForm(form, ctx.Data)
  576. // This stage is already stop creating new pull request, so it does not matter if it has
  577. // something to compare or not.
  578. PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  579. if ctx.Written() {
  580. return
  581. }
  582. ctx.HTML(200, tplComparePull)
  583. return
  584. }
  585. patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
  586. if err != nil {
  587. ctx.Handle(500, "GetPatch", err)
  588. return
  589. }
  590. pullIssue := &models.Issue{
  591. RepoID: repo.ID,
  592. Index: repo.NextIssueIndex(),
  593. Title: form.Title,
  594. PosterID: ctx.User.ID,
  595. Poster: ctx.User,
  596. MilestoneID: milestoneID,
  597. AssigneeID: assigneeID,
  598. IsPull: true,
  599. Content: form.Content,
  600. }
  601. pullRequest := &models.PullRequest{
  602. HeadRepoID: headRepo.ID,
  603. BaseRepoID: repo.ID,
  604. HeadUserName: headUser.Name,
  605. HeadBranch: headBranch,
  606. BaseBranch: baseBranch,
  607. HeadRepo: headRepo,
  608. BaseRepo: repo,
  609. MergeBase: prInfo.MergeBase,
  610. Type: models.PullRequestGitea,
  611. }
  612. // FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
  613. // instead of 500.
  614. if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch); err != nil {
  615. ctx.Handle(500, "NewPullRequest", err)
  616. return
  617. } else if err := pullRequest.PushToBaseRepo(); err != nil {
  618. ctx.Handle(500, "PushToBaseRepo", err)
  619. return
  620. }
  621. notification.Service.NotifyIssue(pullIssue, ctx.User.ID)
  622. log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
  623. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
  624. }
  625. // TriggerTask response for a trigger task request
  626. func TriggerTask(ctx *context.Context) {
  627. pusherID := ctx.QueryInt64("pusher")
  628. branch := ctx.Query("branch")
  629. secret := ctx.Query("secret")
  630. if len(branch) == 0 || len(secret) == 0 || pusherID <= 0 {
  631. ctx.Error(404)
  632. log.Trace("TriggerTask: branch or secret is empty, or pusher ID is not valid")
  633. return
  634. }
  635. owner, repo := parseOwnerAndRepo(ctx)
  636. if ctx.Written() {
  637. return
  638. }
  639. if secret != base.EncodeMD5(owner.Salt) {
  640. ctx.Error(404)
  641. log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
  642. return
  643. }
  644. pusher, err := models.GetUserByID(pusherID)
  645. if err != nil {
  646. if models.IsErrUserNotExist(err) {
  647. ctx.Error(404)
  648. } else {
  649. ctx.Handle(500, "GetUserByID", err)
  650. }
  651. return
  652. }
  653. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  654. go models.HookQueue.Add(repo.ID)
  655. go models.AddTestPullRequestTask(pusher, repo.ID, branch, true)
  656. ctx.Status(202)
  657. }
  658. // CleanUpPullRequest responses for delete merged branch when PR has been merged
  659. func CleanUpPullRequest(ctx *context.Context) {
  660. issue := checkPullInfo(ctx)
  661. if ctx.Written() {
  662. return
  663. }
  664. pr, err := models.GetPullRequestByIssueID(issue.ID)
  665. if err != nil {
  666. if models.IsErrPullRequestNotExist(err) {
  667. ctx.Handle(404, "GetPullRequestByIssueID", nil)
  668. } else {
  669. ctx.Handle(500, "GetPullRequestByIssueID", err)
  670. }
  671. return
  672. }
  673. // Allow cleanup only for merged PR
  674. if !pr.HasMerged {
  675. ctx.Handle(404, "CleanUpPullRequest", nil)
  676. return
  677. }
  678. if err = pr.GetHeadRepo(); err != nil {
  679. ctx.Handle(500, "GetHeadRepo", err)
  680. return
  681. } else if pr.HeadRepo == nil {
  682. // Forked repository has already been deleted
  683. ctx.Handle(404, "CleanUpPullRequest", nil)
  684. return
  685. } else if pr.GetBaseRepo(); err != nil {
  686. ctx.Handle(500, "GetBaseRepo", err)
  687. return
  688. } else if pr.HeadRepo.GetOwner(); err != nil {
  689. ctx.Handle(500, "HeadRepo.GetOwner", err)
  690. return
  691. }
  692. if !ctx.User.IsWriterOfRepo(pr.HeadRepo) {
  693. ctx.Handle(403, "CleanUpPullRequest", nil)
  694. return
  695. }
  696. fullBranchName := pr.HeadRepo.Owner.Name + "/" + pr.HeadBranch
  697. gitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
  698. if err != nil {
  699. ctx.Handle(500, fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.RepoPath()), err)
  700. return
  701. }
  702. gitBaseRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
  703. if err != nil {
  704. ctx.Handle(500, fmt.Sprintf("OpenRepository[%s]", pr.BaseRepo.RepoPath()), err)
  705. return
  706. }
  707. defer func() {
  708. ctx.JSON(200, map[string]interface{}{
  709. "redirect": pr.BaseRepo.Link() + "/pulls/" + com.ToStr(issue.Index),
  710. })
  711. }()
  712. if pr.HeadBranch == pr.HeadRepo.DefaultBranch || !gitRepo.IsBranchExist(pr.HeadBranch) {
  713. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  714. return
  715. }
  716. // Check if branch is not protected
  717. if protected, err := pr.HeadRepo.IsProtectedBranch(pr.HeadBranch); err != nil || protected {
  718. if err != nil {
  719. log.Error(4, "HeadRepo.IsProtectedBranch: %v", err)
  720. }
  721. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  722. return
  723. }
  724. // Check if branch has no new commits
  725. if len(pr.MergedCommitID) > 0 {
  726. branchCommitID, err := gitRepo.GetBranchCommitID(pr.HeadBranch)
  727. if err != nil {
  728. log.Error(4, "GetBranchCommitID: %v", err)
  729. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  730. return
  731. }
  732. commit, err := gitBaseRepo.GetCommit(pr.MergedCommitID)
  733. if err != nil {
  734. log.Error(4, "GetCommit: %v", err)
  735. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  736. return
  737. }
  738. isParent := false
  739. for i := 0; i < commit.ParentCount(); i++ {
  740. if parent, err := commit.Parent(i); err != nil {
  741. log.Error(4, "Parent: %v", err)
  742. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  743. return
  744. } else if parent.ID.String() == branchCommitID {
  745. isParent = true
  746. break
  747. }
  748. }
  749. if !isParent {
  750. ctx.Flash.Error(ctx.Tr("repo.branch.delete_branch_has_new_commits", fullBranchName))
  751. return
  752. }
  753. }
  754. if err := gitRepo.DeleteBranch(pr.HeadBranch, git.DeleteBranchOptions{
  755. Force: true,
  756. }); err != nil {
  757. log.Error(4, "DeleteBranch: %v", err)
  758. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  759. return
  760. }
  761. if err := models.AddDeletePRBranchComment(ctx.User, pr.BaseRepo, issue.ID, pr.HeadBranch); err != nil {
  762. // Do not fail here as branch has already been deleted
  763. log.Error(4, "DeleteBranch: %v", err)
  764. }
  765. ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", fullBranchName))
  766. }