pull.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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. 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. func ViewPullCommits(ctx *context.Context) {
  211. ctx.Data["PageIsPullCommits"] = true
  212. pull := checkPullInfo(ctx)
  213. if ctx.Written() {
  214. return
  215. }
  216. ctx.Data["Username"] = pull.HeadUserName
  217. ctx.Data["Reponame"] = pull.HeadRepo.Name
  218. var commits *list.List
  219. if pull.HasMerged {
  220. PrepareMergedViewPullInfo(ctx, pull)
  221. if ctx.Written() {
  222. return
  223. }
  224. startCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergeBase)
  225. if err != nil {
  226. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  227. return
  228. }
  229. endCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergedCommitID)
  230. if err != nil {
  231. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  232. return
  233. }
  234. commits, err = ctx.Repo.GitRepo.CommitsBetween(endCommit, startCommit)
  235. if err != nil {
  236. ctx.Handle(500, "Repo.GitRepo.CommitsBetween", err)
  237. return
  238. }
  239. } else {
  240. prInfo := PrepareViewPullInfo(ctx, pull)
  241. if ctx.Written() {
  242. return
  243. } else if prInfo == nil {
  244. ctx.Handle(404, "ViewPullCommits", nil)
  245. return
  246. }
  247. commits = prInfo.Commits
  248. }
  249. commits = models.ValidateCommitsWithEmails(commits)
  250. ctx.Data["Commits"] = commits
  251. ctx.Data["CommitCount"] = commits.Len()
  252. ctx.HTML(200, PULL_COMMITS)
  253. }
  254. func ViewPullFiles(ctx *context.Context) {
  255. ctx.Data["PageIsPullFiles"] = true
  256. pull := checkPullInfo(ctx)
  257. if ctx.Written() {
  258. return
  259. }
  260. var (
  261. diffRepoPath string
  262. startCommitID string
  263. endCommitID string
  264. gitRepo *git.Repository
  265. )
  266. if pull.HasMerged {
  267. PrepareMergedViewPullInfo(ctx, pull)
  268. if ctx.Written() {
  269. return
  270. }
  271. diffRepoPath = ctx.Repo.GitRepo.Path
  272. startCommitID = pull.MergeBase
  273. endCommitID = pull.MergedCommitID
  274. gitRepo = ctx.Repo.GitRepo
  275. } else {
  276. prInfo := PrepareViewPullInfo(ctx, pull)
  277. if ctx.Written() {
  278. return
  279. } else if prInfo == nil {
  280. ctx.Handle(404, "ViewPullFiles", nil)
  281. return
  282. }
  283. headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
  284. headGitRepo, err := git.OpenRepository(headRepoPath)
  285. if err != nil {
  286. ctx.Handle(500, "OpenRepository", err)
  287. return
  288. }
  289. headCommitID, err := headGitRepo.GetBranchCommitID(pull.HeadBranch)
  290. if err != nil {
  291. ctx.Handle(500, "GetBranchCommitID", err)
  292. return
  293. }
  294. diffRepoPath = headRepoPath
  295. startCommitID = prInfo.MergeBase
  296. endCommitID = headCommitID
  297. gitRepo = headGitRepo
  298. }
  299. diff, err := models.GetDiffRange(diffRepoPath,
  300. startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
  301. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  302. if err != nil {
  303. ctx.Handle(500, "GetDiffRange", err)
  304. return
  305. }
  306. ctx.Data["Diff"] = diff
  307. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  308. commit, err := gitRepo.GetCommit(endCommitID)
  309. if err != nil {
  310. ctx.Handle(500, "GetCommit", err)
  311. return
  312. }
  313. headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
  314. ctx.Data["Username"] = pull.HeadUserName
  315. ctx.Data["Reponame"] = pull.HeadRepo.Name
  316. ctx.Data["IsImageFile"] = commit.IsImageFile
  317. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", endCommitID)
  318. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", startCommitID)
  319. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", endCommitID)
  320. ctx.Data["RequireHighlightJS"] = true
  321. ctx.HTML(200, PULL_FILES)
  322. }
  323. func MergePullRequest(ctx *context.Context) {
  324. issue := checkPullInfo(ctx)
  325. if ctx.Written() {
  326. return
  327. }
  328. if issue.IsClosed {
  329. ctx.Handle(404, "MergePullRequest", nil)
  330. return
  331. }
  332. pr, err := models.GetPullRequestByIssueID(issue.ID)
  333. if err != nil {
  334. if models.IsErrPullRequestNotExist(err) {
  335. ctx.Handle(404, "GetPullRequestByIssueID", nil)
  336. } else {
  337. ctx.Handle(500, "GetPullRequestByIssueID", err)
  338. }
  339. return
  340. }
  341. if !pr.CanAutoMerge() || pr.HasMerged {
  342. ctx.Handle(404, "MergePullRequest", nil)
  343. return
  344. }
  345. pr.Issue = issue
  346. pr.Issue.Repo = ctx.Repo.Repository
  347. if err = pr.Merge(ctx.User, ctx.Repo.GitRepo); err != nil {
  348. ctx.Handle(500, "Merge", err)
  349. return
  350. }
  351. log.Trace("Pull request merged: %d", pr.ID)
  352. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  353. }
  354. func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
  355. baseRepo := ctx.Repo.Repository
  356. // Get compared branches information
  357. // format: <base branch>...[<head repo>:]<head branch>
  358. // base<-head: master...head:feature
  359. // same repo: master...feature
  360. infos := strings.Split(ctx.Params("*"), "...")
  361. if len(infos) != 2 {
  362. log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)
  363. ctx.Handle(404, "CompareAndPullRequest", nil)
  364. return nil, nil, nil, nil, "", ""
  365. }
  366. baseBranch := infos[0]
  367. ctx.Data["BaseBranch"] = baseBranch
  368. var (
  369. headUser *models.User
  370. headBranch string
  371. isSameRepo bool
  372. err error
  373. )
  374. // If there is no head repository, it means pull request between same repository.
  375. headInfos := strings.Split(infos[1], ":")
  376. if len(headInfos) == 1 {
  377. isSameRepo = true
  378. headUser = ctx.Repo.Owner
  379. headBranch = headInfos[0]
  380. } else if len(headInfos) == 2 {
  381. headUser, err = models.GetUserByName(headInfos[0])
  382. if err != nil {
  383. if models.IsErrUserNotExist(err) {
  384. ctx.Handle(404, "GetUserByName", nil)
  385. } else {
  386. ctx.Handle(500, "GetUserByName", err)
  387. }
  388. return nil, nil, nil, nil, "", ""
  389. }
  390. headBranch = headInfos[1]
  391. } else {
  392. ctx.Handle(404, "CompareAndPullRequest", nil)
  393. return nil, nil, nil, nil, "", ""
  394. }
  395. ctx.Data["HeadUser"] = headUser
  396. ctx.Data["HeadBranch"] = headBranch
  397. ctx.Repo.PullRequest.SameRepo = isSameRepo
  398. // Check if base branch is valid.
  399. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  400. ctx.Handle(404, "IsBranchExist", nil)
  401. return nil, nil, nil, nil, "", ""
  402. }
  403. // Check if current user has fork of repository or in the same repository.
  404. headRepo, has := models.HasForkedRepo(headUser.Id, baseRepo.ID)
  405. if !has && !isSameRepo {
  406. log.Trace("ParseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
  407. ctx.Handle(404, "ParseCompareInfo", nil)
  408. return nil, nil, nil, nil, "", ""
  409. }
  410. var headGitRepo *git.Repository
  411. if isSameRepo {
  412. headRepo = ctx.Repo.Repository
  413. headGitRepo = ctx.Repo.GitRepo
  414. } else {
  415. headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  416. if err != nil {
  417. ctx.Handle(500, "OpenRepository", err)
  418. return nil, nil, nil, nil, "", ""
  419. }
  420. }
  421. if !ctx.User.IsWriterOfRepo(headRepo) && !ctx.User.IsAdmin {
  422. log.Trace("ParseCompareInfo[%d]: does not have write access or site admin", baseRepo.ID)
  423. ctx.Handle(404, "ParseCompareInfo", nil)
  424. return nil, nil, nil, nil, "", ""
  425. }
  426. // Check if head branch is valid.
  427. if !headGitRepo.IsBranchExist(headBranch) {
  428. ctx.Handle(404, "IsBranchExist", nil)
  429. return nil, nil, nil, nil, "", ""
  430. }
  431. headBranches, err := headGitRepo.GetBranches()
  432. if err != nil {
  433. ctx.Handle(500, "GetBranches", err)
  434. return nil, nil, nil, nil, "", ""
  435. }
  436. ctx.Data["HeadBranches"] = headBranches
  437. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
  438. if err != nil {
  439. ctx.Handle(500, "GetPullRequestInfo", err)
  440. return nil, nil, nil, nil, "", ""
  441. }
  442. ctx.Data["BeforeCommitID"] = prInfo.MergeBase
  443. return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
  444. }
  445. func PrepareCompareDiff(
  446. ctx *context.Context,
  447. headUser *models.User,
  448. headRepo *models.Repository,
  449. headGitRepo *git.Repository,
  450. prInfo *git.PullRequestInfo,
  451. baseBranch, headBranch string) bool {
  452. var (
  453. repo = ctx.Repo.Repository
  454. err error
  455. )
  456. // Get diff information.
  457. ctx.Data["CommitRepoLink"] = headRepo.Link()
  458. headCommitID, err := headGitRepo.GetBranchCommitID(headBranch)
  459. if err != nil {
  460. ctx.Handle(500, "GetBranchCommitID", err)
  461. return false
  462. }
  463. ctx.Data["AfterCommitID"] = headCommitID
  464. if headCommitID == prInfo.MergeBase {
  465. ctx.Data["IsNothingToCompare"] = true
  466. return true
  467. }
  468. diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
  469. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
  470. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  471. if err != nil {
  472. ctx.Handle(500, "GetDiffRange", err)
  473. return false
  474. }
  475. ctx.Data["Diff"] = diff
  476. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  477. headCommit, err := headGitRepo.GetCommit(headCommitID)
  478. if err != nil {
  479. ctx.Handle(500, "GetCommit", err)
  480. return false
  481. }
  482. prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
  483. ctx.Data["Commits"] = prInfo.Commits
  484. ctx.Data["CommitCount"] = prInfo.Commits.Len()
  485. ctx.Data["Username"] = headUser.Name
  486. ctx.Data["Reponame"] = headRepo.Name
  487. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  488. headTarget := path.Join(headUser.Name, repo.Name)
  489. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", headCommitID)
  490. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", prInfo.MergeBase)
  491. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", headCommitID)
  492. return false
  493. }
  494. func CompareAndPullRequest(ctx *context.Context) {
  495. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  496. ctx.Data["PageIsComparePull"] = true
  497. ctx.Data["IsDiffCompare"] = true
  498. ctx.Data["RequireHighlightJS"] = true
  499. setTemplateIfExists(ctx, PULL_REQUEST_TEMPLATE_KEY, PullRequestTemplateCandidates)
  500. renderAttachmentSettings(ctx)
  501. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  502. if ctx.Written() {
  503. return
  504. }
  505. pr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
  506. if err != nil {
  507. if !models.IsErrPullRequestNotExist(err) {
  508. ctx.Handle(500, "GetUnmergedPullRequest", err)
  509. return
  510. }
  511. } else {
  512. ctx.Data["HasPullRequest"] = true
  513. ctx.Data["PullRequest"] = pr
  514. ctx.HTML(200, COMPARE_PULL)
  515. return
  516. }
  517. nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  518. if ctx.Written() {
  519. return
  520. }
  521. if !nothingToCompare {
  522. // Setup information for new form.
  523. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  524. if ctx.Written() {
  525. return
  526. }
  527. }
  528. ctx.HTML(200, COMPARE_PULL)
  529. }
  530. func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm) {
  531. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  532. ctx.Data["PageIsComparePull"] = true
  533. ctx.Data["IsDiffCompare"] = true
  534. renderAttachmentSettings(ctx)
  535. var (
  536. repo = ctx.Repo.Repository
  537. attachments []string
  538. )
  539. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  540. if ctx.Written() {
  541. return
  542. }
  543. patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
  544. if err != nil {
  545. ctx.Handle(500, "GetPatch", err)
  546. return
  547. }
  548. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, form)
  549. if ctx.Written() {
  550. return
  551. }
  552. if setting.AttachmentEnabled {
  553. attachments = form.Attachments
  554. }
  555. if ctx.HasError() {
  556. ctx.HTML(200, COMPARE_PULL)
  557. return
  558. }
  559. pullIssue := &models.Issue{
  560. RepoID: repo.ID,
  561. Index: repo.NextIssueIndex(),
  562. Name: form.Title,
  563. PosterID: ctx.User.Id,
  564. Poster: ctx.User,
  565. MilestoneID: milestoneID,
  566. AssigneeID: assigneeID,
  567. IsPull: true,
  568. Content: form.Content,
  569. }
  570. pullRequest := &models.PullRequest{
  571. HeadRepoID: headRepo.ID,
  572. BaseRepoID: repo.ID,
  573. HeadUserName: headUser.Name,
  574. HeadBranch: headBranch,
  575. BaseBranch: baseBranch,
  576. HeadRepo: headRepo,
  577. BaseRepo: repo,
  578. MergeBase: prInfo.MergeBase,
  579. Type: models.PULL_REQUEST_GOGS,
  580. }
  581. if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch); err != nil {
  582. ctx.Handle(500, "NewPullRequest", err)
  583. return
  584. } else if err := pullRequest.PushToBaseRepo(); err != nil {
  585. ctx.Handle(500, "PushToBaseRepo", err)
  586. return
  587. }
  588. log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
  589. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
  590. }
  591. func TriggerTask(ctx *context.Context) {
  592. branch := ctx.Query("branch")
  593. secret := ctx.Query("secret")
  594. if len(branch) == 0 || len(secret) == 0 {
  595. ctx.Error(404)
  596. log.Trace("TriggerTask: branch or secret is empty")
  597. return
  598. }
  599. owner, repo := parseOwnerAndRepo(ctx)
  600. if ctx.Written() {
  601. return
  602. }
  603. if secret != base.EncodeMD5(owner.Salt) {
  604. ctx.Error(404)
  605. log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
  606. return
  607. }
  608. log.Trace("TriggerTask [%d].(new request): %s", repo.ID, branch)
  609. go models.HookQueue.Add(repo.ID)
  610. go models.AddTestPullRequestTask(repo.ID, branch)
  611. ctx.Status(202)
  612. }