pull.go 17 KB

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