pull.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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 getForkRepository(ctx *context.Context) *models.Repository {
  33. forkRepo, err := models.GetRepositoryByID(ctx.ParamsInt64(":repoid"))
  34. if err != nil {
  35. if models.IsErrRepoNotExist(err) {
  36. ctx.Handle(404, "GetRepositoryByID", nil)
  37. } else {
  38. ctx.Handle(500, "GetRepositoryByID", err)
  39. }
  40. return nil
  41. }
  42. if !forkRepo.CanBeForked() || !forkRepo.HasAccess(ctx.User.ID) {
  43. ctx.Handle(404, "getForkRepository", nil)
  44. return nil
  45. }
  46. ctx.Data["repo_name"] = forkRepo.Name
  47. ctx.Data["description"] = forkRepo.Description
  48. ctx.Data["IsPrivate"] = forkRepo.IsPrivate
  49. if err = forkRepo.GetOwner(); err != nil {
  50. ctx.Handle(500, "GetOwner", err)
  51. return nil
  52. }
  53. ctx.Data["ForkFrom"] = forkRepo.Owner.Name + "/" + forkRepo.Name
  54. if err := ctx.User.GetOrganizations(true); err != nil {
  55. ctx.Handle(500, "GetOrganizations", err)
  56. return nil
  57. }
  58. ctx.Data["Orgs"] = ctx.User.Orgs
  59. return forkRepo
  60. }
  61. func Fork(ctx *context.Context) {
  62. ctx.Data["Title"] = ctx.Tr("new_fork")
  63. getForkRepository(ctx)
  64. if ctx.Written() {
  65. return
  66. }
  67. ctx.Data["ContextUser"] = ctx.User
  68. ctx.HTML(200, FORK)
  69. }
  70. func ForkPost(ctx *context.Context, f form.CreateRepo) {
  71. ctx.Data["Title"] = ctx.Tr("new_fork")
  72. forkRepo := getForkRepository(ctx)
  73. if ctx.Written() {
  74. return
  75. }
  76. ctxUser := checkContextUser(ctx, f.Uid)
  77. if ctx.Written() {
  78. return
  79. }
  80. ctx.Data["ContextUser"] = ctxUser
  81. if ctx.HasError() {
  82. ctx.HTML(200, FORK)
  83. return
  84. }
  85. repo, has := models.HasForkedRepo(ctxUser.ID, forkRepo.ID)
  86. if has {
  87. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  88. return
  89. }
  90. // Check ownership of organization.
  91. if ctxUser.IsOrganization() {
  92. if !ctxUser.IsOwnedBy(ctx.User.ID) {
  93. ctx.Error(403)
  94. return
  95. }
  96. }
  97. // Cannot fork to same owner
  98. if ctxUser.ID == forkRepo.OwnerID {
  99. ctx.RenderWithErr(ctx.Tr("repo.settings.cannot_fork_to_same_owner"), FORK, &f)
  100. return
  101. }
  102. repo, err := models.ForkRepository(ctxUser, forkRepo, f.RepoName, f.Description)
  103. if err != nil {
  104. ctx.Data["Err_RepoName"] = true
  105. switch {
  106. case models.IsErrRepoAlreadyExist(err):
  107. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), FORK, &f)
  108. case models.IsErrNameReserved(err):
  109. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &f)
  110. case models.IsErrNamePatternNotAllowed(err):
  111. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), FORK, &f)
  112. default:
  113. ctx.Handle(500, "ForkPost", err)
  114. }
  115. return
  116. }
  117. log.Trace("Repository forked[%d]: %s/%s", forkRepo.ID, ctxUser.Name, repo.Name)
  118. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  119. }
  120. func checkPullInfo(ctx *context.Context) *models.Issue {
  121. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  122. if err != nil {
  123. if models.IsErrIssueNotExist(err) {
  124. ctx.Handle(404, "GetIssueByIndex", err)
  125. } else {
  126. ctx.Handle(500, "GetIssueByIndex", err)
  127. }
  128. return nil
  129. }
  130. ctx.Data["Title"] = issue.Title
  131. ctx.Data["Issue"] = issue
  132. if !issue.IsPull {
  133. ctx.Handle(404, "ViewPullCommits", nil)
  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 pull.HeadRepo != nil {
  172. headGitRepo, err = git.OpenRepository(pull.HeadRepo.RepoPath())
  173. if err != nil {
  174. ctx.Handle(500, "OpenRepository", err)
  175. return nil
  176. }
  177. }
  178. if pull.HeadRepo == nil || !headGitRepo.IsBranchExist(pull.HeadBranch) {
  179. ctx.Data["IsPullReuqestBroken"] = true
  180. ctx.Data["HeadTarget"] = "deleted"
  181. ctx.Data["NumCommits"] = 0
  182. ctx.Data["NumFiles"] = 0
  183. return nil
  184. }
  185. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name),
  186. pull.BaseBranch, pull.HeadBranch)
  187. if err != nil {
  188. if strings.Contains(err.Error(), "fatal: Not a valid object name") {
  189. ctx.Data["IsPullReuqestBroken"] = true
  190. ctx.Data["BaseTarget"] = "deleted"
  191. ctx.Data["NumCommits"] = 0
  192. ctx.Data["NumFiles"] = 0
  193. return nil
  194. }
  195. ctx.Handle(500, "GetPullRequestInfo", err)
  196. return nil
  197. }
  198. ctx.Data["NumCommits"] = prInfo.Commits.Len()
  199. ctx.Data["NumFiles"] = prInfo.NumFiles
  200. return prInfo
  201. }
  202. func ViewPullCommits(ctx *context.Context) {
  203. ctx.Data["PageIsPullList"] = true
  204. ctx.Data["PageIsPullCommits"] = true
  205. issue := checkPullInfo(ctx)
  206. if ctx.Written() {
  207. return
  208. }
  209. pull := issue.PullRequest
  210. ctx.Data["Username"] = pull.HeadUserName
  211. ctx.Data["Reponame"] = pull.HeadRepo.Name
  212. var commits *list.List
  213. if pull.HasMerged {
  214. PrepareMergedViewPullInfo(ctx, issue)
  215. if ctx.Written() {
  216. return
  217. }
  218. startCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergeBase)
  219. if err != nil {
  220. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  221. return
  222. }
  223. endCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergedCommitID)
  224. if err != nil {
  225. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  226. return
  227. }
  228. commits, err = ctx.Repo.GitRepo.CommitsBetween(endCommit, startCommit)
  229. if err != nil {
  230. ctx.Handle(500, "Repo.GitRepo.CommitsBetween", err)
  231. return
  232. }
  233. } else {
  234. prInfo := PrepareViewPullInfo(ctx, issue)
  235. if ctx.Written() {
  236. return
  237. } else if prInfo == nil {
  238. ctx.Handle(404, "ViewPullCommits", nil)
  239. return
  240. }
  241. commits = prInfo.Commits
  242. }
  243. commits = models.ValidateCommitsWithEmails(commits)
  244. ctx.Data["Commits"] = commits
  245. ctx.Data["CommitCount"] = commits.Len()
  246. ctx.HTML(200, PULL_COMMITS)
  247. }
  248. func ViewPullFiles(ctx *context.Context) {
  249. ctx.Data["PageIsPullList"] = true
  250. ctx.Data["PageIsPullFiles"] = true
  251. issue := checkPullInfo(ctx)
  252. if ctx.Written() {
  253. return
  254. }
  255. pull := issue.PullRequest
  256. var (
  257. diffRepoPath string
  258. startCommitID string
  259. endCommitID string
  260. gitRepo *git.Repository
  261. )
  262. if pull.HasMerged {
  263. PrepareMergedViewPullInfo(ctx, issue)
  264. if ctx.Written() {
  265. return
  266. }
  267. diffRepoPath = ctx.Repo.GitRepo.Path
  268. startCommitID = pull.MergeBase
  269. endCommitID = pull.MergedCommitID
  270. gitRepo = ctx.Repo.GitRepo
  271. } else {
  272. prInfo := PrepareViewPullInfo(ctx, issue)
  273. if ctx.Written() {
  274. return
  275. } else if prInfo == nil {
  276. ctx.Handle(404, "ViewPullFiles", nil)
  277. return
  278. }
  279. headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
  280. headGitRepo, err := git.OpenRepository(headRepoPath)
  281. if err != nil {
  282. ctx.Handle(500, "OpenRepository", err)
  283. return
  284. }
  285. headCommitID, err := headGitRepo.GetBranchCommitID(pull.HeadBranch)
  286. if err != nil {
  287. ctx.Handle(500, "GetBranchCommitID", err)
  288. return
  289. }
  290. diffRepoPath = headRepoPath
  291. startCommitID = prInfo.MergeBase
  292. endCommitID = headCommitID
  293. gitRepo = headGitRepo
  294. }
  295. diff, err := models.GetDiffRange(diffRepoPath,
  296. startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
  297. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  298. if err != nil {
  299. ctx.Handle(500, "GetDiffRange", err)
  300. return
  301. }
  302. ctx.Data["Diff"] = diff
  303. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  304. commit, err := gitRepo.GetCommit(endCommitID)
  305. if err != nil {
  306. ctx.Handle(500, "GetCommit", err)
  307. return
  308. }
  309. setEditorconfigIfExists(ctx)
  310. if ctx.Written() {
  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. isSameRepo = headUser.ID == baseRepo.OwnerID
  393. } else {
  394. ctx.Handle(404, "CompareAndPullRequest", nil)
  395. return nil, nil, nil, nil, "", ""
  396. }
  397. ctx.Data["HeadUser"] = headUser
  398. ctx.Data["HeadBranch"] = headBranch
  399. ctx.Repo.PullRequest.SameRepo = isSameRepo
  400. // Check if base branch is valid.
  401. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  402. ctx.Handle(404, "IsBranchExist", nil)
  403. return nil, nil, nil, nil, "", ""
  404. }
  405. var (
  406. headRepo *models.Repository
  407. headGitRepo *git.Repository
  408. )
  409. // In case user included redundant head user name for comparison in same repository,
  410. // no need to check the fork relation.
  411. if !isSameRepo {
  412. var has bool
  413. headRepo, has = models.HasForkedRepo(headUser.ID, baseRepo.ID)
  414. if !has {
  415. log.Trace("ParseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
  416. ctx.Handle(404, "ParseCompareInfo", nil)
  417. return nil, nil, nil, nil, "", ""
  418. }
  419. headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  420. if err != nil {
  421. ctx.Handle(500, "OpenRepository", err)
  422. return nil, nil, nil, nil, "", ""
  423. }
  424. } else {
  425. headRepo = ctx.Repo.Repository
  426. headGitRepo = ctx.Repo.GitRepo
  427. }
  428. if !ctx.User.IsWriterOfRepo(headRepo) && !ctx.User.IsAdmin {
  429. log.Trace("ParseCompareInfo[%d]: does not have write access or site admin", baseRepo.ID)
  430. ctx.Handle(404, "ParseCompareInfo", nil)
  431. return nil, nil, nil, nil, "", ""
  432. }
  433. // Check if head branch is valid.
  434. if !headGitRepo.IsBranchExist(headBranch) {
  435. ctx.Handle(404, "IsBranchExist", nil)
  436. return nil, nil, nil, nil, "", ""
  437. }
  438. headBranches, err := headGitRepo.GetBranches()
  439. if err != nil {
  440. ctx.Handle(500, "GetBranches", err)
  441. return nil, nil, nil, nil, "", ""
  442. }
  443. ctx.Data["HeadBranches"] = headBranches
  444. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
  445. if err != nil {
  446. ctx.Handle(500, "GetPullRequestInfo", err)
  447. return nil, nil, nil, nil, "", ""
  448. }
  449. ctx.Data["BeforeCommitID"] = prInfo.MergeBase
  450. return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
  451. }
  452. func PrepareCompareDiff(
  453. ctx *context.Context,
  454. headUser *models.User,
  455. headRepo *models.Repository,
  456. headGitRepo *git.Repository,
  457. prInfo *git.PullRequestInfo,
  458. baseBranch, headBranch string) bool {
  459. var (
  460. repo = ctx.Repo.Repository
  461. err error
  462. )
  463. // Get diff information.
  464. ctx.Data["CommitRepoLink"] = headRepo.Link()
  465. headCommitID, err := headGitRepo.GetBranchCommitID(headBranch)
  466. if err != nil {
  467. ctx.Handle(500, "GetBranchCommitID", err)
  468. return false
  469. }
  470. ctx.Data["AfterCommitID"] = headCommitID
  471. if headCommitID == prInfo.MergeBase {
  472. ctx.Data["IsNothingToCompare"] = true
  473. return true
  474. }
  475. diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
  476. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
  477. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  478. if err != nil {
  479. ctx.Handle(500, "GetDiffRange", err)
  480. return false
  481. }
  482. ctx.Data["Diff"] = diff
  483. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  484. headCommit, err := headGitRepo.GetCommit(headCommitID)
  485. if err != nil {
  486. ctx.Handle(500, "GetCommit", err)
  487. return false
  488. }
  489. prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
  490. ctx.Data["Commits"] = prInfo.Commits
  491. ctx.Data["CommitCount"] = prInfo.Commits.Len()
  492. ctx.Data["Username"] = headUser.Name
  493. ctx.Data["Reponame"] = headRepo.Name
  494. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  495. headTarget := path.Join(headUser.Name, repo.Name)
  496. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", headCommitID)
  497. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", prInfo.MergeBase)
  498. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", headCommitID)
  499. return false
  500. }
  501. func CompareAndPullRequest(ctx *context.Context) {
  502. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  503. ctx.Data["PageIsComparePull"] = true
  504. ctx.Data["IsDiffCompare"] = true
  505. ctx.Data["RequireHighlightJS"] = true
  506. setTemplateIfExists(ctx, PULL_REQUEST_TEMPLATE_KEY, PullRequestTemplateCandidates)
  507. renderAttachmentSettings(ctx)
  508. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  509. if ctx.Written() {
  510. return
  511. }
  512. pr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
  513. if err != nil {
  514. if !models.IsErrPullRequestNotExist(err) {
  515. ctx.Handle(500, "GetUnmergedPullRequest", err)
  516. return
  517. }
  518. } else {
  519. ctx.Data["HasPullRequest"] = true
  520. ctx.Data["PullRequest"] = pr
  521. ctx.HTML(200, COMPARE_PULL)
  522. return
  523. }
  524. nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  525. if ctx.Written() {
  526. return
  527. }
  528. if !nothingToCompare {
  529. // Setup information for new form.
  530. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  531. if ctx.Written() {
  532. return
  533. }
  534. }
  535. setEditorconfigIfExists(ctx)
  536. if ctx.Written() {
  537. return
  538. }
  539. ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"
  540. ctx.HTML(200, COMPARE_PULL)
  541. }
  542. func CompareAndPullRequestPost(ctx *context.Context, f form.CreateIssue) {
  543. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  544. ctx.Data["PageIsComparePull"] = true
  545. ctx.Data["IsDiffCompare"] = true
  546. ctx.Data["RequireHighlightJS"] = true
  547. renderAttachmentSettings(ctx)
  548. var (
  549. repo = ctx.Repo.Repository
  550. attachments []string
  551. )
  552. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  553. if ctx.Written() {
  554. return
  555. }
  556. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, f)
  557. if ctx.Written() {
  558. return
  559. }
  560. if setting.AttachmentEnabled {
  561. attachments = f.Files
  562. }
  563. if ctx.HasError() {
  564. form.Assign(f, ctx.Data)
  565. // This stage is already stop creating new pull request, so it does not matter if it has
  566. // something to compare or not.
  567. PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  568. if ctx.Written() {
  569. return
  570. }
  571. ctx.HTML(200, COMPARE_PULL)
  572. return
  573. }
  574. patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
  575. if err != nil {
  576. ctx.Handle(500, "GetPatch", err)
  577. return
  578. }
  579. pullIssue := &models.Issue{
  580. RepoID: repo.ID,
  581. Index: repo.NextIssueIndex(),
  582. Title: f.Title,
  583. PosterID: ctx.User.ID,
  584. Poster: ctx.User,
  585. MilestoneID: milestoneID,
  586. AssigneeID: assigneeID,
  587. IsPull: true,
  588. Content: f.Content,
  589. }
  590. pullRequest := &models.PullRequest{
  591. HeadRepoID: headRepo.ID,
  592. BaseRepoID: repo.ID,
  593. HeadUserName: headUser.Name,
  594. HeadBranch: headBranch,
  595. BaseBranch: baseBranch,
  596. HeadRepo: headRepo,
  597. BaseRepo: repo,
  598. MergeBase: prInfo.MergeBase,
  599. Type: models.PULL_REQUEST_GOGS,
  600. }
  601. // FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
  602. // instead of 500.
  603. if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch); err != nil {
  604. ctx.Handle(500, "NewPullRequest", err)
  605. return
  606. } else if err := pullRequest.PushToBaseRepo(); err != nil {
  607. ctx.Handle(500, "PushToBaseRepo", err)
  608. return
  609. }
  610. log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
  611. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
  612. }
  613. func parseOwnerAndRepo(ctx *context.Context) (*models.User, *models.Repository) {
  614. owner, err := models.GetUserByName(ctx.Params(":username"))
  615. if err != nil {
  616. ctx.NotFoundOrServerError("GetUserByName", models.IsErrUserNotExist, err)
  617. return nil, nil
  618. }
  619. repo, err := models.GetRepositoryByName(owner.ID, ctx.Params(":reponame"))
  620. if err != nil {
  621. ctx.NotFoundOrServerError("GetRepositoryByName", models.IsErrRepoNotExist, err)
  622. return nil, nil
  623. }
  624. return owner, repo
  625. }
  626. func TriggerTask(ctx *context.Context) {
  627. pusherID := ctx.QueryInt64("pusher")
  628. branch := ctx.Query("branch")
  629. secret := ctx.Query("secret")
  630. if len(branch) == 0 || len(secret) == 0 || pusherID <= 0 {
  631. ctx.Error(404)
  632. log.Trace("TriggerTask: branch or secret is empty, or pusher ID is not valid")
  633. return
  634. }
  635. owner, repo := parseOwnerAndRepo(ctx)
  636. if ctx.Written() {
  637. return
  638. }
  639. if secret != base.EncodeMD5(owner.Salt) {
  640. ctx.Error(404)
  641. log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
  642. return
  643. }
  644. pusher, err := models.GetUserByID(pusherID)
  645. if err != nil {
  646. if models.IsErrUserNotExist(err) {
  647. ctx.Error(404)
  648. } else {
  649. ctx.Handle(500, "GetUserByID", err)
  650. }
  651. return
  652. }
  653. log.Trace("TriggerTask '%s/%s' by '%s'", repo.Name, branch, pusher.Name)
  654. go models.HookQueue.Add(repo.ID)
  655. go models.AddTestPullRequestTask(pusher, repo.ID, branch, true)
  656. ctx.Status(202)
  657. }