pull.go 20 KB

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