pull.go 20 KB

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