You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pull.go 19KB

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