pull.go 19 KB

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