pull.go 20 KB

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