pull.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. "errors"
  7. "fmt"
  8. "os"
  9. "path"
  10. "strings"
  11. "time"
  12. "github.com/Unknwon/com"
  13. "github.com/go-xorm/xorm"
  14. "github.com/gogits/git-module"
  15. api "github.com/gogits/go-gogs-client"
  16. "github.com/gogits/gogs/modules/log"
  17. "github.com/gogits/gogs/modules/process"
  18. "github.com/gogits/gogs/modules/setting"
  19. "strconv"
  20. )
  21. type PullRequestType int
  22. const (
  23. PULL_REQUEST_GOGS PullRequestType = iota
  24. PLLL_ERQUEST_GIT
  25. )
  26. type PullRequestStatus int
  27. const (
  28. PULL_REQUEST_STATUS_CONFLICT PullRequestStatus = iota
  29. PULL_REQUEST_STATUS_CHECKING
  30. PULL_REQUEST_STATUS_MERGEABLE
  31. )
  32. // PullRequest represents relation between pull request and repositories.
  33. type PullRequest struct {
  34. ID int64 `xorm:"pk autoincr"`
  35. Type PullRequestType
  36. Status PullRequestStatus
  37. IssueID int64 `xorm:"INDEX"`
  38. Issue *Issue `xorm:"-"`
  39. Index int64
  40. HeadRepoID int64
  41. HeadRepo *Repository `xorm:"-"`
  42. BaseRepoID int64
  43. BaseRepo *Repository `xorm:"-"`
  44. HeadUserName string
  45. HeadBranch string
  46. BaseBranch string
  47. MergeBase string `xorm:"VARCHAR(40)"`
  48. HasMerged bool
  49. MergedCommitID string `xorm:"VARCHAR(40)"`
  50. Merged time.Time
  51. MergerID int64
  52. Merger *User `xorm:"-"`
  53. }
  54. // Note: don't try to get Pull because will end up recursive querying.
  55. func (pr *PullRequest) AfterSet(colName string, _ xorm.Cell) {
  56. switch colName {
  57. case "merged":
  58. if !pr.HasMerged {
  59. return
  60. }
  61. pr.Merged = regulateTimeZone(pr.Merged)
  62. }
  63. }
  64. func (pr *PullRequest) getHeadRepo(e Engine) (err error) {
  65. pr.HeadRepo, err = getRepositoryByID(e, pr.HeadRepoID)
  66. if err != nil && !IsErrRepoNotExist(err) {
  67. return fmt.Errorf("getRepositoryByID(head): %v", err)
  68. }
  69. return nil
  70. }
  71. func (pr *PullRequest) GetHeadRepo() (err error) {
  72. return pr.getHeadRepo(x)
  73. }
  74. func (pr *PullRequest) GetBaseRepo() (err error) {
  75. if pr.BaseRepo != nil {
  76. return nil
  77. }
  78. pr.BaseRepo, err = GetRepositoryByID(pr.BaseRepoID)
  79. if err != nil {
  80. return fmt.Errorf("GetRepositoryByID(base): %v", err)
  81. }
  82. return nil
  83. }
  84. func (pr *PullRequest) GetMerger() (err error) {
  85. if !pr.HasMerged || pr.Merger != nil {
  86. return nil
  87. }
  88. pr.Merger, err = GetUserByID(pr.MergerID)
  89. if IsErrUserNotExist(err) {
  90. pr.MergerID = -1
  91. pr.Merger = NewFakeUser()
  92. } else if err != nil {
  93. return fmt.Errorf("GetUserByID: %v", err)
  94. }
  95. return nil
  96. }
  97. // IsChecking returns true if this pull request is still checking conflict.
  98. func (pr *PullRequest) IsChecking() bool {
  99. return pr.Status == PULL_REQUEST_STATUS_CHECKING
  100. }
  101. // CanAutoMerge returns true if this pull request can be merged automatically.
  102. func (pr *PullRequest) CanAutoMerge() bool {
  103. return pr.Status == PULL_REQUEST_STATUS_MERGEABLE
  104. }
  105. // Merge merges pull request to base repository.
  106. func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository) (err error) {
  107. if err = pr.GetHeadRepo(); err != nil {
  108. return fmt.Errorf("GetHeadRepo: %v", err)
  109. } else if err = pr.GetBaseRepo(); err != nil {
  110. return fmt.Errorf("GetBaseRepo: %v", err)
  111. }
  112. sess := x.NewSession()
  113. defer sessionRelease(sess)
  114. if err = sess.Begin(); err != nil {
  115. return err
  116. }
  117. if err = pr.Issue.changeStatus(sess, doer, true); err != nil {
  118. return fmt.Errorf("Issue.changeStatus: %v", err)
  119. }
  120. headRepoPath := RepoPath(pr.HeadUserName, pr.HeadRepo.Name)
  121. headGitRepo, err := git.OpenRepository(headRepoPath)
  122. if err != nil {
  123. return fmt.Errorf("OpenRepository: %v", err)
  124. }
  125. pr.MergedCommitID, err = headGitRepo.GetBranchCommitID(pr.HeadBranch)
  126. if err != nil {
  127. return fmt.Errorf("GetBranchCommitID: %v", err)
  128. }
  129. if err = mergePullRequestAction(sess, doer, pr.Issue.Repo, pr.Issue); err != nil {
  130. return fmt.Errorf("mergePullRequestAction: %v", err)
  131. }
  132. pr.HasMerged = true
  133. pr.Merged = time.Now()
  134. pr.MergerID = doer.Id
  135. if _, err = sess.Id(pr.ID).AllCols().Update(pr); err != nil {
  136. return fmt.Errorf("update pull request: %v", err)
  137. }
  138. // Clone base repo.
  139. tmpBasePath := path.Join(setting.AppDataPath, "tmp/repos", com.ToStr(time.Now().Nanosecond())+".git")
  140. os.MkdirAll(path.Dir(tmpBasePath), os.ModePerm)
  141. defer os.RemoveAll(path.Dir(tmpBasePath))
  142. var stderr string
  143. if _, stderr, err = process.ExecTimeout(5*time.Minute,
  144. fmt.Sprintf("PullRequest.Merge (git clone): %s", tmpBasePath),
  145. "git", "clone", baseGitRepo.Path, tmpBasePath); err != nil {
  146. return fmt.Errorf("git clone: %s", stderr)
  147. }
  148. // Check out base branch.
  149. if _, stderr, err = process.ExecDir(-1, tmpBasePath,
  150. fmt.Sprintf("PullRequest.Merge (git checkout): %s", tmpBasePath),
  151. "git", "checkout", pr.BaseBranch); err != nil {
  152. return fmt.Errorf("git checkout: %s", stderr)
  153. }
  154. // Add head repo remote.
  155. if _, stderr, err = process.ExecDir(-1, tmpBasePath,
  156. fmt.Sprintf("PullRequest.Merge (git remote add): %s", tmpBasePath),
  157. "git", "remote", "add", "head_repo", headRepoPath); err != nil {
  158. return fmt.Errorf("git remote add [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
  159. }
  160. // Merge commits.
  161. if _, stderr, err = process.ExecDir(-1, tmpBasePath,
  162. fmt.Sprintf("PullRequest.Merge (git fetch): %s", tmpBasePath),
  163. "git", "fetch", "head_repo"); err != nil {
  164. return fmt.Errorf("git fetch [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
  165. }
  166. if _, stderr, err = process.ExecDir(-1, tmpBasePath,
  167. fmt.Sprintf("PullRequest.Merge (git merge --no-ff --no-commit): %s", tmpBasePath),
  168. "git", "merge", "--no-ff", "--no-commit", "head_repo/"+pr.HeadBranch); err != nil {
  169. return fmt.Errorf("git merge --no-ff --no-commit [%s]: %v - %s", tmpBasePath, err, stderr)
  170. }
  171. sig := doer.NewGitSig()
  172. if _, stderr, err = process.ExecDir(-1, tmpBasePath,
  173. fmt.Sprintf("PullRequest.Merge (git merge): %s", tmpBasePath),
  174. "git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email),
  175. "-m", fmt.Sprintf("Merge branch '%s' of %s/%s into %s", pr.HeadBranch, pr.HeadUserName, pr.HeadRepo.Name, pr.BaseBranch)); err != nil {
  176. return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, stderr)
  177. }
  178. // Push back to upstream.
  179. if _, stderr, err = process.ExecDir(-1, tmpBasePath,
  180. fmt.Sprintf("PullRequest.Merge (git push): %s", tmpBasePath),
  181. "git", "push", baseGitRepo.Path, pr.BaseBranch); err != nil {
  182. return fmt.Errorf("git push: %s", stderr)
  183. }
  184. if err = sess.Commit(); err != nil {
  185. return fmt.Errorf("Commit: %v", err)
  186. }
  187. // Compose commit repository action
  188. l, err := headGitRepo.CommitsBetweenIDs(pr.MergedCommitID, pr.MergeBase)
  189. if err != nil {
  190. return fmt.Errorf("CommitsBetween: %v", err)
  191. }
  192. p := &api.PushPayload{
  193. Ref: "refs/heads/" + pr.BaseBranch,
  194. Before: pr.MergeBase,
  195. After: pr.MergedCommitID,
  196. CompareUrl: setting.AppUrl + pr.BaseRepo.ComposeCompareURL(pr.MergeBase, pr.MergedCommitID),
  197. Commits: ListToPushCommits(l).ToApiPayloadCommits(pr.BaseRepo.FullRepoLink()),
  198. Repo: pr.BaseRepo.ComposePayload(),
  199. Pusher: &api.PayloadAuthor{
  200. Name: pr.HeadRepo.MustOwner().DisplayName(),
  201. Email: pr.HeadRepo.MustOwner().Email,
  202. UserName: pr.HeadRepo.MustOwner().Name,
  203. },
  204. Sender: &api.PayloadUser{
  205. UserName: doer.Name,
  206. ID: doer.Id,
  207. AvatarUrl: setting.AppUrl + doer.RelAvatarLink(),
  208. },
  209. }
  210. if err = PrepareWebhooks(pr.BaseRepo, HOOK_EVENT_PUSH, p); err != nil {
  211. return fmt.Errorf("PrepareWebhooks: %v", err)
  212. }
  213. go HookQueue.Add(pr.BaseRepo.ID)
  214. return nil
  215. }
  216. // patchConflicts is a list of conflit description from Git.
  217. var patchConflicts = []string{
  218. "patch does not apply",
  219. "already exists in working directory",
  220. "unrecognized input",
  221. }
  222. // testPatch checks if patch can be merged to base repository without conflit.
  223. // FIXME: make a mechanism to clean up stable local copies.
  224. func (pr *PullRequest) testPatch() (err error) {
  225. if pr.BaseRepo == nil {
  226. pr.BaseRepo, err = GetRepositoryByID(pr.BaseRepoID)
  227. if err != nil {
  228. return fmt.Errorf("GetRepositoryByID: %v", err)
  229. }
  230. }
  231. patchPath, err := pr.BaseRepo.PatchPath(pr.Index)
  232. if err != nil {
  233. return fmt.Errorf("BaseRepo.PatchPath: %v", err)
  234. }
  235. // Fast fail if patch does not exist, this assumes data is cruppted.
  236. if !com.IsFile(patchPath) {
  237. log.Trace("PullRequest[%d].testPatch: ignored cruppted data", pr.ID)
  238. return nil
  239. }
  240. log.Trace("PullRequest[%d].testPatch(patchPath): %s", pr.ID, patchPath)
  241. if err := pr.BaseRepo.UpdateLocalCopy(); err != nil {
  242. return fmt.Errorf("UpdateLocalCopy: %v", err)
  243. }
  244. // Checkout base branch.
  245. _, stderr, err := process.ExecDir(-1, pr.BaseRepo.LocalCopyPath(),
  246. fmt.Sprintf("PullRequest.Merge(git checkout): %v", pr.BaseRepo.ID),
  247. "git", "checkout", pr.BaseBranch)
  248. if err != nil {
  249. return fmt.Errorf("git checkout: %s", stderr)
  250. }
  251. pr.Status = PULL_REQUEST_STATUS_CHECKING
  252. _, stderr, err = process.ExecDir(-1, pr.BaseRepo.LocalCopyPath(),
  253. fmt.Sprintf("testPatch(git apply --check): %d", pr.BaseRepo.ID),
  254. "git", "apply", "--check", patchPath)
  255. if err != nil {
  256. for i := range patchConflicts {
  257. if strings.Contains(stderr, patchConflicts[i]) {
  258. log.Trace("PullRequest[%d].testPatch(apply): has conflit", pr.ID)
  259. fmt.Println(stderr)
  260. pr.Status = PULL_REQUEST_STATUS_CONFLICT
  261. return nil
  262. }
  263. }
  264. return fmt.Errorf("git apply --check: %v - %s", err, stderr)
  265. }
  266. return nil
  267. }
  268. // NewPullRequest creates new pull request with labels for repository.
  269. func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest, patch []byte) (err error) {
  270. sess := x.NewSession()
  271. defer sessionRelease(sess)
  272. if err = sess.Begin(); err != nil {
  273. return err
  274. }
  275. if err = newIssue(sess, repo, pull, labelIDs, uuids, true); err != nil {
  276. return fmt.Errorf("newIssue: %v", err)
  277. }
  278. // Notify watchers.
  279. act := &Action{
  280. ActUserID: pull.Poster.Id,
  281. ActUserName: pull.Poster.Name,
  282. ActEmail: pull.Poster.Email,
  283. OpType: CREATE_PULL_REQUEST,
  284. Content: fmt.Sprintf("%d|%s", pull.Index, pull.Name),
  285. RepoID: repo.ID,
  286. RepoUserName: repo.Owner.Name,
  287. RepoName: repo.Name,
  288. IsPrivate: repo.IsPrivate,
  289. }
  290. if err = notifyWatchers(sess, act); err != nil {
  291. return err
  292. }
  293. pr.Index = pull.Index
  294. if err = repo.SavePatch(pr.Index, patch); err != nil {
  295. return fmt.Errorf("SavePatch: %v", err)
  296. }
  297. pr.BaseRepo = repo
  298. if err = pr.testPatch(); err != nil {
  299. return fmt.Errorf("testPatch: %v", err)
  300. }
  301. if pr.Status == PULL_REQUEST_STATUS_CHECKING {
  302. pr.Status = PULL_REQUEST_STATUS_MERGEABLE
  303. }
  304. pr.IssueID = pull.ID
  305. if _, err = sess.Insert(pr); err != nil {
  306. return fmt.Errorf("insert pull repo: %v", err)
  307. }
  308. return sess.Commit()
  309. }
  310. // GetUnmergedPullRequest returnss a pull request that is open and has not been merged
  311. // by given head/base and repo/branch.
  312. func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch string) (*PullRequest, error) {
  313. pr := new(PullRequest)
  314. has, err := x.Where("head_repo_id=? AND head_branch=? AND base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
  315. headRepoID, headBranch, baseRepoID, baseBranch, false, false).
  316. Join("INNER", "issue", "issue.id=pull_request.issue_id").Get(pr)
  317. if err != nil {
  318. return nil, err
  319. } else if !has {
  320. return nil, ErrPullRequestNotExist{0, 0, headRepoID, baseRepoID, headBranch, baseBranch}
  321. }
  322. return pr, nil
  323. }
  324. // GetUnmergedPullRequestsByHeadInfo returnss all pull requests that are open and has not been merged
  325. // by given head information (repo and branch).
  326. func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequest, error) {
  327. prs := make([]*PullRequest, 0, 2)
  328. return prs, x.Where("head_repo_id=? AND head_branch=? AND has_merged=? AND issue.is_closed=?",
  329. repoID, branch, false, false).
  330. Join("INNER", "issue", "issue.id=pull_request.issue_id").Find(&prs)
  331. }
  332. // GetUnmergedPullRequestsByBaseInfo returnss all pull requests that are open and has not been merged
  333. // by given base information (repo and branch).
  334. func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) {
  335. prs := make([]*PullRequest, 0, 2)
  336. return prs, x.Where("base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
  337. repoID, branch, false, false).
  338. Join("INNER", "issue", "issue.id=pull_request.issue_id").Find(&prs)
  339. }
  340. // Gets a Pull Request by the path of the forked repo and the branch from where the PR
  341. // got submitted.
  342. func GetUnmergedPullRequestByRepoPathAndHeadBranch(user, repo, branch string) (*PullRequest, error) {
  343. userLower := strings.ToLower(user)
  344. repoLower := strings.ToLower(repo)
  345. pr := new(PullRequest)
  346. if x == nil {
  347. return nil, errors.New("Fail")
  348. }
  349. has, err := x.
  350. Where("head_user_name=? AND head_branch=? AND has_merged=? AND issue.is_closed=? AND repository.lower_name=?", userLower, branch, 0, 0, repoLower).
  351. Join("INNER", "repository", "repository.id=pull_request.head_repo_id").
  352. Join("INNER", "issue", "issue.id=pull_request.issue_id").
  353. Get(pr)
  354. if err != nil {
  355. return nil, err
  356. } else if !has {
  357. return nil, ErrPullRequestNotExist{0, 0, 0, 0, branch, ""}
  358. }
  359. baseRepo := new(Repository)
  360. has, err = x.Where("repository.id=?", pr.BaseRepoID).
  361. Join("LEFT", "user", "user.id=repository.owner_id").
  362. Get(baseRepo)
  363. if err != nil {
  364. return nil, err
  365. } else if !has {
  366. return nil, ErrRepoNotExist{pr.BaseRepoID, 0, ""}
  367. }
  368. pr.BaseRepo = baseRepo
  369. return pr, nil
  370. }
  371. // GetPullRequestByID returns a pull request by given ID.
  372. func GetPullRequestByID(id int64) (*PullRequest, error) {
  373. pr := new(PullRequest)
  374. has, err := x.Id(id).Get(pr)
  375. if err != nil {
  376. return nil, err
  377. } else if !has {
  378. return nil, ErrPullRequestNotExist{id, 0, 0, 0, "", ""}
  379. }
  380. return pr, nil
  381. }
  382. // GetPullRequestByIssueID returns pull request by given issue ID.
  383. func GetPullRequestByIssueID(issueID int64) (*PullRequest, error) {
  384. pr := &PullRequest{
  385. IssueID: issueID,
  386. }
  387. has, err := x.Get(pr)
  388. if err != nil {
  389. return nil, err
  390. } else if !has {
  391. return nil, ErrPullRequestNotExist{0, issueID, 0, 0, "", ""}
  392. }
  393. return pr, nil
  394. }
  395. // Update updates all fields of pull request.
  396. func (pr *PullRequest) Update() error {
  397. _, err := x.Id(pr.ID).AllCols().Update(pr)
  398. return err
  399. }
  400. // Update updates specific fields of pull request.
  401. func (pr *PullRequest) UpdateCols(cols ...string) error {
  402. _, err := x.Id(pr.ID).Cols(cols...).Update(pr)
  403. return err
  404. }
  405. var PullRequestQueue = NewUniqueQueue(setting.Repository.PullRequestQueueLength)
  406. // UpdatePatch generates and saves a new patch.
  407. func (pr *PullRequest) UpdatePatch() (err error) {
  408. if err = pr.GetHeadRepo(); err != nil {
  409. return fmt.Errorf("GetHeadRepo: %v", err)
  410. } else if pr.HeadRepo == nil {
  411. log.Trace("PullRequest[%d].UpdatePatch: ignored cruppted data", pr.ID)
  412. return nil
  413. }
  414. if err = pr.GetBaseRepo(); err != nil {
  415. return fmt.Errorf("GetBaseRepo: %v", err)
  416. }
  417. headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
  418. if err != nil {
  419. return fmt.Errorf("OpenRepository: %v", err)
  420. }
  421. // Add a temporary remote.
  422. tmpRemote := com.ToStr(time.Now().UnixNano())
  423. if err = headGitRepo.AddRemote(tmpRemote, RepoPath(pr.BaseRepo.MustOwner().Name, pr.BaseRepo.Name), true); err != nil {
  424. return fmt.Errorf("AddRemote: %v", err)
  425. }
  426. defer func() {
  427. headGitRepo.RemoveRemote(tmpRemote)
  428. }()
  429. remoteBranch := "remotes/" + tmpRemote + "/" + pr.BaseBranch
  430. pr.MergeBase, err = headGitRepo.GetMergeBase(remoteBranch, pr.HeadBranch)
  431. if err != nil {
  432. return fmt.Errorf("GetMergeBase: %v", err)
  433. } else if err = pr.Update(); err != nil {
  434. return fmt.Errorf("Update: %v", err)
  435. }
  436. patch, err := headGitRepo.GetPatch(pr.MergeBase, pr.HeadBranch)
  437. if err != nil {
  438. return fmt.Errorf("GetPatch: %v", err)
  439. }
  440. if err = pr.BaseRepo.SavePatch(pr.Index, patch); err != nil {
  441. return fmt.Errorf("BaseRepo.SavePatch: %v", err)
  442. }
  443. return nil
  444. }
  445. func (pr *PullRequest) PushToBaseRepo() (err error) {
  446. log.Trace("PushToBase[%d]: pushing commits to base repo refs/pull/%d/head", pr.ID, pr.ID)
  447. branch := pr.HeadBranch
  448. if err = pr.BaseRepo.GetOwner(); err != nil {
  449. return fmt.Errorf("Could not get base repo owner data: %v", err)
  450. } else if err = pr.HeadRepo.GetOwner(); err != nil {
  451. return fmt.Errorf("Could not get head repo owner data: %v", err)
  452. }
  453. headRepoPath := RepoPath(pr.HeadRepo.Owner.Name, pr.HeadRepo.Name)
  454. prIdStr := strconv.FormatInt(pr.ID, 10)
  455. tmpRemoteName := "tmp-pull-" + branch + "-" + prIdStr
  456. repo, err := git.OpenRepository(headRepoPath)
  457. if err != nil {
  458. return fmt.Errorf("Unable to open head repository: %v", err)
  459. }
  460. if err = repo.AddRemote(tmpRemoteName, RepoPath(pr.BaseRepo.Owner.Name, pr.BaseRepo.Name), false); err != nil {
  461. return fmt.Errorf("Unable to add remote to head repository: %v", err)
  462. }
  463. // Make sure to remove the remote even if the push fails
  464. defer repo.RemoveRemote(tmpRemoteName)
  465. pushRef := branch+":"+"refs/pull/"+prIdStr+"/head"
  466. if err = git.Push(headRepoPath, tmpRemoteName, pushRef); err != nil {
  467. return fmt.Errorf("Error pushing: %v", err)
  468. }
  469. return nil
  470. }
  471. // AddToTaskQueue adds itself to pull request test task queue.
  472. func (pr *PullRequest) AddToTaskQueue() {
  473. go PullRequestQueue.AddFunc(pr.ID, func() {
  474. pr.Status = PULL_REQUEST_STATUS_CHECKING
  475. if err := pr.UpdateCols("status"); err != nil {
  476. log.Error(5, "AddToTaskQueue.UpdateCols[%d].(add to queue): %v", pr.ID, err)
  477. }
  478. })
  479. }
  480. func addHeadRepoTasks(prs []*PullRequest) {
  481. for _, pr := range prs {
  482. log.Trace("addHeadRepoTasks[%d]: composing new test task", pr.ID)
  483. if err := pr.UpdatePatch(); err != nil {
  484. log.Error(4, "UpdatePatch: %v", err)
  485. continue
  486. } else if err := pr.PushToBaseRepo(); err != nil {
  487. log.Error(4, "PushToBaseRepo: %v", err)
  488. continue
  489. }
  490. pr.AddToTaskQueue()
  491. }
  492. }
  493. // AddTestPullRequestTask adds new test tasks by given head/base repository and head/base branch,
  494. // and generate new patch for testing as needed.
  495. func AddTestPullRequestTask(repoID int64, branch string) {
  496. log.Trace("AddTestPullRequestTask[head_repo_id: %d, head_branch: %s]: finding pull requests", repoID, branch)
  497. prs, err := GetUnmergedPullRequestsByHeadInfo(repoID, branch)
  498. if err != nil {
  499. log.Error(4, "Find pull requests[head_repo_id: %d, head_branch: %s]: %v", repoID, branch, err)
  500. return
  501. }
  502. addHeadRepoTasks(prs)
  503. log.Trace("AddTestPullRequestTask[base_repo_id: %d, base_branch: %s]: finding pull requests", repoID, branch)
  504. prs, err = GetUnmergedPullRequestsByBaseInfo(repoID, branch)
  505. if err != nil {
  506. log.Error(4, "Find pull requests[base_repo_id: %d, base_branch: %s]: %v", repoID, branch, err)
  507. return
  508. }
  509. for _, pr := range prs {
  510. pr.AddToTaskQueue()
  511. }
  512. }
  513. func ChangeUsernameInPullRequests(oldUserName, newUserName string) error {
  514. pr := PullRequest{
  515. HeadUserName: strings.ToLower(newUserName),
  516. }
  517. _, err := x.Cols("head_user_name").Where("head_user_name = ?", strings.ToLower(oldUserName)).Update(pr)
  518. return err
  519. }
  520. // checkAndUpdateStatus checks if pull request is possible to levaing checking status,
  521. // and set to be either conflict or mergeable.
  522. func (pr *PullRequest) checkAndUpdateStatus() {
  523. // Status is not changed to conflict means mergeable.
  524. if pr.Status == PULL_REQUEST_STATUS_CHECKING {
  525. pr.Status = PULL_REQUEST_STATUS_MERGEABLE
  526. }
  527. // Make sure there is no waiting test to process before levaing the checking status.
  528. if !PullRequestQueue.Exist(pr.ID) {
  529. if err := pr.UpdateCols("status"); err != nil {
  530. log.Error(4, "Update[%d]: %v", pr.ID, err)
  531. }
  532. }
  533. }
  534. // TestPullRequests checks and tests untested patches of pull requests.
  535. // TODO: test more pull requests at same time.
  536. func TestPullRequests() {
  537. prs := make([]*PullRequest, 0, 10)
  538. x.Iterate(PullRequest{
  539. Status: PULL_REQUEST_STATUS_CHECKING,
  540. },
  541. func(idx int, bean interface{}) error {
  542. pr := bean.(*PullRequest)
  543. if err := pr.GetBaseRepo(); err != nil {
  544. log.Error(3, "GetBaseRepo: %v", err)
  545. return nil
  546. }
  547. if err := pr.testPatch(); err != nil {
  548. log.Error(3, "testPatch: %v", err)
  549. return nil
  550. }
  551. prs = append(prs, pr)
  552. return nil
  553. })
  554. // Update pull request status.
  555. for _, pr := range prs {
  556. pr.checkAndUpdateStatus()
  557. }
  558. // Start listening on new test requests.
  559. for prID := range PullRequestQueue.Queue() {
  560. log.Trace("TestPullRequests[%v]: processing test task", prID)
  561. PullRequestQueue.Remove(prID)
  562. pr, err := GetPullRequestByID(com.StrTo(prID).MustInt64())
  563. if err != nil {
  564. log.Error(4, "GetPullRequestByID[%d]: %v", prID, err)
  565. continue
  566. } else if err = pr.testPatch(); err != nil {
  567. log.Error(4, "testPatch[%d]: %v", pr.ID, err)
  568. continue
  569. }
  570. pr.checkAndUpdateStatus()
  571. }
  572. }
  573. func InitTestPullRequests() {
  574. go TestPullRequests()
  575. }