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