pull.go 20 KB

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