Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Export registered check names #518

Merged
merged 1 commit into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions checks/active.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,32 @@ import (
)

const (
activeStr = "Active"
// CheckActive is the registered name for IsActive.
CheckActive = "Active"
lookbackDays = 90
)

//nolint:gochecknoinits
func init() {
registerCheck(activeStr, IsActive)
registerCheck(CheckActive, IsActive)
}

func IsActive(c *checker.CheckRequest) checker.CheckResult {
commits, _, err := c.Client.Repositories.ListCommits(c.Ctx, c.Owner, c.Repo, &github.CommitsListOptions{})
if err != nil {
return checker.MakeRetryResult(activeStr, err)
return checker.MakeRetryResult(CheckActive, err)
}

tz, err := time.LoadLocation("UTC")
if err != nil {
return checker.MakeRetryResult(activeStr, err)
return checker.MakeRetryResult(CheckActive, err)
}
threshold := time.Now().In(tz).AddDate(0, 0, -1*lookbackDays)
totalCommits := 0
for _, commit := range commits {
commitFull, _, err := c.Client.Git.GetCommit(c.Ctx, c.Owner, c.Repo, commit.GetSHA())
if err != nil {
return checker.MakeRetryResult(activeStr, err)
return checker.MakeRetryResult(CheckActive, err)
}
if commitFull.GetAuthor().GetDate().After(threshold) {
totalCommits++
Expand All @@ -57,7 +58,7 @@ func IsActive(c *checker.CheckRequest) checker.CheckResult {
const numCommits = 2
const confidence = 10
return checker.CheckResult{
Name: activeStr,
Name: CheckActive,
Pass: totalCommits >= numCommits,
Confidence: confidence,
}
Expand Down
15 changes: 8 additions & 7 deletions checks/branch_protected.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,34 @@ import (
)

const (
branchProtectionStr = "Branch-Protection"
minReviews = 1
// CheckBranchProtection is the registered name for BranchProtection.
CheckBranchProtection = "Branch-Protection"
minReviews = 1
)

//nolint:gochecknoinits
func init() {
registerCheck(branchProtectionStr, BranchProtection)
registerCheck(CheckBranchProtection, BranchProtection)
}

func BranchProtection(c *checker.CheckRequest) checker.CheckResult {
repo, _, err := c.Client.Repositories.Get(c.Ctx, c.Owner, c.Repo)
if err != nil {
return checker.MakeRetryResult(branchProtectionStr, err)
return checker.MakeRetryResult(CheckBranchProtection, err)
}

protection, resp, err := c.Client.Repositories.
GetBranchProtection(c.Ctx, c.Owner, c.Repo, *repo.DefaultBranch)
const fileNotFound = 404
if resp.StatusCode == fileNotFound {
return checker.MakeRetryResult(branchProtectionStr, err)
return checker.MakeRetryResult(CheckBranchProtection, err)
}

if err != nil {
c.Logf("!! branch protection not enabled")
const confidence = 10
return checker.CheckResult{
Name: branchProtectionStr,
Name: CheckBranchProtection,
Pass: false,
Confidence: confidence,
}
Expand Down Expand Up @@ -99,7 +100,7 @@ func IsBranchProtected(protection *github.Protection, c *checker.CheckRequest) c
totalSuccess++
}

return checker.MakeProportionalResult(branchProtectionStr, totalSuccess, totalChecks, 1.0)
return checker.MakeProportionalResult(CheckBranchProtection, totalSuccess, totalChecks, 1.0)
}

// Returns true if several PR status checks requirements are enabled. Otherwise returns false and logs why it failed.
Expand Down
18 changes: 9 additions & 9 deletions checks/branch_protected_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestIsBranchProtected(t *testing.T) {
c: checker.CheckRequest{Logf: l.Logf},
},
want: checker.CheckResult{
Name: branchProtectionStr,
Name: CheckBranchProtection,
Pass: false,
Details: nil,
Confidence: 7,
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestIsBranchProtected(t *testing.T) {
c: checker.CheckRequest{Logf: l.Logf},
},
want: checker.CheckResult{
Name: branchProtectionStr,
Name: CheckBranchProtection,
Pass: false,
Details: nil,
Confidence: 5,
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestIsBranchProtected(t *testing.T) {
c: checker.CheckRequest{Logf: l.Logf},
},
want: checker.CheckResult{
Name: branchProtectionStr,
Name: CheckBranchProtection,
Pass: false,
Details: nil,
Confidence: 7,
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestIsBranchProtected(t *testing.T) {
c: checker.CheckRequest{Logf: l.Logf},
},
want: checker.CheckResult{
Name: branchProtectionStr,
Name: CheckBranchProtection,
Pass: false,
Details: nil,
Confidence: 5,
Expand Down Expand Up @@ -274,7 +274,7 @@ func TestIsBranchProtected(t *testing.T) {
c: checker.CheckRequest{Logf: l.Logf},
},
want: checker.CheckResult{
Name: branchProtectionStr,
Name: CheckBranchProtection,
Pass: false,
Details: nil,
Confidence: 5,
Expand Down Expand Up @@ -321,7 +321,7 @@ func TestIsBranchProtected(t *testing.T) {
c: checker.CheckRequest{Logf: l.Logf},
},
want: checker.CheckResult{
Name: branchProtectionStr,
Name: CheckBranchProtection,
Pass: false,
Details: nil,
Confidence: 5,
Expand Down Expand Up @@ -368,7 +368,7 @@ func TestIsBranchProtected(t *testing.T) {
c: checker.CheckRequest{Logf: l.Logf},
},
want: checker.CheckResult{
Name: branchProtectionStr,
Name: CheckBranchProtection,
Pass: false,
Details: nil,
Confidence: 9,
Expand Down Expand Up @@ -415,7 +415,7 @@ func TestIsBranchProtected(t *testing.T) {
c: checker.CheckRequest{Logf: l.Logf},
},
want: checker.CheckResult{
Name: branchProtectionStr,
Name: CheckBranchProtection,
Pass: false,
Details: nil,
Confidence: 9,
Expand Down Expand Up @@ -462,7 +462,7 @@ func TestIsBranchProtected(t *testing.T) {
c: checker.CheckRequest{Logf: l.Logf},
},
want: checker.CheckResult{
Name: branchProtectionStr,
Name: CheckBranchProtection,
Pass: true,
Details: nil,
Confidence: 10,
Expand Down
20 changes: 9 additions & 11 deletions checks/ci_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,29 @@ import (
"github.com/ossf/scorecard/checker"
)

const (
ciTestsStr = "CI-Tests"
success = "success"
)

// States for which CI system is in use.
type ciSystemState int

const (
unknown ciSystemState = iota
// CheckCITests is the registered name for CITests.
CheckCITests = "CI-Tests"
success = "success"
unknown ciSystemState = iota
githubStatuses
githubCheckRuns
)

//nolint:gochecknoinits
func init() {
registerCheck(ciTestsStr, CITests)
registerCheck(CheckCITests, CITests)
}

func CITests(c *checker.CheckRequest) checker.CheckResult {
prs, _, err := c.Client.PullRequests.List(c.Ctx, c.Owner, c.Repo, &github.PullRequestListOptions{
State: "closed",
})
if err != nil {
return checker.MakeRetryResult(ciTestsStr, err)
return checker.MakeRetryResult(CheckCITests, err)
}

usedSystem := unknown
Expand All @@ -65,7 +63,7 @@ func CITests(c *checker.CheckRequest) checker.CheckResult {
if usedSystem != githubCheckRuns {
prSuccessStatus, err := prHasSuccessStatus(pr, c)
if err != nil {
return checker.MakeRetryResult(ciTestsStr, err)
return checker.MakeRetryResult(CheckCITests, err)
}
if prSuccessStatus {
totalTested++
Expand All @@ -79,7 +77,7 @@ func CITests(c *checker.CheckRequest) checker.CheckResult {
if usedSystem != githubStatuses {
prCheckSuccessful, err := prHasSuccessfulCheck(pr, c)
if err != nil {
return checker.MakeRetryResult(ciTestsStr, err)
return checker.MakeRetryResult(CheckCITests, err)
}
if prCheckSuccessful {
totalTested++
Expand All @@ -94,7 +92,7 @@ func CITests(c *checker.CheckRequest) checker.CheckResult {
}

c.Logf("found CI tests for %d of %d merged PRs", totalTested, totalMerged)
return checker.MakeProportionalResult(ciTestsStr, totalTested, totalMerged, .75)
return checker.MakeProportionalResult(CheckCITests, totalTested, totalMerged, .75)
}

// PR has a status marked 'success' and a CI-related context.
Expand Down
19 changes: 10 additions & 9 deletions checks/cii_best_practices.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ import (
"github.com/ossf/scorecard/checker"
)

const ciiBestPracticesStr = "CII-Best-Practices"
// CheckCIIBestPractices is the registered name for CIIBestPractices.
const CheckCIIBestPractices = "CII-Best-Practices"

//nolint:gochecknoinits
func init() {
registerCheck(ciiBestPracticesStr, CIIBestPractices)
registerCheck(CheckCIIBestPractices, CIIBestPractices)
}

type response struct {
Expand All @@ -39,28 +40,28 @@ func CIIBestPractices(c *checker.CheckRequest) checker.CheckResult {
url := fmt.Sprintf("https://bestpractices.coreinfrastructure.org/projects.json?url=%s", repoURL)
req, err := http.NewRequestWithContext(c.Ctx, "GET", url, nil)
if err != nil {
return checker.MakeRetryResult(ciiBestPracticesStr, err)
return checker.MakeRetryResult(CheckCIIBestPractices, err)
}
resp, err := c.HTTPClient.Do(req)
if err != nil {
return checker.MakeRetryResult(ciiBestPracticesStr, err)
return checker.MakeRetryResult(CheckCIIBestPractices, err)
}
defer resp.Body.Close()

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return checker.MakeRetryResult(ciiBestPracticesStr, err)
return checker.MakeRetryResult(CheckCIIBestPractices, err)
}

parsedResponse := []response{}
if err := json.Unmarshal(b, &parsedResponse); err != nil {
return checker.MakeRetryResult(ciiBestPracticesStr, err)
return checker.MakeRetryResult(CheckCIIBestPractices, err)
}

if len(parsedResponse) < 1 {
c.Logf("no badge found")
return checker.CheckResult{
Name: ciiBestPracticesStr,
Name: CheckCIIBestPractices,
Pass: false,
Confidence: checker.MaxResultConfidence,
}
Expand All @@ -71,14 +72,14 @@ func CIIBestPractices(c *checker.CheckRequest) checker.CheckResult {

if result.BadgeLevel != "" {
return checker.CheckResult{
Name: ciiBestPracticesStr,
Name: CheckCIIBestPractices,
Pass: true,
Confidence: checker.MaxResultConfidence,
}
}

return checker.CheckResult{
Name: ciiBestPracticesStr,
Name: CheckCIIBestPractices,
Pass: false,
Confidence: checker.MaxResultConfidence,
}
Expand Down
Loading