pull.go 19 KB

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