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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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. "bytes"
  9. "fmt"
  10. "io/ioutil"
  11. "os"
  12. "path"
  13. "path/filepath"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "code.gitea.io/gitea/modules/base"
  18. "code.gitea.io/gitea/modules/cache"
  19. "code.gitea.io/gitea/modules/git"
  20. "code.gitea.io/gitea/modules/log"
  21. "code.gitea.io/gitea/modules/process"
  22. "code.gitea.io/gitea/modules/setting"
  23. api "code.gitea.io/gitea/modules/structs"
  24. "code.gitea.io/gitea/modules/sync"
  25. "code.gitea.io/gitea/modules/util"
  26. "github.com/Unknwon/com"
  27. "github.com/go-xorm/xorm"
  28. )
  29. var pullRequestQueue = sync.NewUniqueQueue(setting.Repository.PullRequestQueueLength)
  30. // PullRequestType defines pull request type
  31. type PullRequestType int
  32. // Enumerate all the pull request types
  33. const (
  34. PullRequestGitea PullRequestType = iota
  35. PullRequestGit
  36. )
  37. // PullRequestStatus defines pull request status
  38. type PullRequestStatus int
  39. // Enumerate all the pull request status
  40. const (
  41. PullRequestStatusConflict PullRequestStatus = iota
  42. PullRequestStatusChecking
  43. PullRequestStatusMergeable
  44. PullRequestStatusManuallyMerged
  45. )
  46. // PullRequest represents relation between pull request and repositories.
  47. type PullRequest struct {
  48. ID int64 `xorm:"pk autoincr"`
  49. Type PullRequestType
  50. Status PullRequestStatus
  51. ConflictedFiles []string `xorm:"TEXT JSON"`
  52. IssueID int64 `xorm:"INDEX"`
  53. Issue *Issue `xorm:"-"`
  54. Index int64
  55. HeadRepoID int64 `xorm:"INDEX"`
  56. HeadRepo *Repository `xorm:"-"`
  57. BaseRepoID int64 `xorm:"INDEX"`
  58. BaseRepo *Repository `xorm:"-"`
  59. HeadUserName string
  60. HeadBranch string
  61. BaseBranch string
  62. ProtectedBranch *ProtectedBranch `xorm:"-"`
  63. MergeBase string `xorm:"VARCHAR(40)"`
  64. HasMerged bool `xorm:"INDEX"`
  65. MergedCommitID string `xorm:"VARCHAR(40)"`
  66. MergerID int64 `xorm:"INDEX"`
  67. Merger *User `xorm:"-"`
  68. MergedUnix util.TimeStamp `xorm:"updated INDEX"`
  69. }
  70. // Note: don't try to get Issue because will end up recursive querying.
  71. func (pr *PullRequest) loadAttributes(e Engine) (err error) {
  72. if pr.HasMerged && pr.Merger == nil {
  73. pr.Merger, err = getUserByID(e, pr.MergerID)
  74. if IsErrUserNotExist(err) {
  75. pr.MergerID = -1
  76. pr.Merger = NewGhostUser()
  77. } else if err != nil {
  78. return fmt.Errorf("getUserByID [%d]: %v", pr.MergerID, err)
  79. }
  80. }
  81. return nil
  82. }
  83. // LoadAttributes loads pull request attributes from database
  84. func (pr *PullRequest) LoadAttributes() error {
  85. return pr.loadAttributes(x)
  86. }
  87. // LoadIssue loads issue information from database
  88. func (pr *PullRequest) LoadIssue() (err error) {
  89. return pr.loadIssue(x)
  90. }
  91. func (pr *PullRequest) loadIssue(e Engine) (err error) {
  92. if pr.Issue != nil {
  93. return nil
  94. }
  95. pr.Issue, err = getIssueByID(e, pr.IssueID)
  96. return err
  97. }
  98. // LoadProtectedBranch loads the protected branch of the base branch
  99. func (pr *PullRequest) LoadProtectedBranch() (err error) {
  100. if pr.BaseRepo == nil {
  101. if pr.BaseRepoID == 0 {
  102. return nil
  103. }
  104. pr.BaseRepo, err = GetRepositoryByID(pr.BaseRepoID)
  105. if err != nil {
  106. return
  107. }
  108. }
  109. pr.ProtectedBranch, err = GetProtectedBranchBy(pr.BaseRepo.ID, pr.BaseBranch)
  110. return
  111. }
  112. // GetDefaultMergeMessage returns default message used when merging pull request
  113. func (pr *PullRequest) GetDefaultMergeMessage() string {
  114. if pr.HeadRepo == nil {
  115. var err error
  116. pr.HeadRepo, err = GetRepositoryByID(pr.HeadRepoID)
  117. if err != nil {
  118. log.Error("GetRepositoryById[%d]: %v", pr.HeadRepoID, err)
  119. return ""
  120. }
  121. }
  122. return fmt.Sprintf("Merge branch '%s' of %s/%s into %s", pr.HeadBranch, pr.HeadUserName, pr.HeadRepo.Name, pr.BaseBranch)
  123. }
  124. // GetDefaultSquashMessage returns default message used when squash and merging pull request
  125. func (pr *PullRequest) GetDefaultSquashMessage() string {
  126. if err := pr.LoadIssue(); err != nil {
  127. log.Error("LoadIssue: %v", err)
  128. return ""
  129. }
  130. return fmt.Sprintf("%s (#%d)", pr.Issue.Title, pr.Issue.Index)
  131. }
  132. // GetGitRefName returns git ref for hidden pull request branch
  133. func (pr *PullRequest) GetGitRefName() string {
  134. return fmt.Sprintf("refs/pull/%d/head", pr.Index)
  135. }
  136. // APIFormat assumes following fields have been assigned with valid values:
  137. // Required - Issue
  138. // Optional - Merger
  139. func (pr *PullRequest) APIFormat() *api.PullRequest {
  140. return pr.apiFormat(x)
  141. }
  142. func (pr *PullRequest) apiFormat(e Engine) *api.PullRequest {
  143. var (
  144. baseBranch *git.Branch
  145. headBranch *git.Branch
  146. baseCommit *git.Commit
  147. headCommit *git.Commit
  148. err error
  149. )
  150. if err = pr.Issue.loadRepo(e); err != nil {
  151. log.Error("loadRepo[%d]: %v", pr.ID, err)
  152. return nil
  153. }
  154. apiIssue := pr.Issue.apiFormat(e)
  155. if pr.BaseRepo == nil {
  156. pr.BaseRepo, err = getRepositoryByID(e, pr.BaseRepoID)
  157. if err != nil {
  158. log.Error("GetRepositoryById[%d]: %v", pr.ID, err)
  159. return nil
  160. }
  161. }
  162. if pr.HeadRepo == nil {
  163. pr.HeadRepo, err = getRepositoryByID(e, pr.HeadRepoID)
  164. if err != nil {
  165. log.Error("GetRepositoryById[%d]: %v", pr.ID, err)
  166. return nil
  167. }
  168. }
  169. if baseBranch, err = pr.BaseRepo.GetBranch(pr.BaseBranch); err != nil {
  170. log.Error("pr.BaseRepo.GetBranch[%d]: %v", pr.BaseBranch, err)
  171. return nil
  172. }
  173. if baseCommit, err = baseBranch.GetCommit(); err != nil {
  174. log.Error("baseBranch.GetCommit[%d]: %v", pr.ID, err)
  175. return nil
  176. }
  177. if headBranch, err = pr.HeadRepo.GetBranch(pr.HeadBranch); err != nil {
  178. log.Error("pr.HeadRepo.GetBranch[%d]: %v", pr.HeadBranch, err)
  179. return nil
  180. }
  181. if headCommit, err = headBranch.GetCommit(); err != nil {
  182. log.Error("headBranch.GetCommit[%d]: %v", pr.ID, err)
  183. return nil
  184. }
  185. apiBaseBranchInfo := &api.PRBranchInfo{
  186. Name: pr.BaseBranch,
  187. Ref: pr.BaseBranch,
  188. Sha: baseCommit.ID.String(),
  189. RepoID: pr.BaseRepoID,
  190. Repository: pr.BaseRepo.innerAPIFormat(e, AccessModeNone, false),
  191. }
  192. apiHeadBranchInfo := &api.PRBranchInfo{
  193. Name: pr.HeadBranch,
  194. Ref: pr.HeadBranch,
  195. Sha: headCommit.ID.String(),
  196. RepoID: pr.HeadRepoID,
  197. Repository: pr.HeadRepo.innerAPIFormat(e, AccessModeNone, false),
  198. }
  199. if err = pr.Issue.loadRepo(e); err != nil {
  200. log.Error("pr.Issue.loadRepo[%d]: %v", pr.ID, err)
  201. return nil
  202. }
  203. apiPullRequest := &api.PullRequest{
  204. ID: pr.ID,
  205. Index: pr.Index,
  206. Poster: apiIssue.Poster,
  207. Title: apiIssue.Title,
  208. Body: apiIssue.Body,
  209. Labels: apiIssue.Labels,
  210. Milestone: apiIssue.Milestone,
  211. Assignee: apiIssue.Assignee,
  212. Assignees: apiIssue.Assignees,
  213. State: apiIssue.State,
  214. Comments: apiIssue.Comments,
  215. HTMLURL: pr.Issue.HTMLURL(),
  216. DiffURL: pr.Issue.DiffURL(),
  217. PatchURL: pr.Issue.PatchURL(),
  218. HasMerged: pr.HasMerged,
  219. Base: apiBaseBranchInfo,
  220. Head: apiHeadBranchInfo,
  221. MergeBase: pr.MergeBase,
  222. Deadline: apiIssue.Deadline,
  223. Created: pr.Issue.CreatedUnix.AsTimePtr(),
  224. Updated: pr.Issue.UpdatedUnix.AsTimePtr(),
  225. }
  226. if pr.Status != PullRequestStatusChecking {
  227. mergeable := pr.Status != PullRequestStatusConflict && !pr.IsWorkInProgress()
  228. apiPullRequest.Mergeable = mergeable
  229. }
  230. if pr.HasMerged {
  231. apiPullRequest.Merged = pr.MergedUnix.AsTimePtr()
  232. apiPullRequest.MergedCommitID = &pr.MergedCommitID
  233. apiPullRequest.MergedBy = pr.Merger.APIFormat()
  234. }
  235. return apiPullRequest
  236. }
  237. func (pr *PullRequest) getHeadRepo(e Engine) (err error) {
  238. pr.HeadRepo, err = getRepositoryByID(e, pr.HeadRepoID)
  239. if err != nil && !IsErrRepoNotExist(err) {
  240. return fmt.Errorf("getRepositoryByID(head): %v", err)
  241. }
  242. return nil
  243. }
  244. // GetHeadRepo loads the head repository
  245. func (pr *PullRequest) GetHeadRepo() error {
  246. return pr.getHeadRepo(x)
  247. }
  248. // GetBaseRepo loads the target repository
  249. func (pr *PullRequest) GetBaseRepo() (err error) {
  250. if pr.BaseRepo != nil {
  251. return nil
  252. }
  253. pr.BaseRepo, err = GetRepositoryByID(pr.BaseRepoID)
  254. if err != nil {
  255. return fmt.Errorf("GetRepositoryByID(base): %v", err)
  256. }
  257. return nil
  258. }
  259. // IsChecking returns true if this pull request is still checking conflict.
  260. func (pr *PullRequest) IsChecking() bool {
  261. return pr.Status == PullRequestStatusChecking
  262. }
  263. // CanAutoMerge returns true if this pull request can be merged automatically.
  264. func (pr *PullRequest) CanAutoMerge() bool {
  265. return pr.Status == PullRequestStatusMergeable
  266. }
  267. // GetLastCommitStatus returns the last commit status for this pull request.
  268. func (pr *PullRequest) GetLastCommitStatus() (status *CommitStatus, err error) {
  269. if err = pr.GetHeadRepo(); err != nil {
  270. return nil, err
  271. }
  272. if pr.HeadRepo == nil {
  273. return nil, ErrPullRequestHeadRepoMissing{pr.ID, pr.HeadRepoID}
  274. }
  275. headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
  276. if err != nil {
  277. return nil, err
  278. }
  279. repo := pr.HeadRepo
  280. lastCommitID, err := headGitRepo.GetBranchCommitID(pr.HeadBranch)
  281. if err != nil {
  282. return nil, err
  283. }
  284. var statusList []*CommitStatus
  285. statusList, err = GetLatestCommitStatus(repo, lastCommitID, 0)
  286. if err != nil {
  287. return nil, err
  288. }
  289. return CalcCommitStatus(statusList), nil
  290. }
  291. // MergeStyle represents the approach to merge commits into base branch.
  292. type MergeStyle string
  293. const (
  294. // MergeStyleMerge create merge commit
  295. MergeStyleMerge MergeStyle = "merge"
  296. // MergeStyleRebase rebase before merging
  297. MergeStyleRebase MergeStyle = "rebase"
  298. // MergeStyleRebaseMerge rebase before merging with merge commit (--no-ff)
  299. MergeStyleRebaseMerge MergeStyle = "rebase-merge"
  300. // MergeStyleSquash squash commits into single commit before merging
  301. MergeStyleSquash MergeStyle = "squash"
  302. )
  303. // CheckUserAllowedToMerge checks whether the user is allowed to merge
  304. func (pr *PullRequest) CheckUserAllowedToMerge(doer *User) (err error) {
  305. if doer == nil {
  306. return ErrNotAllowedToMerge{
  307. "Not signed in",
  308. }
  309. }
  310. if pr.BaseRepo == nil {
  311. if err = pr.GetBaseRepo(); err != nil {
  312. return fmt.Errorf("GetBaseRepo: %v", err)
  313. }
  314. }
  315. if protected, err := pr.BaseRepo.IsProtectedBranchForMerging(pr, pr.BaseBranch, doer); err != nil {
  316. return fmt.Errorf("IsProtectedBranch: %v", err)
  317. } else if protected {
  318. return ErrNotAllowedToMerge{
  319. "The branch is protected",
  320. }
  321. }
  322. return nil
  323. }
  324. func getDiffTree(repoPath, baseBranch, headBranch string) (string, error) {
  325. getDiffTreeFromBranch := func(repoPath, baseBranch, headBranch string) (string, error) {
  326. var outbuf, errbuf strings.Builder
  327. // Compute the diff-tree for sparse-checkout
  328. // The branch argument must be enclosed with double-quotes ("") in case it contains slashes (e.g "feature/test")
  329. if err := git.NewCommand("diff-tree", "--no-commit-id", "--name-only", "-r", "--root", baseBranch, headBranch).RunInDirPipeline(repoPath, &outbuf, &errbuf); err != nil {
  330. return "", fmt.Errorf("git diff-tree [%s base:%s head:%s]: %s", repoPath, baseBranch, headBranch, errbuf.String())
  331. }
  332. return outbuf.String(), nil
  333. }
  334. list, err := getDiffTreeFromBranch(repoPath, baseBranch, headBranch)
  335. if err != nil {
  336. return "", err
  337. }
  338. // Prefixing '/' for each entry, otherwise all files with the same name in subdirectories would be matched.
  339. out := bytes.Buffer{}
  340. scanner := bufio.NewScanner(strings.NewReader(list))
  341. for scanner.Scan() {
  342. fmt.Fprintf(&out, "/%s\n", scanner.Text())
  343. }
  344. return out.String(), nil
  345. }
  346. // Merge merges pull request to base repository.
  347. // FIXME: add repoWorkingPull make sure two merges does not happen at same time.
  348. func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle MergeStyle, message string) (err error) {
  349. if err = pr.GetHeadRepo(); err != nil {
  350. return fmt.Errorf("GetHeadRepo: %v", err)
  351. } else if err = pr.GetBaseRepo(); err != nil {
  352. return fmt.Errorf("GetBaseRepo: %v", err)
  353. }
  354. prUnit, err := pr.BaseRepo.GetUnit(UnitTypePullRequests)
  355. if err != nil {
  356. return err
  357. }
  358. prConfig := prUnit.PullRequestsConfig()
  359. if err := pr.CheckUserAllowedToMerge(doer); err != nil {
  360. return fmt.Errorf("CheckUserAllowedToMerge: %v", err)
  361. }
  362. // Check if merge style is correct and allowed
  363. if !prConfig.IsMergeStyleAllowed(mergeStyle) {
  364. return ErrInvalidMergeStyle{pr.BaseRepo.ID, mergeStyle}
  365. }
  366. defer func() {
  367. go HookQueue.Add(pr.BaseRepo.ID)
  368. go AddTestPullRequestTask(doer, pr.BaseRepo.ID, pr.BaseBranch, false)
  369. }()
  370. // Clone base repo.
  371. tmpBasePath, err := CreateTemporaryPath("merge")
  372. if err != nil {
  373. return err
  374. }
  375. defer func() {
  376. if err := RemoveTemporaryPath(tmpBasePath); err != nil {
  377. log.Error("Merge: RemoveTemporaryPath: %s", err)
  378. }
  379. }()
  380. headRepoPath := RepoPath(pr.HeadUserName, pr.HeadRepo.Name)
  381. if err := git.Clone(baseGitRepo.Path, tmpBasePath, git.CloneRepoOptions{
  382. Shared: true,
  383. NoCheckout: true,
  384. Branch: pr.BaseBranch,
  385. }); err != nil {
  386. return fmt.Errorf("git clone: %v", err)
  387. }
  388. remoteRepoName := "head_repo"
  389. // Add head repo remote.
  390. addCacheRepo := func(staging, cache string) error {
  391. p := filepath.Join(staging, ".git", "objects", "info", "alternates")
  392. f, err := os.OpenFile(p, os.O_APPEND|os.O_WRONLY, 0600)
  393. if err != nil {
  394. return err
  395. }
  396. defer f.Close()
  397. data := filepath.Join(cache, "objects")
  398. if _, err := fmt.Fprintln(f, data); err != nil {
  399. return err
  400. }
  401. return nil
  402. }
  403. if err := addCacheRepo(tmpBasePath, headRepoPath); err != nil {
  404. return fmt.Errorf("addCacheRepo [%s -> %s]: %v", headRepoPath, tmpBasePath, err)
  405. }
  406. var errbuf strings.Builder
  407. if err := git.NewCommand("remote", "add", remoteRepoName, headRepoPath).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
  408. return fmt.Errorf("git remote add [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
  409. }
  410. // Fetch head branch
  411. if err := git.NewCommand("fetch", remoteRepoName).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
  412. return fmt.Errorf("git fetch [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
  413. }
  414. trackingBranch := path.Join(remoteRepoName, pr.HeadBranch)
  415. stagingBranch := fmt.Sprintf("%s_%s", remoteRepoName, pr.HeadBranch)
  416. // Enable sparse-checkout
  417. sparseCheckoutList, err := getDiffTree(tmpBasePath, pr.BaseBranch, trackingBranch)
  418. if err != nil {
  419. return fmt.Errorf("getDiffTree: %v", err)
  420. }
  421. infoPath := filepath.Join(tmpBasePath, ".git", "info")
  422. if err := os.MkdirAll(infoPath, 0700); err != nil {
  423. return fmt.Errorf("creating directory failed [%s]: %v", infoPath, err)
  424. }
  425. sparseCheckoutListPath := filepath.Join(infoPath, "sparse-checkout")
  426. if err := ioutil.WriteFile(sparseCheckoutListPath, []byte(sparseCheckoutList), 0600); err != nil {
  427. return fmt.Errorf("Writing sparse-checkout file to %s: %v", sparseCheckoutListPath, err)
  428. }
  429. if err := git.NewCommand("config", "--local", "core.sparseCheckout", "true").RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
  430. return fmt.Errorf("git config [core.sparsecheckout -> true]: %v", errbuf.String())
  431. }
  432. // Read base branch index
  433. if err := git.NewCommand("read-tree", "HEAD").RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
  434. return fmt.Errorf("git read-tree HEAD: %s", errbuf.String())
  435. }
  436. // Merge commits.
  437. switch mergeStyle {
  438. case MergeStyleMerge:
  439. if err := git.NewCommand("merge", "--no-ff", "--no-commit", trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
  440. return fmt.Errorf("git merge --no-ff --no-commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
  441. }
  442. sig := doer.NewGitSig()
  443. if err := git.NewCommand("commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
  444. return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
  445. }
  446. case MergeStyleRebase:
  447. // Checkout head branch
  448. if err := git.NewCommand("checkout", "-b", stagingBranch, trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
  449. return fmt.Errorf("git checkout: %s", errbuf.String())
  450. }
  451. // Rebase before merging
  452. if err := git.NewCommand("rebase", "-q", pr.BaseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
  453. return fmt.Errorf("git rebase [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
  454. }
  455. // Checkout base branch again
  456. if err := git.NewCommand("checkout", pr.BaseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
  457. return fmt.Errorf("git checkout: %s", errbuf.String())
  458. }
  459. // Merge fast forward
  460. if err := git.NewCommand("merge", "--ff-only", "-q", stagingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
  461. return fmt.Errorf("git merge --ff-only [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
  462. }
  463. case MergeStyleRebaseMerge:
  464. // Checkout head branch
  465. if err := git.NewCommand("checkout", "-b", stagingBranch, trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
  466. return fmt.Errorf("git checkout: %s", errbuf.String())
  467. }
  468. // Rebase before merging
  469. if err := git.NewCommand("rebase", "-q", pr.BaseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
  470. return fmt.Errorf("git rebase [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
  471. }
  472. // Checkout base branch again
  473. if err := git.NewCommand("checkout", pr.BaseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
  474. return fmt.Errorf("git checkout: %s", errbuf.String())
  475. }
  476. // Prepare merge with commit
  477. if err := git.NewCommand("merge", "--no-ff", "--no-commit", "-q", stagingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
  478. return fmt.Errorf("git merge --no-ff [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
  479. }
  480. // Set custom message and author and create merge commit
  481. sig := doer.NewGitSig()
  482. if err := git.NewCommand("commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
  483. return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
  484. }
  485. case MergeStyleSquash:
  486. // Merge with squash
  487. if err := git.NewCommand("merge", "-q", "--squash", trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
  488. return fmt.Errorf("git merge --squash [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
  489. }
  490. sig := pr.Issue.Poster.NewGitSig()
  491. if err := git.NewCommand("commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
  492. return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
  493. }
  494. default:
  495. return ErrInvalidMergeStyle{pr.BaseRepo.ID, mergeStyle}
  496. }
  497. env := PushingEnvironment(doer, pr.BaseRepo)
  498. // Push back to upstream.
  499. if err := git.NewCommand("push", baseGitRepo.Path, pr.BaseBranch).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, nil, &errbuf); err != nil {
  500. return fmt.Errorf("git push: %s", errbuf.String())
  501. }
  502. pr.MergedCommitID, err = baseGitRepo.GetBranchCommitID(pr.BaseBranch)
  503. if err != nil {
  504. return fmt.Errorf("GetBranchCommit: %v", err)
  505. }
  506. pr.MergedUnix = util.TimeStampNow()
  507. pr.Merger = doer
  508. pr.MergerID = doer.ID
  509. if err = pr.setMerged(); err != nil {
  510. log.Error("setMerged [%d]: %v", pr.ID, err)
  511. }
  512. if err = MergePullRequestAction(doer, pr.Issue.Repo, pr.Issue); err != nil {
  513. log.Error("MergePullRequestAction [%d]: %v", pr.ID, err)
  514. }
  515. // Reset cached commit count
  516. cache.Remove(pr.Issue.Repo.GetCommitsCountCacheKey(pr.BaseBranch, true))
  517. // Reload pull request information.
  518. if err = pr.LoadAttributes(); err != nil {
  519. log.Error("LoadAttributes: %v", err)
  520. return nil
  521. }
  522. mode, _ := AccessLevel(doer, pr.Issue.Repo)
  523. if err = PrepareWebhooks(pr.Issue.Repo, HookEventPullRequest, &api.PullRequestPayload{
  524. Action: api.HookIssueClosed,
  525. Index: pr.Index,
  526. PullRequest: pr.APIFormat(),
  527. Repository: pr.Issue.Repo.APIFormat(mode),
  528. Sender: doer.APIFormat(),
  529. }); err != nil {
  530. log.Error("PrepareWebhooks: %v", err)
  531. } else {
  532. go HookQueue.Add(pr.Issue.Repo.ID)
  533. }
  534. l, err := baseGitRepo.CommitsBetweenIDs(pr.MergedCommitID, pr.MergeBase)
  535. if err != nil {
  536. log.Error("CommitsBetweenIDs: %v", err)
  537. return nil
  538. }
  539. // It is possible that head branch is not fully sync with base branch for merge commits,
  540. // so we need to get latest head commit and append merge commit manually
  541. // to avoid strange diff commits produced.
  542. mergeCommit, err := baseGitRepo.GetBranchCommit(pr.BaseBranch)
  543. if err != nil {
  544. log.Error("GetBranchCommit: %v", err)
  545. return nil
  546. }
  547. if mergeStyle == MergeStyleMerge {
  548. l.PushFront(mergeCommit)
  549. }
  550. p := &api.PushPayload{
  551. Ref: git.BranchPrefix + pr.BaseBranch,
  552. Before: pr.MergeBase,
  553. After: mergeCommit.ID.String(),
  554. CompareURL: setting.AppURL + pr.BaseRepo.ComposeCompareURL(pr.MergeBase, pr.MergedCommitID),
  555. Commits: ListToPushCommits(l).ToAPIPayloadCommits(pr.BaseRepo.HTMLURL()),
  556. Repo: pr.BaseRepo.APIFormat(mode),
  557. Pusher: pr.HeadRepo.MustOwner().APIFormat(),
  558. Sender: doer.APIFormat(),
  559. }
  560. if err = PrepareWebhooks(pr.BaseRepo, HookEventPush, p); err != nil {
  561. log.Error("PrepareWebhooks: %v", err)
  562. } else {
  563. go HookQueue.Add(pr.BaseRepo.ID)
  564. }
  565. return nil
  566. }
  567. // setMerged sets a pull request to merged and closes the corresponding issue
  568. func (pr *PullRequest) setMerged() (err error) {
  569. if pr.HasMerged {
  570. return fmt.Errorf("PullRequest[%d] already merged", pr.Index)
  571. }
  572. if pr.MergedCommitID == "" || pr.MergedUnix == 0 || pr.Merger == nil {
  573. return fmt.Errorf("Unable to merge PullRequest[%d], some required fields are empty", pr.Index)
  574. }
  575. pr.HasMerged = true
  576. sess := x.NewSession()
  577. defer sess.Close()
  578. if err = sess.Begin(); err != nil {
  579. return err
  580. }
  581. if err = pr.loadIssue(sess); err != nil {
  582. return err
  583. }
  584. if err = pr.Issue.loadRepo(sess); err != nil {
  585. return err
  586. }
  587. if err = pr.Issue.Repo.getOwner(sess); err != nil {
  588. return err
  589. }
  590. if err = pr.Issue.changeStatus(sess, pr.Merger, true); err != nil {
  591. return fmt.Errorf("Issue.changeStatus: %v", err)
  592. }
  593. if _, err = sess.ID(pr.ID).Cols("has_merged, status, merged_commit_id, merger_id, merged_unix").Update(pr); err != nil {
  594. return fmt.Errorf("update pull request: %v", err)
  595. }
  596. if err = sess.Commit(); err != nil {
  597. return fmt.Errorf("Commit: %v", err)
  598. }
  599. return nil
  600. }
  601. // manuallyMerged checks if a pull request got manually merged
  602. // When a pull request got manually merged mark the pull request as merged
  603. func (pr *PullRequest) manuallyMerged() bool {
  604. commit, err := pr.getMergeCommit()
  605. if err != nil {
  606. log.Error("PullRequest[%d].getMergeCommit: %v", pr.ID, err)
  607. return false
  608. }
  609. if commit != nil {
  610. pr.MergedCommitID = commit.ID.String()
  611. pr.MergedUnix = util.TimeStamp(commit.Author.When.Unix())
  612. pr.Status = PullRequestStatusManuallyMerged
  613. merger, _ := GetUserByEmail(commit.Author.Email)
  614. // When the commit author is unknown set the BaseRepo owner as merger
  615. if merger == nil {
  616. if pr.BaseRepo.Owner == nil {
  617. if err = pr.BaseRepo.getOwner(x); err != nil {
  618. log.Error("BaseRepo.getOwner[%d]: %v", pr.ID, err)
  619. return false
  620. }
  621. }
  622. merger = pr.BaseRepo.Owner
  623. }
  624. pr.Merger = merger
  625. pr.MergerID = merger.ID
  626. if err = pr.setMerged(); err != nil {
  627. log.Error("PullRequest[%d].setMerged : %v", pr.ID, err)
  628. return false
  629. }
  630. 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())
  631. return true
  632. }
  633. return false
  634. }
  635. // getMergeCommit checks if a pull request got merged
  636. // Returns the git.Commit of the pull request if merged
  637. func (pr *PullRequest) getMergeCommit() (*git.Commit, error) {
  638. if pr.BaseRepo == nil {
  639. var err error
  640. pr.BaseRepo, err = GetRepositoryByID(pr.BaseRepoID)
  641. if err != nil {
  642. return nil, fmt.Errorf("GetRepositoryByID: %v", err)
  643. }
  644. }
  645. indexTmpPath := filepath.Join(os.TempDir(), "gitea-"+pr.BaseRepo.Name+"-"+strconv.Itoa(time.Now().Nanosecond()))
  646. defer os.Remove(indexTmpPath)
  647. headFile := pr.GetGitRefName()
  648. // Check if a pull request is merged into BaseBranch
  649. _, stderr, err := process.GetManager().ExecDirEnv(-1, "", fmt.Sprintf("isMerged (git merge-base --is-ancestor): %d", pr.BaseRepo.ID),
  650. []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()},
  651. "git", "merge-base", "--is-ancestor", headFile, pr.BaseBranch)
  652. if err != nil {
  653. // Errors are signaled by a non-zero status that is not 1
  654. if strings.Contains(err.Error(), "exit status 1") {
  655. return nil, nil
  656. }
  657. return nil, fmt.Errorf("git merge-base --is-ancestor: %v %v", stderr, err)
  658. }
  659. commitIDBytes, err := ioutil.ReadFile(pr.BaseRepo.RepoPath() + "/" + headFile)
  660. if err != nil {
  661. return nil, fmt.Errorf("ReadFile(%s): %v", headFile, err)
  662. }
  663. commitID := string(commitIDBytes)
  664. if len(commitID) < 40 {
  665. return nil, fmt.Errorf(`ReadFile(%s): invalid commit-ID "%s"`, headFile, commitID)
  666. }
  667. cmd := commitID[:40] + ".." + pr.BaseBranch
  668. // Get the commit from BaseBranch where the pull request got merged
  669. mergeCommit, stderr, err := process.GetManager().ExecDirEnv(-1, "", fmt.Sprintf("isMerged (git rev-list --ancestry-path --merges --reverse): %d", pr.BaseRepo.ID),
  670. []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()},
  671. "git", "rev-list", "--ancestry-path", "--merges", "--reverse", cmd)
  672. if err != nil {
  673. return nil, fmt.Errorf("git rev-list --ancestry-path --merges --reverse: %v %v", stderr, err)
  674. } else if len(mergeCommit) < 40 {
  675. // PR was fast-forwarded, so just use last commit of PR
  676. mergeCommit = commitID[:40]
  677. }
  678. gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
  679. if err != nil {
  680. return nil, fmt.Errorf("OpenRepository: %v", err)
  681. }
  682. commit, err := gitRepo.GetCommit(mergeCommit[:40])
  683. if err != nil {
  684. return nil, fmt.Errorf("GetCommit: %v", err)
  685. }
  686. return commit, nil
  687. }
  688. // patchConflicts is a list of conflict description from Git.
  689. var patchConflicts = []string{
  690. "patch does not apply",
  691. "already exists in working directory",
  692. "unrecognized input",
  693. "error:",
  694. }
  695. // testPatch checks if patch can be merged to base repository without conflict.
  696. func (pr *PullRequest) testPatch(e Engine) (err error) {
  697. if pr.BaseRepo == nil {
  698. pr.BaseRepo, err = getRepositoryByID(e, pr.BaseRepoID)
  699. if err != nil {
  700. return fmt.Errorf("GetRepositoryByID: %v", err)
  701. }
  702. }
  703. patchPath, err := pr.BaseRepo.patchPath(e, pr.Index)
  704. if err != nil {
  705. return fmt.Errorf("BaseRepo.PatchPath: %v", err)
  706. }
  707. // Fast fail if patch does not exist, this assumes data is corrupted.
  708. if !com.IsFile(patchPath) {
  709. log.Trace("PullRequest[%d].testPatch: ignored corrupted data", pr.ID)
  710. return nil
  711. }
  712. repoWorkingPool.CheckIn(com.ToStr(pr.BaseRepoID))
  713. defer repoWorkingPool.CheckOut(com.ToStr(pr.BaseRepoID))
  714. log.Trace("PullRequest[%d].testPatch (patchPath): %s", pr.ID, patchPath)
  715. pr.Status = PullRequestStatusChecking
  716. indexTmpPath := filepath.Join(os.TempDir(), "gitea-"+pr.BaseRepo.Name+"-"+strconv.Itoa(time.Now().Nanosecond()))
  717. defer os.Remove(indexTmpPath)
  718. var stderr string
  719. _, stderr, err = process.GetManager().ExecDirEnv(-1, "", fmt.Sprintf("testPatch (git read-tree): %d", pr.BaseRepo.ID),
  720. []string{"GIT_DIR=" + pr.BaseRepo.RepoPath(), "GIT_INDEX_FILE=" + indexTmpPath},
  721. "git", "read-tree", pr.BaseBranch)
  722. if err != nil {
  723. return fmt.Errorf("git read-tree --index-output=%s %s: %v - %s", indexTmpPath, pr.BaseBranch, err, stderr)
  724. }
  725. prUnit, err := pr.BaseRepo.getUnit(e, UnitTypePullRequests)
  726. if err != nil {
  727. return err
  728. }
  729. prConfig := prUnit.PullRequestsConfig()
  730. args := []string{"apply", "--check", "--cached"}
  731. if prConfig.IgnoreWhitespaceConflicts {
  732. args = append(args, "--ignore-whitespace")
  733. }
  734. args = append(args, patchPath)
  735. pr.ConflictedFiles = []string{}
  736. _, stderr, err = process.GetManager().ExecDirEnv(-1, "", fmt.Sprintf("testPatch (git apply --check): %d", pr.BaseRepo.ID),
  737. []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()},
  738. "git", args...)
  739. if err != nil {
  740. for i := range patchConflicts {
  741. if strings.Contains(stderr, patchConflicts[i]) {
  742. log.Trace("PullRequest[%d].testPatch (apply): has conflict", pr.ID)
  743. const prefix = "error: patch failed:"
  744. pr.Status = PullRequestStatusConflict
  745. pr.ConflictedFiles = make([]string, 0, 5)
  746. scanner := bufio.NewScanner(strings.NewReader(stderr))
  747. for scanner.Scan() {
  748. line := scanner.Text()
  749. if strings.HasPrefix(line, prefix) {
  750. pr.ConflictedFiles = append(pr.ConflictedFiles, strings.TrimSpace(strings.Split(line[len(prefix):], ":")[0]))
  751. }
  752. // only list 10 conflicted files
  753. if len(pr.ConflictedFiles) >= 10 {
  754. break
  755. }
  756. }
  757. if len(pr.ConflictedFiles) > 0 {
  758. log.Trace("Found %d files conflicted: %v", len(pr.ConflictedFiles), pr.ConflictedFiles)
  759. }
  760. return nil
  761. }
  762. }
  763. return fmt.Errorf("git apply --check: %v - %s", err, stderr)
  764. }
  765. return nil
  766. }
  767. // NewPullRequest creates new pull request with labels for repository.
  768. func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest, patch []byte, assigneeIDs []int64) (err error) {
  769. sess := x.NewSession()
  770. defer sess.Close()
  771. if err = sess.Begin(); err != nil {
  772. return err
  773. }
  774. if err = newIssue(sess, pull.Poster, NewIssueOptions{
  775. Repo: repo,
  776. Issue: pull,
  777. LabelIDs: labelIDs,
  778. Attachments: uuids,
  779. IsPull: true,
  780. AssigneeIDs: assigneeIDs,
  781. }); err != nil {
  782. if IsErrUserDoesNotHaveAccessToRepo(err) {
  783. return err
  784. }
  785. return fmt.Errorf("newIssue: %v", err)
  786. }
  787. pr.Index = pull.Index
  788. if err = repo.savePatch(sess, pr.Index, patch); err != nil {
  789. return fmt.Errorf("SavePatch: %v", err)
  790. }
  791. pr.BaseRepo = repo
  792. if err = pr.testPatch(sess); err != nil {
  793. return fmt.Errorf("testPatch: %v", err)
  794. }
  795. // No conflict appears after test means mergeable.
  796. if pr.Status == PullRequestStatusChecking {
  797. pr.Status = PullRequestStatusMergeable
  798. }
  799. pr.IssueID = pull.ID
  800. if _, err = sess.Insert(pr); err != nil {
  801. return fmt.Errorf("insert pull repo: %v", err)
  802. }
  803. if err = sess.Commit(); err != nil {
  804. return fmt.Errorf("Commit: %v", err)
  805. }
  806. if err = NotifyWatchers(&Action{
  807. ActUserID: pull.Poster.ID,
  808. ActUser: pull.Poster,
  809. OpType: ActionCreatePullRequest,
  810. Content: fmt.Sprintf("%d|%s", pull.Index, pull.Title),
  811. RepoID: repo.ID,
  812. Repo: repo,
  813. IsPrivate: repo.IsPrivate,
  814. }); err != nil {
  815. log.Error("NotifyWatchers: %v", err)
  816. }
  817. pr.Issue = pull
  818. pull.PullRequest = pr
  819. mode, _ := AccessLevel(pull.Poster, repo)
  820. if err = PrepareWebhooks(repo, HookEventPullRequest, &api.PullRequestPayload{
  821. Action: api.HookIssueOpened,
  822. Index: pull.Index,
  823. PullRequest: pr.APIFormat(),
  824. Repository: repo.APIFormat(mode),
  825. Sender: pull.Poster.APIFormat(),
  826. }); err != nil {
  827. log.Error("PrepareWebhooks: %v", err)
  828. } else {
  829. go HookQueue.Add(repo.ID)
  830. }
  831. return nil
  832. }
  833. // PullRequestsOptions holds the options for PRs
  834. type PullRequestsOptions struct {
  835. Page int
  836. State string
  837. SortType string
  838. Labels []string
  839. MilestoneID int64
  840. }
  841. func listPullRequestStatement(baseRepoID int64, opts *PullRequestsOptions) (*xorm.Session, error) {
  842. sess := x.Where("pull_request.base_repo_id=?", baseRepoID)
  843. sess.Join("INNER", "issue", "pull_request.issue_id = issue.id")
  844. switch opts.State {
  845. case "closed", "open":
  846. sess.And("issue.is_closed=?", opts.State == "closed")
  847. }
  848. if labelIDs, err := base.StringsToInt64s(opts.Labels); err != nil {
  849. return nil, err
  850. } else if len(labelIDs) > 0 {
  851. sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
  852. In("issue_label.label_id", labelIDs)
  853. }
  854. if opts.MilestoneID > 0 {
  855. sess.And("issue.milestone_id=?", opts.MilestoneID)
  856. }
  857. return sess, nil
  858. }
  859. // PullRequests returns all pull requests for a base Repo by the given conditions
  860. func PullRequests(baseRepoID int64, opts *PullRequestsOptions) ([]*PullRequest, int64, error) {
  861. if opts.Page <= 0 {
  862. opts.Page = 1
  863. }
  864. countSession, err := listPullRequestStatement(baseRepoID, opts)
  865. if err != nil {
  866. log.Error("listPullRequestStatement: %v", err)
  867. return nil, 0, err
  868. }
  869. maxResults, err := countSession.Count(new(PullRequest))
  870. if err != nil {
  871. log.Error("Count PRs: %v", err)
  872. return nil, maxResults, err
  873. }
  874. prs := make([]*PullRequest, 0, ItemsPerPage)
  875. findSession, err := listPullRequestStatement(baseRepoID, opts)
  876. sortIssuesSession(findSession, opts.SortType)
  877. if err != nil {
  878. log.Error("listPullRequestStatement: %v", err)
  879. return nil, maxResults, err
  880. }
  881. findSession.Limit(ItemsPerPage, (opts.Page-1)*ItemsPerPage)
  882. return prs, maxResults, findSession.Find(&prs)
  883. }
  884. // GetUnmergedPullRequest returns a pull request that is open and has not been merged
  885. // by given head/base and repo/branch.
  886. func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch string) (*PullRequest, error) {
  887. pr := new(PullRequest)
  888. has, err := x.
  889. Where("head_repo_id=? AND head_branch=? AND base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
  890. headRepoID, headBranch, baseRepoID, baseBranch, false, false).
  891. Join("INNER", "issue", "issue.id=pull_request.issue_id").
  892. Get(pr)
  893. if err != nil {
  894. return nil, err
  895. } else if !has {
  896. return nil, ErrPullRequestNotExist{0, 0, headRepoID, baseRepoID, headBranch, baseBranch}
  897. }
  898. return pr, nil
  899. }
  900. // GetUnmergedPullRequestsByHeadInfo returns all pull requests that are open and has not been merged
  901. // by given head information (repo and branch).
  902. func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequest, error) {
  903. prs := make([]*PullRequest, 0, 2)
  904. return prs, x.
  905. Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ?",
  906. repoID, branch, false, false).
  907. Join("INNER", "issue", "issue.id = pull_request.issue_id").
  908. Find(&prs)
  909. }
  910. // GetUnmergedPullRequestsByBaseInfo returns all pull requests that are open and has not been merged
  911. // by given base information (repo and branch).
  912. func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) {
  913. prs := make([]*PullRequest, 0, 2)
  914. return prs, x.
  915. Where("base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
  916. repoID, branch, false, false).
  917. Join("INNER", "issue", "issue.id=pull_request.issue_id").
  918. Find(&prs)
  919. }
  920. // GetPullRequestByIndex returns a pull request by the given index
  921. func GetPullRequestByIndex(repoID int64, index int64) (*PullRequest, error) {
  922. pr := &PullRequest{
  923. BaseRepoID: repoID,
  924. Index: index,
  925. }
  926. has, err := x.Get(pr)
  927. if err != nil {
  928. return nil, err
  929. } else if !has {
  930. return nil, ErrPullRequestNotExist{0, 0, 0, repoID, "", ""}
  931. }
  932. if err = pr.LoadAttributes(); err != nil {
  933. return nil, err
  934. }
  935. if err = pr.LoadIssue(); err != nil {
  936. return nil, err
  937. }
  938. return pr, nil
  939. }
  940. func getPullRequestByID(e Engine, id int64) (*PullRequest, error) {
  941. pr := new(PullRequest)
  942. has, err := e.ID(id).Get(pr)
  943. if err != nil {
  944. return nil, err
  945. } else if !has {
  946. return nil, ErrPullRequestNotExist{id, 0, 0, 0, "", ""}
  947. }
  948. return pr, pr.loadAttributes(e)
  949. }
  950. // GetPullRequestByID returns a pull request by given ID.
  951. func GetPullRequestByID(id int64) (*PullRequest, error) {
  952. return getPullRequestByID(x, id)
  953. }
  954. func getPullRequestByIssueID(e Engine, issueID int64) (*PullRequest, error) {
  955. pr := &PullRequest{
  956. IssueID: issueID,
  957. }
  958. has, err := e.Get(pr)
  959. if err != nil {
  960. return nil, err
  961. } else if !has {
  962. return nil, ErrPullRequestNotExist{0, issueID, 0, 0, "", ""}
  963. }
  964. return pr, pr.loadAttributes(e)
  965. }
  966. // GetPullRequestByIssueID returns pull request by given issue ID.
  967. func GetPullRequestByIssueID(issueID int64) (*PullRequest, error) {
  968. return getPullRequestByIssueID(x, issueID)
  969. }
  970. // Update updates all fields of pull request.
  971. func (pr *PullRequest) Update() error {
  972. _, err := x.ID(pr.ID).AllCols().Update(pr)
  973. return err
  974. }
  975. // UpdateCols updates specific fields of pull request.
  976. func (pr *PullRequest) UpdateCols(cols ...string) error {
  977. _, err := x.ID(pr.ID).Cols(cols...).Update(pr)
  978. return err
  979. }
  980. // UpdatePatch generates and saves a new patch.
  981. func (pr *PullRequest) UpdatePatch() (err error) {
  982. if err = pr.GetHeadRepo(); err != nil {
  983. return fmt.Errorf("GetHeadRepo: %v", err)
  984. } else if pr.HeadRepo == nil {
  985. log.Trace("PullRequest[%d].UpdatePatch: ignored cruppted data", pr.ID)
  986. return nil
  987. }
  988. if err = pr.GetBaseRepo(); err != nil {
  989. return fmt.Errorf("GetBaseRepo: %v", err)
  990. }
  991. headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
  992. if err != nil {
  993. return fmt.Errorf("OpenRepository: %v", err)
  994. }
  995. // Add a temporary remote.
  996. tmpRemote := com.ToStr(time.Now().UnixNano())
  997. if err = headGitRepo.AddRemote(tmpRemote, RepoPath(pr.BaseRepo.MustOwner().Name, pr.BaseRepo.Name), true); err != nil {
  998. return fmt.Errorf("AddRemote: %v", err)
  999. }
  1000. defer func() {
  1001. if err := headGitRepo.RemoveRemote(tmpRemote); err != nil {
  1002. log.Error("UpdatePatch: RemoveRemote: %s", err)
  1003. }
  1004. }()
  1005. pr.MergeBase, _, err = headGitRepo.GetMergeBase(tmpRemote, pr.BaseBranch, pr.HeadBranch)
  1006. if err != nil {
  1007. return fmt.Errorf("GetMergeBase: %v", err)
  1008. } else if err = pr.Update(); err != nil {
  1009. return fmt.Errorf("Update: %v", err)
  1010. }
  1011. patch, err := headGitRepo.GetPatch(pr.MergeBase, pr.HeadBranch)
  1012. if err != nil {
  1013. return fmt.Errorf("GetPatch: %v", err)
  1014. }
  1015. if err = pr.BaseRepo.SavePatch(pr.Index, patch); err != nil {
  1016. return fmt.Errorf("BaseRepo.SavePatch: %v", err)
  1017. }
  1018. return nil
  1019. }
  1020. // PushToBaseRepo pushes commits from branches of head repository to
  1021. // corresponding branches of base repository.
  1022. // FIXME: Only push branches that are actually updates?
  1023. func (pr *PullRequest) PushToBaseRepo() (err error) {
  1024. log.Trace("PushToBaseRepo[%d]: pushing commits to base repo '%s'", pr.BaseRepoID, pr.GetGitRefName())
  1025. headRepoPath := pr.HeadRepo.RepoPath()
  1026. headGitRepo, err := git.OpenRepository(headRepoPath)
  1027. if err != nil {
  1028. return fmt.Errorf("OpenRepository: %v", err)
  1029. }
  1030. tmpRemoteName := fmt.Sprintf("tmp-pull-%d", pr.ID)
  1031. if err = headGitRepo.AddRemote(tmpRemoteName, pr.BaseRepo.RepoPath(), false); err != nil {
  1032. return fmt.Errorf("headGitRepo.AddRemote: %v", err)
  1033. }
  1034. // Make sure to remove the remote even if the push fails
  1035. defer func() {
  1036. if err := headGitRepo.RemoveRemote(tmpRemoteName); err != nil {
  1037. log.Error("PushToBaseRepo: RemoveRemote: %s", err)
  1038. }
  1039. }()
  1040. headFile := pr.GetGitRefName()
  1041. // Remove head in case there is a conflict.
  1042. file := path.Join(pr.BaseRepo.RepoPath(), headFile)
  1043. _ = os.Remove(file)
  1044. if err = git.Push(headRepoPath, git.PushOptions{
  1045. Remote: tmpRemoteName,
  1046. Branch: fmt.Sprintf("%s:%s", pr.HeadBranch, headFile),
  1047. Force: true,
  1048. }); err != nil {
  1049. return fmt.Errorf("Push: %v", err)
  1050. }
  1051. return nil
  1052. }
  1053. // AddToTaskQueue adds itself to pull request test task queue.
  1054. func (pr *PullRequest) AddToTaskQueue() {
  1055. go pullRequestQueue.AddFunc(pr.ID, func() {
  1056. pr.Status = PullRequestStatusChecking
  1057. if err := pr.UpdateCols("status"); err != nil {
  1058. log.Error("AddToTaskQueue.UpdateCols[%d].(add to queue): %v", pr.ID, err)
  1059. }
  1060. })
  1061. }
  1062. // PullRequestList defines a list of pull requests
  1063. type PullRequestList []*PullRequest
  1064. func (prs PullRequestList) loadAttributes(e Engine) error {
  1065. if len(prs) == 0 {
  1066. return nil
  1067. }
  1068. // Load issues.
  1069. issueIDs := prs.getIssueIDs()
  1070. issues := make([]*Issue, 0, len(issueIDs))
  1071. if err := e.
  1072. Where("id > 0").
  1073. In("id", issueIDs).
  1074. Find(&issues); err != nil {
  1075. return fmt.Errorf("find issues: %v", err)
  1076. }
  1077. set := make(map[int64]*Issue)
  1078. for i := range issues {
  1079. set[issues[i].ID] = issues[i]
  1080. }
  1081. for i := range prs {
  1082. prs[i].Issue = set[prs[i].IssueID]
  1083. }
  1084. return nil
  1085. }
  1086. func (prs PullRequestList) getIssueIDs() []int64 {
  1087. issueIDs := make([]int64, 0, len(prs))
  1088. for i := range prs {
  1089. issueIDs = append(issueIDs, prs[i].IssueID)
  1090. }
  1091. return issueIDs
  1092. }
  1093. // LoadAttributes load all the prs attributes
  1094. func (prs PullRequestList) LoadAttributes() error {
  1095. return prs.loadAttributes(x)
  1096. }
  1097. func (prs PullRequestList) invalidateCodeComments(e Engine, doer *User, repo *git.Repository, branch string) error {
  1098. if len(prs) == 0 {
  1099. return nil
  1100. }
  1101. issueIDs := prs.getIssueIDs()
  1102. var codeComments []*Comment
  1103. if err := e.
  1104. Where("type = ? and invalidated = ?", CommentTypeCode, false).
  1105. In("issue_id", issueIDs).
  1106. Find(&codeComments); err != nil {
  1107. return fmt.Errorf("find code comments: %v", err)
  1108. }
  1109. for _, comment := range codeComments {
  1110. if err := comment.CheckInvalidation(repo, doer, branch); err != nil {
  1111. return err
  1112. }
  1113. }
  1114. return nil
  1115. }
  1116. // InvalidateCodeComments will lookup the prs for code comments which got invalidated by change
  1117. func (prs PullRequestList) InvalidateCodeComments(doer *User, repo *git.Repository, branch string) error {
  1118. return prs.invalidateCodeComments(x, doer, repo, branch)
  1119. }
  1120. func addHeadRepoTasks(prs []*PullRequest) {
  1121. for _, pr := range prs {
  1122. log.Trace("addHeadRepoTasks[%d]: composing new test task", pr.ID)
  1123. if err := pr.UpdatePatch(); err != nil {
  1124. log.Error("UpdatePatch: %v", err)
  1125. continue
  1126. } else if err := pr.PushToBaseRepo(); err != nil {
  1127. log.Error("PushToBaseRepo: %v", err)
  1128. continue
  1129. }
  1130. pr.AddToTaskQueue()
  1131. }
  1132. }
  1133. // AddTestPullRequestTask adds new test tasks by given head/base repository and head/base branch,
  1134. // and generate new patch for testing as needed.
  1135. func AddTestPullRequestTask(doer *User, repoID int64, branch string, isSync bool) {
  1136. log.Trace("AddTestPullRequestTask [head_repo_id: %d, head_branch: %s]: finding pull requests", repoID, branch)
  1137. prs, err := GetUnmergedPullRequestsByHeadInfo(repoID, branch)
  1138. if err != nil {
  1139. log.Error("Find pull requests [head_repo_id: %d, head_branch: %s]: %v", repoID, branch, err)
  1140. return
  1141. }
  1142. if isSync {
  1143. requests := PullRequestList(prs)
  1144. if err = requests.LoadAttributes(); err != nil {
  1145. log.Error("PullRequestList.LoadAttributes: %v", err)
  1146. }
  1147. if invalidationErr := checkForInvalidation(requests, repoID, doer, branch); invalidationErr != nil {
  1148. log.Error("checkForInvalidation: %v", invalidationErr)
  1149. }
  1150. if err == nil {
  1151. for _, pr := range prs {
  1152. pr.Issue.PullRequest = pr
  1153. if err = pr.Issue.LoadAttributes(); err != nil {
  1154. log.Error("LoadAttributes: %v", err)
  1155. continue
  1156. }
  1157. if err = PrepareWebhooks(pr.Issue.Repo, HookEventPullRequest, &api.PullRequestPayload{
  1158. Action: api.HookIssueSynchronized,
  1159. Index: pr.Issue.Index,
  1160. PullRequest: pr.Issue.PullRequest.APIFormat(),
  1161. Repository: pr.Issue.Repo.APIFormat(AccessModeNone),
  1162. Sender: doer.APIFormat(),
  1163. }); err != nil {
  1164. log.Error("PrepareWebhooks [pull_id: %v]: %v", pr.ID, err)
  1165. continue
  1166. }
  1167. go HookQueue.Add(pr.Issue.Repo.ID)
  1168. }
  1169. }
  1170. }
  1171. addHeadRepoTasks(prs)
  1172. log.Trace("AddTestPullRequestTask [base_repo_id: %d, base_branch: %s]: finding pull requests", repoID, branch)
  1173. prs, err = GetUnmergedPullRequestsByBaseInfo(repoID, branch)
  1174. if err != nil {
  1175. log.Error("Find pull requests [base_repo_id: %d, base_branch: %s]: %v", repoID, branch, err)
  1176. return
  1177. }
  1178. for _, pr := range prs {
  1179. pr.AddToTaskQueue()
  1180. }
  1181. }
  1182. func checkForInvalidation(requests PullRequestList, repoID int64, doer *User, branch string) error {
  1183. repo, err := GetRepositoryByID(repoID)
  1184. if err != nil {
  1185. return fmt.Errorf("GetRepositoryByID: %v", err)
  1186. }
  1187. gitRepo, err := git.OpenRepository(repo.RepoPath())
  1188. if err != nil {
  1189. return fmt.Errorf("git.OpenRepository: %v", err)
  1190. }
  1191. go func() {
  1192. err := requests.InvalidateCodeComments(doer, gitRepo, branch)
  1193. if err != nil {
  1194. log.Error("PullRequestList.InvalidateCodeComments: %v", err)
  1195. }
  1196. }()
  1197. return nil
  1198. }
  1199. // ChangeUsernameInPullRequests changes the name of head_user_name
  1200. func ChangeUsernameInPullRequests(oldUserName, newUserName string) error {
  1201. pr := PullRequest{
  1202. HeadUserName: strings.ToLower(newUserName),
  1203. }
  1204. _, err := x.
  1205. Cols("head_user_name").
  1206. Where("head_user_name = ?", strings.ToLower(oldUserName)).
  1207. Update(pr)
  1208. return err
  1209. }
  1210. // checkAndUpdateStatus checks if pull request is possible to leaving checking status,
  1211. // and set to be either conflict or mergeable.
  1212. func (pr *PullRequest) checkAndUpdateStatus() {
  1213. // Status is not changed to conflict means mergeable.
  1214. if pr.Status == PullRequestStatusChecking {
  1215. pr.Status = PullRequestStatusMergeable
  1216. }
  1217. // Make sure there is no waiting test to process before leaving the checking status.
  1218. if !pullRequestQueue.Exist(pr.ID) {
  1219. if err := pr.UpdateCols("status, conflicted_files"); err != nil {
  1220. log.Error("Update[%d]: %v", pr.ID, err)
  1221. }
  1222. }
  1223. }
  1224. // IsWorkInProgress determine if the Pull Request is a Work In Progress by its title
  1225. func (pr *PullRequest) IsWorkInProgress() bool {
  1226. if err := pr.LoadIssue(); err != nil {
  1227. log.Error("LoadIssue: %v", err)
  1228. return false
  1229. }
  1230. for _, prefix := range setting.Repository.PullRequest.WorkInProgressPrefixes {
  1231. if strings.HasPrefix(strings.ToUpper(pr.Issue.Title), prefix) {
  1232. return true
  1233. }
  1234. }
  1235. return false
  1236. }
  1237. // IsFilesConflicted determines if the Pull Request has changes conflicting with the target branch.
  1238. func (pr *PullRequest) IsFilesConflicted() bool {
  1239. return len(pr.ConflictedFiles) > 0
  1240. }
  1241. // GetWorkInProgressPrefix returns the prefix used to mark the pull request as a work in progress.
  1242. // It returns an empty string when none were found
  1243. func (pr *PullRequest) GetWorkInProgressPrefix() string {
  1244. if err := pr.LoadIssue(); err != nil {
  1245. log.Error("LoadIssue: %v", err)
  1246. return ""
  1247. }
  1248. for _, prefix := range setting.Repository.PullRequest.WorkInProgressPrefixes {
  1249. if strings.HasPrefix(strings.ToUpper(pr.Issue.Title), prefix) {
  1250. return pr.Issue.Title[0:len(prefix)]
  1251. }
  1252. }
  1253. return ""
  1254. }
  1255. // TestPullRequests checks and tests untested patches of pull requests.
  1256. // TODO: test more pull requests at same time.
  1257. func TestPullRequests() {
  1258. prs := make([]*PullRequest, 0, 10)
  1259. err := x.Where("status = ?", PullRequestStatusChecking).Find(&prs)
  1260. if err != nil {
  1261. log.Error("Find Checking PRs: %v", err)
  1262. return
  1263. }
  1264. var checkedPRs = make(map[int64]struct{})
  1265. // Update pull request status.
  1266. for _, pr := range prs {
  1267. checkedPRs[pr.ID] = struct{}{}
  1268. if err := pr.GetBaseRepo(); err != nil {
  1269. log.Error("GetBaseRepo: %v", err)
  1270. continue
  1271. }
  1272. if pr.manuallyMerged() {
  1273. continue
  1274. }
  1275. if err := pr.testPatch(x); err != nil {
  1276. log.Error("testPatch: %v", err)
  1277. continue
  1278. }
  1279. pr.checkAndUpdateStatus()
  1280. }
  1281. // Start listening on new test requests.
  1282. for prID := range pullRequestQueue.Queue() {
  1283. log.Trace("TestPullRequests[%v]: processing test task", prID)
  1284. pullRequestQueue.Remove(prID)
  1285. id := com.StrTo(prID).MustInt64()
  1286. if _, ok := checkedPRs[id]; ok {
  1287. continue
  1288. }
  1289. pr, err := GetPullRequestByID(id)
  1290. if err != nil {
  1291. log.Error("GetPullRequestByID[%s]: %v", prID, err)
  1292. continue
  1293. } else if pr.manuallyMerged() {
  1294. continue
  1295. } else if err = pr.testPatch(x); err != nil {
  1296. log.Error("testPatch[%d]: %v", pr.ID, err)
  1297. continue
  1298. }
  1299. pr.checkAndUpdateStatus()
  1300. }
  1301. }
  1302. // InitTestPullRequests runs the task to test all the checking status pull requests
  1303. func InitTestPullRequests() {
  1304. go TestPullRequests()
  1305. }