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

pull.go 29 kB

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