pull.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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/models/errors"
  14. "github.com/gogits/gogs/modules/base"
  15. "github.com/gogits/gogs/modules/context"
  16. "github.com/gogits/gogs/modules/form"
  17. "github.com/gogits/gogs/modules/setting"
  18. )
  19. const (
  20. FORK base.TplName = "repo/pulls/fork"
  21. COMPARE_PULL base.TplName = "repo/pulls/compare"
  22. PULL_COMMITS base.TplName = "repo/pulls/commits"
  23. PULL_FILES base.TplName = "repo/pulls/files"
  24. PULL_REQUEST_TEMPLATE_KEY = "PullRequestTemplate"
  25. )
  26. var (
  27. PullRequestTemplateCandidates = []string{
  28. "PULL_REQUEST.md",
  29. ".gogs/PULL_REQUEST.md",
  30. ".github/PULL_REQUEST.md",
  31. }
  32. )
  33. func parseBaseRepository(ctx *context.Context) *models.Repository {
  34. baseRepo, err := models.GetRepositoryByID(ctx.ParamsInt64(":repoid"))
  35. if err != nil {
  36. ctx.NotFoundOrServerError("GetRepositoryByID", models.IsErrRepoNotExist, err)
  37. return nil
  38. }
  39. if !baseRepo.CanBeForked() || !baseRepo.HasAccess(ctx.User.ID) {
  40. ctx.NotFound()
  41. return nil
  42. }
  43. ctx.Data["repo_name"] = baseRepo.Name
  44. ctx.Data["description"] = baseRepo.Description
  45. ctx.Data["IsPrivate"] = baseRepo.IsPrivate
  46. if err = baseRepo.GetOwner(); err != nil {
  47. ctx.Handle(500, "GetOwner", err)
  48. return nil
  49. }
  50. ctx.Data["ForkFrom"] = baseRepo.Owner.Name + "/" + baseRepo.Name
  51. if err := ctx.User.GetOrganizations(true); err != nil {
  52. ctx.Handle(500, "GetOrganizations", err)
  53. return nil
  54. }
  55. ctx.Data["Orgs"] = ctx.User.Orgs
  56. return baseRepo
  57. }
  58. func Fork(ctx *context.Context) {
  59. ctx.Data["Title"] = ctx.Tr("new_fork")
  60. parseBaseRepository(ctx)
  61. if ctx.Written() {
  62. return
  63. }
  64. ctx.Data["ContextUser"] = ctx.User
  65. ctx.HTML(200, FORK)
  66. }
  67. func ForkPost(ctx *context.Context, f form.CreateRepo) {
  68. ctx.Data["Title"] = ctx.Tr("new_fork")
  69. baseRepo := parseBaseRepository(ctx)
  70. if ctx.Written() {
  71. return
  72. }
  73. ctxUser := checkContextUser(ctx, f.UserID)
  74. if ctx.Written() {
  75. return
  76. }
  77. ctx.Data["ContextUser"] = ctxUser
  78. if ctx.HasError() {
  79. ctx.HTML(200, FORK)
  80. return
  81. }
  82. repo, has := models.HasForkedRepo(ctxUser.ID, baseRepo.ID)
  83. if has {
  84. ctx.Redirect(repo.Link())
  85. return
  86. }
  87. // Check ownership of organization.
  88. if ctxUser.IsOrganization() && !ctxUser.IsOwnedBy(ctx.User.ID) {
  89. ctx.Error(403)
  90. return
  91. }
  92. // Cannot fork to same owner
  93. if ctxUser.ID == baseRepo.OwnerID {
  94. ctx.RenderWithErr(ctx.Tr("repo.settings.cannot_fork_to_same_owner"), FORK, &f)
  95. return
  96. }
  97. repo, err := models.ForkRepository(ctx.User, ctxUser, baseRepo, f.RepoName, f.Description)
  98. if err != nil {
  99. ctx.Data["Err_RepoName"] = true
  100. switch {
  101. case models.IsErrRepoAlreadyExist(err):
  102. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), FORK, &f)
  103. case models.IsErrNameReserved(err):
  104. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &f)
  105. case models.IsErrNamePatternNotAllowed(err):
  106. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), FORK, &f)
  107. default:
  108. ctx.Handle(500, "ForkPost", err)
  109. }
  110. return
  111. }
  112. log.Trace("Repository forked from '%s' -> '%s'", baseRepo.FullName(), repo.FullName())
  113. ctx.Redirect(repo.Link())
  114. }
  115. func checkPullInfo(ctx *context.Context) *models.Issue {
  116. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  117. if err != nil {
  118. if models.IsErrIssueNotExist(err) {
  119. ctx.Handle(404, "GetIssueByIndex", err)
  120. } else {
  121. ctx.Handle(500, "GetIssueByIndex", err)
  122. }
  123. return nil
  124. }
  125. ctx.Data["Title"] = issue.Title
  126. ctx.Data["Issue"] = issue
  127. if !issue.IsPull {
  128. ctx.Handle(404, "ViewPullCommits", nil)
  129. return nil
  130. }
  131. if ctx.IsSigned {
  132. // Update issue-user.
  133. if err = issue.ReadBy(ctx.User.ID); err != nil {
  134. ctx.Handle(500, "ReadBy", err)
  135. return nil
  136. }
  137. }
  138. return issue
  139. }
  140. func PrepareMergedViewPullInfo(ctx *context.Context, issue *models.Issue) {
  141. pull := issue.PullRequest
  142. ctx.Data["HasMerged"] = true
  143. ctx.Data["HeadTarget"] = issue.PullRequest.HeadUserName + "/" + pull.HeadBranch
  144. ctx.Data["BaseTarget"] = ctx.Repo.Owner.Name + "/" + pull.BaseBranch
  145. var err error
  146. ctx.Data["NumCommits"], err = ctx.Repo.GitRepo.CommitsCountBetween(pull.MergeBase, pull.MergedCommitID)
  147. if err != nil {
  148. ctx.Handle(500, "Repo.GitRepo.CommitsCountBetween", err)
  149. return
  150. }
  151. ctx.Data["NumFiles"], err = ctx.Repo.GitRepo.FilesCountBetween(pull.MergeBase, pull.MergedCommitID)
  152. if err != nil {
  153. ctx.Handle(500, "Repo.GitRepo.FilesCountBetween", err)
  154. return
  155. }
  156. }
  157. func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.PullRequestInfo {
  158. repo := ctx.Repo.Repository
  159. pull := issue.PullRequest
  160. ctx.Data["HeadTarget"] = pull.HeadUserName + "/" + pull.HeadBranch
  161. ctx.Data["BaseTarget"] = ctx.Repo.Owner.Name + "/" + pull.BaseBranch
  162. var (
  163. headGitRepo *git.Repository
  164. err error
  165. )
  166. if pull.HeadRepo != nil {
  167. headGitRepo, err = git.OpenRepository(pull.HeadRepo.RepoPath())
  168. if err != nil {
  169. ctx.Handle(500, "OpenRepository", err)
  170. return nil
  171. }
  172. }
  173. if pull.HeadRepo == nil || !headGitRepo.IsBranchExist(pull.HeadBranch) {
  174. ctx.Data["IsPullReuqestBroken"] = true
  175. ctx.Data["HeadTarget"] = "deleted"
  176. ctx.Data["NumCommits"] = 0
  177. ctx.Data["NumFiles"] = 0
  178. return nil
  179. }
  180. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name),
  181. pull.BaseBranch, pull.HeadBranch)
  182. if err != nil {
  183. if strings.Contains(err.Error(), "fatal: Not a valid object name") {
  184. ctx.Data["IsPullReuqestBroken"] = true
  185. ctx.Data["BaseTarget"] = "deleted"
  186. ctx.Data["NumCommits"] = 0
  187. ctx.Data["NumFiles"] = 0
  188. return nil
  189. }
  190. ctx.Handle(500, "GetPullRequestInfo", err)
  191. return nil
  192. }
  193. ctx.Data["NumCommits"] = prInfo.Commits.Len()
  194. ctx.Data["NumFiles"] = prInfo.NumFiles
  195. return prInfo
  196. }
  197. func ViewPullCommits(ctx *context.Context) {
  198. ctx.Data["PageIsPullList"] = true
  199. ctx.Data["PageIsPullCommits"] = true
  200. issue := checkPullInfo(ctx)
  201. if ctx.Written() {
  202. return
  203. }
  204. pull := issue.PullRequest
  205. if pull.HeadRepo != nil {
  206. ctx.Data["Username"] = pull.HeadUserName
  207. ctx.Data["Reponame"] = pull.HeadRepo.Name
  208. }
  209. var commits *list.List
  210. if pull.HasMerged {
  211. PrepareMergedViewPullInfo(ctx, issue)
  212. if ctx.Written() {
  213. return
  214. }
  215. startCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergeBase)
  216. if err != nil {
  217. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  218. return
  219. }
  220. endCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergedCommitID)
  221. if err != nil {
  222. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  223. return
  224. }
  225. commits, err = ctx.Repo.GitRepo.CommitsBetween(endCommit, startCommit)
  226. if err != nil {
  227. ctx.Handle(500, "Repo.GitRepo.CommitsBetween", err)
  228. return
  229. }
  230. } else {
  231. prInfo := PrepareViewPullInfo(ctx, issue)
  232. if ctx.Written() {
  233. return
  234. } else if prInfo == nil {
  235. ctx.Handle(404, "ViewPullCommits", nil)
  236. return
  237. }
  238. commits = prInfo.Commits
  239. }
  240. commits = models.ValidateCommitsWithEmails(commits)
  241. ctx.Data["Commits"] = commits
  242. ctx.Data["CommitsCount"] = commits.Len()
  243. ctx.HTML(200, PULL_COMMITS)
  244. }
  245. func ViewPullFiles(ctx *context.Context) {
  246. ctx.Data["PageIsPullList"] = true
  247. ctx.Data["PageIsPullFiles"] = true
  248. issue := checkPullInfo(ctx)
  249. if ctx.Written() {
  250. return
  251. }
  252. pull := issue.PullRequest
  253. var (
  254. diffRepoPath string
  255. startCommitID string
  256. endCommitID string
  257. gitRepo *git.Repository
  258. )
  259. if pull.HasMerged {
  260. PrepareMergedViewPullInfo(ctx, issue)
  261. if ctx.Written() {
  262. return
  263. }
  264. diffRepoPath = ctx.Repo.GitRepo.Path
  265. startCommitID = pull.MergeBase
  266. endCommitID = pull.MergedCommitID
  267. gitRepo = ctx.Repo.GitRepo
  268. } else {
  269. prInfo := PrepareViewPullInfo(ctx, issue)
  270. if ctx.Written() {
  271. return
  272. } else if prInfo == nil {
  273. ctx.Handle(404, "ViewPullFiles", nil)
  274. return
  275. }
  276. headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
  277. headGitRepo, err := git.OpenRepository(headRepoPath)
  278. if err != nil {
  279. ctx.Handle(500, "OpenRepository", err)
  280. return
  281. }
  282. headCommitID, err := headGitRepo.GetBranchCommitID(pull.HeadBranch)
  283. if err != nil {
  284. ctx.Handle(500, "GetBranchCommitID", err)
  285. return
  286. }
  287. diffRepoPath = headRepoPath
  288. startCommitID = prInfo.MergeBase
  289. endCommitID = headCommitID
  290. gitRepo = headGitRepo
  291. }
  292. diff, err := models.GetDiffRange(diffRepoPath,
  293. startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
  294. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  295. if err != nil {
  296. ctx.Handle(500, "GetDiffRange", err)
  297. return
  298. }
  299. ctx.Data["Diff"] = diff
  300. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  301. commit, err := gitRepo.GetCommit(endCommitID)
  302. if err != nil {
  303. ctx.Handle(500, "GetCommit", err)
  304. return
  305. }
  306. setEditorconfigIfExists(ctx)
  307. if ctx.Written() {
  308. return
  309. }
  310. ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"
  311. ctx.Data["IsImageFile"] = commit.IsImageFile
  312. // It is possible head repo has been deleted for merged pull requests
  313. if pull.HeadRepo != nil {
  314. ctx.Data["Username"] = pull.HeadUserName
  315. ctx.Data["Reponame"] = pull.HeadRepo.Name
  316. headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
  317. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", endCommitID)
  318. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", startCommitID)
  319. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", endCommitID)
  320. }
  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. ctx.NotFoundOrServerError("GetPullRequestByIssueID", models.IsErrPullRequestNotExist, err)
  336. return
  337. }
  338. if !pr.CanAutoMerge() || pr.HasMerged {
  339. ctx.Handle(404, "MergePullRequest", nil)
  340. return
  341. }
  342. pr.Issue = issue
  343. pr.Issue.Repo = ctx.Repo.Repository
  344. if err = pr.Merge(ctx.User, ctx.Repo.GitRepo); err != nil {
  345. ctx.Handle(500, "Merge", err)
  346. return
  347. }
  348. log.Trace("Pull request merged: %d", pr.ID)
  349. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  350. }
  351. func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
  352. baseRepo := ctx.Repo.Repository
  353. // Get compared branches information
  354. // format: <base branch>...[<head repo>:]<head branch>
  355. // base<-head: master...head:feature
  356. // same repo: master...feature
  357. infos := strings.Split(ctx.Params("*"), "...")
  358. if len(infos) != 2 {
  359. log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)
  360. ctx.Handle(404, "CompareAndPullRequest", nil)
  361. return nil, nil, nil, nil, "", ""
  362. }
  363. baseBranch := infos[0]
  364. ctx.Data["BaseBranch"] = baseBranch
  365. var (
  366. headUser *models.User
  367. headBranch string
  368. isSameRepo bool
  369. err error
  370. )
  371. // If there is no head repository, it means pull request between same repository.
  372. headInfos := strings.Split(infos[1], ":")
  373. if len(headInfos) == 1 {
  374. isSameRepo = true
  375. headUser = ctx.Repo.Owner
  376. headBranch = headInfos[0]
  377. } else if len(headInfos) == 2 {
  378. headUser, err = models.GetUserByName(headInfos[0])
  379. if err != nil {
  380. ctx.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
  381. return nil, nil, nil, nil, "", ""
  382. }
  383. headBranch = headInfos[1]
  384. isSameRepo = headUser.ID == baseRepo.OwnerID
  385. } else {
  386. ctx.Handle(404, "CompareAndPullRequest", nil)
  387. return nil, nil, nil, nil, "", ""
  388. }
  389. ctx.Data["HeadUser"] = headUser
  390. ctx.Data["HeadBranch"] = headBranch
  391. ctx.Repo.PullRequest.SameRepo = isSameRepo
  392. // Check if base branch is valid.
  393. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  394. ctx.Handle(404, "IsBranchExist", nil)
  395. return nil, nil, nil, nil, "", ""
  396. }
  397. var (
  398. headRepo *models.Repository
  399. headGitRepo *git.Repository
  400. )
  401. // In case user included redundant head user name for comparison in same repository,
  402. // no need to check the fork relation.
  403. if !isSameRepo {
  404. var has bool
  405. headRepo, has = models.HasForkedRepo(headUser.ID, baseRepo.ID)
  406. if !has {
  407. log.Trace("ParseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
  408. ctx.Handle(404, "ParseCompareInfo", nil)
  409. return nil, nil, nil, nil, "", ""
  410. }
  411. headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  412. if err != nil {
  413. ctx.Handle(500, "OpenRepository", err)
  414. return nil, nil, nil, nil, "", ""
  415. }
  416. } else {
  417. headRepo = ctx.Repo.Repository
  418. headGitRepo = ctx.Repo.GitRepo
  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. setEditorconfigIfExists(ctx)
  528. if ctx.Written() {
  529. return
  530. }
  531. ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"
  532. ctx.HTML(200, COMPARE_PULL)
  533. }
  534. func CompareAndPullRequestPost(ctx *context.Context, f form.NewIssue) {
  535. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  536. ctx.Data["PageIsComparePull"] = true
  537. ctx.Data["IsDiffCompare"] = true
  538. ctx.Data["RequireHighlightJS"] = true
  539. renderAttachmentSettings(ctx)
  540. var (
  541. repo = ctx.Repo.Repository
  542. attachments []string
  543. )
  544. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  545. if ctx.Written() {
  546. return
  547. }
  548. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, f)
  549. if ctx.Written() {
  550. return
  551. }
  552. if setting.AttachmentEnabled {
  553. attachments = f.Files
  554. }
  555. if ctx.HasError() {
  556. form.Assign(f, ctx.Data)
  557. // This stage is already stop creating new pull request, so it does not matter if it has
  558. // something to compare or not.
  559. PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  560. if ctx.Written() {
  561. return
  562. }
  563. ctx.HTML(200, COMPARE_PULL)
  564. return
  565. }
  566. patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
  567. if err != nil {
  568. ctx.Handle(500, "GetPatch", err)
  569. return
  570. }
  571. pullIssue := &models.Issue{
  572. RepoID: repo.ID,
  573. Index: repo.NextIssueIndex(),
  574. Title: f.Title,
  575. PosterID: ctx.User.ID,
  576. Poster: ctx.User,
  577. MilestoneID: milestoneID,
  578. AssigneeID: assigneeID,
  579. IsPull: true,
  580. Content: f.Content,
  581. }
  582. pullRequest := &models.PullRequest{
  583. HeadRepoID: headRepo.ID,
  584. BaseRepoID: repo.ID,
  585. HeadUserName: headUser.Name,
  586. HeadBranch: headBranch,
  587. BaseBranch: baseBranch,
  588. HeadRepo: headRepo,
  589. BaseRepo: repo,
  590. MergeBase: prInfo.MergeBase,
  591. Type: models.PULL_REQUEST_GOGS,
  592. }
  593. // FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
  594. // instead of 500.
  595. if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch); err != nil {
  596. ctx.Handle(500, "NewPullRequest", err)
  597. return
  598. } else if err := pullRequest.PushToBaseRepo(); err != nil {
  599. ctx.Handle(500, "PushToBaseRepo", err)
  600. return
  601. }
  602. log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
  603. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
  604. }
  605. func parseOwnerAndRepo(ctx *context.Context) (*models.User, *models.Repository) {
  606. owner, err := models.GetUserByName(ctx.Params(":username"))
  607. if err != nil {
  608. ctx.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
  609. return nil, nil
  610. }
  611. repo, err := models.GetRepositoryByName(owner.ID, ctx.Params(":reponame"))
  612. if err != nil {
  613. ctx.NotFoundOrServerError("GetRepositoryByName", models.IsErrRepoNotExist, err)
  614. return nil, nil
  615. }
  616. return owner, repo
  617. }
  618. func TriggerTask(ctx *context.Context) {
  619. pusherID := ctx.QueryInt64("pusher")
  620. branch := ctx.Query("branch")
  621. secret := ctx.Query("secret")
  622. if len(branch) == 0 || len(secret) == 0 || pusherID <= 0 {
  623. ctx.Error(404)
  624. log.Trace("TriggerTask: branch or secret is empty, or pusher ID is not valid")
  625. return
  626. }
  627. owner, repo := parseOwnerAndRepo(ctx)
  628. if ctx.Written() {
  629. return
  630. }
  631. if secret != base.EncodeMD5(owner.Salt) {
  632. ctx.Error(404)
  633. log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
  634. return
  635. }
  636. pusher, err := models.GetUserByID(pusherID)
  637. if err != nil {
  638. ctx.NotFoundOrServerError("GetUserByID", errors.IsUserNotExist, err)
  639. return
  640. }
  641. log.Trace("TriggerTask '%s/%s' by '%s'", repo.Name, branch, pusher.Name)
  642. go models.HookQueue.Add(repo.ID)
  643. go models.AddTestPullRequestTask(pusher, repo.ID, branch, true)
  644. ctx.Status(202)
  645. }