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 28KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "bufio"
  8. "fmt"
  9. "io/ioutil"
  10. "os"
  11. "path"
  12. "path/filepath"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "code.gitea.io/gitea/modules/git"
  17. "code.gitea.io/gitea/modules/log"
  18. "code.gitea.io/gitea/modules/setting"
  19. api "code.gitea.io/gitea/modules/structs"
  20. "code.gitea.io/gitea/modules/sync"
  21. "code.gitea.io/gitea/modules/timeutil"
  22. "github.com/unknwon/com"
  23. )
  24. var pullRequestQueue = sync.NewUniqueQueue(setting.Repository.PullRequestQueueLength)
  25. // PullRequestType defines pull request type
  26. type PullRequestType int
  27. // Enumerate all the pull request types
  28. const (
  29. PullRequestGitea PullRequestType = iota
  30. PullRequestGit
  31. )
  32. // PullRequestStatus defines pull request status
  33. type PullRequestStatus int
  34. // Enumerate all the pull request status
  35. const (
  36. PullRequestStatusConflict PullRequestStatus = iota
  37. PullRequestStatusChecking
  38. PullRequestStatusMergeable
  39. PullRequestStatusManuallyMerged
  40. )
  41. // PullRequest represents relation between pull request and repositories.
  42. type PullRequest struct {
  43. ID int64 `xorm:"pk autoincr"`
  44. Type PullRequestType
  45. Status PullRequestStatus
  46. ConflictedFiles []string `xorm:"TEXT JSON"`
  47. IssueID int64 `xorm:"INDEX"`
  48. Issue *Issue `xorm:"-"`
  49. Index int64
  50. HeadRepoID int64 `xorm:"INDEX"`
  51. HeadRepo *Repository `xorm:"-"`
  52. BaseRepoID int64 `xorm:"INDEX"`
  53. BaseRepo *Repository `xorm:"-"`
  54. HeadBranch string
  55. BaseBranch string
  56. ProtectedBranch *ProtectedBranch `xorm:"-"`
  57. MergeBase string `xorm:"VARCHAR(40)"`
  58. HasMerged bool `xorm:"INDEX"`
  59. MergedCommitID string `xorm:"VARCHAR(40)"`
  60. MergerID int64 `xorm:"INDEX"`
  61. Merger *User `xorm:"-"`
  62. MergedUnix timeutil.TimeStamp `xorm:"updated INDEX"`
  63. }
  64. // MustHeadUserName returns the HeadRepo's username if failed return blank
  65. func (pr *PullRequest) MustHeadUserName() string {
  66. if err := pr.LoadHeadRepo(); err != nil {
  67. log.Error("LoadHeadRepo: %v", err)
  68. return ""
  69. }
  70. return pr.HeadRepo.MustOwnerName()
  71. }
  72. // Note: don't try to get Issue because will end up recursive querying.
  73. func (pr *PullRequest) loadAttributes(e Engine) (err error) {
  74. if pr.HasMerged && pr.Merger == nil {
  75. pr.Merger, err = getUserByID(e, pr.MergerID)
  76. if IsErrUserNotExist(err) {
  77. pr.MergerID = -1
  78. pr.Merger = NewGhostUser()
  79. } else if err != nil {
  80. return fmt.Errorf("getUserByID [%d]: %v", pr.MergerID, err)
  81. }
  82. }
  83. return nil
  84. }
  85. // LoadAttributes loads pull request attributes from database
  86. func (pr *PullRequest) LoadAttributes() error {
  87. return pr.loadAttributes(x)
  88. }
  89. // LoadBaseRepo loads pull request base repository from database
  90. func (pr *PullRequest) LoadBaseRepo() error {
  91. if pr.BaseRepo == nil {
  92. if pr.HeadRepoID == pr.BaseRepoID && pr.HeadRepo != nil {
  93. pr.BaseRepo = pr.HeadRepo
  94. return nil
  95. }
  96. var repo Repository
  97. if has, err := x.ID(pr.BaseRepoID).Get(&repo); err != nil {
  98. return err
  99. } else if !has {
  100. return ErrRepoNotExist{ID: pr.BaseRepoID}
  101. }
  102. pr.BaseRepo = &repo
  103. }
  104. return nil
  105. }
  106. // LoadHeadRepo loads pull request head repository from database
  107. func (pr *PullRequest) LoadHeadRepo() error {
  108. if pr.HeadRepo == nil {
  109. if pr.HeadRepoID == pr.BaseRepoID && pr.BaseRepo != nil {
  110. pr.HeadRepo = pr.BaseRepo
  111. return nil
  112. }
  113. var repo Repository
  114. if has, err := x.ID(pr.HeadRepoID).Get(&repo); err != nil {
  115. return err
  116. } else if !has {
  117. return ErrRepoNotExist{ID: pr.BaseRepoID}
  118. }
  119. pr.HeadRepo = &repo
  120. }
  121. return nil
  122. }
  123. // LoadIssue loads issue information from database
  124. func (pr *PullRequest) LoadIssue() (err error) {
  125. return pr.loadIssue(x)
  126. }
  127. func (pr *PullRequest) loadIssue(e Engine) (err error) {
  128. if pr.Issue != nil {
  129. return nil
  130. }
  131. pr.Issue, err = getIssueByID(e, pr.IssueID)
  132. if err == nil {
  133. pr.Issue.PullRequest = pr
  134. }
  135. return err
  136. }
  137. // LoadProtectedBranch loads the protected branch of the base branch
  138. func (pr *PullRequest) LoadProtectedBranch() (err error) {
  139. return pr.loadProtectedBranch(x)
  140. }
  141. func (pr *PullRequest) loadProtectedBranch(e Engine) (err error) {
  142. if pr.BaseRepo == nil {
  143. if pr.BaseRepoID == 0 {
  144. return nil
  145. }
  146. pr.BaseRepo, err = getRepositoryByID(e, pr.BaseRepoID)
  147. if err != nil {
  148. return
  149. }
  150. }
  151. pr.ProtectedBranch, err = getProtectedBranchBy(e, pr.BaseRepo.ID, pr.BaseBranch)
  152. return
  153. }
  154. // GetDefaultMergeMessage returns default message used when merging pull request
  155. func (pr *PullRequest) GetDefaultMergeMessage() string {
  156. if pr.HeadRepo == nil {
  157. var err error
  158. pr.HeadRepo, err = GetRepositoryByID(pr.HeadRepoID)
  159. if err != nil {
  160. log.Error("GetRepositoryById[%d]: %v", pr.HeadRepoID, err)
  161. return ""
  162. }
  163. }
  164. return fmt.Sprintf("Merge branch '%s' of %s/%s into %s", pr.HeadBranch, pr.MustHeadUserName(), pr.HeadRepo.Name, pr.BaseBranch)
  165. }
  166. // GetDefaultSquashMessage returns default message used when squash and merging pull request
  167. func (pr *PullRequest) GetDefaultSquashMessage() string {
  168. if err := pr.LoadIssue(); err != nil {
  169. log.Error("LoadIssue: %v", err)
  170. return ""
  171. }
  172. return fmt.Sprintf("%s (#%d)", pr.Issue.Title, pr.Issue.Index)
  173. }
  174. // GetGitRefName returns git ref for hidden pull request branch
  175. func (pr *PullRequest) GetGitRefName() string {
  176. return fmt.Sprintf("refs/pull/%d/head", pr.Index)
  177. }
  178. // APIFormat assumes following fields have been assigned with valid values:
  179. // Required - Issue
  180. // Optional - Merger
  181. func (pr *PullRequest) APIFormat() *api.PullRequest {
  182. return pr.apiFormat(x)
  183. }
  184. func (pr *PullRequest) apiFormat(e Engine) *api.PullRequest {
  185. var (
  186. baseBranch *git.Branch
  187. headBranch *git.Branch
  188. baseCommit *git.Commit
  189. headCommit *git.Commit
  190. err error
  191. )
  192. if err = pr.Issue.loadRepo(e); err != nil {
  193. log.Error("loadRepo[%d]: %v", pr.ID, err)
  194. return nil
  195. }
  196. apiIssue := pr.Issue.apiFormat(e)
  197. if pr.BaseRepo == nil {
  198. pr.BaseRepo, err = getRepositoryByID(e, pr.BaseRepoID)
  199. if err != nil {
  200. log.Error("GetRepositoryById[%d]: %v", pr.ID, err)
  201. return nil
  202. }
  203. }
  204. if pr.HeadRepo == nil {
  205. pr.HeadRepo, err = getRepositoryByID(e, pr.HeadRepoID)
  206. if err != nil {
  207. log.Error("GetRepositoryById[%d]: %v", pr.ID, err)
  208. return nil
  209. }
  210. }
  211. if err = pr.Issue.loadRepo(e); err != nil {
  212. log.Error("pr.Issue.loadRepo[%d]: %v", pr.ID, err)
  213. return nil
  214. }
  215. apiPullRequest := &api.PullRequest{
  216. ID: pr.ID,
  217. URL: pr.Issue.HTMLURL(),
  218. Index: pr.Index,
  219. Poster: apiIssue.Poster,
  220. Title: apiIssue.Title,
  221. Body: apiIssue.Body,
  222. Labels: apiIssue.Labels,
  223. Milestone: apiIssue.Milestone,
  224. Assignee: apiIssue.Assignee,
  225. Assignees: apiIssue.Assignees,
  226. State: apiIssue.State,
  227. Comments: apiIssue.Comments,
  228. HTMLURL: pr.Issue.HTMLURL(),
  229. DiffURL: pr.Issue.DiffURL(),
  230. PatchURL: pr.Issue.PatchURL(),
  231. HasMerged: pr.HasMerged,
  232. MergeBase: pr.MergeBase,
  233. Deadline: apiIssue.Deadline,
  234. Created: pr.Issue.CreatedUnix.AsTimePtr(),
  235. Updated: pr.Issue.UpdatedUnix.AsTimePtr(),
  236. }
  237. baseBranch, err = pr.BaseRepo.GetBranch(pr.BaseBranch)
  238. if err != nil {
  239. if git.IsErrBranchNotExist(err) {
  240. apiPullRequest.Base = nil
  241. } else {
  242. log.Error("GetBranch[%s]: %v", pr.BaseBranch, err)
  243. return nil
  244. }
  245. } else {
  246. apiBaseBranchInfo := &api.PRBranchInfo{
  247. Name: pr.BaseBranch,
  248. Ref: pr.BaseBranch,
  249. RepoID: pr.BaseRepoID,
  250. Repository: pr.BaseRepo.innerAPIFormat(e, AccessModeNone, false),
  251. }
  252. baseCommit, err = baseBranch.GetCommit()
  253. if err != nil {
  254. if git.IsErrNotExist(err) {
  255. apiBaseBranchInfo.Sha = ""
  256. } else {
  257. log.Error("GetCommit[%s]: %v", baseBranch.Name, err)
  258. return nil
  259. }
  260. } else {
  261. apiBaseBranchInfo.Sha = baseCommit.ID.String()
  262. }
  263. apiPullRequest.Base = apiBaseBranchInfo
  264. }
  265. headBranch, err = pr.HeadRepo.GetBranch(pr.HeadBranch)
  266. if err != nil {
  267. if git.IsErrBranchNotExist(err) {
  268. apiPullRequest.Head = nil
  269. } else {
  270. log.Error("GetBranch[%s]: %v", pr.HeadBranch, err)
  271. return nil
  272. }
  273. } else {
  274. apiHeadBranchInfo := &api.PRBranchInfo{
  275. Name: pr.HeadBranch,
  276. Ref: pr.HeadBranch,
  277. RepoID: pr.HeadRepoID,
  278. Repository: pr.HeadRepo.innerAPIFormat(e, AccessModeNone, false),
  279. }
  280. headCommit, err = headBranch.GetCommit()
  281. if err != nil {
  282. if git.IsErrNotExist(err) {
  283. apiHeadBranchInfo.Sha = ""
  284. } else {
  285. log.Error("GetCommit[%s]: %v", headBranch.Name, err)
  286. return nil
  287. }
  288. } else {
  289. apiHeadBranchInfo.Sha = headCommit.ID.String()
  290. }
  291. apiPullRequest.Head = apiHeadBranchInfo
  292. }
  293. if pr.Status != PullRequestStatusChecking {
  294. mergeable := pr.Status != PullRequestStatusConflict && !pr.IsWorkInProgress()
  295. apiPullRequest.Mergeable = mergeable
  296. }
  297. if pr.HasMerged {
  298. apiPullRequest.Merged = pr.MergedUnix.AsTimePtr()
  299. apiPullRequest.MergedCommitID = &pr.MergedCommitID
  300. apiPullRequest.MergedBy = pr.Merger.APIFormat()
  301. }
  302. return apiPullRequest
  303. }
  304. func (pr *PullRequest) getHeadRepo(e Engine) (err error) {
  305. pr.HeadRepo, err = getRepositoryByID(e, pr.HeadRepoID)
  306. if err != nil && !IsErrRepoNotExist(err) {
  307. return fmt.Errorf("getRepositoryByID(head): %v", err)
  308. }
  309. return nil
  310. }
  311. // GetHeadRepo loads the head repository
  312. func (pr *PullRequest) GetHeadRepo() error {
  313. return pr.getHeadRepo(x)
  314. }
  315. // GetBaseRepo loads the target repository
  316. func (pr *PullRequest) GetBaseRepo() (err error) {
  317. if pr.BaseRepo != nil {
  318. return nil
  319. }
  320. pr.BaseRepo, err = GetRepositoryByID(pr.BaseRepoID)
  321. if err != nil {
  322. return fmt.Errorf("GetRepositoryByID(base): %v", err)
  323. }
  324. return nil
  325. }
  326. // IsChecking returns true if this pull request is still checking conflict.
  327. func (pr *PullRequest) IsChecking() bool {
  328. return pr.Status == PullRequestStatusChecking
  329. }
  330. // CanAutoMerge returns true if this pull request can be merged automatically.
  331. func (pr *PullRequest) CanAutoMerge() bool {
  332. return pr.Status == PullRequestStatusMergeable
  333. }
  334. // GetLastCommitStatus returns the last commit status for this pull request.
  335. func (pr *PullRequest) GetLastCommitStatus() (status *CommitStatus, err error) {
  336. if err = pr.GetHeadRepo(); err != nil {
  337. return nil, err
  338. }
  339. if pr.HeadRepo == nil {
  340. return nil, ErrPullRequestHeadRepoMissing{pr.ID, pr.HeadRepoID}
  341. }
  342. headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
  343. if err != nil {
  344. return nil, err
  345. }
  346. defer headGitRepo.Close()
  347. lastCommitID, err := headGitRepo.GetBranchCommitID(pr.HeadBranch)
  348. if err != nil {
  349. return nil, err
  350. }
  351. err = pr.LoadBaseRepo()
  352. if err != nil {
  353. return nil, err
  354. }
  355. statusList, err := GetLatestCommitStatus(pr.BaseRepo, lastCommitID, 0)
  356. if err != nil {
  357. return nil, err
  358. }
  359. return CalcCommitStatus(statusList), nil
  360. }
  361. // MergeStyle represents the approach to merge commits into base branch.
  362. type MergeStyle string
  363. const (
  364. // MergeStyleMerge create merge commit
  365. MergeStyleMerge MergeStyle = "merge"
  366. // MergeStyleRebase rebase before merging
  367. MergeStyleRebase MergeStyle = "rebase"
  368. // MergeStyleRebaseMerge rebase before merging with merge commit (--no-ff)
  369. MergeStyleRebaseMerge MergeStyle = "rebase-merge"
  370. // MergeStyleSquash squash commits into single commit before merging
  371. MergeStyleSquash MergeStyle = "squash"
  372. )
  373. // CheckUserAllowedToMerge checks whether the user is allowed to merge
  374. func (pr *PullRequest) CheckUserAllowedToMerge(doer *User) (err error) {
  375. if doer == nil {
  376. return ErrNotAllowedToMerge{
  377. "Not signed in",
  378. }
  379. }
  380. if pr.BaseRepo == nil {
  381. if err = pr.GetBaseRepo(); err != nil {
  382. return fmt.Errorf("GetBaseRepo: %v", err)
  383. }
  384. }
  385. if protected, err := pr.BaseRepo.IsProtectedBranchForMerging(pr, pr.BaseBranch, doer); err != nil {
  386. return fmt.Errorf("IsProtectedBranch: %v", err)
  387. } else if protected {
  388. return ErrNotAllowedToMerge{
  389. "The branch is protected",
  390. }
  391. }
  392. return nil
  393. }
  394. // SetMerged sets a pull request to merged and closes the corresponding issue
  395. func (pr *PullRequest) SetMerged() (err error) {
  396. if pr.HasMerged {
  397. return fmt.Errorf("PullRequest[%d] already merged", pr.Index)
  398. }
  399. if pr.MergedCommitID == "" || pr.MergedUnix == 0 || pr.Merger == nil {
  400. return fmt.Errorf("Unable to merge PullRequest[%d], some required fields are empty", pr.Index)
  401. }
  402. pr.HasMerged = true
  403. sess := x.NewSession()
  404. defer sess.Close()
  405. if err = sess.Begin(); err != nil {
  406. return err
  407. }
  408. if err = pr.loadIssue(sess); err != nil {
  409. return err
  410. }
  411. if err = pr.Issue.loadRepo(sess); err != nil {
  412. return err
  413. }
  414. if err = pr.Issue.Repo.getOwner(sess); err != nil {
  415. return err
  416. }
  417. if err = pr.Issue.changeStatus(sess, pr.Merger, true); err != nil {
  418. return fmt.Errorf("Issue.changeStatus: %v", err)
  419. }
  420. if _, err = sess.ID(pr.ID).Cols("has_merged, status, merged_commit_id, merger_id, merged_unix").Update(pr); err != nil {
  421. return fmt.Errorf("update pull request: %v", err)
  422. }
  423. if err = sess.Commit(); err != nil {
  424. return fmt.Errorf("Commit: %v", err)
  425. }
  426. return nil
  427. }
  428. // manuallyMerged checks if a pull request got manually merged
  429. // When a pull request got manually merged mark the pull request as merged
  430. func (pr *PullRequest) manuallyMerged() bool {
  431. commit, err := pr.getMergeCommit()
  432. if err != nil {
  433. log.Error("PullRequest[%d].getMergeCommit: %v", pr.ID, err)
  434. return false
  435. }
  436. if commit != nil {
  437. pr.MergedCommitID = commit.ID.String()
  438. pr.MergedUnix = timeutil.TimeStamp(commit.Author.When.Unix())
  439. pr.Status = PullRequestStatusManuallyMerged
  440. merger, _ := GetUserByEmail(commit.Author.Email)
  441. // When the commit author is unknown set the BaseRepo owner as merger
  442. if merger == nil {
  443. if pr.BaseRepo.Owner == nil {
  444. if err = pr.BaseRepo.getOwner(x); err != nil {
  445. log.Error("BaseRepo.getOwner[%d]: %v", pr.ID, err)
  446. return false
  447. }
  448. }
  449. merger = pr.BaseRepo.Owner
  450. }
  451. pr.Merger = merger
  452. pr.MergerID = merger.ID
  453. if err = pr.SetMerged(); err != nil {
  454. log.Error("PullRequest[%d].setMerged : %v", pr.ID, err)
  455. return false
  456. }
  457. log.Info("manuallyMerged[%d]: Marked as manually merged into %s/%s by commit id: %s", pr.ID, pr.BaseRepo.Name, pr.BaseBranch, commit.ID.String())
  458. return true
  459. }
  460. return false
  461. }
  462. // getMergeCommit checks if a pull request got merged
  463. // Returns the git.Commit of the pull request if merged
  464. func (pr *PullRequest) getMergeCommit() (*git.Commit, error) {
  465. if pr.BaseRepo == nil {
  466. var err error
  467. pr.BaseRepo, err = GetRepositoryByID(pr.BaseRepoID)
  468. if err != nil {
  469. return nil, fmt.Errorf("GetRepositoryByID: %v", err)
  470. }
  471. }
  472. indexTmpPath := filepath.Join(os.TempDir(), "gitea-"+pr.BaseRepo.Name+"-"+strconv.Itoa(time.Now().Nanosecond()))
  473. defer os.Remove(indexTmpPath)
  474. headFile := pr.GetGitRefName()
  475. // Check if a pull request is merged into BaseBranch
  476. _, err := git.NewCommand("merge-base", "--is-ancestor", headFile, pr.BaseBranch).RunInDirWithEnv(pr.BaseRepo.RepoPath(), []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()})
  477. if err != nil {
  478. // Errors are signaled by a non-zero status that is not 1
  479. if strings.Contains(err.Error(), "exit status 1") {
  480. return nil, nil
  481. }
  482. return nil, fmt.Errorf("git merge-base --is-ancestor: %v", err)
  483. }
  484. commitIDBytes, err := ioutil.ReadFile(pr.BaseRepo.RepoPath() + "/" + headFile)
  485. if err != nil {
  486. return nil, fmt.Errorf("ReadFile(%s): %v", headFile, err)
  487. }
  488. commitID := string(commitIDBytes)
  489. if len(commitID) < 40 {
  490. return nil, fmt.Errorf(`ReadFile(%s): invalid commit-ID "%s"`, headFile, commitID)
  491. }
  492. cmd := commitID[:40] + ".." + pr.BaseBranch
  493. // Get the commit from BaseBranch where the pull request got merged
  494. mergeCommit, err := git.NewCommand("rev-list", "--ancestry-path", "--merges", "--reverse", cmd).RunInDirWithEnv("", []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()})
  495. if err != nil {
  496. return nil, fmt.Errorf("git rev-list --ancestry-path --merges --reverse: %v", err)
  497. } else if len(mergeCommit) < 40 {
  498. // PR was fast-forwarded, so just use last commit of PR
  499. mergeCommit = commitID[:40]
  500. }
  501. gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
  502. if err != nil {
  503. return nil, fmt.Errorf("OpenRepository: %v", err)
  504. }
  505. defer gitRepo.Close()
  506. commit, err := gitRepo.GetCommit(mergeCommit[:40])
  507. if err != nil {
  508. return nil, fmt.Errorf("GetCommit: %v", err)
  509. }
  510. return commit, nil
  511. }
  512. // patchConflicts is a list of conflict description from Git.
  513. var patchConflicts = []string{
  514. "patch does not apply",
  515. "already exists in working directory",
  516. "unrecognized input",
  517. "error:",
  518. }
  519. // testPatch checks if patch can be merged to base repository without conflict.
  520. func (pr *PullRequest) testPatch(e Engine) (err error) {
  521. if pr.BaseRepo == nil {
  522. pr.BaseRepo, err = getRepositoryByID(e, pr.BaseRepoID)
  523. if err != nil {
  524. return fmt.Errorf("GetRepositoryByID: %v", err)
  525. }
  526. }
  527. patchPath, err := pr.BaseRepo.patchPath(e, pr.Index)
  528. if err != nil {
  529. return fmt.Errorf("BaseRepo.PatchPath: %v", err)
  530. }
  531. // Fast fail if patch does not exist, this assumes data is corrupted.
  532. if !com.IsFile(patchPath) {
  533. log.Trace("PullRequest[%d].testPatch: ignored corrupted data", pr.ID)
  534. return nil
  535. }
  536. repoWorkingPool.CheckIn(com.ToStr(pr.BaseRepoID))
  537. defer repoWorkingPool.CheckOut(com.ToStr(pr.BaseRepoID))
  538. log.Trace("PullRequest[%d].testPatch (patchPath): %s", pr.ID, patchPath)
  539. pr.Status = PullRequestStatusChecking
  540. indexTmpPath := filepath.Join(os.TempDir(), "gitea-"+pr.BaseRepo.Name+"-"+strconv.Itoa(time.Now().Nanosecond()))
  541. defer os.Remove(indexTmpPath)
  542. _, err = git.NewCommand("read-tree", pr.BaseBranch).RunInDirWithEnv("", []string{"GIT_DIR=" + pr.BaseRepo.RepoPath(), "GIT_INDEX_FILE=" + indexTmpPath})
  543. if err != nil {
  544. return fmt.Errorf("git read-tree --index-output=%s %s: %v", indexTmpPath, pr.BaseBranch, err)
  545. }
  546. prUnit, err := pr.BaseRepo.getUnit(e, UnitTypePullRequests)
  547. if err != nil {
  548. return err
  549. }
  550. prConfig := prUnit.PullRequestsConfig()
  551. args := []string{"apply", "--check", "--cached"}
  552. if prConfig.IgnoreWhitespaceConflicts {
  553. args = append(args, "--ignore-whitespace")
  554. }
  555. args = append(args, patchPath)
  556. pr.ConflictedFiles = []string{}
  557. stderrBuilder := new(strings.Builder)
  558. err = git.NewCommand(args...).RunInDirTimeoutEnvPipeline(
  559. []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()},
  560. -1,
  561. "",
  562. nil,
  563. stderrBuilder)
  564. stderr := stderrBuilder.String()
  565. if err != nil {
  566. for i := range patchConflicts {
  567. if strings.Contains(stderr, patchConflicts[i]) {
  568. log.Trace("PullRequest[%d].testPatch (apply): has conflict: %s", pr.ID, stderr)
  569. const prefix = "error: patch failed:"
  570. pr.Status = PullRequestStatusConflict
  571. pr.ConflictedFiles = make([]string, 0, 5)
  572. scanner := bufio.NewScanner(strings.NewReader(stderr))
  573. for scanner.Scan() {
  574. line := scanner.Text()
  575. if strings.HasPrefix(line, prefix) {
  576. var found bool
  577. var filepath = strings.TrimSpace(strings.Split(line[len(prefix):], ":")[0])
  578. for _, f := range pr.ConflictedFiles {
  579. if f == filepath {
  580. found = true
  581. break
  582. }
  583. }
  584. if !found {
  585. pr.ConflictedFiles = append(pr.ConflictedFiles, filepath)
  586. }
  587. }
  588. // only list 10 conflicted files
  589. if len(pr.ConflictedFiles) >= 10 {
  590. break
  591. }
  592. }
  593. if len(pr.ConflictedFiles) > 0 {
  594. log.Trace("Found %d files conflicted: %v", len(pr.ConflictedFiles), pr.ConflictedFiles)
  595. }
  596. return nil
  597. }
  598. }
  599. return fmt.Errorf("git apply --check: %v - %s", err, stderr)
  600. }
  601. return nil
  602. }
  603. // NewPullRequest creates new pull request with labels for repository.
  604. func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest, patch []byte) (err error) {
  605. // Retry several times in case INSERT fails due to duplicate key for (repo_id, index); see #7887
  606. i := 0
  607. for {
  608. if err = newPullRequestAttempt(repo, pull, labelIDs, uuids, pr, patch); err == nil {
  609. return nil
  610. }
  611. if !IsErrNewIssueInsert(err) {
  612. return err
  613. }
  614. if i++; i == issueMaxDupIndexAttempts {
  615. break
  616. }
  617. log.Error("NewPullRequest: error attempting to insert the new issue; will retry. Original error: %v", err)
  618. }
  619. return fmt.Errorf("NewPullRequest: too many errors attempting to insert the new issue. Last error was: %v", err)
  620. }
  621. func newPullRequestAttempt(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest, patch []byte) (err error) {
  622. sess := x.NewSession()
  623. defer sess.Close()
  624. if err = sess.Begin(); err != nil {
  625. return err
  626. }
  627. if err = newIssue(sess, pull.Poster, NewIssueOptions{
  628. Repo: repo,
  629. Issue: pull,
  630. LabelIDs: labelIDs,
  631. Attachments: uuids,
  632. IsPull: true,
  633. }); err != nil {
  634. if IsErrUserDoesNotHaveAccessToRepo(err) || IsErrNewIssueInsert(err) {
  635. return err
  636. }
  637. return fmt.Errorf("newIssue: %v", err)
  638. }
  639. pr.Index = pull.Index
  640. pr.BaseRepo = repo
  641. pr.Status = PullRequestStatusChecking
  642. if len(patch) > 0 {
  643. if err = repo.savePatch(sess, pr.Index, patch); err != nil {
  644. return fmt.Errorf("SavePatch: %v", err)
  645. }
  646. if err = pr.testPatch(sess); err != nil {
  647. return fmt.Errorf("testPatch: %v", err)
  648. }
  649. }
  650. // No conflict appears after test means mergeable.
  651. if pr.Status == PullRequestStatusChecking {
  652. pr.Status = PullRequestStatusMergeable
  653. }
  654. pr.IssueID = pull.ID
  655. if _, err = sess.Insert(pr); err != nil {
  656. return fmt.Errorf("insert pull repo: %v", err)
  657. }
  658. if err = sess.Commit(); err != nil {
  659. return fmt.Errorf("Commit: %v", err)
  660. }
  661. return nil
  662. }
  663. // GetUnmergedPullRequest returns a pull request that is open and has not been merged
  664. // by given head/base and repo/branch.
  665. func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch string) (*PullRequest, error) {
  666. pr := new(PullRequest)
  667. has, err := x.
  668. Where("head_repo_id=? AND head_branch=? AND base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
  669. headRepoID, headBranch, baseRepoID, baseBranch, false, false).
  670. Join("INNER", "issue", "issue.id=pull_request.issue_id").
  671. Get(pr)
  672. if err != nil {
  673. return nil, err
  674. } else if !has {
  675. return nil, ErrPullRequestNotExist{0, 0, headRepoID, baseRepoID, headBranch, baseBranch}
  676. }
  677. return pr, nil
  678. }
  679. // GetLatestPullRequestByHeadInfo returns the latest pull request (regardless of its status)
  680. // by given head information (repo and branch).
  681. func GetLatestPullRequestByHeadInfo(repoID int64, branch string) (*PullRequest, error) {
  682. pr := new(PullRequest)
  683. has, err := x.
  684. Where("head_repo_id = ? AND head_branch = ?", repoID, branch).
  685. OrderBy("id DESC").
  686. Get(pr)
  687. if !has {
  688. return nil, err
  689. }
  690. return pr, err
  691. }
  692. // GetPullRequestByIndex returns a pull request by the given index
  693. func GetPullRequestByIndex(repoID int64, index int64) (*PullRequest, error) {
  694. pr := &PullRequest{
  695. BaseRepoID: repoID,
  696. Index: index,
  697. }
  698. has, err := x.Get(pr)
  699. if err != nil {
  700. return nil, err
  701. } else if !has {
  702. return nil, ErrPullRequestNotExist{0, 0, 0, repoID, "", ""}
  703. }
  704. if err = pr.LoadAttributes(); err != nil {
  705. return nil, err
  706. }
  707. if err = pr.LoadIssue(); err != nil {
  708. return nil, err
  709. }
  710. return pr, nil
  711. }
  712. func getPullRequestByID(e Engine, id int64) (*PullRequest, error) {
  713. pr := new(PullRequest)
  714. has, err := e.ID(id).Get(pr)
  715. if err != nil {
  716. return nil, err
  717. } else if !has {
  718. return nil, ErrPullRequestNotExist{id, 0, 0, 0, "", ""}
  719. }
  720. return pr, pr.loadAttributes(e)
  721. }
  722. // GetPullRequestByID returns a pull request by given ID.
  723. func GetPullRequestByID(id int64) (*PullRequest, error) {
  724. return getPullRequestByID(x, id)
  725. }
  726. func getPullRequestByIssueID(e Engine, issueID int64) (*PullRequest, error) {
  727. pr := &PullRequest{
  728. IssueID: issueID,
  729. }
  730. has, err := e.Get(pr)
  731. if err != nil {
  732. return nil, err
  733. } else if !has {
  734. return nil, ErrPullRequestNotExist{0, issueID, 0, 0, "", ""}
  735. }
  736. return pr, pr.loadAttributes(e)
  737. }
  738. // GetPullRequestByIssueID returns pull request by given issue ID.
  739. func GetPullRequestByIssueID(issueID int64) (*PullRequest, error) {
  740. return getPullRequestByIssueID(x, issueID)
  741. }
  742. // Update updates all fields of pull request.
  743. func (pr *PullRequest) Update() error {
  744. _, err := x.ID(pr.ID).AllCols().Update(pr)
  745. return err
  746. }
  747. // UpdateCols updates specific fields of pull request.
  748. func (pr *PullRequest) UpdateCols(cols ...string) error {
  749. _, err := x.ID(pr.ID).Cols(cols...).Update(pr)
  750. return err
  751. }
  752. // UpdatePatch generates and saves a new patch.
  753. func (pr *PullRequest) UpdatePatch() (err error) {
  754. if err = pr.GetHeadRepo(); err != nil {
  755. return fmt.Errorf("GetHeadRepo: %v", err)
  756. } else if pr.HeadRepo == nil {
  757. log.Trace("PullRequest[%d].UpdatePatch: ignored corrupted data", pr.ID)
  758. return nil
  759. }
  760. if err = pr.GetBaseRepo(); err != nil {
  761. return fmt.Errorf("GetBaseRepo: %v", err)
  762. }
  763. headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
  764. if err != nil {
  765. return fmt.Errorf("OpenRepository: %v", err)
  766. }
  767. defer headGitRepo.Close()
  768. // Add a temporary remote.
  769. tmpRemote := com.ToStr(time.Now().UnixNano())
  770. if err = headGitRepo.AddRemote(tmpRemote, RepoPath(pr.BaseRepo.MustOwner().Name, pr.BaseRepo.Name), true); err != nil {
  771. return fmt.Errorf("AddRemote: %v", err)
  772. }
  773. defer func() {
  774. if err := headGitRepo.RemoveRemote(tmpRemote); err != nil {
  775. log.Error("UpdatePatch: RemoveRemote: %s", err)
  776. }
  777. }()
  778. pr.MergeBase, _, err = headGitRepo.GetMergeBase(tmpRemote, pr.BaseBranch, pr.HeadBranch)
  779. if err != nil {
  780. return fmt.Errorf("GetMergeBase: %v", err)
  781. } else if err = pr.Update(); err != nil {
  782. return fmt.Errorf("Update: %v", err)
  783. }
  784. patch, err := headGitRepo.GetPatch(pr.MergeBase, pr.HeadBranch)
  785. if err != nil {
  786. return fmt.Errorf("GetPatch: %v", err)
  787. }
  788. if err = pr.BaseRepo.SavePatch(pr.Index, patch); err != nil {
  789. return fmt.Errorf("BaseRepo.SavePatch: %v", err)
  790. }
  791. return nil
  792. }
  793. // PushToBaseRepo pushes commits from branches of head repository to
  794. // corresponding branches of base repository.
  795. // FIXME: Only push branches that are actually updates?
  796. func (pr *PullRequest) PushToBaseRepo() (err error) {
  797. log.Trace("PushToBaseRepo[%d]: pushing commits to base repo '%s'", pr.BaseRepoID, pr.GetGitRefName())
  798. headRepoPath := pr.HeadRepo.RepoPath()
  799. headGitRepo, err := git.OpenRepository(headRepoPath)
  800. if err != nil {
  801. return fmt.Errorf("OpenRepository: %v", err)
  802. }
  803. defer headGitRepo.Close()
  804. tmpRemoteName := fmt.Sprintf("tmp-pull-%d", pr.ID)
  805. if err = headGitRepo.AddRemote(tmpRemoteName, pr.BaseRepo.RepoPath(), false); err != nil {
  806. return fmt.Errorf("headGitRepo.AddRemote: %v", err)
  807. }
  808. // Make sure to remove the remote even if the push fails
  809. defer func() {
  810. if err := headGitRepo.RemoveRemote(tmpRemoteName); err != nil {
  811. log.Error("PushToBaseRepo: RemoveRemote: %s", err)
  812. }
  813. }()
  814. headFile := pr.GetGitRefName()
  815. // Remove head in case there is a conflict.
  816. file := path.Join(pr.BaseRepo.RepoPath(), headFile)
  817. _ = os.Remove(file)
  818. if err = git.Push(headRepoPath, git.PushOptions{
  819. Remote: tmpRemoteName,
  820. Branch: fmt.Sprintf("%s:%s", pr.HeadBranch, headFile),
  821. Force: true,
  822. }); err != nil {
  823. return fmt.Errorf("Push: %v", err)
  824. }
  825. return nil
  826. }
  827. // AddToTaskQueue adds itself to pull request test task queue.
  828. func (pr *PullRequest) AddToTaskQueue() {
  829. go pullRequestQueue.AddFunc(pr.ID, func() {
  830. pr.Status = PullRequestStatusChecking
  831. if err := pr.UpdateCols("status"); err != nil {
  832. log.Error("AddToTaskQueue.UpdateCols[%d].(add to queue): %v", pr.ID, err)
  833. }
  834. })
  835. }
  836. // checkAndUpdateStatus checks if pull request is possible to leaving checking status,
  837. // and set to be either conflict or mergeable.
  838. func (pr *PullRequest) checkAndUpdateStatus() {
  839. // Status is not changed to conflict means mergeable.
  840. if pr.Status == PullRequestStatusChecking {
  841. pr.Status = PullRequestStatusMergeable
  842. }
  843. // Make sure there is no waiting test to process before leaving the checking status.
  844. if !pullRequestQueue.Exist(pr.ID) {
  845. if err := pr.UpdateCols("status, conflicted_files"); err != nil {
  846. log.Error("Update[%d]: %v", pr.ID, err)
  847. }
  848. }
  849. }
  850. // IsWorkInProgress determine if the Pull Request is a Work In Progress by its title
  851. func (pr *PullRequest) IsWorkInProgress() bool {
  852. if err := pr.LoadIssue(); err != nil {
  853. log.Error("LoadIssue: %v", err)
  854. return false
  855. }
  856. for _, prefix := range setting.Repository.PullRequest.WorkInProgressPrefixes {
  857. if strings.HasPrefix(strings.ToUpper(pr.Issue.Title), prefix) {
  858. return true
  859. }
  860. }
  861. return false
  862. }
  863. // IsFilesConflicted determines if the Pull Request has changes conflicting with the target branch.
  864. func (pr *PullRequest) IsFilesConflicted() bool {
  865. return len(pr.ConflictedFiles) > 0
  866. }
  867. // GetWorkInProgressPrefix returns the prefix used to mark the pull request as a work in progress.
  868. // It returns an empty string when none were found
  869. func (pr *PullRequest) GetWorkInProgressPrefix() string {
  870. if err := pr.LoadIssue(); err != nil {
  871. log.Error("LoadIssue: %v", err)
  872. return ""
  873. }
  874. for _, prefix := range setting.Repository.PullRequest.WorkInProgressPrefixes {
  875. if strings.HasPrefix(strings.ToUpper(pr.Issue.Title), prefix) {
  876. return pr.Issue.Title[0:len(prefix)]
  877. }
  878. }
  879. return ""
  880. }