pull.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. "path"
  8. "strings"
  9. "github.com/Unknwon/com"
  10. "github.com/gogits/git-module"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/auth"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/context"
  15. "github.com/gogits/gogs/modules/log"
  16. "github.com/gogits/gogs/modules/setting"
  17. )
  18. const (
  19. FORK base.TplName = "repo/pulls/fork"
  20. COMPARE_PULL base.TplName = "repo/pulls/compare"
  21. PULL_COMMITS base.TplName = "repo/pulls/commits"
  22. PULL_FILES base.TplName = "repo/pulls/files"
  23. PULL_REQUEST_TEMPLATE_KEY = "PullRequestTemplate"
  24. )
  25. var (
  26. PullRequestTemplateCandidates = []string{
  27. "PULL_REQUEST.md",
  28. ".gogs/PULL_REQUEST.md",
  29. ".github/PULL_REQUEST.md",
  30. }
  31. )
  32. func getForkRepository(ctx *context.Context) *models.Repository {
  33. forkRepo, err := models.GetRepositoryByID(ctx.ParamsInt64(":repoid"))
  34. if err != nil {
  35. if models.IsErrRepoNotExist(err) {
  36. ctx.Handle(404, "GetRepositoryByID", nil)
  37. } else {
  38. ctx.Handle(500, "GetRepositoryByID", err)
  39. }
  40. return nil
  41. }
  42. if !forkRepo.CanBeForked() {
  43. ctx.Handle(404, "getForkRepository", nil)
  44. return nil
  45. }
  46. ctx.Data["repo_name"] = forkRepo.Name
  47. ctx.Data["description"] = forkRepo.Description
  48. ctx.Data["IsPrivate"] = forkRepo.IsPrivate
  49. if err = forkRepo.GetOwner(); err != nil {
  50. ctx.Handle(500, "GetOwner", err)
  51. return nil
  52. }
  53. ctx.Data["ForkFrom"] = forkRepo.Owner.Name + "/" + forkRepo.Name
  54. if err := ctx.User.GetOrganizations(true); err != nil {
  55. ctx.Handle(500, "GetOrganizations", err)
  56. return nil
  57. }
  58. ctx.Data["Orgs"] = ctx.User.Orgs
  59. return forkRepo
  60. }
  61. func Fork(ctx *context.Context) {
  62. ctx.Data["Title"] = ctx.Tr("new_fork")
  63. getForkRepository(ctx)
  64. if ctx.Written() {
  65. return
  66. }
  67. ctx.Data["ContextUser"] = ctx.User
  68. ctx.HTML(200, FORK)
  69. }
  70. func ForkPost(ctx *context.Context, form auth.CreateRepoForm) {
  71. ctx.Data["Title"] = ctx.Tr("new_fork")
  72. forkRepo := getForkRepository(ctx)
  73. if ctx.Written() {
  74. return
  75. }
  76. ctxUser := checkContextUser(ctx, form.Uid)
  77. if ctx.Written() {
  78. return
  79. }
  80. ctx.Data["ContextUser"] = ctxUser
  81. if ctx.HasError() {
  82. ctx.HTML(200, FORK)
  83. return
  84. }
  85. repo, has := models.HasForkedRepo(ctxUser.Id, forkRepo.ID)
  86. if has {
  87. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  88. return
  89. }
  90. // Check ownership of organization.
  91. if ctxUser.IsOrganization() {
  92. if !ctxUser.IsOwnedBy(ctx.User.Id) {
  93. ctx.Error(403)
  94. return
  95. }
  96. }
  97. repo, err := models.ForkRepository(ctxUser, forkRepo, form.RepoName, form.Description)
  98. if err != nil {
  99. ctx.Data["Err_RepoName"] = true
  100. switch {
  101. case models.IsErrRepoAlreadyExist(err):
  102. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), FORK, &form)
  103. case models.IsErrNameReserved(err):
  104. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &form)
  105. case models.IsErrNamePatternNotAllowed(err):
  106. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), FORK, &form)
  107. default:
  108. ctx.Handle(500, "ForkPost", err)
  109. }
  110. return
  111. }
  112. log.Trace("Repository forked[%d]: %s/%s", forkRepo.ID, ctxUser.Name, repo.Name)
  113. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  114. }
  115. func checkPullInfo(ctx *context.Context) *models.Issue {
  116. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  117. if err != nil {
  118. if models.IsErrIssueNotExist(err) {
  119. ctx.Handle(404, "GetIssueByIndex", err)
  120. } else {
  121. ctx.Handle(500, "GetIssueByIndex", err)
  122. }
  123. return nil
  124. }
  125. ctx.Data["Title"] = issue.Name
  126. ctx.Data["Issue"] = issue
  127. if !issue.IsPull {
  128. ctx.Handle(404, "ViewPullCommits", nil)
  129. return nil
  130. }
  131. if err = issue.GetPullRequest(); err != nil {
  132. ctx.Handle(500, "GetPullRequest", err)
  133. return nil
  134. } else if err = issue.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. func PrepareMergedViewPullInfo(ctx *context.Context, pull *models.Issue) {
  148. ctx.Data["HasMerged"] = true
  149. var err error
  150. if err = pull.GetMerger(); err != nil {
  151. ctx.Handle(500, "GetMerger", err)
  152. return
  153. }
  154. ctx.Data["HeadTarget"] = pull.HeadUserName + "/" + pull.HeadBranch
  155. ctx.Data["BaseTarget"] = ctx.Repo.Owner.Name + "/" + pull.BaseBranch
  156. ctx.Data["NumCommits"], err = ctx.Repo.GitRepo.CommitsCountBetween(pull.MergeBase, pull.MergedCommitID)
  157. if err != nil {
  158. ctx.Handle(500, "Repo.GitRepo.CommitsCountBetween", err)
  159. return
  160. }
  161. ctx.Data["NumFiles"], err = ctx.Repo.GitRepo.FilesCountBetween(pull.MergeBase, pull.MergedCommitID)
  162. if err != nil {
  163. ctx.Handle(500, "Repo.GitRepo.FilesCountBetween", err)
  164. return
  165. }
  166. }
  167. func PrepareViewPullInfo(ctx *context.Context, pull *models.Issue) *git.PullRequestInfo {
  168. repo := ctx.Repo.Repository
  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. ctx.Handle(500, "GetPullRequestInfo", err)
  197. return nil
  198. }
  199. ctx.Data["NumCommits"] = prInfo.Commits.Len()
  200. ctx.Data["NumFiles"] = prInfo.NumFiles
  201. return prInfo
  202. }
  203. func ViewPullCommits(ctx *context.Context) {
  204. ctx.Data["PageIsPullCommits"] = true
  205. pull := checkPullInfo(ctx)
  206. if ctx.Written() {
  207. return
  208. }
  209. ctx.Data["Username"] = pull.HeadUserName
  210. ctx.Data["Reponame"] = pull.HeadRepo.Name
  211. var commits *list.List
  212. if pull.HasMerged {
  213. PrepareMergedViewPullInfo(ctx, pull)
  214. if ctx.Written() {
  215. return
  216. }
  217. startCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergeBase)
  218. if err != nil {
  219. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  220. return
  221. }
  222. endCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergedCommitID)
  223. if err != nil {
  224. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  225. return
  226. }
  227. commits, err = ctx.Repo.GitRepo.CommitsBetween(endCommit, startCommit)
  228. if err != nil {
  229. ctx.Handle(500, "Repo.GitRepo.CommitsBetween", err)
  230. return
  231. }
  232. } else {
  233. prInfo := PrepareViewPullInfo(ctx, pull)
  234. if ctx.Written() {
  235. return
  236. } else if prInfo == nil {
  237. ctx.Handle(404, "ViewPullCommits", nil)
  238. return
  239. }
  240. commits = prInfo.Commits
  241. }
  242. commits = models.ValidateCommitsWithEmails(commits)
  243. ctx.Data["Commits"] = commits
  244. ctx.Data["CommitCount"] = commits.Len()
  245. ctx.HTML(200, PULL_COMMITS)
  246. }
  247. func ViewPullFiles(ctx *context.Context) {
  248. ctx.Data["PageIsPullFiles"] = true
  249. pull := checkPullInfo(ctx)
  250. if ctx.Written() {
  251. return
  252. }
  253. var (
  254. diffRepoPath string
  255. startCommitID string
  256. endCommitID string
  257. gitRepo *git.Repository
  258. )
  259. if pull.HasMerged {
  260. PrepareMergedViewPullInfo(ctx, pull)
  261. if ctx.Written() {
  262. return
  263. }
  264. diffRepoPath = ctx.Repo.GitRepo.Path
  265. startCommitID = pull.MergeBase
  266. endCommitID = pull.MergedCommitID
  267. gitRepo = ctx.Repo.GitRepo
  268. } else {
  269. prInfo := PrepareViewPullInfo(ctx, pull)
  270. if ctx.Written() {
  271. return
  272. } else if prInfo == nil {
  273. ctx.Handle(404, "ViewPullFiles", nil)
  274. return
  275. }
  276. headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
  277. headGitRepo, err := git.OpenRepository(headRepoPath)
  278. if err != nil {
  279. ctx.Handle(500, "OpenRepository", err)
  280. return
  281. }
  282. headCommitID, err := headGitRepo.GetBranchCommitID(pull.HeadBranch)
  283. if err != nil {
  284. ctx.Handle(500, "GetBranchCommitID", err)
  285. return
  286. }
  287. diffRepoPath = headRepoPath
  288. startCommitID = prInfo.MergeBase
  289. endCommitID = headCommitID
  290. gitRepo = headGitRepo
  291. }
  292. diff, err := models.GetDiffRange(diffRepoPath,
  293. startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
  294. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  295. if err != nil {
  296. ctx.Handle(500, "GetDiffRange", err)
  297. return
  298. }
  299. ctx.Data["Diff"] = diff
  300. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  301. commit, err := gitRepo.GetCommit(endCommitID)
  302. if err != nil {
  303. ctx.Handle(500, "GetCommit", err)
  304. return
  305. }
  306. headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
  307. ctx.Data["Username"] = pull.HeadUserName
  308. ctx.Data["Reponame"] = pull.HeadRepo.Name
  309. ctx.Data["IsImageFile"] = commit.IsImageFile
  310. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", endCommitID)
  311. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", startCommitID)
  312. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", endCommitID)
  313. ctx.Data["RequireHighlightJS"] = true
  314. ctx.HTML(200, PULL_FILES)
  315. }
  316. func MergePullRequest(ctx *context.Context) {
  317. issue := checkPullInfo(ctx)
  318. if ctx.Written() {
  319. return
  320. }
  321. if issue.IsClosed {
  322. ctx.Handle(404, "MergePullRequest", nil)
  323. return
  324. }
  325. pr, err := models.GetPullRequestByIssueID(issue.ID)
  326. if err != nil {
  327. if models.IsErrPullRequestNotExist(err) {
  328. ctx.Handle(404, "GetPullRequestByIssueID", nil)
  329. } else {
  330. ctx.Handle(500, "GetPullRequestByIssueID", err)
  331. }
  332. return
  333. }
  334. if !pr.CanAutoMerge() || pr.HasMerged {
  335. ctx.Handle(404, "MergePullRequest", nil)
  336. return
  337. }
  338. pr.Issue = issue
  339. pr.Issue.Repo = ctx.Repo.Repository
  340. if err = pr.Merge(ctx.User, ctx.Repo.GitRepo); err != nil {
  341. ctx.Handle(500, "Merge", err)
  342. return
  343. }
  344. log.Trace("Pull request merged: %d", pr.ID)
  345. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  346. }
  347. func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
  348. baseRepo := ctx.Repo.Repository
  349. // Get compared branches information
  350. // format: <base branch>...[<head repo>:]<head branch>
  351. // base<-head: master...head:feature
  352. // same repo: master...feature
  353. infos := strings.Split(ctx.Params("*"), "...")
  354. if len(infos) != 2 {
  355. log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)
  356. ctx.Handle(404, "CompareAndPullRequest", nil)
  357. return nil, nil, nil, nil, "", ""
  358. }
  359. baseBranch := infos[0]
  360. ctx.Data["BaseBranch"] = baseBranch
  361. var (
  362. headUser *models.User
  363. headBranch string
  364. isSameRepo bool
  365. err error
  366. )
  367. // If there is no head repository, it means pull request between same repository.
  368. headInfos := strings.Split(infos[1], ":")
  369. if len(headInfos) == 1 {
  370. isSameRepo = true
  371. headUser = ctx.Repo.Owner
  372. headBranch = headInfos[0]
  373. } else if len(headInfos) == 2 {
  374. headUser, err = models.GetUserByName(headInfos[0])
  375. if err != nil {
  376. if models.IsErrUserNotExist(err) {
  377. ctx.Handle(404, "GetUserByName", nil)
  378. } else {
  379. ctx.Handle(500, "GetUserByName", err)
  380. }
  381. return nil, nil, nil, nil, "", ""
  382. }
  383. headBranch = headInfos[1]
  384. } else {
  385. ctx.Handle(404, "CompareAndPullRequest", nil)
  386. return nil, nil, nil, nil, "", ""
  387. }
  388. ctx.Data["HeadUser"] = headUser
  389. ctx.Data["HeadBranch"] = headBranch
  390. ctx.Repo.PullRequest.SameRepo = isSameRepo
  391. // Check if base branch is valid.
  392. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  393. ctx.Handle(404, "IsBranchExist", nil)
  394. return nil, nil, nil, nil, "", ""
  395. }
  396. // Check if current user has fork of repository or in the same repository.
  397. headRepo, has := models.HasForkedRepo(headUser.Id, baseRepo.ID)
  398. if !has && !isSameRepo {
  399. log.Trace("ParseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
  400. ctx.Handle(404, "ParseCompareInfo", nil)
  401. return nil, nil, nil, nil, "", ""
  402. }
  403. var headGitRepo *git.Repository
  404. if isSameRepo {
  405. headRepo = ctx.Repo.Repository
  406. headGitRepo = ctx.Repo.GitRepo
  407. } else {
  408. headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  409. if err != nil {
  410. ctx.Handle(500, "OpenRepository", err)
  411. return nil, nil, nil, nil, "", ""
  412. }
  413. }
  414. if !ctx.User.IsWriterOfRepo(headRepo) && !ctx.User.IsAdmin {
  415. log.Trace("ParseCompareInfo[%d]: does not have write access or site admin", baseRepo.ID)
  416. ctx.Handle(404, "ParseCompareInfo", nil)
  417. return nil, nil, nil, nil, "", ""
  418. }
  419. // Check if head branch is valid.
  420. if !headGitRepo.IsBranchExist(headBranch) {
  421. ctx.Handle(404, "IsBranchExist", nil)
  422. return nil, nil, nil, nil, "", ""
  423. }
  424. headBranches, err := headGitRepo.GetBranches()
  425. if err != nil {
  426. ctx.Handle(500, "GetBranches", err)
  427. return nil, nil, nil, nil, "", ""
  428. }
  429. ctx.Data["HeadBranches"] = headBranches
  430. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
  431. if err != nil {
  432. ctx.Handle(500, "GetPullRequestInfo", err)
  433. return nil, nil, nil, nil, "", ""
  434. }
  435. ctx.Data["BeforeCommitID"] = prInfo.MergeBase
  436. return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
  437. }
  438. func PrepareCompareDiff(
  439. ctx *context.Context,
  440. headUser *models.User,
  441. headRepo *models.Repository,
  442. headGitRepo *git.Repository,
  443. prInfo *git.PullRequestInfo,
  444. baseBranch, headBranch string) bool {
  445. var (
  446. repo = ctx.Repo.Repository
  447. err error
  448. )
  449. // Get diff information.
  450. ctx.Data["CommitRepoLink"] = headRepo.Link()
  451. headCommitID, err := headGitRepo.GetBranchCommitID(headBranch)
  452. if err != nil {
  453. ctx.Handle(500, "GetBranchCommitID", err)
  454. return false
  455. }
  456. ctx.Data["AfterCommitID"] = headCommitID
  457. if headCommitID == prInfo.MergeBase {
  458. ctx.Data["IsNothingToCompare"] = true
  459. return true
  460. }
  461. diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
  462. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
  463. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  464. if err != nil {
  465. ctx.Handle(500, "GetDiffRange", err)
  466. return false
  467. }
  468. ctx.Data["Diff"] = diff
  469. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  470. headCommit, err := headGitRepo.GetCommit(headCommitID)
  471. if err != nil {
  472. ctx.Handle(500, "GetCommit", err)
  473. return false
  474. }
  475. prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
  476. ctx.Data["Commits"] = prInfo.Commits
  477. ctx.Data["CommitCount"] = prInfo.Commits.Len()
  478. ctx.Data["Username"] = headUser.Name
  479. ctx.Data["Reponame"] = headRepo.Name
  480. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  481. headTarget := path.Join(headUser.Name, repo.Name)
  482. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", headCommitID)
  483. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", prInfo.MergeBase)
  484. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", headCommitID)
  485. return false
  486. }
  487. func CompareAndPullRequest(ctx *context.Context) {
  488. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  489. ctx.Data["PageIsComparePull"] = true
  490. ctx.Data["IsDiffCompare"] = true
  491. ctx.Data["RequireHighlightJS"] = true
  492. setTemplateIfExists(ctx, PULL_REQUEST_TEMPLATE_KEY, PullRequestTemplateCandidates)
  493. renderAttachmentSettings(ctx)
  494. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  495. if ctx.Written() {
  496. return
  497. }
  498. pr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
  499. if err != nil {
  500. if !models.IsErrPullRequestNotExist(err) {
  501. ctx.Handle(500, "GetUnmergedPullRequest", err)
  502. return
  503. }
  504. } else {
  505. ctx.Data["HasPullRequest"] = true
  506. ctx.Data["PullRequest"] = pr
  507. ctx.HTML(200, COMPARE_PULL)
  508. return
  509. }
  510. nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  511. if ctx.Written() {
  512. return
  513. }
  514. if !nothingToCompare {
  515. // Setup information for new form.
  516. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  517. if ctx.Written() {
  518. return
  519. }
  520. }
  521. ctx.HTML(200, COMPARE_PULL)
  522. }
  523. func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm) {
  524. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  525. ctx.Data["PageIsComparePull"] = true
  526. ctx.Data["IsDiffCompare"] = true
  527. renderAttachmentSettings(ctx)
  528. var (
  529. repo = ctx.Repo.Repository
  530. attachments []string
  531. )
  532. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  533. if ctx.Written() {
  534. return
  535. }
  536. patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
  537. if err != nil {
  538. ctx.Handle(500, "GetPatch", err)
  539. return
  540. }
  541. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, form)
  542. if ctx.Written() {
  543. return
  544. }
  545. if setting.AttachmentEnabled {
  546. attachments = form.Attachments
  547. }
  548. if ctx.HasError() {
  549. ctx.HTML(200, COMPARE_PULL)
  550. return
  551. }
  552. pullIssue := &models.Issue{
  553. RepoID: repo.ID,
  554. Index: repo.NextIssueIndex(),
  555. Name: form.Title,
  556. PosterID: ctx.User.Id,
  557. Poster: ctx.User,
  558. MilestoneID: milestoneID,
  559. AssigneeID: assigneeID,
  560. IsPull: true,
  561. Content: form.Content,
  562. }
  563. pullRequest := &models.PullRequest{
  564. HeadRepoID: headRepo.ID,
  565. BaseRepoID: repo.ID,
  566. HeadUserName: headUser.Name,
  567. HeadBranch: headBranch,
  568. BaseBranch: baseBranch,
  569. HeadRepo: headRepo,
  570. BaseRepo: repo,
  571. MergeBase: prInfo.MergeBase,
  572. Type: models.PULL_REQUEST_GOGS,
  573. }
  574. if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch); err != nil {
  575. ctx.Handle(500, "NewPullRequest", err)
  576. return
  577. } else if err := pullRequest.PushToBaseRepo(); err != nil {
  578. ctx.Handle(500, "PushToBaseRepo", err)
  579. return
  580. } else if err := MailWatchersAndMentions(ctx, pullIssue); err != nil {
  581. ctx.Handle(500, "MailWatchersAndMentions", err)
  582. return
  583. }
  584. log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
  585. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
  586. }
  587. func TriggerTask(ctx *context.Context) {
  588. branch := ctx.Query("branch")
  589. secret := ctx.Query("secret")
  590. if len(branch) == 0 || len(secret) == 0 {
  591. ctx.Error(404)
  592. log.Trace("TriggerTask: branch or secret is empty")
  593. return
  594. }
  595. owner, repo := parseOwnerAndRepo(ctx)
  596. if ctx.Written() {
  597. return
  598. }
  599. if secret != base.EncodeMD5(owner.Salt) {
  600. ctx.Error(404)
  601. log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
  602. return
  603. }
  604. log.Trace("TriggerTask [%d].(new request): %s", repo.ID, branch)
  605. go models.HookQueue.Add(repo.ID)
  606. go models.AddTestPullRequestTask(repo.ID, branch)
  607. ctx.Status(202)
  608. }