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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "os"
  8. "path"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "code.gitea.io/git"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/process"
  16. "code.gitea.io/gitea/modules/setting"
  17. "code.gitea.io/gitea/modules/sync"
  18. api "code.gitea.io/sdk/gitea"
  19. "github.com/Unknwon/com"
  20. "github.com/go-xorm/xorm"
  21. )
  22. var pullRequestQueue = sync.NewUniqueQueue(setting.Repository.PullRequestQueueLength)
  23. // PullRequestType defines pull request type
  24. type PullRequestType int
  25. // Enumerate all the pull request types
  26. const (
  27. PullRequestGitea PullRequestType = iota
  28. PullRequestGit
  29. )
  30. // PullRequestStatus defines pull request status
  31. type PullRequestStatus int
  32. // Enumerate all the pull request status
  33. const (
  34. PullRequestStatusConflict PullRequestStatus = iota
  35. PullRequestStatusChecking
  36. PullRequestStatusMergeable
  37. )
  38. // PullRequest represents relation between pull request and repositories.
  39. type PullRequest struct {
  40. ID int64 `xorm:"pk autoincr"`
  41. Type PullRequestType
  42. Status PullRequestStatus
  43. IssueID int64 `xorm:"INDEX"`
  44. Issue *Issue `xorm:"-"`
  45. Index int64
  46. HeadRepoID int64
  47. HeadRepo *Repository `xorm:"-"`
  48. BaseRepoID int64
  49. BaseRepo *Repository `xorm:"-"`
  50. HeadUserName string
  51. HeadBranch string
  52. BaseBranch string
  53. MergeBase string `xorm:"VARCHAR(40)"`
  54. HasMerged bool
  55. MergedCommitID string `xorm:"VARCHAR(40)"`
  56. MergerID int64
  57. Merger *User `xorm:"-"`
  58. Merged time.Time `xorm:"-"`
  59. MergedUnix int64
  60. }
  61. // BeforeUpdate is invoked from XORM before updating an object of this type.
  62. func (pr *PullRequest) BeforeUpdate() {
  63. pr.MergedUnix = pr.Merged.Unix()
  64. }
  65. // AfterSet is invoked from XORM after setting the value of a field of this object.
  66. // Note: don't try to get Issue because will end up recursive querying.
  67. func (pr *PullRequest) AfterSet(colName string, _ xorm.Cell) {
  68. switch colName {
  69. case "merged_unix":
  70. if !pr.HasMerged {
  71. return
  72. }
  73. pr.Merged = time.Unix(pr.MergedUnix, 0).Local()
  74. }
  75. }
  76. // Note: don't try to get Issue because will end up recursive querying.
  77. func (pr *PullRequest) loadAttributes(e Engine) (err error) {
  78. if pr.HasMerged && pr.Merger == nil {
  79. pr.Merger, err = getUserByID(e, pr.MergerID)
  80. if IsErrUserNotExist(err) {
  81. pr.MergerID = -1
  82. pr.Merger = NewGhostUser()
  83. } else if err != nil {
  84. return fmt.Errorf("getUserByID [%d]: %v", pr.MergerID, err)
  85. }
  86. }
  87. return nil
  88. }
  89. // LoadAttributes loads pull request attributes from database
  90. func (pr *PullRequest) LoadAttributes() error {
  91. return pr.loadAttributes(x)
  92. }
  93. // LoadIssue loads issue information from database
  94. func (pr *PullRequest) LoadIssue() (err error) {
  95. if pr.Issue != nil {
  96. return nil
  97. }
  98. pr.Issue, err = GetIssueByID(pr.IssueID)
  99. return err
  100. }
  101. // APIFormat assumes following fields have been assigned with valid values:
  102. // Required - Issue
  103. // Optional - Merger
  104. func (pr *PullRequest) APIFormat() *api.PullRequest {
  105. var (
  106. baseBranch *Branch
  107. headBranch *Branch
  108. baseCommit *git.Commit
  109. headCommit *git.Commit
  110. err error
  111. )
  112. apiIssue := pr.Issue.APIFormat()
  113. if pr.BaseRepo == nil {
  114. pr.BaseRepo, err = GetRepositoryByID(pr.BaseRepoID)
  115. if err != nil {
  116. log.Error(log.ERROR, "GetRepositoryById[%d]: %v", pr.ID, err)
  117. return nil
  118. }
  119. }
  120. if pr.HeadRepo == nil {
  121. pr.HeadRepo, err = GetRepositoryByID(pr.HeadRepoID)
  122. if err != nil {
  123. log.Error(log.ERROR, "GetRepositoryById[%d]: %v", pr.ID, err)
  124. return nil
  125. }
  126. }
  127. if baseBranch, err = pr.BaseRepo.GetBranch(pr.BaseBranch); err != nil {
  128. return nil
  129. }
  130. if baseCommit, err = baseBranch.GetCommit(); err != nil {
  131. return nil
  132. }
  133. if headBranch, err = pr.HeadRepo.GetBranch(pr.HeadBranch); err != nil {
  134. return nil
  135. }
  136. if headCommit, err = headBranch.GetCommit(); err != nil {
  137. return nil
  138. }
  139. apiBaseBranchInfo := &api.PRBranchInfo{
  140. Name: pr.BaseBranch,
  141. Ref: pr.BaseBranch,
  142. Sha: baseCommit.ID.String(),
  143. RepoID: pr.BaseRepoID,
  144. Repository: pr.BaseRepo.APIFormat(AccessModeNone),
  145. }
  146. apiHeadBranchInfo := &api.PRBranchInfo{
  147. Name: pr.HeadBranch,
  148. Ref: pr.HeadBranch,
  149. Sha: headCommit.ID.String(),
  150. RepoID: pr.HeadRepoID,
  151. Repository: pr.HeadRepo.APIFormat(AccessModeNone),
  152. }
  153. apiPullRequest := &api.PullRequest{
  154. ID: pr.ID,
  155. Index: pr.Index,
  156. Poster: apiIssue.Poster,
  157. Title: apiIssue.Title,
  158. Body: apiIssue.Body,
  159. Labels: apiIssue.Labels,
  160. Milestone: apiIssue.Milestone,
  161. Assignee: apiIssue.Assignee,
  162. State: apiIssue.State,
  163. Comments: apiIssue.Comments,
  164. HTMLURL: pr.Issue.HTMLURL(),
  165. DiffURL: pr.Issue.DiffURL(),
  166. PatchURL: pr.Issue.PatchURL(),
  167. HasMerged: pr.HasMerged,
  168. Base: apiBaseBranchInfo,
  169. Head: apiHeadBranchInfo,
  170. MergeBase: pr.MergeBase,
  171. }
  172. if pr.Status != PullRequestStatusChecking {
  173. mergeable := pr.Status != PullRequestStatusConflict
  174. apiPullRequest.Mergeable = mergeable
  175. }
  176. if pr.HasMerged {
  177. apiPullRequest.Merged = &pr.Merged
  178. apiPullRequest.MergedCommitID = &pr.MergedCommitID
  179. apiPullRequest.MergedBy = pr.Merger.APIFormat()
  180. }
  181. return apiPullRequest
  182. }
  183. func (pr *PullRequest) getHeadRepo(e Engine) (err error) {
  184. pr.HeadRepo, err = getRepositoryByID(e, pr.HeadRepoID)
  185. if err != nil && !IsErrRepoNotExist(err) {
  186. return fmt.Errorf("getRepositoryByID(head): %v", err)
  187. }
  188. return nil
  189. }
  190. // GetHeadRepo loads the head repository
  191. func (pr *PullRequest) GetHeadRepo() error {
  192. return pr.getHeadRepo(x)
  193. }
  194. // GetBaseRepo loads the target repository
  195. func (pr *PullRequest) GetBaseRepo() (err error) {
  196. if pr.BaseRepo != nil {
  197. return nil
  198. }
  199. pr.BaseRepo, err = GetRepositoryByID(pr.BaseRepoID)
  200. if err != nil {
  201. return fmt.Errorf("GetRepositoryByID(base): %v", err)
  202. }
  203. return nil
  204. }
  205. // IsChecking returns true if this pull request is still checking conflict.
  206. func (pr *PullRequest) IsChecking() bool {
  207. return pr.Status == PullRequestStatusChecking
  208. }
  209. // CanAutoMerge returns true if this pull request can be merged automatically.
  210. func (pr *PullRequest) CanAutoMerge() bool {
  211. return pr.Status == PullRequestStatusMergeable
  212. }
  213. // Merge merges pull request to base repository.
  214. // FIXME: add repoWorkingPull make sure two merges does not happen at same time.
  215. func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository) (err error) {
  216. if err = pr.GetHeadRepo(); err != nil {
  217. return fmt.Errorf("GetHeadRepo: %v", err)
  218. } else if err = pr.GetBaseRepo(); err != nil {
  219. return fmt.Errorf("GetBaseRepo: %v", err)
  220. }
  221. defer func() {
  222. go HookQueue.Add(pr.BaseRepo.ID)
  223. go AddTestPullRequestTask(doer, pr.BaseRepo.ID, pr.BaseBranch, false)
  224. }()
  225. sess := x.NewSession()
  226. defer sessionRelease(sess)
  227. if err = sess.Begin(); err != nil {
  228. return err
  229. }
  230. if err = pr.Issue.changeStatus(sess, doer, pr.Issue.Repo, true); err != nil {
  231. return fmt.Errorf("Issue.changeStatus: %v", err)
  232. }
  233. headRepoPath := RepoPath(pr.HeadUserName, pr.HeadRepo.Name)
  234. headGitRepo, err := git.OpenRepository(headRepoPath)
  235. if err != nil {
  236. return fmt.Errorf("OpenRepository: %v", err)
  237. }
  238. // Clone base repo.
  239. tmpBasePath := path.Join(setting.AppDataPath, "tmp/repos", com.ToStr(time.Now().Nanosecond())+".git")
  240. if err := os.MkdirAll(path.Dir(tmpBasePath), os.ModePerm); err != nil {
  241. return fmt.Errorf("Fail to create dir %s: %v", tmpBasePath, err)
  242. }
  243. defer os.RemoveAll(path.Dir(tmpBasePath))
  244. var stderr string
  245. if _, stderr, err = process.ExecTimeout(5*time.Minute,
  246. fmt.Sprintf("PullRequest.Merge (git clone): %s", tmpBasePath),
  247. "git", "clone", baseGitRepo.Path, tmpBasePath); err != nil {
  248. return fmt.Errorf("git clone: %s", stderr)
  249. }
  250. // Check out base branch.
  251. if _, stderr, err = process.ExecDir(-1, tmpBasePath,
  252. fmt.Sprintf("PullRequest.Merge (git checkout): %s", tmpBasePath),
  253. "git", "checkout", pr.BaseBranch); err != nil {
  254. return fmt.Errorf("git checkout: %s", stderr)
  255. }
  256. // Add head repo remote.
  257. if _, stderr, err = process.ExecDir(-1, tmpBasePath,
  258. fmt.Sprintf("PullRequest.Merge (git remote add): %s", tmpBasePath),
  259. "git", "remote", "add", "head_repo", headRepoPath); err != nil {
  260. return fmt.Errorf("git remote add [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
  261. }
  262. // Merge commits.
  263. if _, stderr, err = process.ExecDir(-1, tmpBasePath,
  264. fmt.Sprintf("PullRequest.Merge (git fetch): %s", tmpBasePath),
  265. "git", "fetch", "head_repo"); err != nil {
  266. return fmt.Errorf("git fetch [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
  267. }
  268. if _, stderr, err = process.ExecDir(-1, tmpBasePath,
  269. fmt.Sprintf("PullRequest.Merge (git merge --no-ff --no-commit): %s", tmpBasePath),
  270. "git", "merge", "--no-ff", "--no-commit", "head_repo/"+pr.HeadBranch); err != nil {
  271. return fmt.Errorf("git merge --no-ff --no-commit [%s]: %v - %s", tmpBasePath, err, stderr)
  272. }
  273. sig := doer.NewGitSig()
  274. if _, stderr, err = process.ExecDir(-1, tmpBasePath,
  275. fmt.Sprintf("PullRequest.Merge (git merge): %s", tmpBasePath),
  276. "git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email),
  277. "-m", fmt.Sprintf("Merge branch '%s' of %s/%s into %s", pr.HeadBranch, pr.HeadUserName, pr.HeadRepo.Name, pr.BaseBranch)); err != nil {
  278. return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, stderr)
  279. }
  280. // Push back to upstream.
  281. if _, stderr, err = process.ExecDir(-1, tmpBasePath,
  282. fmt.Sprintf("PullRequest.Merge (git push): %s", tmpBasePath),
  283. "git", "push", baseGitRepo.Path, pr.BaseBranch); err != nil {
  284. return fmt.Errorf("git push: %s", stderr)
  285. }
  286. pr.MergedCommitID, err = headGitRepo.GetBranchCommitID(pr.HeadBranch)
  287. if err != nil {
  288. return fmt.Errorf("GetBranchCommit: %v", err)
  289. }
  290. pr.HasMerged = true
  291. pr.Merged = time.Now()
  292. pr.MergerID = doer.ID
  293. if _, err = sess.Id(pr.ID).AllCols().Update(pr); err != nil {
  294. return fmt.Errorf("update pull request: %v", err)
  295. }
  296. if err = sess.Commit(); err != nil {
  297. return fmt.Errorf("Commit: %v", err)
  298. }
  299. if err = MergePullRequestAction(doer, pr.Issue.Repo, pr.Issue); err != nil {
  300. log.Error(4, "MergePullRequestAction [%d]: %v", pr.ID, err)
  301. }
  302. // Reload pull request information.
  303. if err = pr.LoadAttributes(); err != nil {
  304. log.Error(4, "LoadAttributes: %v", err)
  305. return nil
  306. }
  307. if err = PrepareWebhooks(pr.Issue.Repo, HookEventPullRequest, &api.PullRequestPayload{
  308. Action: api.HookIssueClosed,
  309. Index: pr.Index,
  310. PullRequest: pr.APIFormat(),
  311. Repository: pr.Issue.Repo.APIFormat(AccessModeNone),
  312. Sender: doer.APIFormat(),
  313. }); err != nil {
  314. log.Error(4, "PrepareWebhooks: %v", err)
  315. return nil
  316. }
  317. l, err := headGitRepo.CommitsBetweenIDs(pr.MergedCommitID, pr.MergeBase)
  318. if err != nil {
  319. log.Error(4, "CommitsBetweenIDs: %v", err)
  320. return nil
  321. }
  322. // TODO: when squash commits, no need to append merge commit.
  323. // It is possible that head branch is not fully sync with base branch for merge commits,
  324. // so we need to get latest head commit and append merge commit manully
  325. // to avoid strange diff commits produced.
  326. mergeCommit, err := baseGitRepo.GetBranchCommit(pr.BaseBranch)
  327. if err != nil {
  328. log.Error(4, "GetBranchCommit: %v", err)
  329. return nil
  330. }
  331. l.PushFront(mergeCommit)
  332. p := &api.PushPayload{
  333. Ref: git.BranchPrefix + pr.BaseBranch,
  334. Before: pr.MergeBase,
  335. After: pr.MergedCommitID,
  336. CompareURL: setting.AppURL + pr.BaseRepo.ComposeCompareURL(pr.MergeBase, pr.MergedCommitID),
  337. Commits: ListToPushCommits(l).ToAPIPayloadCommits(pr.BaseRepo.HTMLURL()),
  338. Repo: pr.BaseRepo.APIFormat(AccessModeNone),
  339. Pusher: pr.HeadRepo.MustOwner().APIFormat(),
  340. Sender: doer.APIFormat(),
  341. }
  342. if err = PrepareWebhooks(pr.BaseRepo, HookEventPush, p); err != nil {
  343. return fmt.Errorf("PrepareWebhooks: %v", err)
  344. }
  345. return nil
  346. }
  347. // patchConflicts is a list of conflict description from Git.
  348. var patchConflicts = []string{
  349. "patch does not apply",
  350. "already exists in working directory",
  351. "unrecognized input",
  352. "error:",
  353. }
  354. // testPatch checks if patch can be merged to base repository without conflict.
  355. func (pr *PullRequest) testPatch() (err error) {
  356. if pr.BaseRepo == nil {
  357. pr.BaseRepo, err = GetRepositoryByID(pr.BaseRepoID)
  358. if err != nil {
  359. return fmt.Errorf("GetRepositoryByID: %v", err)
  360. }
  361. }
  362. patchPath, err := pr.BaseRepo.PatchPath(pr.Index)
  363. if err != nil {
  364. return fmt.Errorf("BaseRepo.PatchPath: %v", err)
  365. }
  366. // Fast fail if patch does not exist, this assumes data is cruppted.
  367. if !com.IsFile(patchPath) {
  368. log.Trace("PullRequest[%d].testPatch: ignored cruppted data", pr.ID)
  369. return nil
  370. }
  371. repoWorkingPool.CheckIn(com.ToStr(pr.BaseRepoID))
  372. defer repoWorkingPool.CheckOut(com.ToStr(pr.BaseRepoID))
  373. log.Trace("PullRequest[%d].testPatch (patchPath): %s", pr.ID, patchPath)
  374. pr.Status = PullRequestStatusChecking
  375. indexTmpPath := filepath.Join(os.TempDir(), "gitea-"+pr.BaseRepo.Name+"-"+strconv.Itoa(time.Now().Nanosecond()))
  376. defer os.Remove(indexTmpPath)
  377. var stderr string
  378. _, stderr, err = process.ExecDirEnv(-1, "", fmt.Sprintf("testPatch (git read-tree): %d", pr.BaseRepo.ID),
  379. []string{"GIT_DIR=" + pr.BaseRepo.RepoPath(), "GIT_INDEX_FILE=" + indexTmpPath},
  380. "git", "read-tree", pr.BaseBranch)
  381. if err != nil {
  382. return fmt.Errorf("git read-tree --index-output=%s %s: %v - %s", indexTmpPath, pr.BaseBranch, err, stderr)
  383. }
  384. _, stderr, err = process.ExecDirEnv(-1, "", fmt.Sprintf("testPatch (git apply --check): %d", pr.BaseRepo.ID),
  385. []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()},
  386. "git", "apply", "--check", "--cached", patchPath)
  387. if err != nil {
  388. for i := range patchConflicts {
  389. if strings.Contains(stderr, patchConflicts[i]) {
  390. log.Trace("PullRequest[%d].testPatch (apply): has conflict", pr.ID)
  391. fmt.Println(stderr)
  392. pr.Status = PullRequestStatusConflict
  393. return nil
  394. }
  395. }
  396. return fmt.Errorf("git apply --check: %v - %s", err, stderr)
  397. }
  398. return nil
  399. }
  400. // NewPullRequest creates new pull request with labels for repository.
  401. func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest, patch []byte) (err error) {
  402. sess := x.NewSession()
  403. defer sessionRelease(sess)
  404. if err = sess.Begin(); err != nil {
  405. return err
  406. }
  407. if err = newIssue(sess, NewIssueOptions{
  408. Repo: repo,
  409. Issue: pull,
  410. LableIDs: labelIDs,
  411. Attachments: uuids,
  412. IsPull: true,
  413. }); err != nil {
  414. return fmt.Errorf("newIssue: %v", err)
  415. }
  416. pr.Index = pull.Index
  417. if err = repo.SavePatch(pr.Index, patch); err != nil {
  418. return fmt.Errorf("SavePatch: %v", err)
  419. }
  420. pr.BaseRepo = repo
  421. if err = pr.testPatch(); err != nil {
  422. return fmt.Errorf("testPatch: %v", err)
  423. }
  424. // No conflict appears after test means mergeable.
  425. if pr.Status == PullRequestStatusChecking {
  426. pr.Status = PullRequestStatusMergeable
  427. }
  428. pr.IssueID = pull.ID
  429. if _, err = sess.Insert(pr); err != nil {
  430. return fmt.Errorf("insert pull repo: %v", err)
  431. }
  432. if err = sess.Commit(); err != nil {
  433. return fmt.Errorf("Commit: %v", err)
  434. }
  435. if err = NotifyWatchers(&Action{
  436. ActUserID: pull.Poster.ID,
  437. ActUserName: pull.Poster.Name,
  438. OpType: ActionCreatePullRequest,
  439. Content: fmt.Sprintf("%d|%s", pull.Index, pull.Title),
  440. RepoID: repo.ID,
  441. RepoUserName: repo.Owner.Name,
  442. RepoName: repo.Name,
  443. IsPrivate: repo.IsPrivate,
  444. }); err != nil {
  445. log.Error(4, "NotifyWatchers: %v", err)
  446. } else if err = pull.MailParticipants(); err != nil {
  447. log.Error(4, "MailParticipants: %v", err)
  448. }
  449. pr.Issue = pull
  450. pull.PullRequest = pr
  451. if err = PrepareWebhooks(repo, HookEventPullRequest, &api.PullRequestPayload{
  452. Action: api.HookIssueOpened,
  453. Index: pull.Index,
  454. PullRequest: pr.APIFormat(),
  455. Repository: repo.APIFormat(AccessModeNone),
  456. Sender: pull.Poster.APIFormat(),
  457. }); err != nil {
  458. log.Error(4, "PrepareWebhooks: %v", err)
  459. }
  460. go HookQueue.Add(repo.ID)
  461. return nil
  462. }
  463. // PullRequestsOptions holds the options for PRs
  464. type PullRequestsOptions struct {
  465. Page int
  466. State string
  467. SortType string
  468. Labels []string
  469. MilestoneID int64
  470. }
  471. func listPullRequestStatement(baseRepoID int64, opts *PullRequestsOptions) *xorm.Session {
  472. sess := x.Where("pull_request.base_repo_id=?", baseRepoID)
  473. sess.Join("INNER", "issue", "pull_request.issue_id = issue.id")
  474. switch opts.State {
  475. case "closed", "open":
  476. sess.And("issue.is_closed=?", opts.State == "closed")
  477. }
  478. return sess
  479. }
  480. // PullRequests returns all pull requests for a base Repo by the given conditions
  481. func PullRequests(baseRepoID int64, opts *PullRequestsOptions) ([]*PullRequest, int64, error) {
  482. if opts.Page <= 0 {
  483. opts.Page = 1
  484. }
  485. countSession := listPullRequestStatement(baseRepoID, opts)
  486. maxResults, err := countSession.Count(new(PullRequest))
  487. if err != nil {
  488. log.Error(4, "Count PRs", err)
  489. return nil, maxResults, err
  490. }
  491. prs := make([]*PullRequest, 0, ItemsPerPage)
  492. findSession := listPullRequestStatement(baseRepoID, opts)
  493. findSession.Limit(ItemsPerPage, (opts.Page-1)*ItemsPerPage)
  494. return prs, maxResults, findSession.Find(&prs)
  495. }
  496. // GetUnmergedPullRequest returnss a pull request that is open and has not been merged
  497. // by given head/base and repo/branch.
  498. func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch string) (*PullRequest, error) {
  499. pr := new(PullRequest)
  500. has, err := x.
  501. Where("head_repo_id=? AND head_branch=? AND base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
  502. headRepoID, headBranch, baseRepoID, baseBranch, false, false).
  503. Join("INNER", "issue", "issue.id=pull_request.issue_id").
  504. Get(pr)
  505. if err != nil {
  506. return nil, err
  507. } else if !has {
  508. return nil, ErrPullRequestNotExist{0, 0, headRepoID, baseRepoID, headBranch, baseBranch}
  509. }
  510. return pr, nil
  511. }
  512. // GetUnmergedPullRequestsByHeadInfo returnss all pull requests that are open and has not been merged
  513. // by given head information (repo and branch).
  514. func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequest, error) {
  515. prs := make([]*PullRequest, 0, 2)
  516. return prs, x.
  517. Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ?",
  518. repoID, branch, false, false).
  519. Join("INNER", "issue", "issue.id = pull_request.issue_id").
  520. Find(&prs)
  521. }
  522. // GetUnmergedPullRequestsByBaseInfo returnss all pull requests that are open and has not been merged
  523. // by given base information (repo and branch).
  524. func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) {
  525. prs := make([]*PullRequest, 0, 2)
  526. return prs, x.
  527. Where("base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
  528. repoID, branch, false, false).
  529. Join("INNER", "issue", "issue.id=pull_request.issue_id").
  530. Find(&prs)
  531. }
  532. // GetPullRequestByIndex returns a pull request by the given index
  533. func GetPullRequestByIndex(repoID int64, index int64) (*PullRequest, error) {
  534. pr := &PullRequest{
  535. BaseRepoID: repoID,
  536. Index: index,
  537. }
  538. has, err := x.Get(pr)
  539. if err != nil {
  540. return nil, err
  541. } else if !has {
  542. return nil, ErrPullRequestNotExist{0, repoID, index, 0, "", ""}
  543. }
  544. if err = pr.LoadAttributes(); err != nil {
  545. return nil, err
  546. }
  547. if err = pr.LoadIssue(); err != nil {
  548. return nil, err
  549. }
  550. return pr, nil
  551. }
  552. func getPullRequestByID(e Engine, id int64) (*PullRequest, error) {
  553. pr := new(PullRequest)
  554. has, err := e.Id(id).Get(pr)
  555. if err != nil {
  556. return nil, err
  557. } else if !has {
  558. return nil, ErrPullRequestNotExist{id, 0, 0, 0, "", ""}
  559. }
  560. return pr, pr.loadAttributes(e)
  561. }
  562. // GetPullRequestByID returns a pull request by given ID.
  563. func GetPullRequestByID(id int64) (*PullRequest, error) {
  564. return getPullRequestByID(x, id)
  565. }
  566. func getPullRequestByIssueID(e Engine, issueID int64) (*PullRequest, error) {
  567. pr := &PullRequest{
  568. IssueID: issueID,
  569. }
  570. has, err := e.Get(pr)
  571. if err != nil {
  572. return nil, err
  573. } else if !has {
  574. return nil, ErrPullRequestNotExist{0, issueID, 0, 0, "", ""}
  575. }
  576. return pr, pr.loadAttributes(e)
  577. }
  578. // GetPullRequestByIssueID returns pull request by given issue ID.
  579. func GetPullRequestByIssueID(issueID int64) (*PullRequest, error) {
  580. return getPullRequestByIssueID(x, issueID)
  581. }
  582. // Update updates all fields of pull request.
  583. func (pr *PullRequest) Update() error {
  584. _, err := x.Id(pr.ID).AllCols().Update(pr)
  585. return err
  586. }
  587. // UpdateCols updates specific fields of pull request.
  588. func (pr *PullRequest) UpdateCols(cols ...string) error {
  589. _, err := x.Id(pr.ID).Cols(cols...).Update(pr)
  590. return err
  591. }
  592. // UpdatePatch generates and saves a new patch.
  593. func (pr *PullRequest) UpdatePatch() (err error) {
  594. if err = pr.GetHeadRepo(); err != nil {
  595. return fmt.Errorf("GetHeadRepo: %v", err)
  596. } else if pr.HeadRepo == nil {
  597. log.Trace("PullRequest[%d].UpdatePatch: ignored cruppted data", pr.ID)
  598. return nil
  599. }
  600. if err = pr.GetBaseRepo(); err != nil {
  601. return fmt.Errorf("GetBaseRepo: %v", err)
  602. }
  603. headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
  604. if err != nil {
  605. return fmt.Errorf("OpenRepository: %v", err)
  606. }
  607. // Add a temporary remote.
  608. tmpRemote := com.ToStr(time.Now().UnixNano())
  609. if err = headGitRepo.AddRemote(tmpRemote, RepoPath(pr.BaseRepo.MustOwner().Name, pr.BaseRepo.Name), true); err != nil {
  610. return fmt.Errorf("AddRemote: %v", err)
  611. }
  612. defer func() {
  613. headGitRepo.RemoveRemote(tmpRemote)
  614. }()
  615. remoteBranch := "remotes/" + tmpRemote + "/" + pr.BaseBranch
  616. pr.MergeBase, err = headGitRepo.GetMergeBase(remoteBranch, pr.HeadBranch)
  617. if err != nil {
  618. return fmt.Errorf("GetMergeBase: %v", err)
  619. } else if err = pr.Update(); err != nil {
  620. return fmt.Errorf("Update: %v", err)
  621. }
  622. patch, err := headGitRepo.GetPatch(pr.MergeBase, pr.HeadBranch)
  623. if err != nil {
  624. return fmt.Errorf("GetPatch: %v", err)
  625. }
  626. if err = pr.BaseRepo.SavePatch(pr.Index, patch); err != nil {
  627. return fmt.Errorf("BaseRepo.SavePatch: %v", err)
  628. }
  629. return nil
  630. }
  631. // PushToBaseRepo pushes commits from branches of head repository to
  632. // corresponding branches of base repository.
  633. // FIXME: Only push branches that are actually updates?
  634. func (pr *PullRequest) PushToBaseRepo() (err error) {
  635. log.Trace("PushToBaseRepo[%d]: pushing commits to base repo 'refs/pull/%d/head'", pr.BaseRepoID, pr.Index)
  636. headRepoPath := pr.HeadRepo.RepoPath()
  637. headGitRepo, err := git.OpenRepository(headRepoPath)
  638. if err != nil {
  639. return fmt.Errorf("OpenRepository: %v", err)
  640. }
  641. tmpRemoteName := fmt.Sprintf("tmp-pull-%d", pr.ID)
  642. if err = headGitRepo.AddRemote(tmpRemoteName, pr.BaseRepo.RepoPath(), false); err != nil {
  643. return fmt.Errorf("headGitRepo.AddRemote: %v", err)
  644. }
  645. // Make sure to remove the remote even if the push fails
  646. defer headGitRepo.RemoveRemote(tmpRemoteName)
  647. headFile := fmt.Sprintf("refs/pull/%d/head", pr.Index)
  648. // Remove head in case there is a conflict.
  649. file := path.Join(pr.BaseRepo.RepoPath(), headFile)
  650. _ = os.Remove(file)
  651. if err = git.Push(headRepoPath, tmpRemoteName, fmt.Sprintf("%s:%s", pr.HeadBranch, headFile)); err != nil {
  652. return fmt.Errorf("Push: %v", err)
  653. }
  654. return nil
  655. }
  656. // AddToTaskQueue adds itself to pull request test task queue.
  657. func (pr *PullRequest) AddToTaskQueue() {
  658. go pullRequestQueue.AddFunc(pr.ID, func() {
  659. pr.Status = PullRequestStatusChecking
  660. if err := pr.UpdateCols("status"); err != nil {
  661. log.Error(5, "AddToTaskQueue.UpdateCols[%d].(add to queue): %v", pr.ID, err)
  662. }
  663. })
  664. }
  665. // PullRequestList defines a list of pull requests
  666. type PullRequestList []*PullRequest
  667. func (prs PullRequestList) loadAttributes(e Engine) error {
  668. if len(prs) == 0 {
  669. return nil
  670. }
  671. // Load issues.
  672. issueIDs := make([]int64, 0, len(prs))
  673. for i := range prs {
  674. issueIDs = append(issueIDs, prs[i].IssueID)
  675. }
  676. issues := make([]*Issue, 0, len(issueIDs))
  677. if err := e.
  678. Where("id > 0").
  679. In("id", issueIDs).
  680. Find(&issues); err != nil {
  681. return fmt.Errorf("find issues: %v", err)
  682. }
  683. set := make(map[int64]*Issue)
  684. for i := range issues {
  685. set[issues[i].ID] = issues[i]
  686. }
  687. for i := range prs {
  688. prs[i].Issue = set[prs[i].IssueID]
  689. }
  690. return nil
  691. }
  692. // LoadAttributes load all the prs attributes
  693. func (prs PullRequestList) LoadAttributes() error {
  694. return prs.loadAttributes(x)
  695. }
  696. func addHeadRepoTasks(prs []*PullRequest) {
  697. for _, pr := range prs {
  698. log.Trace("addHeadRepoTasks[%d]: composing new test task", pr.ID)
  699. if err := pr.UpdatePatch(); err != nil {
  700. log.Error(4, "UpdatePatch: %v", err)
  701. continue
  702. } else if err := pr.PushToBaseRepo(); err != nil {
  703. log.Error(4, "PushToBaseRepo: %v", err)
  704. continue
  705. }
  706. pr.AddToTaskQueue()
  707. }
  708. }
  709. // AddTestPullRequestTask adds new test tasks by given head/base repository and head/base branch,
  710. // and generate new patch for testing as needed.
  711. func AddTestPullRequestTask(doer *User, repoID int64, branch string, isSync bool) {
  712. log.Trace("AddTestPullRequestTask [head_repo_id: %d, head_branch: %s]: finding pull requests", repoID, branch)
  713. prs, err := GetUnmergedPullRequestsByHeadInfo(repoID, branch)
  714. if err != nil {
  715. log.Error(4, "Find pull requests [head_repo_id: %d, head_branch: %s]: %v", repoID, branch, err)
  716. return
  717. }
  718. if isSync {
  719. if err = PullRequestList(prs).LoadAttributes(); err != nil {
  720. log.Error(4, "PullRequestList.LoadAttributes: %v", err)
  721. }
  722. if err == nil {
  723. for _, pr := range prs {
  724. pr.Issue.PullRequest = pr
  725. if err = pr.Issue.LoadAttributes(); err != nil {
  726. log.Error(4, "LoadAttributes: %v", err)
  727. continue
  728. }
  729. if err = PrepareWebhooks(pr.Issue.Repo, HookEventPullRequest, &api.PullRequestPayload{
  730. Action: api.HookIssueSynchronized,
  731. Index: pr.Issue.Index,
  732. PullRequest: pr.Issue.PullRequest.APIFormat(),
  733. Repository: pr.Issue.Repo.APIFormat(AccessModeNone),
  734. Sender: doer.APIFormat(),
  735. }); err != nil {
  736. log.Error(4, "PrepareWebhooks [pull_id: %v]: %v", pr.ID, err)
  737. continue
  738. }
  739. go HookQueue.Add(pr.Issue.Repo.ID)
  740. }
  741. }
  742. }
  743. addHeadRepoTasks(prs)
  744. log.Trace("AddTestPullRequestTask [base_repo_id: %d, base_branch: %s]: finding pull requests", repoID, branch)
  745. prs, err = GetUnmergedPullRequestsByBaseInfo(repoID, branch)
  746. if err != nil {
  747. log.Error(4, "Find pull requests [base_repo_id: %d, base_branch: %s]: %v", repoID, branch, err)
  748. return
  749. }
  750. for _, pr := range prs {
  751. pr.AddToTaskQueue()
  752. }
  753. }
  754. // ChangeUsernameInPullRequests changes the name of head_user_name
  755. func ChangeUsernameInPullRequests(oldUserName, newUserName string) error {
  756. pr := PullRequest{
  757. HeadUserName: strings.ToLower(newUserName),
  758. }
  759. _, err := x.
  760. Cols("head_user_name").
  761. Where("head_user_name = ?", strings.ToLower(oldUserName)).
  762. Update(pr)
  763. return err
  764. }
  765. // checkAndUpdateStatus checks if pull request is possible to levaing checking status,
  766. // and set to be either conflict or mergeable.
  767. func (pr *PullRequest) checkAndUpdateStatus() {
  768. // Status is not changed to conflict means mergeable.
  769. if pr.Status == PullRequestStatusChecking {
  770. pr.Status = PullRequestStatusMergeable
  771. }
  772. // Make sure there is no waiting test to process before levaing the checking status.
  773. if !pullRequestQueue.Exist(pr.ID) {
  774. if err := pr.UpdateCols("status"); err != nil {
  775. log.Error(4, "Update[%d]: %v", pr.ID, err)
  776. }
  777. }
  778. }
  779. // TestPullRequests checks and tests untested patches of pull requests.
  780. // TODO: test more pull requests at same time.
  781. func TestPullRequests() {
  782. prs := make([]*PullRequest, 0, 10)
  783. x.Iterate(PullRequest{
  784. Status: PullRequestStatusChecking,
  785. },
  786. func(idx int, bean interface{}) error {
  787. pr := bean.(*PullRequest)
  788. if err := pr.GetBaseRepo(); err != nil {
  789. log.Error(3, "GetBaseRepo: %v", err)
  790. return nil
  791. }
  792. if err := pr.testPatch(); err != nil {
  793. log.Error(3, "testPatch: %v", err)
  794. return nil
  795. }
  796. prs = append(prs, pr)
  797. return nil
  798. })
  799. // Update pull request status.
  800. for _, pr := range prs {
  801. pr.checkAndUpdateStatus()
  802. }
  803. // Start listening on new test requests.
  804. for prID := range pullRequestQueue.Queue() {
  805. log.Trace("TestPullRequests[%v]: processing test task", prID)
  806. pullRequestQueue.Remove(prID)
  807. pr, err := GetPullRequestByID(com.StrTo(prID).MustInt64())
  808. if err != nil {
  809. log.Error(4, "GetPullRequestByID[%s]: %v", prID, err)
  810. continue
  811. } else if err = pr.testPatch(); err != nil {
  812. log.Error(4, "testPatch[%d]: %v", pr.ID, err)
  813. continue
  814. }
  815. pr.checkAndUpdateStatus()
  816. }
  817. }
  818. // InitTestPullRequests runs the task to test all the checking status pull requests
  819. func InitTestPullRequests() {
  820. go TestPullRequests()
  821. }