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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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. git "github.com/gogits/git-module"
  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. setEditorconfigIfExists(ctx)
  314. if ctx.Written() {
  315. return
  316. }
  317. headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
  318. ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"
  319. ctx.Data["Username"] = pull.HeadUserName
  320. ctx.Data["Reponame"] = pull.HeadRepo.Name
  321. ctx.Data["IsImageFile"] = commit.IsImageFile
  322. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", endCommitID)
  323. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", startCommitID)
  324. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", endCommitID)
  325. ctx.Data["RequireHighlightJS"] = true
  326. ctx.HTML(200, PULL_FILES)
  327. }
  328. func MergePullRequest(ctx *context.Context) {
  329. issue := checkPullInfo(ctx)
  330. if ctx.Written() {
  331. return
  332. }
  333. if issue.IsClosed {
  334. ctx.Handle(404, "MergePullRequest", nil)
  335. return
  336. }
  337. pr, err := models.GetPullRequestByIssueID(issue.ID)
  338. if err != nil {
  339. if models.IsErrPullRequestNotExist(err) {
  340. ctx.Handle(404, "GetPullRequestByIssueID", nil)
  341. } else {
  342. ctx.Handle(500, "GetPullRequestByIssueID", err)
  343. }
  344. return
  345. }
  346. if !pr.CanAutoMerge() || pr.HasMerged {
  347. ctx.Handle(404, "MergePullRequest", nil)
  348. return
  349. }
  350. pr.Issue = issue
  351. pr.Issue.Repo = ctx.Repo.Repository
  352. if err = pr.Merge(ctx.User, ctx.Repo.GitRepo); err != nil {
  353. ctx.Handle(500, "Merge", err)
  354. return
  355. }
  356. log.Trace("Pull request merged: %d", pr.ID)
  357. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  358. }
  359. func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
  360. baseRepo := ctx.Repo.Repository
  361. // Get compared branches information
  362. // format: <base branch>...[<head repo>:]<head branch>
  363. // base<-head: master...head:feature
  364. // same repo: master...feature
  365. infos := strings.Split(ctx.Params("*"), "...")
  366. if len(infos) != 2 {
  367. log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)
  368. ctx.Handle(404, "CompareAndPullRequest", nil)
  369. return nil, nil, nil, nil, "", ""
  370. }
  371. baseBranch := infos[0]
  372. ctx.Data["BaseBranch"] = baseBranch
  373. var (
  374. headUser *models.User
  375. headBranch string
  376. isSameRepo bool
  377. err error
  378. )
  379. // If there is no head repository, it means pull request between same repository.
  380. headInfos := strings.Split(infos[1], ":")
  381. if len(headInfos) == 1 {
  382. isSameRepo = true
  383. headUser = ctx.Repo.Owner
  384. headBranch = headInfos[0]
  385. } else if len(headInfos) == 2 {
  386. headUser, err = models.GetUserByName(headInfos[0])
  387. if err != nil {
  388. if models.IsErrUserNotExist(err) {
  389. ctx.Handle(404, "GetUserByName", nil)
  390. } else {
  391. ctx.Handle(500, "GetUserByName", err)
  392. }
  393. return nil, nil, nil, nil, "", ""
  394. }
  395. headBranch = headInfos[1]
  396. } else {
  397. ctx.Handle(404, "CompareAndPullRequest", nil)
  398. return nil, nil, nil, nil, "", ""
  399. }
  400. ctx.Data["HeadUser"] = headUser
  401. ctx.Data["HeadBranch"] = headBranch
  402. ctx.Repo.PullRequest.SameRepo = isSameRepo
  403. // Check if base branch is valid.
  404. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  405. ctx.Handle(404, "IsBranchExist", nil)
  406. return nil, nil, nil, nil, "", ""
  407. }
  408. // Check if current user has fork of repository or in the same repository.
  409. headRepo, has := models.HasForkedRepo(headUser.ID, baseRepo.ID)
  410. if !has && !isSameRepo {
  411. log.Trace("ParseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
  412. ctx.Handle(404, "ParseCompareInfo", nil)
  413. return nil, nil, nil, nil, "", ""
  414. }
  415. var headGitRepo *git.Repository
  416. if isSameRepo {
  417. headRepo = ctx.Repo.Repository
  418. headGitRepo = ctx.Repo.GitRepo
  419. } else {
  420. headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  421. if err != nil {
  422. ctx.Handle(500, "OpenRepository", err)
  423. return nil, nil, nil, nil, "", ""
  424. }
  425. }
  426. if !ctx.User.IsWriterOfRepo(headRepo) && !ctx.User.IsAdmin {
  427. log.Trace("ParseCompareInfo[%d]: does not have write access or site admin", baseRepo.ID)
  428. ctx.Handle(404, "ParseCompareInfo", nil)
  429. return nil, nil, nil, nil, "", ""
  430. }
  431. // Check if head branch is valid.
  432. if !headGitRepo.IsBranchExist(headBranch) {
  433. ctx.Handle(404, "IsBranchExist", nil)
  434. return nil, nil, nil, nil, "", ""
  435. }
  436. headBranches, err := headGitRepo.GetBranches()
  437. if err != nil {
  438. ctx.Handle(500, "GetBranches", err)
  439. return nil, nil, nil, nil, "", ""
  440. }
  441. ctx.Data["HeadBranches"] = headBranches
  442. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
  443. if err != nil {
  444. ctx.Handle(500, "GetPullRequestInfo", err)
  445. return nil, nil, nil, nil, "", ""
  446. }
  447. ctx.Data["BeforeCommitID"] = prInfo.MergeBase
  448. return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
  449. }
  450. func PrepareCompareDiff(
  451. ctx *context.Context,
  452. headUser *models.User,
  453. headRepo *models.Repository,
  454. headGitRepo *git.Repository,
  455. prInfo *git.PullRequestInfo,
  456. baseBranch, headBranch string) bool {
  457. var (
  458. repo = ctx.Repo.Repository
  459. err error
  460. )
  461. // Get diff information.
  462. ctx.Data["CommitRepoLink"] = headRepo.Link()
  463. headCommitID, err := headGitRepo.GetBranchCommitID(headBranch)
  464. if err != nil {
  465. ctx.Handle(500, "GetBranchCommitID", err)
  466. return false
  467. }
  468. ctx.Data["AfterCommitID"] = headCommitID
  469. if headCommitID == prInfo.MergeBase {
  470. ctx.Data["IsNothingToCompare"] = true
  471. return true
  472. }
  473. diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
  474. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
  475. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  476. if err != nil {
  477. ctx.Handle(500, "GetDiffRange", err)
  478. return false
  479. }
  480. ctx.Data["Diff"] = diff
  481. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  482. headCommit, err := headGitRepo.GetCommit(headCommitID)
  483. if err != nil {
  484. ctx.Handle(500, "GetCommit", err)
  485. return false
  486. }
  487. prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
  488. ctx.Data["Commits"] = prInfo.Commits
  489. ctx.Data["CommitCount"] = prInfo.Commits.Len()
  490. ctx.Data["Username"] = headUser.Name
  491. ctx.Data["Reponame"] = headRepo.Name
  492. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  493. headTarget := path.Join(headUser.Name, repo.Name)
  494. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", headCommitID)
  495. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", prInfo.MergeBase)
  496. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", headCommitID)
  497. return false
  498. }
  499. func CompareAndPullRequest(ctx *context.Context) {
  500. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  501. ctx.Data["PageIsComparePull"] = true
  502. ctx.Data["IsDiffCompare"] = true
  503. ctx.Data["RequireHighlightJS"] = true
  504. setTemplateIfExists(ctx, PULL_REQUEST_TEMPLATE_KEY, PullRequestTemplateCandidates)
  505. renderAttachmentSettings(ctx)
  506. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  507. if ctx.Written() {
  508. return
  509. }
  510. pr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
  511. if err != nil {
  512. if !models.IsErrPullRequestNotExist(err) {
  513. ctx.Handle(500, "GetUnmergedPullRequest", err)
  514. return
  515. }
  516. } else {
  517. ctx.Data["HasPullRequest"] = true
  518. ctx.Data["PullRequest"] = pr
  519. ctx.HTML(200, COMPARE_PULL)
  520. return
  521. }
  522. nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  523. if ctx.Written() {
  524. return
  525. }
  526. if !nothingToCompare {
  527. // Setup information for new form.
  528. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  529. if ctx.Written() {
  530. return
  531. }
  532. }
  533. setEditorconfigIfExists(ctx)
  534. if ctx.Written() {
  535. return
  536. }
  537. ctx.HTML(200, COMPARE_PULL)
  538. }
  539. func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm) {
  540. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  541. ctx.Data["PageIsComparePull"] = true
  542. ctx.Data["IsDiffCompare"] = true
  543. ctx.Data["RequireHighlightJS"] = true
  544. renderAttachmentSettings(ctx)
  545. var (
  546. repo = ctx.Repo.Repository
  547. attachments []string
  548. )
  549. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  550. if ctx.Written() {
  551. return
  552. }
  553. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, form)
  554. if ctx.Written() {
  555. return
  556. }
  557. if setting.AttachmentEnabled {
  558. attachments = form.Files
  559. }
  560. if ctx.HasError() {
  561. auth.AssignForm(form, ctx.Data)
  562. // This stage is already stop creating new pull request, so it does not matter if it has
  563. // something to compare or not.
  564. PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  565. if ctx.Written() {
  566. return
  567. }
  568. ctx.HTML(200, COMPARE_PULL)
  569. return
  570. }
  571. patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
  572. if err != nil {
  573. ctx.Handle(500, "GetPatch", err)
  574. return
  575. }
  576. pullIssue := &models.Issue{
  577. RepoID: repo.ID,
  578. Index: repo.NextIssueIndex(),
  579. Title: form.Title,
  580. PosterID: ctx.User.ID,
  581. Poster: ctx.User,
  582. MilestoneID: milestoneID,
  583. AssigneeID: assigneeID,
  584. IsPull: true,
  585. Content: form.Content,
  586. }
  587. pullRequest := &models.PullRequest{
  588. HeadRepoID: headRepo.ID,
  589. BaseRepoID: repo.ID,
  590. HeadUserName: headUser.Name,
  591. HeadBranch: headBranch,
  592. BaseBranch: baseBranch,
  593. HeadRepo: headRepo,
  594. BaseRepo: repo,
  595. MergeBase: prInfo.MergeBase,
  596. Type: models.PULL_REQUEST_GOGS,
  597. }
  598. // FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
  599. // instead of 500.
  600. if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch); err != nil {
  601. ctx.Handle(500, "NewPullRequest", err)
  602. return
  603. } else if err := pullRequest.PushToBaseRepo(); err != nil {
  604. ctx.Handle(500, "PushToBaseRepo", err)
  605. return
  606. }
  607. log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
  608. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
  609. }
  610. func TriggerTask(ctx *context.Context) {
  611. pusherID := ctx.QueryInt64("pusher")
  612. branch := ctx.Query("branch")
  613. secret := ctx.Query("secret")
  614. if len(branch) == 0 || len(secret) == 0 || pusherID <= 0 {
  615. ctx.Error(404)
  616. log.Trace("TriggerTask: branch or secret is empty, or pusher ID is not valid")
  617. return
  618. }
  619. owner, repo := parseOwnerAndRepo(ctx)
  620. if ctx.Written() {
  621. return
  622. }
  623. if secret != base.EncodeMD5(owner.Salt) {
  624. ctx.Error(404)
  625. log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
  626. return
  627. }
  628. pusher, err := models.GetUserByID(pusherID)
  629. if err != nil {
  630. if models.IsErrUserNotExist(err) {
  631. ctx.Error(404)
  632. } else {
  633. ctx.Handle(500, "GetUserByID", err)
  634. }
  635. return
  636. }
  637. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  638. go models.HookQueue.Add(repo.ID)
  639. go models.AddTestPullRequestTask(pusher, repo.ID, branch, true)
  640. ctx.Status(202)
  641. }