Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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.Title
  126. ctx.Data["Issue"] = issue
  127. if !issue.IsPull {
  128. ctx.Handle(404, "ViewPullCommits", nil)
  129. return nil
  130. }
  131. if err = issue.GetHeadRepo(); err != nil {
  132. ctx.Handle(500, "GetHeadRepo", err)
  133. return nil
  134. }
  135. if ctx.IsSigned {
  136. // Update issue-user.
  137. if err = issue.ReadBy(ctx.User.ID); err != nil {
  138. ctx.Handle(500, "ReadBy", err)
  139. return nil
  140. }
  141. }
  142. return issue
  143. }
  144. func PrepareMergedViewPullInfo(ctx *context.Context, pull *models.Issue) {
  145. ctx.Data["HasMerged"] = true
  146. ctx.Data["HeadTarget"] = pull.HeadUserName + "/" + pull.HeadBranch
  147. ctx.Data["BaseTarget"] = ctx.Repo.Owner.Name + "/" + pull.BaseBranch
  148. var err error
  149. ctx.Data["NumCommits"], err = ctx.Repo.GitRepo.CommitsCountBetween(pull.MergeBase, pull.MergedCommitID)
  150. if err != nil {
  151. ctx.Handle(500, "Repo.GitRepo.CommitsCountBetween", err)
  152. return
  153. }
  154. ctx.Data["NumFiles"], err = ctx.Repo.GitRepo.FilesCountBetween(pull.MergeBase, pull.MergedCommitID)
  155. if err != nil {
  156. ctx.Handle(500, "Repo.GitRepo.FilesCountBetween", err)
  157. return
  158. }
  159. }
  160. func PrepareViewPullInfo(ctx *context.Context, pull *models.Issue) *git.PullRequestInfo {
  161. repo := ctx.Repo.Repository
  162. ctx.Data["HeadTarget"] = pull.HeadUserName + "/" + pull.HeadBranch
  163. ctx.Data["BaseTarget"] = ctx.Repo.Owner.Name + "/" + pull.BaseBranch
  164. var (
  165. headGitRepo *git.Repository
  166. err error
  167. )
  168. if err = pull.GetHeadRepo(); err != nil {
  169. ctx.Handle(500, "GetHeadRepo", err)
  170. return nil
  171. }
  172. if pull.HeadRepo != nil {
  173. headGitRepo, err = git.OpenRepository(pull.HeadRepo.RepoPath())
  174. if err != nil {
  175. ctx.Handle(500, "OpenRepository", err)
  176. return nil
  177. }
  178. }
  179. if pull.HeadRepo == nil || !headGitRepo.IsBranchExist(pull.HeadBranch) {
  180. ctx.Data["IsPullReuqestBroken"] = true
  181. ctx.Data["HeadTarget"] = "deleted"
  182. ctx.Data["NumCommits"] = 0
  183. ctx.Data["NumFiles"] = 0
  184. return nil
  185. }
  186. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name),
  187. pull.BaseBranch, pull.HeadBranch)
  188. if err != nil {
  189. if strings.Contains(err.Error(), "fatal: Not a valid object name") {
  190. ctx.Data["IsPullReuqestBroken"] = true
  191. ctx.Data["BaseTarget"] = "deleted"
  192. ctx.Data["NumCommits"] = 0
  193. ctx.Data["NumFiles"] = 0
  194. return nil
  195. }
  196. ctx.Handle(500, "GetPullRequestInfo", err)
  197. return nil
  198. }
  199. ctx.Data["NumCommits"] = prInfo.Commits.Len()
  200. ctx.Data["NumFiles"] = prInfo.NumFiles
  201. return prInfo
  202. }
  203. func ViewPullCommits(ctx *context.Context) {
  204. ctx.Data["PageIsPullList"] = true
  205. ctx.Data["PageIsPullCommits"] = true
  206. pull := checkPullInfo(ctx)
  207. if ctx.Written() {
  208. return
  209. }
  210. ctx.Data["Username"] = pull.HeadUserName
  211. ctx.Data["Reponame"] = pull.HeadRepo.Name
  212. var commits *list.List
  213. if pull.HasMerged {
  214. PrepareMergedViewPullInfo(ctx, pull)
  215. if ctx.Written() {
  216. return
  217. }
  218. startCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergeBase)
  219. if err != nil {
  220. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  221. return
  222. }
  223. endCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergedCommitID)
  224. if err != nil {
  225. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  226. return
  227. }
  228. commits, err = ctx.Repo.GitRepo.CommitsBetween(endCommit, startCommit)
  229. if err != nil {
  230. ctx.Handle(500, "Repo.GitRepo.CommitsBetween", err)
  231. return
  232. }
  233. } else {
  234. prInfo := PrepareViewPullInfo(ctx, pull)
  235. if ctx.Written() {
  236. return
  237. } else if prInfo == nil {
  238. ctx.Handle(404, "ViewPullCommits", nil)
  239. return
  240. }
  241. commits = prInfo.Commits
  242. }
  243. commits = models.ValidateCommitsWithEmails(commits)
  244. ctx.Data["Commits"] = commits
  245. ctx.Data["CommitCount"] = commits.Len()
  246. ctx.HTML(200, PULL_COMMITS)
  247. }
  248. func ViewPullFiles(ctx *context.Context) {
  249. ctx.Data["PageIsPullList"] = true
  250. ctx.Data["PageIsPullFiles"] = true
  251. pull := checkPullInfo(ctx)
  252. if ctx.Written() {
  253. return
  254. }
  255. var (
  256. diffRepoPath string
  257. startCommitID string
  258. endCommitID string
  259. gitRepo *git.Repository
  260. )
  261. if pull.HasMerged {
  262. PrepareMergedViewPullInfo(ctx, pull)
  263. if ctx.Written() {
  264. return
  265. }
  266. diffRepoPath = ctx.Repo.GitRepo.Path
  267. startCommitID = pull.MergeBase
  268. endCommitID = pull.MergedCommitID
  269. gitRepo = ctx.Repo.GitRepo
  270. } else {
  271. prInfo := PrepareViewPullInfo(ctx, pull)
  272. if ctx.Written() {
  273. return
  274. } else if prInfo == nil {
  275. ctx.Handle(404, "ViewPullFiles", nil)
  276. return
  277. }
  278. headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
  279. headGitRepo, err := git.OpenRepository(headRepoPath)
  280. if err != nil {
  281. ctx.Handle(500, "OpenRepository", err)
  282. return
  283. }
  284. headCommitID, err := headGitRepo.GetBranchCommitID(pull.HeadBranch)
  285. if err != nil {
  286. ctx.Handle(500, "GetBranchCommitID", err)
  287. return
  288. }
  289. diffRepoPath = headRepoPath
  290. startCommitID = prInfo.MergeBase
  291. endCommitID = headCommitID
  292. gitRepo = headGitRepo
  293. }
  294. diff, err := models.GetDiffRange(diffRepoPath,
  295. startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
  296. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  297. if err != nil {
  298. ctx.Handle(500, "GetDiffRange", err)
  299. return
  300. }
  301. ctx.Data["Diff"] = diff
  302. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  303. commit, err := gitRepo.GetCommit(endCommitID)
  304. if err != nil {
  305. ctx.Handle(500, "GetCommit", err)
  306. return
  307. }
  308. ec, err := ctx.Repo.GetEditorconfig()
  309. if err != nil && !git.IsErrNotExist(err) {
  310. ctx.Handle(500, "ErrGettingEditorconfig", err)
  311. return
  312. }
  313. ctx.Data["Editorconfig"] = ec
  314. headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
  315. ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"
  316. ctx.Data["Username"] = pull.HeadUserName
  317. ctx.Data["Reponame"] = pull.HeadRepo.Name
  318. ctx.Data["IsImageFile"] = commit.IsImageFile
  319. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", endCommitID)
  320. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", startCommitID)
  321. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", endCommitID)
  322. ctx.Data["RequireHighlightJS"] = true
  323. ctx.HTML(200, PULL_FILES)
  324. }
  325. func MergePullRequest(ctx *context.Context) {
  326. issue := checkPullInfo(ctx)
  327. if ctx.Written() {
  328. return
  329. }
  330. if issue.IsClosed {
  331. ctx.Handle(404, "MergePullRequest", nil)
  332. return
  333. }
  334. pr, err := models.GetPullRequestByIssueID(issue.ID)
  335. if err != nil {
  336. if models.IsErrPullRequestNotExist(err) {
  337. ctx.Handle(404, "GetPullRequestByIssueID", nil)
  338. } else {
  339. ctx.Handle(500, "GetPullRequestByIssueID", err)
  340. }
  341. return
  342. }
  343. if !pr.CanAutoMerge() || pr.HasMerged {
  344. ctx.Handle(404, "MergePullRequest", nil)
  345. return
  346. }
  347. pr.Issue = issue
  348. pr.Issue.Repo = ctx.Repo.Repository
  349. if err = pr.Merge(ctx.User, ctx.Repo.GitRepo); err != nil {
  350. ctx.Handle(500, "Merge", err)
  351. return
  352. }
  353. log.Trace("Pull request merged: %d", pr.ID)
  354. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  355. }
  356. func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
  357. baseRepo := ctx.Repo.Repository
  358. // Get compared branches information
  359. // format: <base branch>...[<head repo>:]<head branch>
  360. // base<-head: master...head:feature
  361. // same repo: master...feature
  362. infos := strings.Split(ctx.Params("*"), "...")
  363. if len(infos) != 2 {
  364. log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)
  365. ctx.Handle(404, "CompareAndPullRequest", nil)
  366. return nil, nil, nil, nil, "", ""
  367. }
  368. baseBranch := infos[0]
  369. ctx.Data["BaseBranch"] = baseBranch
  370. var (
  371. headUser *models.User
  372. headBranch string
  373. isSameRepo bool
  374. err error
  375. )
  376. // If there is no head repository, it means pull request between same repository.
  377. headInfos := strings.Split(infos[1], ":")
  378. if len(headInfos) == 1 {
  379. isSameRepo = true
  380. headUser = ctx.Repo.Owner
  381. headBranch = headInfos[0]
  382. } else if len(headInfos) == 2 {
  383. headUser, err = models.GetUserByName(headInfos[0])
  384. if err != nil {
  385. if models.IsErrUserNotExist(err) {
  386. ctx.Handle(404, "GetUserByName", nil)
  387. } else {
  388. ctx.Handle(500, "GetUserByName", err)
  389. }
  390. return nil, nil, nil, nil, "", ""
  391. }
  392. headBranch = headInfos[1]
  393. } else {
  394. ctx.Handle(404, "CompareAndPullRequest", nil)
  395. return nil, nil, nil, nil, "", ""
  396. }
  397. ctx.Data["HeadUser"] = headUser
  398. ctx.Data["HeadBranch"] = headBranch
  399. ctx.Repo.PullRequest.SameRepo = isSameRepo
  400. // Check if base branch is valid.
  401. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  402. ctx.Handle(404, "IsBranchExist", nil)
  403. return nil, nil, nil, nil, "", ""
  404. }
  405. // Check if current user has fork of repository or in the same repository.
  406. headRepo, has := models.HasForkedRepo(headUser.ID, baseRepo.ID)
  407. if !has && !isSameRepo {
  408. log.Trace("ParseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
  409. ctx.Handle(404, "ParseCompareInfo", nil)
  410. return nil, nil, nil, nil, "", ""
  411. }
  412. var headGitRepo *git.Repository
  413. if isSameRepo {
  414. headRepo = ctx.Repo.Repository
  415. headGitRepo = ctx.Repo.GitRepo
  416. } else {
  417. headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  418. if err != nil {
  419. ctx.Handle(500, "OpenRepository", err)
  420. return nil, nil, nil, nil, "", ""
  421. }
  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. ec, err := ctx.Repo.GetEditorconfig()
  531. if err != nil && !git.IsErrNotExist(err) {
  532. ctx.Handle(500, "ErrGettingEditorconfig", err)
  533. return
  534. }
  535. ctx.Data["Editorconfig"] = ec
  536. ctx.HTML(200, COMPARE_PULL)
  537. }
  538. func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm) {
  539. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  540. ctx.Data["PageIsComparePull"] = true
  541. ctx.Data["IsDiffCompare"] = true
  542. renderAttachmentSettings(ctx)
  543. var (
  544. repo = ctx.Repo.Repository
  545. attachments []string
  546. )
  547. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  548. if ctx.Written() {
  549. return
  550. }
  551. patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
  552. if err != nil {
  553. ctx.Handle(500, "GetPatch", err)
  554. return
  555. }
  556. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, form)
  557. if ctx.Written() {
  558. return
  559. }
  560. if setting.AttachmentEnabled {
  561. attachments = form.Attachments
  562. }
  563. if ctx.HasError() {
  564. ctx.HTML(200, COMPARE_PULL)
  565. return
  566. }
  567. pullIssue := &models.Issue{
  568. RepoID: repo.ID,
  569. Index: repo.NextIssueIndex(),
  570. Title: form.Title,
  571. PosterID: ctx.User.ID,
  572. Poster: ctx.User,
  573. MilestoneID: milestoneID,
  574. AssigneeID: assigneeID,
  575. IsPull: true,
  576. Content: form.Content,
  577. }
  578. pullRequest := &models.PullRequest{
  579. HeadRepoID: headRepo.ID,
  580. BaseRepoID: repo.ID,
  581. HeadUserName: headUser.Name,
  582. HeadBranch: headBranch,
  583. BaseBranch: baseBranch,
  584. HeadRepo: headRepo,
  585. BaseRepo: repo,
  586. MergeBase: prInfo.MergeBase,
  587. Type: models.PULL_REQUEST_GOGS,
  588. }
  589. if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch); err != nil {
  590. ctx.Handle(500, "NewPullRequest", err)
  591. return
  592. } else if err := pullRequest.PushToBaseRepo(); err != nil {
  593. ctx.Handle(500, "PushToBaseRepo", err)
  594. return
  595. }
  596. log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
  597. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
  598. }
  599. func TriggerTask(ctx *context.Context) {
  600. pusherID := ctx.QueryInt64("pusher")
  601. branch := ctx.Query("branch")
  602. secret := ctx.Query("secret")
  603. if len(branch) == 0 || len(secret) == 0 || pusherID <= 0 {
  604. ctx.Error(404)
  605. log.Trace("TriggerTask: branch or secret is empty, or pusher ID is not valid")
  606. return
  607. }
  608. owner, repo := parseOwnerAndRepo(ctx)
  609. if ctx.Written() {
  610. return
  611. }
  612. if secret != base.EncodeMD5(owner.Salt) {
  613. ctx.Error(404)
  614. log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
  615. return
  616. }
  617. pusher, err := models.GetUserByID(pusherID)
  618. if err != nil {
  619. if models.IsErrUserNotExist(err) {
  620. ctx.Error(404)
  621. } else {
  622. ctx.Handle(500, "GetUserByID", err)
  623. }
  624. return
  625. }
  626. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  627. go models.HookQueue.Add(repo.ID)
  628. go models.AddTestPullRequestTask(pusher, repo.ID, branch, true)
  629. ctx.Status(202)
  630. }