You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pull.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. "errors"
  8. "path"
  9. "strings"
  10. "github.com/Unknwon/com"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/auth"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/git"
  15. "github.com/gogits/gogs/modules/log"
  16. "github.com/gogits/gogs/modules/middleware"
  17. "github.com/gogits/gogs/modules/setting"
  18. )
  19. const (
  20. FORK base.TplName = "repo/pulls/fork"
  21. COMPARE_PULL base.TplName = "repo/pulls/compare"
  22. PULL_COMMITS base.TplName = "repo/pulls/commits"
  23. PULL_FILES base.TplName = "repo/pulls/files"
  24. )
  25. func getForkRepository(ctx *middleware.Context) *models.Repository {
  26. forkRepo, err := models.GetRepositoryByID(ctx.ParamsInt64(":repoid"))
  27. if err != nil {
  28. if models.IsErrRepoNotExist(err) {
  29. ctx.Handle(404, "GetRepositoryByID", nil)
  30. } else {
  31. ctx.Handle(500, "GetRepositoryByID", err)
  32. }
  33. return nil
  34. }
  35. if !forkRepo.CanBeForked() {
  36. ctx.Handle(404, "getForkRepository", nil)
  37. return nil
  38. }
  39. ctx.Data["repo_name"] = forkRepo.Name
  40. ctx.Data["desc"] = forkRepo.Description
  41. ctx.Data["IsPrivate"] = forkRepo.IsPrivate
  42. if err = forkRepo.GetOwner(); err != nil {
  43. ctx.Handle(500, "GetOwner", err)
  44. return nil
  45. }
  46. ctx.Data["ForkFrom"] = forkRepo.Owner.Name + "/" + forkRepo.Name
  47. if err := ctx.User.GetOrganizations(); err != nil {
  48. ctx.Handle(500, "GetOrganizations", err)
  49. return nil
  50. }
  51. ctx.Data["Orgs"] = ctx.User.Orgs
  52. return forkRepo
  53. }
  54. func Fork(ctx *middleware.Context) {
  55. ctx.Data["Title"] = ctx.Tr("new_fork")
  56. getForkRepository(ctx)
  57. if ctx.Written() {
  58. return
  59. }
  60. ctx.Data["ContextUser"] = ctx.User
  61. ctx.HTML(200, FORK)
  62. }
  63. func ForkPost(ctx *middleware.Context, form auth.CreateRepoForm) {
  64. ctx.Data["Title"] = ctx.Tr("new_fork")
  65. forkRepo := getForkRepository(ctx)
  66. if ctx.Written() {
  67. return
  68. }
  69. ctxUser := checkContextUser(ctx, form.Uid)
  70. if ctx.Written() {
  71. return
  72. }
  73. ctx.Data["ContextUser"] = ctxUser
  74. if ctx.HasError() {
  75. ctx.HTML(200, FORK)
  76. return
  77. }
  78. repo, has := models.HasForkedRepo(ctxUser.Id, forkRepo.ID)
  79. if has {
  80. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  81. return
  82. }
  83. // Check ownership of organization.
  84. if ctxUser.IsOrganization() {
  85. if !ctxUser.IsOwnedBy(ctx.User.Id) {
  86. ctx.Error(403)
  87. return
  88. }
  89. }
  90. repo, err := models.ForkRepository(ctxUser, forkRepo, form.RepoName, form.Description)
  91. if err != nil {
  92. ctx.Data["Err_RepoName"] = true
  93. switch {
  94. case models.IsErrRepoAlreadyExist(err):
  95. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), FORK, &form)
  96. case models.IsErrNameReserved(err):
  97. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &form)
  98. case models.IsErrNamePatternNotAllowed(err):
  99. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), FORK, &form)
  100. default:
  101. ctx.Handle(500, "ForkPost", err)
  102. }
  103. return
  104. }
  105. log.Trace("Repository forked[%d]: %s/%s", forkRepo.ID, ctxUser.Name, repo.Name)
  106. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  107. }
  108. func checkPullInfo(ctx *middleware.Context) *models.Issue {
  109. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  110. if err != nil {
  111. if models.IsErrIssueNotExist(err) {
  112. ctx.Handle(404, "GetIssueByIndex", err)
  113. } else {
  114. ctx.Handle(500, "GetIssueByIndex", err)
  115. }
  116. return nil
  117. }
  118. ctx.Data["Title"] = issue.Name
  119. ctx.Data["Issue"] = issue
  120. if !issue.IsPull {
  121. ctx.Handle(404, "ViewPullCommits", nil)
  122. return nil
  123. }
  124. if err = issue.GetPoster(); err != nil {
  125. ctx.Handle(500, "GetPoster", err)
  126. return nil
  127. } else if issue.GetHeadRepo(); err != nil {
  128. ctx.Handle(500, "GetHeadRepo", err)
  129. return nil
  130. }
  131. if ctx.IsSigned {
  132. // Update issue-user.
  133. if err = issue.ReadBy(ctx.User.Id); err != nil {
  134. ctx.Handle(500, "ReadBy", err)
  135. return nil
  136. }
  137. }
  138. return issue
  139. }
  140. func PrepareMergedViewPullInfo(ctx *middleware.Context, pull *models.Issue) {
  141. ctx.Data["HasMerged"] = true
  142. var err error
  143. if err = pull.GetMerger(); err != nil {
  144. ctx.Handle(500, "GetMerger", err)
  145. return
  146. }
  147. ctx.Data["HeadTarget"] = pull.HeadUserName + "/" + pull.HeadBranch
  148. ctx.Data["BaseTarget"] = ctx.Repo.Owner.Name + "/" + pull.BaseBranch
  149. ctx.Data["NumCommits"], err = ctx.Repo.GitRepo.CommitsCountBetween(pull.MergeBase, pull.MergedCommitID)
  150. if err != nil {
  151. ctx.Handle(500, "Repo.GitRepo.CommitsCountBetween", err)
  152. return
  153. }
  154. ctx.Data["NumFiles"], err = ctx.Repo.GitRepo.FilesCountBetween(pull.MergeBase, pull.MergedCommitID)
  155. if err != nil {
  156. ctx.Handle(500, "Repo.GitRepo.FilesCountBetween", err)
  157. return
  158. }
  159. }
  160. func PrepareViewPullInfo(ctx *middleware.Context, pull *models.Issue) *git.PullRequestInfo {
  161. repo := ctx.Repo.Repository
  162. ctx.Data["HeadTarget"] = pull.HeadUserName + "/" + pull.HeadBranch
  163. ctx.Data["BaseTarget"] = ctx.Repo.Owner.Name + "/" + pull.BaseBranch
  164. var (
  165. headGitRepo *git.Repository
  166. err error
  167. )
  168. if err = pull.GetHeadRepo(); err != nil {
  169. ctx.Handle(500, "GetHeadRepo", err)
  170. return nil
  171. }
  172. if pull.HeadRepo != nil {
  173. headRepoPath, err := pull.HeadRepo.RepoPath()
  174. if err != nil {
  175. ctx.Handle(500, "HeadRepo.RepoPath", err)
  176. return nil
  177. }
  178. headGitRepo, err = git.OpenRepository(headRepoPath)
  179. if err != nil {
  180. ctx.Handle(500, "OpenRepository", err)
  181. return nil
  182. }
  183. }
  184. if pull.HeadRepo == nil || !headGitRepo.IsBranchExist(pull.HeadBranch) {
  185. ctx.Data["IsPullReuqestBroken"] = true
  186. ctx.Data["HeadTarget"] = "deleted"
  187. ctx.Data["NumCommits"] = 0
  188. ctx.Data["NumFiles"] = 0
  189. return nil
  190. }
  191. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name),
  192. pull.BaseBranch, pull.HeadBranch)
  193. if err != nil {
  194. ctx.Handle(500, "GetPullRequestInfo", err)
  195. return nil
  196. }
  197. ctx.Data["NumCommits"] = prInfo.Commits.Len()
  198. ctx.Data["NumFiles"] = prInfo.NumFiles
  199. return prInfo
  200. }
  201. func ViewPullCommits(ctx *middleware.Context) {
  202. ctx.Data["PageIsPullCommits"] = true
  203. pull := checkPullInfo(ctx)
  204. if ctx.Written() {
  205. return
  206. }
  207. ctx.Data["Username"] = pull.HeadUserName
  208. ctx.Data["Reponame"] = pull.HeadRepo.Name
  209. var commits *list.List
  210. if pull.HasMerged {
  211. PrepareMergedViewPullInfo(ctx, pull)
  212. if ctx.Written() {
  213. return
  214. }
  215. startCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergeBase)
  216. if err != nil {
  217. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  218. return
  219. }
  220. endCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergedCommitID)
  221. if err != nil {
  222. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  223. return
  224. }
  225. commits, err = ctx.Repo.GitRepo.CommitsBetween(endCommit, startCommit)
  226. if err != nil {
  227. ctx.Handle(500, "Repo.GitRepo.CommitsBetween", err)
  228. return
  229. }
  230. } else {
  231. prInfo := PrepareViewPullInfo(ctx, pull)
  232. if ctx.Written() {
  233. return
  234. } else if prInfo == nil {
  235. ctx.Handle(404, "ViewPullCommits", nil)
  236. return
  237. }
  238. commits = prInfo.Commits
  239. }
  240. commits = models.ValidateCommitsWithEmails(commits)
  241. ctx.Data["Commits"] = commits
  242. ctx.Data["CommitCount"] = commits.Len()
  243. ctx.HTML(200, PULL_COMMITS)
  244. }
  245. func ViewPullFiles(ctx *middleware.Context) {
  246. ctx.Data["PageIsPullFiles"] = true
  247. pull := checkPullInfo(ctx)
  248. if ctx.Written() {
  249. return
  250. }
  251. var (
  252. diffRepoPath string
  253. startCommitID string
  254. endCommitID string
  255. gitRepo *git.Repository
  256. )
  257. if pull.HasMerged {
  258. PrepareMergedViewPullInfo(ctx, pull)
  259. if ctx.Written() {
  260. return
  261. }
  262. diffRepoPath = ctx.Repo.GitRepo.Path
  263. startCommitID = pull.MergeBase
  264. endCommitID = pull.MergedCommitID
  265. gitRepo = ctx.Repo.GitRepo
  266. } else {
  267. prInfo := PrepareViewPullInfo(ctx, pull)
  268. if ctx.Written() {
  269. return
  270. } else if prInfo == nil {
  271. ctx.Handle(404, "ViewPullFiles", nil)
  272. return
  273. }
  274. headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
  275. headGitRepo, err := git.OpenRepository(headRepoPath)
  276. if err != nil {
  277. ctx.Handle(500, "OpenRepository", err)
  278. return
  279. }
  280. headCommitID, err := headGitRepo.GetCommitIdOfBranch(pull.HeadBranch)
  281. if err != nil {
  282. ctx.Handle(500, "GetCommitIdOfBranch", err)
  283. return
  284. }
  285. diffRepoPath = headRepoPath
  286. startCommitID = prInfo.MergeBase
  287. endCommitID = headCommitID
  288. gitRepo = headGitRepo
  289. }
  290. diff, err := models.GetDiffRange(diffRepoPath,
  291. startCommitID, endCommitID, setting.Git.MaxGitDiffLines)
  292. if err != nil {
  293. ctx.Handle(500, "GetDiffRange", err)
  294. return
  295. }
  296. ctx.Data["Diff"] = diff
  297. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  298. commit, err := gitRepo.GetCommit(endCommitID)
  299. if err != nil {
  300. ctx.Handle(500, "GetCommit", err)
  301. return
  302. }
  303. headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
  304. ctx.Data["Username"] = pull.HeadUserName
  305. ctx.Data["Reponame"] = pull.HeadRepo.Name
  306. ctx.Data["IsImageFile"] = commit.IsImageFile
  307. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", endCommitID)
  308. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", startCommitID)
  309. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", endCommitID)
  310. ctx.HTML(200, PULL_FILES)
  311. }
  312. func MergePullRequest(ctx *middleware.Context) {
  313. issue := checkPullInfo(ctx)
  314. if ctx.Written() {
  315. return
  316. }
  317. if issue.IsClosed {
  318. ctx.Handle(404, "MergePullRequest", nil)
  319. return
  320. }
  321. pr, err := models.GetPullRequestByIssueID(issue.ID)
  322. if err != nil {
  323. if models.IsErrPullRequestNotExist(err) {
  324. ctx.Handle(404, "GetPullRequestByIssueID", nil)
  325. } else {
  326. ctx.Handle(500, "GetPullRequestByIssueID", err)
  327. }
  328. return
  329. }
  330. if !pr.CanAutoMerge() || pr.HasMerged {
  331. ctx.Handle(404, "MergePullRequest", nil)
  332. return
  333. }
  334. pr.Issue = issue
  335. pr.Issue.Repo = ctx.Repo.Repository
  336. if err = pr.Merge(ctx.User, ctx.Repo.GitRepo); err != nil {
  337. ctx.Handle(500, "Merge", err)
  338. return
  339. }
  340. log.Trace("Pull request merged: %d", pr.ID)
  341. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  342. }
  343. func ParseCompareInfo(ctx *middleware.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
  344. // Get compare branch information.
  345. infos := strings.Split(ctx.Params("*"), "...")
  346. if len(infos) != 2 {
  347. ctx.Handle(404, "CompareAndPullRequest", nil)
  348. return nil, nil, nil, nil, "", ""
  349. }
  350. baseBranch := infos[0]
  351. ctx.Data["BaseBranch"] = baseBranch
  352. headInfos := strings.Split(infos[1], ":")
  353. if len(headInfos) != 2 {
  354. ctx.Handle(404, "CompareAndPullRequest", nil)
  355. return nil, nil, nil, nil, "", ""
  356. }
  357. headUsername := headInfos[0]
  358. headBranch := headInfos[1]
  359. ctx.Data["HeadBranch"] = headBranch
  360. headUser, err := models.GetUserByName(headUsername)
  361. if err != nil {
  362. if models.IsErrUserNotExist(err) {
  363. ctx.Handle(404, "GetUserByName", nil)
  364. } else {
  365. ctx.Handle(500, "GetUserByName", err)
  366. }
  367. return nil, nil, nil, nil, "", ""
  368. }
  369. repo := ctx.Repo.Repository
  370. // Check if base branch is valid.
  371. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  372. ctx.Handle(404, "IsBranchExist", nil)
  373. return nil, nil, nil, nil, "", ""
  374. }
  375. // Check if current user has fork of repository.
  376. headRepo, has := models.HasForkedRepo(headUser.Id, repo.ID)
  377. if !has || !ctx.User.IsAdminOfRepo(headRepo) {
  378. ctx.Handle(404, "HasForkedRepo", nil)
  379. return nil, nil, nil, nil, "", ""
  380. }
  381. headGitRepo, err := git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  382. if err != nil {
  383. ctx.Handle(500, "OpenRepository", err)
  384. return nil, nil, nil, nil, "", ""
  385. }
  386. // Check if head branch is valid.
  387. if !headGitRepo.IsBranchExist(headBranch) {
  388. ctx.Handle(404, "IsBranchExist", nil)
  389. return nil, nil, nil, nil, "", ""
  390. }
  391. headBranches, err := headGitRepo.GetBranches()
  392. if err != nil {
  393. ctx.Handle(500, "GetBranches", err)
  394. return nil, nil, nil, nil, "", ""
  395. }
  396. ctx.Data["HeadBranches"] = headBranches
  397. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name), baseBranch, headBranch)
  398. if err != nil {
  399. ctx.Handle(500, "GetPullRequestInfo", err)
  400. return nil, nil, nil, nil, "", ""
  401. }
  402. ctx.Data["BeforeCommitID"] = prInfo.MergeBase
  403. return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
  404. }
  405. func PrepareCompareDiff(
  406. ctx *middleware.Context,
  407. headUser *models.User,
  408. headRepo *models.Repository,
  409. headGitRepo *git.Repository,
  410. prInfo *git.PullRequestInfo,
  411. baseBranch, headBranch string) bool {
  412. var (
  413. repo = ctx.Repo.Repository
  414. err error
  415. )
  416. // Get diff information.
  417. ctx.Data["CommitRepoLink"], err = headRepo.RepoLink()
  418. if err != nil {
  419. ctx.Handle(500, "RepoLink", err)
  420. return false
  421. }
  422. headCommitID, err := headGitRepo.GetCommitIdOfBranch(headBranch)
  423. if err != nil {
  424. ctx.Handle(500, "GetCommitIdOfBranch", err)
  425. return false
  426. }
  427. ctx.Data["AfterCommitID"] = headCommitID
  428. if headCommitID == prInfo.MergeBase {
  429. ctx.Data["IsNothingToCompare"] = true
  430. return true
  431. }
  432. diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
  433. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines)
  434. if err != nil {
  435. ctx.Handle(500, "GetDiffRange", err)
  436. return false
  437. }
  438. ctx.Data["Diff"] = diff
  439. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  440. headCommit, err := headGitRepo.GetCommit(headCommitID)
  441. if err != nil {
  442. ctx.Handle(500, "GetCommit", err)
  443. return false
  444. }
  445. prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
  446. ctx.Data["Commits"] = prInfo.Commits
  447. ctx.Data["CommitCount"] = prInfo.Commits.Len()
  448. ctx.Data["Username"] = headUser.Name
  449. ctx.Data["Reponame"] = headRepo.Name
  450. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  451. headTarget := path.Join(headUser.Name, repo.Name)
  452. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", headCommitID)
  453. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", prInfo.MergeBase)
  454. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", headCommitID)
  455. return false
  456. }
  457. func CompareAndPullRequest(ctx *middleware.Context) {
  458. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  459. ctx.Data["PageIsComparePull"] = true
  460. ctx.Data["IsDiffCompare"] = true
  461. renderAttachmentSettings(ctx)
  462. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  463. if ctx.Written() {
  464. return
  465. }
  466. pr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
  467. if err != nil {
  468. if !models.IsErrPullRequestNotExist(err) {
  469. ctx.Handle(500, "GetUnmergedPullRequest", err)
  470. return
  471. }
  472. } else {
  473. ctx.Data["HasPullRequest"] = true
  474. ctx.Data["PullRequest"] = pr
  475. ctx.HTML(200, COMPARE_PULL)
  476. return
  477. }
  478. nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  479. if ctx.Written() {
  480. return
  481. }
  482. if !nothingToCompare {
  483. // Setup information for new form.
  484. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  485. if ctx.Written() {
  486. return
  487. }
  488. }
  489. ctx.HTML(200, COMPARE_PULL)
  490. }
  491. func CompareAndPullRequestPost(ctx *middleware.Context, form auth.CreateIssueForm) {
  492. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  493. ctx.Data["PageIsComparePull"] = true
  494. ctx.Data["IsDiffCompare"] = true
  495. renderAttachmentSettings(ctx)
  496. var (
  497. repo = ctx.Repo.Repository
  498. attachments []string
  499. )
  500. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  501. if ctx.Written() {
  502. return
  503. }
  504. patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
  505. if err != nil {
  506. ctx.Handle(500, "GetPatch", err)
  507. return
  508. }
  509. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, form)
  510. if ctx.Written() {
  511. return
  512. }
  513. if setting.AttachmentEnabled {
  514. attachments = form.Attachments
  515. }
  516. if ctx.HasError() {
  517. ctx.HTML(200, COMPARE_PULL)
  518. return
  519. }
  520. pull := &models.Issue{
  521. RepoID: repo.ID,
  522. Index: repo.NextIssueIndex(),
  523. Name: form.Title,
  524. PosterID: ctx.User.Id,
  525. Poster: ctx.User,
  526. MilestoneID: milestoneID,
  527. AssigneeID: assigneeID,
  528. IsPull: true,
  529. Content: form.Content,
  530. }
  531. if err := models.NewPullRequest(repo, pull, labelIDs, attachments, &models.PullRequest{
  532. HeadRepoID: headRepo.ID,
  533. BaseRepoID: repo.ID,
  534. HeadUserName: headUser.Name,
  535. HeadBranch: headBranch,
  536. BaseBranch: baseBranch,
  537. MergeBase: prInfo.MergeBase,
  538. Type: models.PULL_REQUEST_GOGS,
  539. }, patch); err != nil {
  540. ctx.Handle(500, "NewPullRequest", err)
  541. return
  542. }
  543. log.Trace("Pull request created: %d/%d", repo.ID, pull.ID)
  544. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pull.Index))
  545. }
  546. func TriggerTask(ctx *middleware.Context) {
  547. _, repo := parseOwnerAndRepo(ctx)
  548. if ctx.Written() {
  549. return
  550. }
  551. branch := ctx.Query("branch")
  552. if len(branch) == 0 {
  553. ctx.Handle(422, "TriggerTask", errors.New("branch is empty"))
  554. return
  555. }
  556. log.Trace("TriggerTask[%d].(new request): %s", repo.ID, branch)
  557. go models.HookQueue.Add(repo.ID)
  558. go models.AddTestPullRequestTask(repo.ID, branch)
  559. ctx.Status(202)
  560. }