|
8 | 8 | "bufio"
|
9 | 9 | "bytes"
|
10 | 10 | "context"
|
11 |
| - "errors" |
12 | 11 | "fmt"
|
13 | 12 | "strings"
|
14 | 13 | "time"
|
@@ -643,33 +642,74 @@ func GetSquashMergeCommitMessages(pr *models.PullRequest) string {
|
643 | 642 | return stringBuilder.String()
|
644 | 643 | }
|
645 | 644 |
|
| 645 | +// GetIssuesLastCommitStatus returns a map |
| 646 | +func GetIssuesLastCommitStatus(issues models.IssueList) (map[int64]*models.CommitStatus, error) { |
| 647 | + if err := issues.LoadPullRequests(); err != nil { |
| 648 | + return nil, err |
| 649 | + } |
| 650 | + if _, err := issues.LoadRepositories(); err != nil { |
| 651 | + return nil, err |
| 652 | + } |
| 653 | + |
| 654 | + var ( |
| 655 | + gitRepos = make(map[int64]*git.Repository) |
| 656 | + res = make(map[int64]*models.CommitStatus) |
| 657 | + err error |
| 658 | + ) |
| 659 | + defer func() { |
| 660 | + for _, gitRepo := range gitRepos { |
| 661 | + gitRepo.Close() |
| 662 | + } |
| 663 | + }() |
| 664 | + |
| 665 | + for _, issue := range issues { |
| 666 | + if !issue.IsPull { |
| 667 | + continue |
| 668 | + } |
| 669 | + gitRepo, ok := gitRepos[issue.RepoID] |
| 670 | + if !ok { |
| 671 | + gitRepo, err = git.OpenRepository(issue.Repo.RepoPath()) |
| 672 | + if err != nil { |
| 673 | + return nil, err |
| 674 | + } |
| 675 | + gitRepos[issue.RepoID] = gitRepo |
| 676 | + } |
| 677 | + |
| 678 | + status, err := getLastCommitStatus(gitRepo, issue.PullRequest) |
| 679 | + if err != nil { |
| 680 | + return nil, err |
| 681 | + } |
| 682 | + res[issue.PullRequest.ID] = status |
| 683 | + } |
| 684 | + return res, nil |
| 685 | +} |
| 686 | + |
646 | 687 | // GetLastCommitStatus returns list of commit statuses for latest commit on this pull request.
|
647 |
| -func GetLastCommitStatus(pr *models.PullRequest) (status []*models.CommitStatus, err error) { |
| 688 | +func GetLastCommitStatus(pr *models.PullRequest) (status *models.CommitStatus, err error) { |
648 | 689 | if err = pr.LoadBaseRepo(); err != nil {
|
649 | 690 | return nil, err
|
650 | 691 | }
|
651 |
| - |
652 | 692 | gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
|
653 | 693 | if err != nil {
|
654 | 694 | return nil, err
|
655 | 695 | }
|
656 | 696 | defer gitRepo.Close()
|
657 | 697 |
|
658 |
| - compareInfo, err := gitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.MergeBase, pr.GetGitRefName()) |
| 698 | + return getLastCommitStatus(gitRepo, pr) |
| 699 | +} |
| 700 | + |
| 701 | +// getLastCommitStatus get pr's last commit status. PR's last commit status is the head commit id's last commit status |
| 702 | +func getLastCommitStatus(gitRepo *git.Repository, pr *models.PullRequest) (status *models.CommitStatus, err error) { |
| 703 | + sha, err := gitRepo.GetRefCommitID(pr.GetGitRefName()) |
659 | 704 | if err != nil {
|
660 | 705 | return nil, err
|
661 | 706 | }
|
662 | 707 |
|
663 |
| - if compareInfo.Commits.Len() == 0 { |
664 |
| - return nil, errors.New("pull request has no commits") |
665 |
| - } |
666 |
| - |
667 |
| - sha := compareInfo.Commits.Front().Value.(*git.Commit).ID.String() |
668 | 708 | statusList, err := models.GetLatestCommitStatus(pr.BaseRepo.ID, sha, models.ListOptions{})
|
669 | 709 | if err != nil {
|
670 | 710 | return nil, err
|
671 | 711 | }
|
672 |
| - return statusList, nil |
| 712 | + return models.CalcCommitStatus(statusList), nil |
673 | 713 | }
|
674 | 714 |
|
675 | 715 | // IsHeadEqualWithBranch returns if the commits of branchName are available in pull request head
|
|
0 commit comments