pull.go 19 KB

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