pull.go 20 KB

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