pull.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package repo
  7. import (
  8. "container/list"
  9. "gitote/gitote/models"
  10. "gitote/gitote/models/errors"
  11. "gitote/gitote/pkg/context"
  12. "gitote/gitote/pkg/form"
  13. "gitote/gitote/pkg/setting"
  14. "gitote/gitote/pkg/tool"
  15. "path"
  16. "strings"
  17. "gitlab.com/gitote/com"
  18. "gitlab.com/gitote/git-module"
  19. log "gopkg.in/clog.v1"
  20. )
  21. const (
  22. // ForkTPL page template
  23. ForkTPL = "repo/pulls/fork"
  24. // ComparePullTPL page template
  25. ComparePullTPL = "repo/pulls/compare"
  26. // PullCommitsTPL page template
  27. PullCommitsTPL = "repo/pulls/commits"
  28. // PullFilesTPL page template
  29. PullFilesTPL = "repo/pulls/files"
  30. // PullRequestTemplateKeyTPL page template
  31. PullRequestTemplateKeyTPL = "PullRequestTemplate"
  32. )
  33. var (
  34. PullRequestTemplateCandidates = []string{
  35. "PULL_REQUEST.md",
  36. ".gitote/PULL_REQUEST.md",
  37. ".github/PULL_REQUEST.md",
  38. }
  39. )
  40. func parseBaseRepository(c *context.Context) *models.Repository {
  41. baseRepo, err := models.GetRepositoryByID(c.ParamsInt64(":repoid"))
  42. if err != nil {
  43. c.NotFoundOrServerError("GetRepositoryByID", errors.IsRepoNotExist, err)
  44. return nil
  45. }
  46. if !baseRepo.CanBeForked() || !baseRepo.HasAccess(c.User.ID) {
  47. c.NotFound()
  48. return nil
  49. }
  50. c.Data["repo_name"] = baseRepo.Name
  51. c.Data["description"] = baseRepo.Description
  52. c.Data["IsPrivate"] = baseRepo.IsPrivate
  53. if err = baseRepo.GetOwner(); err != nil {
  54. c.ServerError("GetOwner", err)
  55. return nil
  56. }
  57. c.Data["ForkFrom"] = baseRepo.Owner.Name + "/" + baseRepo.Name
  58. if err := c.User.GetOrganizations(true); err != nil {
  59. c.ServerError("GetOrganizations", err)
  60. return nil
  61. }
  62. c.Data["Orgs"] = c.User.Orgs
  63. return baseRepo
  64. }
  65. // Fork render repository fork page
  66. func Fork(c *context.Context) {
  67. c.Data["Title"] = c.Tr("new_fork")
  68. parseBaseRepository(c)
  69. if c.Written() {
  70. return
  71. }
  72. c.Data["ContextUser"] = c.User
  73. c.Success(ForkTPL)
  74. }
  75. // ForkPost forks a repo
  76. func ForkPost(c *context.Context, f form.CreateRepo) {
  77. c.Data["Title"] = c.Tr("new_fork")
  78. baseRepo := parseBaseRepository(c)
  79. if c.Written() {
  80. return
  81. }
  82. ctxUser := checkContextUser(c, f.UserID)
  83. if c.Written() {
  84. return
  85. }
  86. c.Data["ContextUser"] = ctxUser
  87. if c.HasError() {
  88. c.Success(ForkTPL)
  89. return
  90. }
  91. repo, has, err := models.HasForkedRepo(ctxUser.ID, baseRepo.ID)
  92. if err != nil {
  93. c.ServerError("HasForkedRepo", err)
  94. return
  95. } else if has {
  96. c.Redirect(repo.Link())
  97. return
  98. }
  99. // Check ownership of organization.
  100. if ctxUser.IsOrganization() && !ctxUser.IsOwnedBy(c.User.ID) {
  101. c.Error(403)
  102. return
  103. }
  104. // Cannot fork to same owner
  105. if ctxUser.ID == baseRepo.OwnerID {
  106. c.RenderWithErr(c.Tr("repo.settings.cannot_fork_to_same_owner"), ForkTPL, &f)
  107. return
  108. }
  109. repo, err = models.ForkRepository(c.User, ctxUser, baseRepo, f.RepoName, f.Description)
  110. if err != nil {
  111. c.Data["Err_RepoName"] = true
  112. switch {
  113. case errors.IsReachLimitOfRepo(err):
  114. c.RenderWithErr(c.Tr("repo.form.reach_limit_of_creation", c.User.RepoCreationNum()), ForkTPL, &f)
  115. case models.IsErrRepoAlreadyExist(err):
  116. c.RenderWithErr(c.Tr("repo.settings.new_owner_has_same_repo"), ForkTPL, &f)
  117. case models.IsErrNameReserved(err):
  118. c.RenderWithErr(c.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), ForkTPL, &f)
  119. case models.IsErrNamePatternNotAllowed(err):
  120. c.RenderWithErr(c.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), ForkTPL, &f)
  121. default:
  122. c.ServerError("ForkPost", err)
  123. }
  124. return
  125. }
  126. log.Trace("Repository forked from '%s' -> '%s'", baseRepo.FullName(), repo.FullName())
  127. c.Redirect(repo.Link())
  128. }
  129. func checkPullInfo(c *context.Context) *models.Issue {
  130. issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  131. if err != nil {
  132. c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
  133. return nil
  134. }
  135. c.Data["Title"] = issue.Title
  136. c.Data["Issue"] = issue
  137. if !issue.IsPull {
  138. c.Handle(404, "ViewPullCommits", nil)
  139. return nil
  140. }
  141. if c.IsLogged {
  142. // Update issue-user.
  143. if err = issue.ReadBy(c.User.ID); err != nil {
  144. c.ServerError("ReadBy", err)
  145. return nil
  146. }
  147. }
  148. return issue
  149. }
  150. // PrepareMergedViewPullInfo show meta information for a merged pull request view page
  151. func PrepareMergedViewPullInfo(c *context.Context, issue *models.Issue) {
  152. pull := issue.PullRequest
  153. c.Data["HasMerged"] = true
  154. c.Data["HeadTarget"] = issue.PullRequest.HeadUserName + "/" + pull.HeadBranch
  155. c.Data["BaseTarget"] = c.Repo.Owner.Name + "/" + pull.BaseBranch
  156. var err error
  157. c.Data["NumCommits"], err = c.Repo.GitRepo.CommitsCountBetween(pull.MergeBase, pull.MergedCommitID)
  158. if err != nil {
  159. c.ServerError("Repo.GitRepo.CommitsCountBetween", err)
  160. return
  161. }
  162. c.Data["NumFiles"], err = c.Repo.GitRepo.FilesCountBetween(pull.MergeBase, pull.MergedCommitID)
  163. if err != nil {
  164. c.ServerError("Repo.GitRepo.FilesCountBetween", err)
  165. return
  166. }
  167. }
  168. // PrepareViewPullInfo show meta information for a pull request preview page
  169. func PrepareViewPullInfo(c *context.Context, issue *models.Issue) *git.PullRequestInfo {
  170. repo := c.Repo.Repository
  171. pull := issue.PullRequest
  172. c.Data["HeadTarget"] = pull.HeadUserName + "/" + pull.HeadBranch
  173. c.Data["BaseTarget"] = c.Repo.Owner.Name + "/" + pull.BaseBranch
  174. var (
  175. headGitRepo *git.Repository
  176. err error
  177. )
  178. if pull.HeadRepo != nil {
  179. headGitRepo, err = git.OpenRepository(pull.HeadRepo.RepoPath())
  180. if err != nil {
  181. c.ServerError("OpenRepository", err)
  182. return nil
  183. }
  184. }
  185. if pull.HeadRepo == nil || !headGitRepo.IsBranchExist(pull.HeadBranch) {
  186. c.Data["IsPullReuqestBroken"] = true
  187. c.Data["HeadTarget"] = "deleted"
  188. c.Data["NumCommits"] = 0
  189. c.Data["NumFiles"] = 0
  190. return nil
  191. }
  192. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name),
  193. pull.BaseBranch, pull.HeadBranch)
  194. if err != nil {
  195. if strings.Contains(err.Error(), "fatal: Not a valid object name") {
  196. c.Data["IsPullReuqestBroken"] = true
  197. c.Data["BaseTarget"] = "deleted"
  198. c.Data["NumCommits"] = 0
  199. c.Data["NumFiles"] = 0
  200. return nil
  201. }
  202. c.ServerError("GetPullRequestInfo", err)
  203. return nil
  204. }
  205. c.Data["NumCommits"] = prInfo.Commits.Len()
  206. c.Data["NumFiles"] = prInfo.NumFiles
  207. return prInfo
  208. }
  209. // ViewPullCommits show commits for a pull request
  210. func ViewPullCommits(c *context.Context) {
  211. c.Data["PageIsPullList"] = true
  212. c.Data["PageIsPullCommits"] = true
  213. issue := checkPullInfo(c)
  214. if c.Written() {
  215. return
  216. }
  217. pull := issue.PullRequest
  218. if pull.HeadRepo != nil {
  219. c.Data["Username"] = pull.HeadUserName
  220. c.Data["Reponame"] = pull.HeadRepo.Name
  221. }
  222. var commits *list.List
  223. if pull.HasMerged {
  224. PrepareMergedViewPullInfo(c, issue)
  225. if c.Written() {
  226. return
  227. }
  228. startCommit, err := c.Repo.GitRepo.GetCommit(pull.MergeBase)
  229. if err != nil {
  230. c.ServerError("Repo.GitRepo.GetCommit", err)
  231. return
  232. }
  233. endCommit, err := c.Repo.GitRepo.GetCommit(pull.MergedCommitID)
  234. if err != nil {
  235. c.ServerError("Repo.GitRepo.GetCommit", err)
  236. return
  237. }
  238. commits, err = c.Repo.GitRepo.CommitsBetween(endCommit, startCommit)
  239. if err != nil {
  240. c.ServerError("Repo.GitRepo.CommitsBetween", err)
  241. return
  242. }
  243. } else {
  244. prInfo := PrepareViewPullInfo(c, issue)
  245. if c.Written() {
  246. return
  247. } else if prInfo == nil {
  248. c.NotFound()
  249. return
  250. }
  251. commits = prInfo.Commits
  252. }
  253. commits = models.ValidateCommitsWithEmails(commits)
  254. c.Data["Commits"] = commits
  255. c.Data["CommitsCount"] = commits.Len()
  256. c.Success(PullCommitsTPL)
  257. }
  258. // ViewPullFiles render pull request changed files list page
  259. func ViewPullFiles(c *context.Context) {
  260. c.Data["PageIsPullList"] = true
  261. c.Data["PageIsPullFiles"] = true
  262. issue := checkPullInfo(c)
  263. if c.Written() {
  264. return
  265. }
  266. pull := issue.PullRequest
  267. var (
  268. diffRepoPath string
  269. startCommitID string
  270. endCommitID string
  271. gitRepo *git.Repository
  272. )
  273. if pull.HasMerged {
  274. PrepareMergedViewPullInfo(c, issue)
  275. if c.Written() {
  276. return
  277. }
  278. diffRepoPath = c.Repo.GitRepo.Path
  279. startCommitID = pull.MergeBase
  280. endCommitID = pull.MergedCommitID
  281. gitRepo = c.Repo.GitRepo
  282. } else {
  283. prInfo := PrepareViewPullInfo(c, issue)
  284. if c.Written() {
  285. return
  286. } else if prInfo == nil {
  287. c.Handle(404, "ViewPullFiles", nil)
  288. return
  289. }
  290. headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
  291. headGitRepo, err := git.OpenRepository(headRepoPath)
  292. if err != nil {
  293. c.ServerError("OpenRepository", err)
  294. return
  295. }
  296. headCommitID, err := headGitRepo.GetBranchCommitID(pull.HeadBranch)
  297. if err != nil {
  298. c.ServerError("GetBranchCommitID", err)
  299. return
  300. }
  301. diffRepoPath = headRepoPath
  302. startCommitID = prInfo.MergeBase
  303. endCommitID = headCommitID
  304. gitRepo = headGitRepo
  305. }
  306. diff, err := models.GetDiffRange(diffRepoPath,
  307. startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
  308. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  309. if err != nil {
  310. c.ServerError("GetDiffRange", err)
  311. return
  312. }
  313. c.Data["Diff"] = diff
  314. c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  315. commit, err := gitRepo.GetCommit(endCommitID)
  316. if err != nil {
  317. c.ServerError("GetCommit", err)
  318. return
  319. }
  320. setEditorconfigIfExists(c)
  321. if c.Written() {
  322. return
  323. }
  324. c.Data["IsSplitStyle"] = c.Query("style") == "split"
  325. c.Data["IsImageFile"] = commit.IsImageFile
  326. // It is possible head repo has been deleted for merged pull requests
  327. if pull.HeadRepo != nil {
  328. c.Data["Username"] = pull.HeadUserName
  329. c.Data["Reponame"] = pull.HeadRepo.Name
  330. headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
  331. c.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", endCommitID)
  332. c.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", startCommitID)
  333. c.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", endCommitID)
  334. }
  335. c.Data["RequireHighlightJS"] = true
  336. c.Success(PullFilesTPL)
  337. }
  338. // MergePullRequest response for merging pull request
  339. func MergePullRequest(c *context.Context) {
  340. issue := checkPullInfo(c)
  341. if c.Written() {
  342. return
  343. }
  344. if issue.IsClosed {
  345. c.NotFound()
  346. return
  347. }
  348. pr, err := models.GetPullRequestByIssueID(issue.ID)
  349. if err != nil {
  350. c.NotFoundOrServerError("GetPullRequestByIssueID", models.IsErrPullRequestNotExist, err)
  351. return
  352. }
  353. if !pr.CanAutoMerge() || pr.HasMerged {
  354. c.NotFound()
  355. return
  356. }
  357. pr.Issue = issue
  358. pr.Issue.Repo = c.Repo.Repository
  359. if err = pr.Merge(c.User, c.Repo.GitRepo, models.MergeStyle(c.Query("merge_style")), c.Query("commit_description")); err != nil {
  360. c.ServerError("Merge", err)
  361. return
  362. }
  363. log.Trace("Pull request merged: %d", pr.ID)
  364. c.Redirect(c.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  365. }
  366. // ParseCompareInfo parse compare info between two commit for preparing pull request
  367. func ParseCompareInfo(c *context.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
  368. baseRepo := c.Repo.Repository
  369. // Get compared branches information
  370. // format: <base branch>...[<head repo>:]<head branch>
  371. // base<-head: master...head:feature
  372. // same repo: master...feature
  373. infos := strings.Split(c.Params("*"), "...")
  374. if len(infos) != 2 {
  375. log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)
  376. c.NotFound()
  377. return nil, nil, nil, nil, "", ""
  378. }
  379. baseBranch := infos[0]
  380. c.Data["BaseBranch"] = baseBranch
  381. var (
  382. headUser *models.User
  383. headBranch string
  384. isSameRepo bool
  385. err error
  386. )
  387. // If there is no head repository, it means pull request between same repository.
  388. headInfos := strings.Split(infos[1], ":")
  389. if len(headInfos) == 1 {
  390. isSameRepo = true
  391. headUser = c.Repo.Owner
  392. headBranch = headInfos[0]
  393. } else if len(headInfos) == 2 {
  394. headUser, err = models.GetUserByName(headInfos[0])
  395. if err != nil {
  396. c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
  397. return nil, nil, nil, nil, "", ""
  398. }
  399. headBranch = headInfos[1]
  400. isSameRepo = headUser.ID == baseRepo.OwnerID
  401. } else {
  402. c.NotFound()
  403. return nil, nil, nil, nil, "", ""
  404. }
  405. c.Data["HeadUser"] = headUser
  406. c.Data["HeadBranch"] = headBranch
  407. c.Repo.PullRequest.SameRepo = isSameRepo
  408. // Check if base branch is valid.
  409. if !c.Repo.GitRepo.IsBranchExist(baseBranch) {
  410. c.NotFound()
  411. return nil, nil, nil, nil, "", ""
  412. }
  413. var (
  414. headRepo *models.Repository
  415. headGitRepo *git.Repository
  416. )
  417. // In case user included redundant head user name for comparison in same repository,
  418. // no need to check the fork relation.
  419. if !isSameRepo {
  420. var has bool
  421. headRepo, has, err = models.HasForkedRepo(headUser.ID, baseRepo.ID)
  422. if err != nil {
  423. c.ServerError("HasForkedRepo", err)
  424. return nil, nil, nil, nil, "", ""
  425. } else if !has {
  426. log.Trace("ParseCompareInfo [base_repo_id: %d]: does not have fork or in same repository", baseRepo.ID)
  427. c.NotFound()
  428. return nil, nil, nil, nil, "", ""
  429. }
  430. headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  431. if err != nil {
  432. c.ServerError("OpenRepository", err)
  433. return nil, nil, nil, nil, "", ""
  434. }
  435. } else {
  436. headRepo = c.Repo.Repository
  437. headGitRepo = c.Repo.GitRepo
  438. }
  439. if !c.User.IsWriterOfRepo(headRepo) && !c.User.IsAdmin {
  440. log.Trace("ParseCompareInfo [base_repo_id: %d]: does not have write access or site admin", baseRepo.ID)
  441. c.NotFound()
  442. return nil, nil, nil, nil, "", ""
  443. }
  444. // Check if head branch is valid.
  445. if !headGitRepo.IsBranchExist(headBranch) {
  446. c.NotFound()
  447. return nil, nil, nil, nil, "", ""
  448. }
  449. headBranches, err := headGitRepo.GetBranches()
  450. if err != nil {
  451. c.ServerError("GetBranches", err)
  452. return nil, nil, nil, nil, "", ""
  453. }
  454. c.Data["HeadBranches"] = headBranches
  455. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
  456. if err != nil {
  457. if git.IsErrNoMergeBase(err) {
  458. c.Data["IsNoMergeBase"] = true
  459. c.Success(ComparePullTPL)
  460. } else {
  461. c.ServerError("GetPullRequestInfo", err)
  462. }
  463. return nil, nil, nil, nil, "", ""
  464. }
  465. c.Data["BeforeCommitID"] = prInfo.MergeBase
  466. return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
  467. }
  468. // PrepareCompareDiff render pull request preview diff page
  469. func PrepareCompareDiff(
  470. c *context.Context,
  471. headUser *models.User,
  472. headRepo *models.Repository,
  473. headGitRepo *git.Repository,
  474. prInfo *git.PullRequestInfo,
  475. baseBranch, headBranch string) bool {
  476. var (
  477. repo = c.Repo.Repository
  478. err error
  479. )
  480. // Get diff information.
  481. c.Data["CommitRepoLink"] = headRepo.Link()
  482. headCommitID, err := headGitRepo.GetBranchCommitID(headBranch)
  483. if err != nil {
  484. c.ServerError("GetBranchCommitID", err)
  485. return false
  486. }
  487. c.Data["AfterCommitID"] = headCommitID
  488. if headCommitID == prInfo.MergeBase {
  489. c.Data["IsNothingToCompare"] = true
  490. return true
  491. }
  492. diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
  493. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
  494. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  495. if err != nil {
  496. c.ServerError("GetDiffRange", err)
  497. return false
  498. }
  499. c.Data["Diff"] = diff
  500. c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  501. headCommit, err := headGitRepo.GetCommit(headCommitID)
  502. if err != nil {
  503. c.ServerError("GetCommit", err)
  504. return false
  505. }
  506. prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
  507. c.Data["Commits"] = prInfo.Commits
  508. c.Data["CommitCount"] = prInfo.Commits.Len()
  509. c.Data["Username"] = headUser.Name
  510. c.Data["Reponame"] = headRepo.Name
  511. c.Data["IsImageFile"] = headCommit.IsImageFile
  512. headTarget := path.Join(headUser.Name, repo.Name)
  513. c.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", headCommitID)
  514. c.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", prInfo.MergeBase)
  515. c.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", headCommitID)
  516. return false
  517. }
  518. // CompareAndPullRequest render pull request preview page
  519. func CompareAndPullRequest(c *context.Context) {
  520. c.Data["Title"] = c.Tr("repo.pulls.compare_changes")
  521. c.Data["PageIsComparePull"] = true
  522. c.Data["IsDiffCompare"] = true
  523. c.Data["RequireHighlightJS"] = true
  524. setTemplateIfExists(c, PullRequestTemplateKeyTPL, PullRequestTemplateCandidates)
  525. renderAttachmentSettings(c)
  526. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(c)
  527. if c.Written() {
  528. return
  529. }
  530. pr, err := models.GetUnmergedPullRequest(headRepo.ID, c.Repo.Repository.ID, headBranch, baseBranch)
  531. if err != nil {
  532. if !models.IsErrPullRequestNotExist(err) {
  533. c.ServerError("GetUnmergedPullRequest", err)
  534. return
  535. }
  536. } else {
  537. c.Data["HasPullRequest"] = true
  538. c.Data["PullRequest"] = pr
  539. c.Success(ComparePullTPL)
  540. return
  541. }
  542. nothingToCompare := PrepareCompareDiff(c, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  543. if c.Written() {
  544. return
  545. }
  546. if !nothingToCompare {
  547. // Setup information for new form.
  548. RetrieveRepoMetas(c, c.Repo.Repository)
  549. if c.Written() {
  550. return
  551. }
  552. }
  553. setEditorconfigIfExists(c)
  554. if c.Written() {
  555. return
  556. }
  557. c.Data["IsSplitStyle"] = c.Query("style") == "split"
  558. c.Success(ComparePullTPL)
  559. }
  560. // CompareAndPullRequestPost compares branches/forks and create a new pull request
  561. func CompareAndPullRequestPost(c *context.Context, f form.NewIssue) {
  562. c.Data["Title"] = c.Tr("repo.pulls.compare_changes")
  563. c.Data["PageIsComparePull"] = true
  564. c.Data["IsDiffCompare"] = true
  565. c.Data["RequireHighlightJS"] = true
  566. renderAttachmentSettings(c)
  567. var (
  568. repo = c.Repo.Repository
  569. attachments []string
  570. )
  571. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(c)
  572. if c.Written() {
  573. return
  574. }
  575. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(c, f)
  576. if c.Written() {
  577. return
  578. }
  579. if setting.AttachmentEnabled {
  580. attachments = f.Files
  581. }
  582. if c.HasError() {
  583. form.Assign(f, c.Data)
  584. // This stage is already stop creating new pull request, so it does not matter if it has
  585. // something to compare or not.
  586. PrepareCompareDiff(c, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  587. if c.Written() {
  588. return
  589. }
  590. c.Success(ComparePullTPL)
  591. return
  592. }
  593. patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
  594. if err != nil {
  595. c.ServerError("GetPatch", err)
  596. return
  597. }
  598. pullIssue := &models.Issue{
  599. RepoID: repo.ID,
  600. Index: repo.NextIssueIndex(),
  601. Title: f.Title,
  602. PosterID: c.User.ID,
  603. Poster: c.User,
  604. MilestoneID: milestoneID,
  605. AssigneeID: assigneeID,
  606. IsPull: true,
  607. Content: f.Content,
  608. }
  609. pullRequest := &models.PullRequest{
  610. HeadRepoID: headRepo.ID,
  611. BaseRepoID: repo.ID,
  612. HeadUserName: headUser.Name,
  613. HeadBranch: headBranch,
  614. BaseBranch: baseBranch,
  615. HeadRepo: headRepo,
  616. BaseRepo: repo,
  617. MergeBase: prInfo.MergeBase,
  618. Type: models.PullRequestGitote,
  619. }
  620. // FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
  621. // instead of 500.
  622. if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch); err != nil {
  623. c.ServerError("NewPullRequest", err)
  624. return
  625. } else if err := pullRequest.PushToBaseRepo(); err != nil {
  626. c.ServerError("PushToBaseRepo", err)
  627. return
  628. }
  629. log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
  630. c.Redirect(c.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
  631. }
  632. func parseOwnerAndRepo(c *context.Context) (*models.User, *models.Repository) {
  633. owner, err := models.GetUserByName(c.Params(":username"))
  634. if err != nil {
  635. c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
  636. return nil, nil
  637. }
  638. repo, err := models.GetRepositoryByName(owner.ID, c.Params(":reponame"))
  639. if err != nil {
  640. c.NotFoundOrServerError("GetRepositoryByName", errors.IsRepoNotExist, err)
  641. return nil, nil
  642. }
  643. return owner, repo
  644. }
  645. // TriggerTask response for a trigger task request
  646. func TriggerTask(c *context.Context) {
  647. pusherID := c.QueryInt64("pusher")
  648. branch := c.Query("branch")
  649. secret := c.Query("secret")
  650. if len(branch) == 0 || len(secret) == 0 || pusherID <= 0 {
  651. c.Error(404)
  652. log.Trace("TriggerTask: branch or secret is empty, or pusher ID is not valid")
  653. return
  654. }
  655. owner, repo := parseOwnerAndRepo(c)
  656. if c.Written() {
  657. return
  658. }
  659. if secret != tool.MD5(owner.Salt) {
  660. c.Error(404)
  661. log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
  662. return
  663. }
  664. pusher, err := models.GetUserByID(pusherID)
  665. if err != nil {
  666. c.NotFoundOrServerError("GetUserByID", errors.IsUserNotExist, err)
  667. return
  668. }
  669. log.Trace("TriggerTask '%s/%s' by '%s'", repo.Name, branch, pusher.Name)
  670. go models.HookQueue.Add(repo.ID)
  671. go models.AddTestPullRequestTask(pusher, repo.ID, branch, true)
  672. c.Status(202)
  673. }