From efa9a8a6e308489cf5b5e0174007d78390c5f0e6 Mon Sep 17 00:00:00 2001 From: Cirno the Strongest <1447794+CirnoT@users.noreply.github.com> Date: Fri, 18 Dec 2020 13:37:55 +0100 Subject: [PATCH] Show status check for merged PRs (#13975) * Show status check for merged PRs * Handle PRs with no commits * Styling Co-authored-by: Lauris BH Co-authored-by: techknowlogick Co-authored-by: 6543 <6543@obermui.de> --- routers/repo/pull.go | 14 +++++++++++++ services/pull/pull.go | 21 +++++++++----------- templates/repo/pulls/status.tmpl | 34 +++++++++++++++++--------------- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/routers/repo/pull.go b/routers/repo/pull.go index bee3bbc1d..901a66863 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -325,6 +325,20 @@ func PrepareMergedViewPullInfo(ctx *context.Context, issue *models.Issue) *git.C } ctx.Data["NumCommits"] = compareInfo.Commits.Len() ctx.Data["NumFiles"] = compareInfo.NumFiles + + if compareInfo.Commits.Len() != 0 { + sha := compareInfo.Commits.Front().Value.(*git.Commit).ID.String() + commitStatuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, sha, models.ListOptions{}) + if err != nil { + ctx.ServerError("GetLatestCommitStatus", err) + return nil + } + if len(commitStatuses) != 0 { + ctx.Data["LatestCommitStatuses"] = commitStatuses + ctx.Data["LatestCommitStatus"] = models.CalcCommitStatus(commitStatuses) + } + } + return compareInfo } diff --git a/services/pull/pull.go b/services/pull/pull.go index f3fb76230..23a4bf977 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -9,6 +9,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "strings" "time" @@ -641,31 +642,27 @@ func GetCommitMessages(pr *models.PullRequest) string { // GetLastCommitStatus returns the last commit status for this pull request. func GetLastCommitStatus(pr *models.PullRequest) (status *models.CommitStatus, err error) { - if err = pr.LoadHeadRepo(); err != nil { + if err = pr.LoadBaseRepo(); err != nil { return nil, err } - if pr.HeadRepo == nil { - return nil, models.ErrPullRequestHeadRepoMissing{ID: pr.ID, HeadRepoID: pr.HeadRepoID} - } - - headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath()) + gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath()) if err != nil { return nil, err } - defer headGitRepo.Close() + defer gitRepo.Close() - lastCommitID, err := headGitRepo.GetBranchCommitID(pr.HeadBranch) + compareInfo, err := gitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.MergeBase, pr.GetGitRefName()) if err != nil { return nil, err } - err = pr.LoadBaseRepo() - if err != nil { - return nil, err + if compareInfo.Commits.Len() == 0 { + return nil, errors.New("pull request has no commits") } - statusList, err := models.GetLatestCommitStatus(pr.BaseRepo.ID, lastCommitID, models.ListOptions{}) + sha := compareInfo.Commits.Front().Value.(*git.Commit).ID.String() + statusList, err := models.GetLatestCommitStatus(pr.BaseRepo.ID, sha, models.ListOptions{}) if err != nil { return nil, err } diff --git a/templates/repo/pulls/status.tmpl b/templates/repo/pulls/status.tmpl index 2847903a5..e48f5f3fe 100644 --- a/templates/repo/pulls/status.tmpl +++ b/templates/repo/pulls/status.tmpl @@ -1,19 +1,21 @@ {{if $.LatestCommitStatus}} -
- {{if eq .LatestCommitStatus.State "pending"}} - {{$.i18n.Tr "repo.pulls.status_checking"}} - {{else if eq .LatestCommitStatus.State "success"}} - {{$.i18n.Tr "repo.pulls.status_checks_success"}} - {{else if eq .LatestCommitStatus.State "warning"}} - {{$.i18n.Tr "repo.pulls.status_checks_warning"}} - {{else if eq .LatestCommitStatus.State "failure"}} - {{$.i18n.Tr "repo.pulls.status_checks_failure"}} - {{else if eq .LatestCommitStatus.State "error"}} - {{$.i18n.Tr "repo.pulls.status_checks_error"}} - {{else}} - {{$.i18n.Tr "repo.pulls.status_checking"}} - {{end}} -
+ {{if not $.Issue.PullRequest.HasMerged}} +
+ {{if eq .LatestCommitStatus.State "pending"}} + {{$.i18n.Tr "repo.pulls.status_checking"}} + {{else if eq .LatestCommitStatus.State "success"}} + {{$.i18n.Tr "repo.pulls.status_checks_success"}} + {{else if eq .LatestCommitStatus.State "warning"}} + {{$.i18n.Tr "repo.pulls.status_checks_warning"}} + {{else if eq .LatestCommitStatus.State "failure"}} + {{$.i18n.Tr "repo.pulls.status_checks_failure"}} + {{else if eq .LatestCommitStatus.State "error"}} + {{$.i18n.Tr "repo.pulls.status_checks_error"}} + {{else}} + {{$.i18n.Tr "repo.pulls.status_checking"}} + {{end}} +
+ {{end}} {{range $.LatestCommitStatuses}}
@@ -21,7 +23,7 @@ {{.Context}} {{.Description}}
{{if $.is_context_required}} - {{if (call $.is_context_required .Context)}}
{{$.i18n.Tr "repo.pulls.status_checks_requested"}}
{{end}} + {{if (call $.is_context_required .Context)}}
{{$.i18n.Tr "repo.pulls.status_checks_requested"}}
{{end}} {{end}} {{if .TargetURL}}{{$.i18n.Tr "repo.pulls.status_checks_details"}}{{end}}