pull.go 19 KB

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