Fix bugs in test and code review detection.

This commit is contained in:
Dan Lorenc 2020-10-12 12:18:40 -05:00
parent 812b3f46f6
commit c3ccf114bf
2 changed files with 71 additions and 11 deletions

View File

@ -120,6 +120,10 @@ func ProwCodeReview(c *checker.Checker) CheckResult {
Confidence: int(actual * 10),
}
}
if totalReviewed == 0 {
return InconclusiveResult
}
return CheckResult{
Pass: false,
Confidence: int(10 - int(actual*10)),

View File

@ -10,11 +10,11 @@ import (
func init() {
AllChecks = append(AllChecks, NamedCheck{
Name: "CI-Tests",
Fn: GithubChecks,
Fn: MultiCheck(GithubStatuses, GithubCheckRuns),
})
}
func GithubChecks(c *checker.Checker) CheckResult {
func GithubStatuses(c *checker.Checker) CheckResult {
prs, _, err := c.Client.PullRequests.List(c.Ctx, c.Owner, c.Repo, &github.PullRequestListOptions{
State: "closed",
})
@ -37,15 +37,7 @@ func GithubChecks(c *checker.Checker) CheckResult {
if status.GetState() != "success" {
continue
}
c := status.GetContext()
hadTest := false
for _, pattern := range []string{"travis-ci", "buildkite", "e2e"} {
if strings.Contains(c, pattern) {
hadTest = true
break
}
}
if hadTest {
if isTest(status.GetContext()) {
totalTested++
break
}
@ -59,6 +51,70 @@ func GithubChecks(c *checker.Checker) CheckResult {
Confidence: int(actual * 10),
}
}
if totalTested == 0 {
return InconclusiveResult
}
return CheckResult{
Pass: false,
Confidence: int(10 - int(actual*10)),
}
}
func isTest(s string) bool {
l := strings.ToLower(s)
// Add more patterns here!
for _, pattern := range []string{"travis-ci", "buildkite", "e2e", "test", "mergeable"} {
if strings.Contains(l, pattern) {
return true
}
}
return false
}
func GithubCheckRuns(c *checker.Checker) CheckResult {
prs, _, err := c.Client.PullRequests.List(c.Ctx, c.Owner, c.Repo, &github.PullRequestListOptions{
State: "closed",
})
if err != nil {
return RetryResult(err)
}
totalMerged := 0
totalTested := 0
for _, pr := range prs {
if pr.MergedAt == nil {
continue
}
totalMerged++
crs, _, err := c.Client.Checks.ListCheckRunsForRef(c.Ctx, c.Owner, c.Repo, pr.GetHead().GetSHA(), &github.ListCheckRunsOptions{})
if err != nil {
return RetryResult(err)
}
for _, cr := range crs.CheckRuns {
if cr.GetStatus() != "completed" {
continue
}
if cr.GetConclusion() != "success" {
continue
}
if isTest(cr.GetName()) {
totalTested++
break
}
}
}
// Threshold is 3/4 of merged PRs
actual := float32(totalTested) / float32(totalMerged)
if actual >= .75 {
return CheckResult{
Pass: true,
Confidence: int(actual * 10),
}
}
if totalTested == 0 {
return InconclusiveResult
}
return CheckResult{
Pass: false,
Confidence: int(10 - int(actual*10)),