pull.go 20 KB

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