pull.go 20 KB

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