pull.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. commit, err := gitRepo.GetCommit(endCommitID)
  296. if err != nil {
  297. ctx.Handle(500, "GetCommit", err)
  298. return
  299. }
  300. headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
  301. ctx.Data["Username"] = pull.HeadUserName
  302. ctx.Data["Reponame"] = pull.HeadRepo.Name
  303. ctx.Data["IsImageFile"] = commit.IsImageFile
  304. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", endCommitID)
  305. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", startCommitID)
  306. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", endCommitID)
  307. ctx.HTML(200, PULL_FILES)
  308. }
  309. func MergePullRequest(ctx *middleware.Context) {
  310. issue := checkPullInfo(ctx)
  311. if ctx.Written() {
  312. return
  313. }
  314. if issue.IsClosed {
  315. ctx.Handle(404, "MergePullRequest", nil)
  316. return
  317. }
  318. pr, err := models.GetPullRequestByIssueID(issue.ID)
  319. if err != nil {
  320. if models.IsErrPullRequestNotExist(err) {
  321. ctx.Handle(404, "GetPullRequestByIssueID", nil)
  322. } else {
  323. ctx.Handle(500, "GetPullRequestByIssueID", err)
  324. }
  325. return
  326. }
  327. if !pr.CanAutoMerge() || pr.HasMerged {
  328. ctx.Handle(404, "MergePullRequest", nil)
  329. return
  330. }
  331. pr.Issue = issue
  332. pr.Issue.Repo = ctx.Repo.Repository
  333. if err = pr.Merge(ctx.User, ctx.Repo.GitRepo); err != nil {
  334. ctx.Handle(500, "Merge", err)
  335. return
  336. }
  337. log.Trace("Pull request merged: %d", pr.ID)
  338. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  339. }
  340. func ParseCompareInfo(ctx *middleware.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
  341. // Get compare branch information.
  342. infos := strings.Split(ctx.Params("*"), "...")
  343. if len(infos) != 2 {
  344. ctx.Handle(404, "CompareAndPullRequest", nil)
  345. return nil, nil, nil, nil, "", ""
  346. }
  347. baseBranch := infos[0]
  348. ctx.Data["BaseBranch"] = baseBranch
  349. headInfos := strings.Split(infos[1], ":")
  350. if len(headInfos) != 2 {
  351. ctx.Handle(404, "CompareAndPullRequest", nil)
  352. return nil, nil, nil, nil, "", ""
  353. }
  354. headUsername := headInfos[0]
  355. headBranch := headInfos[1]
  356. ctx.Data["HeadBranch"] = headBranch
  357. headUser, err := models.GetUserByName(headUsername)
  358. if err != nil {
  359. if models.IsErrUserNotExist(err) {
  360. ctx.Handle(404, "GetUserByName", nil)
  361. } else {
  362. ctx.Handle(500, "GetUserByName", err)
  363. }
  364. return nil, nil, nil, nil, "", ""
  365. }
  366. ctx.Data["HeadUser"] = headUser
  367. repo := ctx.Repo.Repository
  368. // Check if base branch is valid.
  369. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  370. ctx.Handle(404, "IsBranchExist", nil)
  371. return nil, nil, nil, nil, "", ""
  372. }
  373. // Check if current user has fork of repository.
  374. headRepo, has := models.HasForkedRepo(headUser.Id, repo.ID)
  375. if !has || (!ctx.User.IsAdminOfRepo(headRepo) && !ctx.User.IsAdmin) {
  376. ctx.Handle(404, "HasForkedRepo", nil)
  377. return nil, nil, nil, nil, "", ""
  378. }
  379. headGitRepo, err := git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  380. if err != nil {
  381. ctx.Handle(500, "OpenRepository", err)
  382. return nil, nil, nil, nil, "", ""
  383. }
  384. // Check if head branch is valid.
  385. if !headGitRepo.IsBranchExist(headBranch) {
  386. ctx.Handle(404, "IsBranchExist", nil)
  387. return nil, nil, nil, nil, "", ""
  388. }
  389. headBranches, err := headGitRepo.GetBranches()
  390. if err != nil {
  391. ctx.Handle(500, "GetBranches", err)
  392. return nil, nil, nil, nil, "", ""
  393. }
  394. ctx.Data["HeadBranches"] = headBranches
  395. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name), baseBranch, headBranch)
  396. if err != nil {
  397. ctx.Handle(500, "GetPullRequestInfo", err)
  398. return nil, nil, nil, nil, "", ""
  399. }
  400. ctx.Data["BeforeCommitID"] = prInfo.MergeBase
  401. return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
  402. }
  403. func PrepareCompareDiff(
  404. ctx *middleware.Context,
  405. headUser *models.User,
  406. headRepo *models.Repository,
  407. headGitRepo *git.Repository,
  408. prInfo *git.PullRequestInfo,
  409. baseBranch, headBranch string) bool {
  410. var (
  411. repo = ctx.Repo.Repository
  412. err error
  413. )
  414. // Get diff information.
  415. ctx.Data["CommitRepoLink"] = headRepo.RepoLink()
  416. headCommitID, err := headGitRepo.GetBranchCommitID(headBranch)
  417. if err != nil {
  418. ctx.Handle(500, "GetBranchCommitID", err)
  419. return false
  420. }
  421. ctx.Data["AfterCommitID"] = headCommitID
  422. if headCommitID == prInfo.MergeBase {
  423. ctx.Data["IsNothingToCompare"] = true
  424. return true
  425. }
  426. diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
  427. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines)
  428. if err != nil {
  429. ctx.Handle(500, "GetDiffRange", err)
  430. return false
  431. }
  432. ctx.Data["Diff"] = diff
  433. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  434. headCommit, err := headGitRepo.GetCommit(headCommitID)
  435. if err != nil {
  436. ctx.Handle(500, "GetCommit", err)
  437. return false
  438. }
  439. prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
  440. ctx.Data["Commits"] = prInfo.Commits
  441. ctx.Data["CommitCount"] = prInfo.Commits.Len()
  442. ctx.Data["Username"] = headUser.Name
  443. ctx.Data["Reponame"] = headRepo.Name
  444. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  445. headTarget := path.Join(headUser.Name, repo.Name)
  446. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", headCommitID)
  447. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", prInfo.MergeBase)
  448. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", headCommitID)
  449. return false
  450. }
  451. func CompareAndPullRequest(ctx *middleware.Context) {
  452. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  453. ctx.Data["PageIsComparePull"] = true
  454. ctx.Data["IsDiffCompare"] = true
  455. renderAttachmentSettings(ctx)
  456. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  457. if ctx.Written() {
  458. return
  459. }
  460. pr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
  461. if err != nil {
  462. if !models.IsErrPullRequestNotExist(err) {
  463. ctx.Handle(500, "GetUnmergedPullRequest", err)
  464. return
  465. }
  466. } else {
  467. ctx.Data["HasPullRequest"] = true
  468. ctx.Data["PullRequest"] = pr
  469. ctx.HTML(200, COMPARE_PULL)
  470. return
  471. }
  472. nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  473. if ctx.Written() {
  474. return
  475. }
  476. if !nothingToCompare {
  477. // Setup information for new form.
  478. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  479. if ctx.Written() {
  480. return
  481. }
  482. }
  483. ctx.HTML(200, COMPARE_PULL)
  484. }
  485. func CompareAndPullRequestPost(ctx *middleware.Context, form auth.CreateIssueForm) {
  486. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  487. ctx.Data["PageIsComparePull"] = true
  488. ctx.Data["IsDiffCompare"] = true
  489. renderAttachmentSettings(ctx)
  490. var (
  491. repo = ctx.Repo.Repository
  492. attachments []string
  493. )
  494. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  495. if ctx.Written() {
  496. return
  497. }
  498. patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
  499. if err != nil {
  500. ctx.Handle(500, "GetPatch", err)
  501. return
  502. }
  503. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, form)
  504. if ctx.Written() {
  505. return
  506. }
  507. if setting.AttachmentEnabled {
  508. attachments = form.Attachments
  509. }
  510. if ctx.HasError() {
  511. ctx.HTML(200, COMPARE_PULL)
  512. return
  513. }
  514. pull := &models.Issue{
  515. RepoID: repo.ID,
  516. Index: repo.NextIssueIndex(),
  517. Name: form.Title,
  518. PosterID: ctx.User.Id,
  519. Poster: ctx.User,
  520. MilestoneID: milestoneID,
  521. AssigneeID: assigneeID,
  522. IsPull: true,
  523. Content: form.Content,
  524. }
  525. if err := models.NewPullRequest(repo, pull, labelIDs, attachments, &models.PullRequest{
  526. HeadRepoID: headRepo.ID,
  527. BaseRepoID: repo.ID,
  528. HeadUserName: headUser.Name,
  529. HeadBranch: headBranch,
  530. BaseBranch: baseBranch,
  531. MergeBase: prInfo.MergeBase,
  532. Type: models.PULL_REQUEST_GOGS,
  533. }, patch); err != nil {
  534. ctx.Handle(500, "NewPullRequest", err)
  535. return
  536. }
  537. notifyWatchersAndMentions(ctx, pull)
  538. if ctx.Written() {
  539. return
  540. }
  541. log.Trace("Pull request created: %d/%d", repo.ID, pull.ID)
  542. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pull.Index))
  543. }
  544. func TriggerTask(ctx *middleware.Context) {
  545. branch := ctx.Query("branch")
  546. secret := ctx.Query("secret")
  547. if len(branch) == 0 || len(secret) == 0 {
  548. ctx.Error(404)
  549. log.Trace("TriggerTask: branch or secret is empty")
  550. return
  551. }
  552. owner, repo := parseOwnerAndRepo(ctx)
  553. if ctx.Written() {
  554. return
  555. }
  556. if secret != base.EncodeMD5(owner.Salt) {
  557. ctx.Error(404)
  558. log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
  559. return
  560. }
  561. log.Trace("TriggerTask [%d].(new request): %s", repo.ID, branch)
  562. go models.HookQueue.Add(repo.ID)
  563. go models.AddTestPullRequestTask(repo.ID, branch)
  564. ctx.Status(202)
  565. }