You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pull.go 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. // Copyright 2018 The Gitea Authors.
  2. // Copyright 2014 The Gogs Authors.
  3. // All rights reserved.
  4. // Use of this source code is governed by a MIT-style
  5. // license that can be found in the LICENSE file.
  6. package repo
  7. import (
  8. "container/list"
  9. "crypto/subtle"
  10. "fmt"
  11. "html"
  12. "net/http"
  13. "path"
  14. "strings"
  15. "time"
  16. "code.gitea.io/gitea/models"
  17. "code.gitea.io/gitea/modules/auth"
  18. "code.gitea.io/gitea/modules/base"
  19. "code.gitea.io/gitea/modules/context"
  20. "code.gitea.io/gitea/modules/git"
  21. "code.gitea.io/gitea/modules/log"
  22. "code.gitea.io/gitea/modules/notification"
  23. "code.gitea.io/gitea/modules/repofiles"
  24. "code.gitea.io/gitea/modules/setting"
  25. "code.gitea.io/gitea/modules/util"
  26. "code.gitea.io/gitea/services/gitdiff"
  27. pull_service "code.gitea.io/gitea/services/pull"
  28. repo_service "code.gitea.io/gitea/services/repository"
  29. "github.com/unknwon/com"
  30. )
  31. const (
  32. tplFork base.TplName = "repo/pulls/fork"
  33. tplCompareDiff base.TplName = "repo/diff/compare"
  34. tplPullCommits base.TplName = "repo/pulls/commits"
  35. tplPullFiles base.TplName = "repo/pulls/files"
  36. pullRequestTemplateKey = "PullRequestTemplate"
  37. )
  38. var (
  39. pullRequestTemplateCandidates = []string{
  40. "PULL_REQUEST_TEMPLATE.md",
  41. "pull_request_template.md",
  42. ".gitea/PULL_REQUEST_TEMPLATE.md",
  43. ".gitea/pull_request_template.md",
  44. ".github/PULL_REQUEST_TEMPLATE.md",
  45. ".github/pull_request_template.md",
  46. }
  47. )
  48. func getRepository(ctx *context.Context, repoID int64) *models.Repository {
  49. repo, err := models.GetRepositoryByID(repoID)
  50. if err != nil {
  51. if models.IsErrRepoNotExist(err) {
  52. ctx.NotFound("GetRepositoryByID", nil)
  53. } else {
  54. ctx.ServerError("GetRepositoryByID", err)
  55. }
  56. return nil
  57. }
  58. perm, err := models.GetUserRepoPermission(repo, ctx.User)
  59. if err != nil {
  60. ctx.ServerError("GetUserRepoPermission", err)
  61. return nil
  62. }
  63. if !perm.CanRead(models.UnitTypeCode) {
  64. log.Trace("Permission Denied: User %-v cannot read %-v of repo %-v\n"+
  65. "User in repo has Permissions: %-+v",
  66. ctx.User,
  67. models.UnitTypeCode,
  68. ctx.Repo,
  69. perm)
  70. ctx.NotFound("getRepository", nil)
  71. return nil
  72. }
  73. return repo
  74. }
  75. func getForkRepository(ctx *context.Context) *models.Repository {
  76. forkRepo := getRepository(ctx, ctx.ParamsInt64(":repoid"))
  77. if ctx.Written() {
  78. return nil
  79. }
  80. if forkRepo.IsEmpty {
  81. log.Trace("Empty repository %-v", forkRepo)
  82. ctx.NotFound("getForkRepository", nil)
  83. return nil
  84. }
  85. ctx.Data["repo_name"] = forkRepo.Name
  86. ctx.Data["description"] = forkRepo.Description
  87. ctx.Data["IsPrivate"] = forkRepo.IsPrivate
  88. canForkToUser := forkRepo.OwnerID != ctx.User.ID && !ctx.User.HasForkedRepo(forkRepo.ID)
  89. if err := forkRepo.GetOwner(); err != nil {
  90. ctx.ServerError("GetOwner", err)
  91. return nil
  92. }
  93. ctx.Data["ForkFrom"] = forkRepo.Owner.Name + "/" + forkRepo.Name
  94. ctx.Data["ForkFromOwnerID"] = forkRepo.Owner.ID
  95. if err := ctx.User.GetOwnedOrganizations(); err != nil {
  96. ctx.ServerError("GetOwnedOrganizations", err)
  97. return nil
  98. }
  99. var orgs []*models.User
  100. for _, org := range ctx.User.OwnedOrgs {
  101. if forkRepo.OwnerID != org.ID && !org.HasForkedRepo(forkRepo.ID) {
  102. orgs = append(orgs, org)
  103. }
  104. }
  105. var traverseParentRepo = forkRepo
  106. var err error
  107. for {
  108. if ctx.User.ID == traverseParentRepo.OwnerID {
  109. canForkToUser = false
  110. } else {
  111. for i, org := range orgs {
  112. if org.ID == traverseParentRepo.OwnerID {
  113. orgs = append(orgs[:i], orgs[i+1:]...)
  114. break
  115. }
  116. }
  117. }
  118. if !traverseParentRepo.IsFork {
  119. break
  120. }
  121. traverseParentRepo, err = models.GetRepositoryByID(traverseParentRepo.ForkID)
  122. if err != nil {
  123. ctx.ServerError("GetRepositoryByID", err)
  124. return nil
  125. }
  126. }
  127. ctx.Data["CanForkToUser"] = canForkToUser
  128. ctx.Data["Orgs"] = orgs
  129. if canForkToUser {
  130. ctx.Data["ContextUser"] = ctx.User
  131. } else if len(orgs) > 0 {
  132. ctx.Data["ContextUser"] = orgs[0]
  133. }
  134. return forkRepo
  135. }
  136. // Fork render repository fork page
  137. func Fork(ctx *context.Context) {
  138. ctx.Data["Title"] = ctx.Tr("new_fork")
  139. getForkRepository(ctx)
  140. if ctx.Written() {
  141. return
  142. }
  143. ctx.HTML(200, tplFork)
  144. }
  145. // ForkPost response for forking a repository
  146. func ForkPost(ctx *context.Context, form auth.CreateRepoForm) {
  147. ctx.Data["Title"] = ctx.Tr("new_fork")
  148. ctxUser := checkContextUser(ctx, form.UID)
  149. if ctx.Written() {
  150. return
  151. }
  152. forkRepo := getForkRepository(ctx)
  153. if ctx.Written() {
  154. return
  155. }
  156. ctx.Data["ContextUser"] = ctxUser
  157. if ctx.HasError() {
  158. ctx.HTML(200, tplFork)
  159. return
  160. }
  161. var err error
  162. var traverseParentRepo = forkRepo
  163. for {
  164. if ctxUser.ID == traverseParentRepo.OwnerID {
  165. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplFork, &form)
  166. return
  167. }
  168. repo, has := models.HasForkedRepo(ctxUser.ID, traverseParentRepo.ID)
  169. if has {
  170. ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + repo.Name)
  171. return
  172. }
  173. if !traverseParentRepo.IsFork {
  174. break
  175. }
  176. traverseParentRepo, err = models.GetRepositoryByID(traverseParentRepo.ForkID)
  177. if err != nil {
  178. ctx.ServerError("GetRepositoryByID", err)
  179. return
  180. }
  181. }
  182. // Check ownership of organization.
  183. if ctxUser.IsOrganization() {
  184. isOwner, err := ctxUser.IsOwnedBy(ctx.User.ID)
  185. if err != nil {
  186. ctx.ServerError("IsOwnedBy", err)
  187. return
  188. } else if !isOwner {
  189. ctx.Error(403)
  190. return
  191. }
  192. }
  193. repo, err := repo_service.ForkRepository(ctx.User, ctxUser, forkRepo, form.RepoName, form.Description)
  194. if err != nil {
  195. ctx.Data["Err_RepoName"] = true
  196. switch {
  197. case models.IsErrRepoAlreadyExist(err):
  198. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplFork, &form)
  199. case models.IsErrNameReserved(err):
  200. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), tplFork, &form)
  201. case models.IsErrNamePatternNotAllowed(err):
  202. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplFork, &form)
  203. default:
  204. ctx.ServerError("ForkPost", err)
  205. }
  206. return
  207. }
  208. log.Trace("Repository forked[%d]: %s/%s", forkRepo.ID, ctxUser.Name, repo.Name)
  209. ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + repo.Name)
  210. }
  211. func checkPullInfo(ctx *context.Context) *models.Issue {
  212. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  213. if err != nil {
  214. if models.IsErrIssueNotExist(err) {
  215. ctx.NotFound("GetIssueByIndex", err)
  216. } else {
  217. ctx.ServerError("GetIssueByIndex", err)
  218. }
  219. return nil
  220. }
  221. if err = issue.LoadPoster(); err != nil {
  222. ctx.ServerError("LoadPoster", err)
  223. return nil
  224. }
  225. if err := issue.LoadRepo(); err != nil {
  226. ctx.ServerError("LoadRepo", err)
  227. return nil
  228. }
  229. ctx.Data["Title"] = fmt.Sprintf("#%d - %s", issue.Index, issue.Title)
  230. ctx.Data["Issue"] = issue
  231. if !issue.IsPull {
  232. ctx.NotFound("ViewPullCommits", nil)
  233. return nil
  234. }
  235. if err = issue.LoadPullRequest(); err != nil {
  236. ctx.ServerError("LoadPullRequest", err)
  237. return nil
  238. }
  239. if err = issue.PullRequest.GetHeadRepo(); err != nil {
  240. ctx.ServerError("GetHeadRepo", err)
  241. return nil
  242. }
  243. if ctx.IsSigned {
  244. // Update issue-user.
  245. if err = issue.ReadBy(ctx.User.ID); err != nil {
  246. ctx.ServerError("ReadBy", err)
  247. return nil
  248. }
  249. }
  250. return issue
  251. }
  252. func setMergeTarget(ctx *context.Context, pull *models.PullRequest) {
  253. if ctx.Repo.Owner.Name == pull.MustHeadUserName() {
  254. ctx.Data["HeadTarget"] = pull.HeadBranch
  255. } else if pull.HeadRepo == nil {
  256. ctx.Data["HeadTarget"] = pull.MustHeadUserName() + ":" + pull.HeadBranch
  257. } else {
  258. ctx.Data["HeadTarget"] = pull.MustHeadUserName() + "/" + pull.HeadRepo.Name + ":" + pull.HeadBranch
  259. }
  260. ctx.Data["BaseTarget"] = pull.BaseBranch
  261. }
  262. // PrepareMergedViewPullInfo show meta information for a merged pull request view page
  263. func PrepareMergedViewPullInfo(ctx *context.Context, issue *models.Issue) *git.CompareInfo {
  264. pull := issue.PullRequest
  265. setMergeTarget(ctx, pull)
  266. ctx.Data["HasMerged"] = true
  267. compareInfo, err := ctx.Repo.GitRepo.GetCompareInfo(ctx.Repo.Repository.RepoPath(),
  268. pull.MergeBase, pull.GetGitRefName())
  269. if err != nil {
  270. if strings.Contains(err.Error(), "fatal: Not a valid object name") {
  271. ctx.Data["IsPullRequestBroken"] = true
  272. ctx.Data["BaseTarget"] = "deleted"
  273. ctx.Data["NumCommits"] = 0
  274. ctx.Data["NumFiles"] = 0
  275. return nil
  276. }
  277. ctx.ServerError("GetCompareInfo", err)
  278. return nil
  279. }
  280. ctx.Data["NumCommits"] = compareInfo.Commits.Len()
  281. ctx.Data["NumFiles"] = compareInfo.NumFiles
  282. return compareInfo
  283. }
  284. // PrepareViewPullInfo show meta information for a pull request preview page
  285. func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.CompareInfo {
  286. repo := ctx.Repo.Repository
  287. pull := issue.PullRequest
  288. if err := pull.GetHeadRepo(); err != nil {
  289. ctx.ServerError("GetHeadRepo", err)
  290. return nil
  291. }
  292. if err := pull.GetBaseRepo(); err != nil {
  293. ctx.ServerError("GetBaseRepo", err)
  294. return nil
  295. }
  296. setMergeTarget(ctx, pull)
  297. if err := pull.LoadProtectedBranch(); err != nil {
  298. ctx.ServerError("LoadProtectedBranch", err)
  299. return nil
  300. }
  301. ctx.Data["EnableStatusCheck"] = pull.ProtectedBranch != nil && pull.ProtectedBranch.EnableStatusCheck
  302. baseGitRepo, err := git.OpenRepository(pull.BaseRepo.RepoPath())
  303. if err != nil {
  304. ctx.ServerError("OpenRepository", err)
  305. return nil
  306. }
  307. defer baseGitRepo.Close()
  308. var headBranchExist bool
  309. var headBranchSha string
  310. // HeadRepo may be missing
  311. if pull.HeadRepo != nil {
  312. var err error
  313. headGitRepo, err := git.OpenRepository(pull.HeadRepo.RepoPath())
  314. if err != nil {
  315. ctx.ServerError("OpenRepository", err)
  316. return nil
  317. }
  318. defer headGitRepo.Close()
  319. headBranchExist = headGitRepo.IsBranchExist(pull.HeadBranch)
  320. if headBranchExist {
  321. headBranchSha, err = headGitRepo.GetBranchCommitID(pull.HeadBranch)
  322. if err != nil {
  323. ctx.ServerError("GetBranchCommitID", err)
  324. return nil
  325. }
  326. }
  327. }
  328. if headBranchExist {
  329. allowUpdate, err := pull_service.IsUserAllowedToUpdate(pull, ctx.User)
  330. if err != nil {
  331. ctx.ServerError("IsUserAllowedToUpdate", err)
  332. return nil
  333. }
  334. ctx.Data["UpdateAllowed"] = allowUpdate
  335. divergence, err := pull_service.GetDiverging(pull)
  336. if err != nil {
  337. ctx.ServerError("GetDiverging", err)
  338. return nil
  339. }
  340. ctx.Data["Divergence"] = divergence
  341. }
  342. sha, err := baseGitRepo.GetRefCommitID(pull.GetGitRefName())
  343. if err != nil {
  344. ctx.ServerError(fmt.Sprintf("GetRefCommitID(%s)", pull.GetGitRefName()), err)
  345. return nil
  346. }
  347. commitStatuses, err := models.GetLatestCommitStatus(repo, sha, 0)
  348. if err != nil {
  349. ctx.ServerError("GetLatestCommitStatus", err)
  350. return nil
  351. }
  352. if len(commitStatuses) > 0 {
  353. ctx.Data["LatestCommitStatuses"] = commitStatuses
  354. ctx.Data["LatestCommitStatus"] = models.CalcCommitStatus(commitStatuses)
  355. }
  356. if pull.ProtectedBranch != nil && pull.ProtectedBranch.EnableStatusCheck {
  357. ctx.Data["is_context_required"] = func(context string) bool {
  358. for _, c := range pull.ProtectedBranch.StatusCheckContexts {
  359. if c == context {
  360. return true
  361. }
  362. }
  363. return false
  364. }
  365. state := pull_service.MergeRequiredContextsCommitStatus(commitStatuses, pull.ProtectedBranch.StatusCheckContexts)
  366. ctx.Data["RequiredStatusCheckState"] = state
  367. ctx.Data["IsRequiredStatusCheckSuccess"] = state.IsSuccess()
  368. }
  369. ctx.Data["HeadBranchMovedOn"] = headBranchSha != sha
  370. ctx.Data["HeadBranchCommitID"] = headBranchSha
  371. ctx.Data["PullHeadCommitID"] = sha
  372. if pull.HeadRepo == nil || !headBranchExist || headBranchSha != sha {
  373. ctx.Data["IsPullRequestBroken"] = true
  374. ctx.Data["HeadTarget"] = "deleted"
  375. }
  376. compareInfo, err := baseGitRepo.GetCompareInfo(pull.BaseRepo.RepoPath(),
  377. pull.BaseBranch, pull.GetGitRefName())
  378. if err != nil {
  379. if strings.Contains(err.Error(), "fatal: Not a valid object name") {
  380. ctx.Data["IsPullRequestBroken"] = true
  381. ctx.Data["BaseTarget"] = "deleted"
  382. ctx.Data["NumCommits"] = 0
  383. ctx.Data["NumFiles"] = 0
  384. return nil
  385. }
  386. ctx.ServerError("GetCompareInfo", err)
  387. return nil
  388. }
  389. if pull.IsWorkInProgress() {
  390. ctx.Data["IsPullWorkInProgress"] = true
  391. ctx.Data["WorkInProgressPrefix"] = pull.GetWorkInProgressPrefix()
  392. }
  393. if pull.IsFilesConflicted() {
  394. ctx.Data["IsPullFilesConflicted"] = true
  395. ctx.Data["ConflictedFiles"] = pull.ConflictedFiles
  396. }
  397. ctx.Data["NumCommits"] = compareInfo.Commits.Len()
  398. ctx.Data["NumFiles"] = compareInfo.NumFiles
  399. return compareInfo
  400. }
  401. // ViewPullCommits show commits for a pull request
  402. func ViewPullCommits(ctx *context.Context) {
  403. ctx.Data["PageIsPullList"] = true
  404. ctx.Data["PageIsPullCommits"] = true
  405. issue := checkPullInfo(ctx)
  406. if ctx.Written() {
  407. return
  408. }
  409. pull := issue.PullRequest
  410. var commits *list.List
  411. var prInfo *git.CompareInfo
  412. if pull.HasMerged {
  413. prInfo = PrepareMergedViewPullInfo(ctx, issue)
  414. } else {
  415. prInfo = PrepareViewPullInfo(ctx, issue)
  416. }
  417. if ctx.Written() {
  418. return
  419. } else if prInfo == nil {
  420. ctx.NotFound("ViewPullCommits", nil)
  421. return
  422. }
  423. ctx.Data["Username"] = ctx.Repo.Owner.Name
  424. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  425. commits = prInfo.Commits
  426. commits = models.ValidateCommitsWithEmails(commits)
  427. commits = models.ParseCommitsWithSignature(commits)
  428. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  429. ctx.Data["Commits"] = commits
  430. ctx.Data["CommitCount"] = commits.Len()
  431. getBranchData(ctx, issue)
  432. ctx.HTML(200, tplPullCommits)
  433. }
  434. // ViewPullFiles render pull request changed files list page
  435. func ViewPullFiles(ctx *context.Context) {
  436. ctx.Data["PageIsPullList"] = true
  437. ctx.Data["PageIsPullFiles"] = true
  438. issue := checkPullInfo(ctx)
  439. if ctx.Written() {
  440. return
  441. }
  442. pull := issue.PullRequest
  443. whitespaceFlags := map[string]string{
  444. "ignore-all": "-w",
  445. "ignore-change": "-b",
  446. "ignore-eol": "--ignore-space-at-eol",
  447. "": ""}
  448. var (
  449. diffRepoPath string
  450. startCommitID string
  451. endCommitID string
  452. gitRepo *git.Repository
  453. )
  454. var headTarget string
  455. var prInfo *git.CompareInfo
  456. if pull.HasMerged {
  457. prInfo = PrepareMergedViewPullInfo(ctx, issue)
  458. } else {
  459. prInfo = PrepareViewPullInfo(ctx, issue)
  460. }
  461. if ctx.Written() {
  462. return
  463. } else if prInfo == nil {
  464. ctx.NotFound("ViewPullFiles", nil)
  465. return
  466. }
  467. diffRepoPath = ctx.Repo.GitRepo.Path
  468. gitRepo = ctx.Repo.GitRepo
  469. headCommitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName())
  470. if err != nil {
  471. ctx.ServerError("GetRefCommitID", err)
  472. return
  473. }
  474. startCommitID = prInfo.MergeBase
  475. endCommitID = headCommitID
  476. headTarget = path.Join(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  477. ctx.Data["Username"] = ctx.Repo.Owner.Name
  478. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  479. ctx.Data["AfterCommitID"] = endCommitID
  480. diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(diffRepoPath,
  481. startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
  482. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles,
  483. whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)])
  484. if err != nil {
  485. ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err)
  486. return
  487. }
  488. if err = diff.LoadComments(issue, ctx.User); err != nil {
  489. ctx.ServerError("LoadComments", err)
  490. return
  491. }
  492. ctx.Data["Diff"] = diff
  493. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  494. baseCommit, err := ctx.Repo.GitRepo.GetCommit(startCommitID)
  495. if err != nil {
  496. ctx.ServerError("GetCommit", err)
  497. return
  498. }
  499. commit, err := gitRepo.GetCommit(endCommitID)
  500. if err != nil {
  501. ctx.ServerError("GetCommit", err)
  502. return
  503. }
  504. setImageCompareContext(ctx, baseCommit, commit)
  505. setPathsCompareContext(ctx, baseCommit, commit, headTarget)
  506. ctx.Data["RequireHighlightJS"] = true
  507. ctx.Data["RequireSimpleMDE"] = true
  508. ctx.Data["RequireTribute"] = true
  509. if ctx.Data["Assignees"], err = ctx.Repo.Repository.GetAssignees(); err != nil {
  510. ctx.ServerError("GetAssignees", err)
  511. return
  512. }
  513. ctx.Data["CurrentReview"], err = models.GetCurrentReview(ctx.User, issue)
  514. if err != nil && !models.IsErrReviewNotExist(err) {
  515. ctx.ServerError("GetCurrentReview", err)
  516. return
  517. }
  518. getBranchData(ctx, issue)
  519. ctx.HTML(200, tplPullFiles)
  520. }
  521. // UpdatePullRequest merge master into PR
  522. func UpdatePullRequest(ctx *context.Context) {
  523. issue := checkPullInfo(ctx)
  524. if ctx.Written() {
  525. return
  526. }
  527. if issue.IsClosed {
  528. ctx.NotFound("MergePullRequest", nil)
  529. return
  530. }
  531. if issue.PullRequest.HasMerged {
  532. ctx.NotFound("MergePullRequest", nil)
  533. return
  534. }
  535. if err := issue.PullRequest.LoadBaseRepo(); err != nil {
  536. ctx.InternalServerError(err)
  537. return
  538. }
  539. if err := issue.PullRequest.LoadHeadRepo(); err != nil {
  540. ctx.InternalServerError(err)
  541. return
  542. }
  543. allowedUpdate, err := pull_service.IsUserAllowedToUpdate(issue.PullRequest, ctx.User)
  544. if err != nil {
  545. ctx.ServerError("IsUserAllowedToMerge", err)
  546. return
  547. }
  548. // ToDo: add check if maintainers are allowed to change branch ... (need migration & co)
  549. if !allowedUpdate {
  550. ctx.Flash.Error(ctx.Tr("repo.pulls.update_not_allowed"))
  551. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))
  552. return
  553. }
  554. // default merge commit message
  555. message := fmt.Sprintf("Merge branch '%s' into %s", issue.PullRequest.BaseBranch, issue.PullRequest.HeadBranch)
  556. if err = pull_service.Update(issue.PullRequest, ctx.User, message); err != nil {
  557. sanitize := func(x string) string {
  558. runes := []rune(x)
  559. if len(runes) > 512 {
  560. x = "..." + string(runes[len(runes)-512:])
  561. }
  562. return strings.Replace(html.EscapeString(x), "\n", "<br>", -1)
  563. }
  564. if models.IsErrMergeConflicts(err) {
  565. conflictError := err.(models.ErrMergeConflicts)
  566. ctx.Flash.Error(ctx.Tr("repo.pulls.merge_conflict", sanitize(conflictError.StdErr), sanitize(conflictError.StdOut)))
  567. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))
  568. return
  569. }
  570. ctx.Flash.Error(err.Error())
  571. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))
  572. }
  573. time.Sleep(1 * time.Second)
  574. ctx.Flash.Success(ctx.Tr("repo.pulls.update_branch_success"))
  575. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))
  576. }
  577. // MergePullRequest response for merging pull request
  578. func MergePullRequest(ctx *context.Context, form auth.MergePullRequestForm) {
  579. issue := checkPullInfo(ctx)
  580. if ctx.Written() {
  581. return
  582. }
  583. if issue.IsClosed {
  584. if issue.IsPull {
  585. ctx.Flash.Error(ctx.Tr("repo.pulls.is_closed"))
  586. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))
  587. return
  588. }
  589. ctx.Flash.Error(ctx.Tr("repo.issues.closed_title"))
  590. ctx.Redirect(ctx.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index))
  591. return
  592. }
  593. pr := issue.PullRequest
  594. allowedMerge, err := pull_service.IsUserAllowedToMerge(pr, ctx.Repo.Permission, ctx.User)
  595. if err != nil {
  596. ctx.ServerError("IsUserAllowedToMerge", err)
  597. return
  598. }
  599. if !allowedMerge {
  600. ctx.Flash.Error(ctx.Tr("repo.pulls.update_not_allowed"))
  601. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))
  602. return
  603. }
  604. if !pr.CanAutoMerge() {
  605. ctx.Flash.Error(ctx.Tr("repo.pulls.no_merge_not_ready"))
  606. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))
  607. return
  608. }
  609. if pr.HasMerged {
  610. ctx.Flash.Error(ctx.Tr("repo.pulls.has_merged"))
  611. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))
  612. return
  613. }
  614. if pr.IsWorkInProgress() {
  615. ctx.Flash.Error(ctx.Tr("repo.pulls.no_merge_wip"))
  616. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  617. return
  618. }
  619. if err := pull_service.CheckPRReadyToMerge(pr); err != nil {
  620. if !models.IsErrNotAllowedToMerge(err) {
  621. ctx.ServerError("Merge PR status", err)
  622. return
  623. }
  624. if isRepoAdmin, err := models.IsUserRepoAdmin(pr.BaseRepo, ctx.User); err != nil {
  625. ctx.ServerError("IsUserRepoAdmin", err)
  626. return
  627. } else if !isRepoAdmin {
  628. ctx.Flash.Error(ctx.Tr("repo.pulls.no_merge_not_ready"))
  629. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  630. return
  631. }
  632. }
  633. if ctx.HasError() {
  634. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  635. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  636. return
  637. }
  638. message := strings.TrimSpace(form.MergeTitleField)
  639. if len(message) == 0 {
  640. if models.MergeStyle(form.Do) == models.MergeStyleMerge {
  641. message = pr.GetDefaultMergeMessage()
  642. }
  643. if models.MergeStyle(form.Do) == models.MergeStyleRebaseMerge {
  644. message = pr.GetDefaultMergeMessage()
  645. }
  646. if models.MergeStyle(form.Do) == models.MergeStyleSquash {
  647. message = pr.GetDefaultSquashMessage()
  648. }
  649. }
  650. form.MergeMessageField = strings.TrimSpace(form.MergeMessageField)
  651. if len(form.MergeMessageField) > 0 {
  652. message += "\n\n" + form.MergeMessageField
  653. }
  654. pr.Issue = issue
  655. pr.Issue.Repo = ctx.Repo.Repository
  656. noDeps, err := models.IssueNoDependenciesLeft(issue)
  657. if err != nil {
  658. return
  659. }
  660. if !noDeps {
  661. ctx.Flash.Error(ctx.Tr("repo.issues.dependency.pr_close_blocked"))
  662. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  663. return
  664. }
  665. if err = pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil {
  666. sanitize := func(x string) string {
  667. runes := []rune(x)
  668. if len(runes) > 512 {
  669. x = "..." + string(runes[len(runes)-512:])
  670. }
  671. return strings.Replace(html.EscapeString(x), "\n", "<br>", -1)
  672. }
  673. if models.IsErrInvalidMergeStyle(err) {
  674. ctx.Flash.Error(ctx.Tr("repo.pulls.invalid_merge_option"))
  675. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  676. return
  677. } else if models.IsErrMergeConflicts(err) {
  678. conflictError := err.(models.ErrMergeConflicts)
  679. ctx.Flash.Error(ctx.Tr("repo.pulls.merge_conflict", sanitize(conflictError.StdErr), sanitize(conflictError.StdOut)))
  680. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  681. return
  682. } else if models.IsErrRebaseConflicts(err) {
  683. conflictError := err.(models.ErrRebaseConflicts)
  684. ctx.Flash.Error(ctx.Tr("repo.pulls.rebase_conflict", sanitize(conflictError.CommitSHA), sanitize(conflictError.StdErr), sanitize(conflictError.StdOut)))
  685. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  686. return
  687. } else if models.IsErrMergeUnrelatedHistories(err) {
  688. log.Debug("MergeUnrelatedHistories error: %v", err)
  689. ctx.Flash.Error(ctx.Tr("repo.pulls.unrelated_histories"))
  690. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  691. return
  692. } else if models.IsErrMergePushOutOfDate(err) {
  693. log.Debug("MergePushOutOfDate error: %v", err)
  694. ctx.Flash.Error(ctx.Tr("repo.pulls.merge_out_of_date"))
  695. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  696. return
  697. }
  698. ctx.ServerError("Merge", err)
  699. return
  700. }
  701. if err := stopTimerIfAvailable(ctx.User, issue); err != nil {
  702. ctx.ServerError("CreateOrStopIssueStopwatch", err)
  703. return
  704. }
  705. log.Trace("Pull request merged: %d", pr.ID)
  706. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  707. }
  708. func stopTimerIfAvailable(user *models.User, issue *models.Issue) error {
  709. if models.StopwatchExists(user.ID, issue.ID) {
  710. if err := models.CreateOrStopIssueStopwatch(user, issue); err != nil {
  711. return err
  712. }
  713. }
  714. return nil
  715. }
  716. // CompareAndPullRequestPost response for creating pull request
  717. func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm) {
  718. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  719. ctx.Data["PageIsComparePull"] = true
  720. ctx.Data["IsDiffCompare"] = true
  721. ctx.Data["RequireHighlightJS"] = true
  722. ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
  723. renderAttachmentSettings(ctx)
  724. var (
  725. repo = ctx.Repo.Repository
  726. attachments []string
  727. )
  728. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  729. if ctx.Written() {
  730. return
  731. }
  732. defer headGitRepo.Close()
  733. labelIDs, assigneeIDs, milestoneID := ValidateRepoMetas(ctx, form, true)
  734. if ctx.Written() {
  735. return
  736. }
  737. if setting.AttachmentEnabled {
  738. attachments = form.Files
  739. }
  740. if ctx.HasError() {
  741. auth.AssignForm(form, ctx.Data)
  742. // This stage is already stop creating new pull request, so it does not matter if it has
  743. // something to compare or not.
  744. PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  745. if ctx.Written() {
  746. return
  747. }
  748. ctx.HTML(200, tplCompareDiff)
  749. return
  750. }
  751. if util.IsEmptyString(form.Title) {
  752. PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  753. if ctx.Written() {
  754. return
  755. }
  756. ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplCompareDiff, form)
  757. return
  758. }
  759. pullIssue := &models.Issue{
  760. RepoID: repo.ID,
  761. Title: form.Title,
  762. PosterID: ctx.User.ID,
  763. Poster: ctx.User,
  764. MilestoneID: milestoneID,
  765. IsPull: true,
  766. Content: form.Content,
  767. }
  768. pullRequest := &models.PullRequest{
  769. HeadRepoID: headRepo.ID,
  770. BaseRepoID: repo.ID,
  771. HeadBranch: headBranch,
  772. BaseBranch: baseBranch,
  773. HeadRepo: headRepo,
  774. BaseRepo: repo,
  775. MergeBase: prInfo.MergeBase,
  776. Type: models.PullRequestGitea,
  777. }
  778. // FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
  779. // instead of 500.
  780. if err := pull_service.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, assigneeIDs); err != nil {
  781. if models.IsErrUserDoesNotHaveAccessToRepo(err) {
  782. ctx.Error(400, "UserDoesNotHaveAccessToRepo", err.Error())
  783. return
  784. }
  785. ctx.ServerError("NewPullRequest", err)
  786. return
  787. }
  788. log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
  789. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
  790. }
  791. // TriggerTask response for a trigger task request
  792. func TriggerTask(ctx *context.Context) {
  793. pusherID := ctx.QueryInt64("pusher")
  794. branch := ctx.Query("branch")
  795. secret := ctx.Query("secret")
  796. if len(branch) == 0 || len(secret) == 0 || pusherID <= 0 {
  797. ctx.Error(404)
  798. log.Trace("TriggerTask: branch or secret is empty, or pusher ID is not valid")
  799. return
  800. }
  801. owner, repo := parseOwnerAndRepo(ctx)
  802. if ctx.Written() {
  803. return
  804. }
  805. got := []byte(base.EncodeMD5(owner.Salt))
  806. want := []byte(secret)
  807. if subtle.ConstantTimeCompare(got, want) != 1 {
  808. ctx.Error(404)
  809. log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
  810. return
  811. }
  812. pusher, err := models.GetUserByID(pusherID)
  813. if err != nil {
  814. if models.IsErrUserNotExist(err) {
  815. ctx.Error(404)
  816. } else {
  817. ctx.ServerError("GetUserByID", err)
  818. }
  819. return
  820. }
  821. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  822. go pull_service.AddTestPullRequestTask(pusher, repo.ID, branch, true, "", "")
  823. ctx.Status(202)
  824. }
  825. // CleanUpPullRequest responses for delete merged branch when PR has been merged
  826. func CleanUpPullRequest(ctx *context.Context) {
  827. issue := checkPullInfo(ctx)
  828. if ctx.Written() {
  829. return
  830. }
  831. pr := issue.PullRequest
  832. // Don't cleanup unmerged and unclosed PRs
  833. if !pr.HasMerged && !issue.IsClosed {
  834. ctx.NotFound("CleanUpPullRequest", nil)
  835. return
  836. }
  837. if err := pr.GetHeadRepo(); err != nil {
  838. ctx.ServerError("GetHeadRepo", err)
  839. return
  840. } else if pr.HeadRepo == nil {
  841. // Forked repository has already been deleted
  842. ctx.NotFound("CleanUpPullRequest", nil)
  843. return
  844. } else if err = pr.GetBaseRepo(); err != nil {
  845. ctx.ServerError("GetBaseRepo", err)
  846. return
  847. } else if err = pr.HeadRepo.GetOwner(); err != nil {
  848. ctx.ServerError("HeadRepo.GetOwner", err)
  849. return
  850. }
  851. perm, err := models.GetUserRepoPermission(pr.HeadRepo, ctx.User)
  852. if err != nil {
  853. ctx.ServerError("GetUserRepoPermission", err)
  854. return
  855. }
  856. if !perm.CanWrite(models.UnitTypeCode) {
  857. ctx.NotFound("CleanUpPullRequest", nil)
  858. return
  859. }
  860. fullBranchName := pr.HeadRepo.Owner.Name + "/" + pr.HeadBranch
  861. gitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
  862. if err != nil {
  863. ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.RepoPath()), err)
  864. return
  865. }
  866. defer gitRepo.Close()
  867. gitBaseRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
  868. if err != nil {
  869. ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.BaseRepo.RepoPath()), err)
  870. return
  871. }
  872. defer gitBaseRepo.Close()
  873. defer func() {
  874. ctx.JSON(200, map[string]interface{}{
  875. "redirect": pr.BaseRepo.Link() + "/pulls/" + com.ToStr(issue.Index),
  876. })
  877. }()
  878. if pr.HeadBranch == pr.HeadRepo.DefaultBranch || !gitRepo.IsBranchExist(pr.HeadBranch) {
  879. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  880. return
  881. }
  882. // Check if branch is not protected
  883. if protected, err := pr.HeadRepo.IsProtectedBranch(pr.HeadBranch, ctx.User); err != nil || protected {
  884. if err != nil {
  885. log.Error("HeadRepo.IsProtectedBranch: %v", err)
  886. }
  887. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  888. return
  889. }
  890. // Check if branch has no new commits
  891. headCommitID, err := gitBaseRepo.GetRefCommitID(pr.GetGitRefName())
  892. if err != nil {
  893. log.Error("GetRefCommitID: %v", err)
  894. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  895. return
  896. }
  897. branchCommitID, err := gitRepo.GetBranchCommitID(pr.HeadBranch)
  898. if err != nil {
  899. log.Error("GetBranchCommitID: %v", err)
  900. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  901. return
  902. }
  903. if headCommitID != branchCommitID {
  904. ctx.Flash.Error(ctx.Tr("repo.branch.delete_branch_has_new_commits", fullBranchName))
  905. return
  906. }
  907. if err := gitRepo.DeleteBranch(pr.HeadBranch, git.DeleteBranchOptions{
  908. Force: true,
  909. }); err != nil {
  910. log.Error("DeleteBranch: %v", err)
  911. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  912. return
  913. }
  914. if err := repofiles.PushUpdate(
  915. pr.HeadRepo,
  916. pr.HeadBranch,
  917. repofiles.PushUpdateOptions{
  918. RefFullName: git.BranchPrefix + pr.HeadBranch,
  919. OldCommitID: branchCommitID,
  920. NewCommitID: git.EmptySHA,
  921. PusherID: ctx.User.ID,
  922. PusherName: ctx.User.Name,
  923. RepoUserName: pr.HeadRepo.Owner.Name,
  924. RepoName: pr.HeadRepo.Name,
  925. }); err != nil {
  926. log.Error("Update: %v", err)
  927. }
  928. if err := models.AddDeletePRBranchComment(ctx.User, pr.BaseRepo, issue.ID, pr.HeadBranch); err != nil {
  929. // Do not fail here as branch has already been deleted
  930. log.Error("DeleteBranch: %v", err)
  931. }
  932. ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", fullBranchName))
  933. }
  934. // DownloadPullDiff render a pull's raw diff
  935. func DownloadPullDiff(ctx *context.Context) {
  936. DownloadPullDiffOrPatch(ctx, false)
  937. }
  938. // DownloadPullPatch render a pull's raw patch
  939. func DownloadPullPatch(ctx *context.Context) {
  940. DownloadPullDiffOrPatch(ctx, true)
  941. }
  942. // DownloadPullDiffOrPatch render a pull's raw diff or patch
  943. func DownloadPullDiffOrPatch(ctx *context.Context, patch bool) {
  944. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  945. if err != nil {
  946. if models.IsErrIssueNotExist(err) {
  947. ctx.NotFound("GetIssueByIndex", err)
  948. } else {
  949. ctx.ServerError("GetIssueByIndex", err)
  950. }
  951. return
  952. }
  953. // Return not found if it's not a pull request
  954. if !issue.IsPull {
  955. ctx.NotFound("DownloadPullDiff",
  956. fmt.Errorf("Issue is not a pull request"))
  957. return
  958. }
  959. if err = issue.LoadPullRequest(); err != nil {
  960. ctx.ServerError("LoadPullRequest", err)
  961. return
  962. }
  963. pr := issue.PullRequest
  964. if err := pull_service.DownloadDiffOrPatch(pr, ctx, patch); err != nil {
  965. ctx.ServerError("DownloadDiffOrPatch", err)
  966. return
  967. }
  968. }
  969. // UpdatePullRequestTarget change pull request's target branch
  970. func UpdatePullRequestTarget(ctx *context.Context) {
  971. issue := GetActionIssue(ctx)
  972. pr := issue.PullRequest
  973. if ctx.Written() {
  974. return
  975. }
  976. if !issue.IsPull {
  977. ctx.Error(http.StatusNotFound)
  978. return
  979. }
  980. if !ctx.IsSigned || (!issue.IsPoster(ctx.User.ID) && !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)) {
  981. ctx.Error(http.StatusForbidden)
  982. return
  983. }
  984. targetBranch := ctx.QueryTrim("target_branch")
  985. if len(targetBranch) == 0 {
  986. ctx.Error(http.StatusNoContent)
  987. return
  988. }
  989. if err := pull_service.ChangeTargetBranch(pr, ctx.User, targetBranch); err != nil {
  990. if models.IsErrPullRequestAlreadyExists(err) {
  991. err := err.(models.ErrPullRequestAlreadyExists)
  992. RepoRelPath := ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name
  993. errorMessage := ctx.Tr("repo.pulls.has_pull_request", ctx.Repo.RepoLink, RepoRelPath, err.IssueID)
  994. ctx.Flash.Error(errorMessage)
  995. ctx.JSON(http.StatusConflict, map[string]interface{}{
  996. "error": err.Error(),
  997. "user_error": errorMessage,
  998. })
  999. } else if models.IsErrIssueIsClosed(err) {
  1000. errorMessage := ctx.Tr("repo.pulls.is_closed")
  1001. ctx.Flash.Error(errorMessage)
  1002. ctx.JSON(http.StatusConflict, map[string]interface{}{
  1003. "error": err.Error(),
  1004. "user_error": errorMessage,
  1005. })
  1006. } else if models.IsErrPullRequestHasMerged(err) {
  1007. errorMessage := ctx.Tr("repo.pulls.has_merged")
  1008. ctx.Flash.Error(errorMessage)
  1009. ctx.JSON(http.StatusConflict, map[string]interface{}{
  1010. "error": err.Error(),
  1011. "user_error": errorMessage,
  1012. })
  1013. } else if models.IsErrBranchesEqual(err) {
  1014. errorMessage := ctx.Tr("repo.pulls.nothing_to_compare")
  1015. ctx.Flash.Error(errorMessage)
  1016. ctx.JSON(http.StatusBadRequest, map[string]interface{}{
  1017. "error": err.Error(),
  1018. "user_error": errorMessage,
  1019. })
  1020. } else {
  1021. ctx.ServerError("UpdatePullRequestTarget", err)
  1022. }
  1023. return
  1024. }
  1025. notification.NotifyPullRequestChangeTargetBranch(ctx.User, pr, targetBranch)
  1026. ctx.JSON(http.StatusOK, map[string]interface{}{
  1027. "base_branch": pr.BaseBranch,
  1028. })
  1029. }