pull.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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.Title
  126. ctx.Data["Issue"] = issue
  127. if !issue.IsPull {
  128. ctx.Handle(404, "ViewPullCommits", nil)
  129. return nil
  130. }
  131. if err = issue.PullRequest.GetHeadRepo(); err != nil {
  132. ctx.Handle(500, "GetHeadRepo", err)
  133. return nil
  134. }
  135. if ctx.IsSigned {
  136. // Update issue-user.
  137. if err = issue.ReadBy(ctx.User.ID); err != nil {
  138. ctx.Handle(500, "ReadBy", err)
  139. return nil
  140. }
  141. }
  142. return issue
  143. }
  144. func PrepareMergedViewPullInfo(ctx *context.Context, issue *models.Issue) {
  145. pull := issue.PullRequest
  146. ctx.Data["HasMerged"] = true
  147. ctx.Data["HeadTarget"] = issue.PullRequest.HeadUserName + "/" + pull.HeadBranch
  148. ctx.Data["BaseTarget"] = ctx.Repo.Owner.Name + "/" + pull.BaseBranch
  149. var err error
  150. ctx.Data["NumCommits"], err = ctx.Repo.GitRepo.CommitsCountBetween(pull.MergeBase, pull.MergedCommitID)
  151. if err != nil {
  152. ctx.Handle(500, "Repo.GitRepo.CommitsCountBetween", err)
  153. return
  154. }
  155. ctx.Data["NumFiles"], err = ctx.Repo.GitRepo.FilesCountBetween(pull.MergeBase, pull.MergedCommitID)
  156. if err != nil {
  157. ctx.Handle(500, "Repo.GitRepo.FilesCountBetween", err)
  158. return
  159. }
  160. }
  161. func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.PullRequestInfo {
  162. repo := ctx.Repo.Repository
  163. pull := issue.PullRequest
  164. ctx.Data["HeadTarget"] = pull.HeadUserName + "/" + pull.HeadBranch
  165. ctx.Data["BaseTarget"] = ctx.Repo.Owner.Name + "/" + pull.BaseBranch
  166. var (
  167. headGitRepo *git.Repository
  168. err error
  169. )
  170. if err = pull.GetHeadRepo(); err != nil {
  171. ctx.Handle(500, "GetHeadRepo", err)
  172. return nil
  173. }
  174. if pull.HeadRepo != nil {
  175. headGitRepo, err = git.OpenRepository(pull.HeadRepo.RepoPath())
  176. if err != nil {
  177. ctx.Handle(500, "OpenRepository", err)
  178. return nil
  179. }
  180. }
  181. if pull.HeadRepo == nil || !headGitRepo.IsBranchExist(pull.HeadBranch) {
  182. ctx.Data["IsPullReuqestBroken"] = true
  183. ctx.Data["HeadTarget"] = "deleted"
  184. ctx.Data["NumCommits"] = 0
  185. ctx.Data["NumFiles"] = 0
  186. return nil
  187. }
  188. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name),
  189. pull.BaseBranch, pull.HeadBranch)
  190. if err != nil {
  191. if strings.Contains(err.Error(), "fatal: Not a valid object name") {
  192. ctx.Data["IsPullReuqestBroken"] = true
  193. ctx.Data["BaseTarget"] = "deleted"
  194. ctx.Data["NumCommits"] = 0
  195. ctx.Data["NumFiles"] = 0
  196. return nil
  197. }
  198. ctx.Handle(500, "GetPullRequestInfo", err)
  199. return nil
  200. }
  201. ctx.Data["NumCommits"] = prInfo.Commits.Len()
  202. ctx.Data["NumFiles"] = prInfo.NumFiles
  203. return prInfo
  204. }
  205. func ViewPullCommits(ctx *context.Context) {
  206. ctx.Data["PageIsPullList"] = true
  207. ctx.Data["PageIsPullCommits"] = true
  208. issue := checkPullInfo(ctx)
  209. if ctx.Written() {
  210. return
  211. }
  212. pull := issue.PullRequest
  213. ctx.Data["Username"] = pull.HeadUserName
  214. ctx.Data["Reponame"] = pull.HeadRepo.Name
  215. var commits *list.List
  216. if pull.HasMerged {
  217. PrepareMergedViewPullInfo(ctx, issue)
  218. if ctx.Written() {
  219. return
  220. }
  221. startCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergeBase)
  222. if err != nil {
  223. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  224. return
  225. }
  226. endCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergedCommitID)
  227. if err != nil {
  228. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  229. return
  230. }
  231. commits, err = ctx.Repo.GitRepo.CommitsBetween(endCommit, startCommit)
  232. if err != nil {
  233. ctx.Handle(500, "Repo.GitRepo.CommitsBetween", err)
  234. return
  235. }
  236. } else {
  237. prInfo := PrepareViewPullInfo(ctx, issue)
  238. if ctx.Written() {
  239. return
  240. } else if prInfo == nil {
  241. ctx.Handle(404, "ViewPullCommits", nil)
  242. return
  243. }
  244. commits = prInfo.Commits
  245. }
  246. commits = models.ValidateCommitsWithEmails(commits)
  247. ctx.Data["Commits"] = commits
  248. ctx.Data["CommitCount"] = commits.Len()
  249. ctx.HTML(200, PULL_COMMITS)
  250. }
  251. func ViewPullFiles(ctx *context.Context) {
  252. ctx.Data["PageIsPullList"] = true
  253. ctx.Data["PageIsPullFiles"] = true
  254. issue := checkPullInfo(ctx)
  255. if ctx.Written() {
  256. return
  257. }
  258. pull := issue.PullRequest
  259. var (
  260. diffRepoPath string
  261. startCommitID string
  262. endCommitID string
  263. gitRepo *git.Repository
  264. )
  265. if pull.HasMerged {
  266. PrepareMergedViewPullInfo(ctx, issue)
  267. if ctx.Written() {
  268. return
  269. }
  270. diffRepoPath = ctx.Repo.GitRepo.Path
  271. startCommitID = pull.MergeBase
  272. endCommitID = pull.MergedCommitID
  273. gitRepo = ctx.Repo.GitRepo
  274. } else {
  275. prInfo := PrepareViewPullInfo(ctx, issue)
  276. if ctx.Written() {
  277. return
  278. } else if prInfo == nil {
  279. ctx.Handle(404, "ViewPullFiles", nil)
  280. return
  281. }
  282. headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
  283. headGitRepo, err := git.OpenRepository(headRepoPath)
  284. if err != nil {
  285. ctx.Handle(500, "OpenRepository", err)
  286. return
  287. }
  288. headCommitID, err := headGitRepo.GetBranchCommitID(pull.HeadBranch)
  289. if err != nil {
  290. ctx.Handle(500, "GetBranchCommitID", err)
  291. return
  292. }
  293. diffRepoPath = headRepoPath
  294. startCommitID = prInfo.MergeBase
  295. endCommitID = headCommitID
  296. gitRepo = headGitRepo
  297. }
  298. diff, err := models.GetDiffRange(diffRepoPath,
  299. startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
  300. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  301. if err != nil {
  302. ctx.Handle(500, "GetDiffRange", err)
  303. return
  304. }
  305. ctx.Data["Diff"] = diff
  306. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  307. commit, err := gitRepo.GetCommit(endCommitID)
  308. if err != nil {
  309. ctx.Handle(500, "GetCommit", err)
  310. return
  311. }
  312. ec, err := ctx.Repo.GetEditorconfig()
  313. if err != nil && !git.IsErrNotExist(err) {
  314. ctx.Handle(500, "ErrGettingEditorconfig", err)
  315. return
  316. }
  317. ctx.Data["Editorconfig"] = ec
  318. headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
  319. ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"
  320. ctx.Data["Username"] = pull.HeadUserName
  321. ctx.Data["Reponame"] = pull.HeadRepo.Name
  322. ctx.Data["IsImageFile"] = commit.IsImageFile
  323. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", endCommitID)
  324. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", startCommitID)
  325. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", endCommitID)
  326. ctx.Data["RequireHighlightJS"] = true
  327. ctx.HTML(200, PULL_FILES)
  328. }
  329. func MergePullRequest(ctx *context.Context) {
  330. issue := checkPullInfo(ctx)
  331. if ctx.Written() {
  332. return
  333. }
  334. if issue.IsClosed {
  335. ctx.Handle(404, "MergePullRequest", nil)
  336. return
  337. }
  338. pr, err := models.GetPullRequestByIssueID(issue.ID)
  339. if err != nil {
  340. if models.IsErrPullRequestNotExist(err) {
  341. ctx.Handle(404, "GetPullRequestByIssueID", nil)
  342. } else {
  343. ctx.Handle(500, "GetPullRequestByIssueID", err)
  344. }
  345. return
  346. }
  347. if !pr.CanAutoMerge() || pr.HasMerged {
  348. ctx.Handle(404, "MergePullRequest", nil)
  349. return
  350. }
  351. pr.Issue = issue
  352. pr.Issue.Repo = ctx.Repo.Repository
  353. if err = pr.Merge(ctx.User, ctx.Repo.GitRepo); err != nil {
  354. ctx.Handle(500, "Merge", err)
  355. return
  356. }
  357. log.Trace("Pull request merged: %d", pr.ID)
  358. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  359. }
  360. func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
  361. baseRepo := ctx.Repo.Repository
  362. // Get compared branches information
  363. // format: <base branch>...[<head repo>:]<head branch>
  364. // base<-head: master...head:feature
  365. // same repo: master...feature
  366. infos := strings.Split(ctx.Params("*"), "...")
  367. if len(infos) != 2 {
  368. log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)
  369. ctx.Handle(404, "CompareAndPullRequest", nil)
  370. return nil, nil, nil, nil, "", ""
  371. }
  372. baseBranch := infos[0]
  373. ctx.Data["BaseBranch"] = baseBranch
  374. var (
  375. headUser *models.User
  376. headBranch string
  377. isSameRepo bool
  378. err error
  379. )
  380. // If there is no head repository, it means pull request between same repository.
  381. headInfos := strings.Split(infos[1], ":")
  382. if len(headInfos) == 1 {
  383. isSameRepo = true
  384. headUser = ctx.Repo.Owner
  385. headBranch = headInfos[0]
  386. } else if len(headInfos) == 2 {
  387. headUser, err = models.GetUserByName(headInfos[0])
  388. if err != nil {
  389. if models.IsErrUserNotExist(err) {
  390. ctx.Handle(404, "GetUserByName", nil)
  391. } else {
  392. ctx.Handle(500, "GetUserByName", err)
  393. }
  394. return nil, nil, nil, nil, "", ""
  395. }
  396. headBranch = headInfos[1]
  397. } else {
  398. ctx.Handle(404, "CompareAndPullRequest", nil)
  399. return nil, nil, nil, nil, "", ""
  400. }
  401. ctx.Data["HeadUser"] = headUser
  402. ctx.Data["HeadBranch"] = headBranch
  403. ctx.Repo.PullRequest.SameRepo = isSameRepo
  404. // Check if base branch is valid.
  405. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  406. ctx.Handle(404, "IsBranchExist", nil)
  407. return nil, nil, nil, nil, "", ""
  408. }
  409. // Check if current user has fork of repository or in the same repository.
  410. headRepo, has := models.HasForkedRepo(headUser.ID, baseRepo.ID)
  411. if !has && !isSameRepo {
  412. log.Trace("ParseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
  413. ctx.Handle(404, "ParseCompareInfo", nil)
  414. return nil, nil, nil, nil, "", ""
  415. }
  416. var headGitRepo *git.Repository
  417. if isSameRepo {
  418. headRepo = ctx.Repo.Repository
  419. headGitRepo = ctx.Repo.GitRepo
  420. } else {
  421. headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  422. if err != nil {
  423. ctx.Handle(500, "OpenRepository", err)
  424. return nil, nil, nil, nil, "", ""
  425. }
  426. }
  427. if !ctx.User.IsWriterOfRepo(headRepo) && !ctx.User.IsAdmin {
  428. log.Trace("ParseCompareInfo[%d]: does not have write access or site admin", baseRepo.ID)
  429. ctx.Handle(404, "ParseCompareInfo", nil)
  430. return nil, nil, nil, nil, "", ""
  431. }
  432. // Check if head branch is valid.
  433. if !headGitRepo.IsBranchExist(headBranch) {
  434. ctx.Handle(404, "IsBranchExist", nil)
  435. return nil, nil, nil, nil, "", ""
  436. }
  437. headBranches, err := headGitRepo.GetBranches()
  438. if err != nil {
  439. ctx.Handle(500, "GetBranches", err)
  440. return nil, nil, nil, nil, "", ""
  441. }
  442. ctx.Data["HeadBranches"] = headBranches
  443. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
  444. if err != nil {
  445. ctx.Handle(500, "GetPullRequestInfo", err)
  446. return nil, nil, nil, nil, "", ""
  447. }
  448. ctx.Data["BeforeCommitID"] = prInfo.MergeBase
  449. return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
  450. }
  451. func PrepareCompareDiff(
  452. ctx *context.Context,
  453. headUser *models.User,
  454. headRepo *models.Repository,
  455. headGitRepo *git.Repository,
  456. prInfo *git.PullRequestInfo,
  457. baseBranch, headBranch string) bool {
  458. var (
  459. repo = ctx.Repo.Repository
  460. err error
  461. )
  462. // Get diff information.
  463. ctx.Data["CommitRepoLink"] = headRepo.Link()
  464. headCommitID, err := headGitRepo.GetBranchCommitID(headBranch)
  465. if err != nil {
  466. ctx.Handle(500, "GetBranchCommitID", err)
  467. return false
  468. }
  469. ctx.Data["AfterCommitID"] = headCommitID
  470. if headCommitID == prInfo.MergeBase {
  471. ctx.Data["IsNothingToCompare"] = true
  472. return true
  473. }
  474. diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
  475. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
  476. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  477. if err != nil {
  478. ctx.Handle(500, "GetDiffRange", err)
  479. return false
  480. }
  481. ctx.Data["Diff"] = diff
  482. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  483. headCommit, err := headGitRepo.GetCommit(headCommitID)
  484. if err != nil {
  485. ctx.Handle(500, "GetCommit", err)
  486. return false
  487. }
  488. prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
  489. ctx.Data["Commits"] = prInfo.Commits
  490. ctx.Data["CommitCount"] = prInfo.Commits.Len()
  491. ctx.Data["Username"] = headUser.Name
  492. ctx.Data["Reponame"] = headRepo.Name
  493. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  494. headTarget := path.Join(headUser.Name, repo.Name)
  495. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", headCommitID)
  496. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", prInfo.MergeBase)
  497. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", headCommitID)
  498. return false
  499. }
  500. func CompareAndPullRequest(ctx *context.Context) {
  501. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  502. ctx.Data["PageIsComparePull"] = true
  503. ctx.Data["IsDiffCompare"] = true
  504. ctx.Data["RequireHighlightJS"] = true
  505. setTemplateIfExists(ctx, PULL_REQUEST_TEMPLATE_KEY, PullRequestTemplateCandidates)
  506. renderAttachmentSettings(ctx)
  507. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  508. if ctx.Written() {
  509. return
  510. }
  511. pr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
  512. if err != nil {
  513. if !models.IsErrPullRequestNotExist(err) {
  514. ctx.Handle(500, "GetUnmergedPullRequest", err)
  515. return
  516. }
  517. } else {
  518. ctx.Data["HasPullRequest"] = true
  519. ctx.Data["PullRequest"] = pr
  520. ctx.HTML(200, COMPARE_PULL)
  521. return
  522. }
  523. nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  524. if ctx.Written() {
  525. return
  526. }
  527. if !nothingToCompare {
  528. // Setup information for new form.
  529. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  530. if ctx.Written() {
  531. return
  532. }
  533. }
  534. ec, err := ctx.Repo.GetEditorconfig()
  535. if err != nil && !git.IsErrNotExist(err) {
  536. ctx.Handle(500, "ErrGettingEditorconfig", err)
  537. return
  538. }
  539. ctx.Data["Editorconfig"] = ec
  540. ctx.HTML(200, COMPARE_PULL)
  541. }
  542. func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm) {
  543. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  544. ctx.Data["PageIsComparePull"] = true
  545. ctx.Data["IsDiffCompare"] = true
  546. ctx.Data["RequireHighlightJS"] = true
  547. renderAttachmentSettings(ctx)
  548. var (
  549. repo = ctx.Repo.Repository
  550. attachments []string
  551. )
  552. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  553. if ctx.Written() {
  554. return
  555. }
  556. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, form)
  557. if ctx.Written() {
  558. return
  559. }
  560. if setting.AttachmentEnabled {
  561. attachments = form.Files
  562. }
  563. if ctx.HasError() {
  564. auth.AssignForm(form, ctx.Data)
  565. // This stage is already stop creating new pull request, so it does not matter if it has
  566. // something to compare or not.
  567. PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  568. if ctx.Written() {
  569. return
  570. }
  571. ctx.HTML(200, COMPARE_PULL)
  572. return
  573. }
  574. patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
  575. if err != nil {
  576. ctx.Handle(500, "GetPatch", err)
  577. return
  578. }
  579. pullIssue := &models.Issue{
  580. RepoID: repo.ID,
  581. Index: repo.NextIssueIndex(),
  582. Title: form.Title,
  583. PosterID: ctx.User.ID,
  584. Poster: ctx.User,
  585. MilestoneID: milestoneID,
  586. AssigneeID: assigneeID,
  587. IsPull: true,
  588. Content: form.Content,
  589. }
  590. pullRequest := &models.PullRequest{
  591. HeadRepoID: headRepo.ID,
  592. BaseRepoID: repo.ID,
  593. HeadUserName: headUser.Name,
  594. HeadBranch: headBranch,
  595. BaseBranch: baseBranch,
  596. HeadRepo: headRepo,
  597. BaseRepo: repo,
  598. MergeBase: prInfo.MergeBase,
  599. Type: models.PULL_REQUEST_GOGS,
  600. }
  601. // FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
  602. // instead of 500.
  603. if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch); err != nil {
  604. ctx.Handle(500, "NewPullRequest", err)
  605. return
  606. } else if err := pullRequest.PushToBaseRepo(); err != nil {
  607. ctx.Handle(500, "PushToBaseRepo", err)
  608. return
  609. }
  610. log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
  611. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
  612. }
  613. func TriggerTask(ctx *context.Context) {
  614. pusherID := ctx.QueryInt64("pusher")
  615. branch := ctx.Query("branch")
  616. secret := ctx.Query("secret")
  617. if len(branch) == 0 || len(secret) == 0 || pusherID <= 0 {
  618. ctx.Error(404)
  619. log.Trace("TriggerTask: branch or secret is empty, or pusher ID is not valid")
  620. return
  621. }
  622. owner, repo := parseOwnerAndRepo(ctx)
  623. if ctx.Written() {
  624. return
  625. }
  626. if secret != base.EncodeMD5(owner.Salt) {
  627. ctx.Error(404)
  628. log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
  629. return
  630. }
  631. pusher, err := models.GetUserByID(pusherID)
  632. if err != nil {
  633. if models.IsErrUserNotExist(err) {
  634. ctx.Error(404)
  635. } else {
  636. ctx.Handle(500, "GetUserByID", err)
  637. }
  638. return
  639. }
  640. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  641. go models.HookQueue.Add(repo.ID)
  642. go models.AddTestPullRequestTask(pusher, repo.ID, branch, true)
  643. ctx.Status(202)
  644. }