Skip to content

Commit

Permalink
use helper to test for boolean values
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Korczynski <[email protected]>
  • Loading branch information
AdamKorcz committed Dec 8, 2023
1 parent bcbb385 commit dd371e3
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 182 deletions.
1 change: 0 additions & 1 deletion probes/blocksDeleteOnBranches/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func Run(raw *checker.RawResults) ([]finding.Finding, string, error) {
text = fmt.Sprintf("'allow deletion' disabled on branch '%s'", *branch.Name)
outcome = finding.OutcomePositive
default:

Check warning on line 55 in probes/blocksDeleteOnBranches/impl.go

View check run for this annotation

Codecov / codecov/patch

probes/blocksDeleteOnBranches/impl.go#L55

Added line #L55 was not covered by tests
//foo
}
f, err := finding.NewWith(fs, Probe, text, nil, outcome)
if err != nil {
Expand Down
54 changes: 18 additions & 36 deletions probes/branchProtectionAppliesToAdmins/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/finding"
"github.com/ossf/scorecard/v4/probes/internal/utils/branchprotection"
"github.com/ossf/scorecard/v4/probes/internal/utils/uerror"
)

Expand All @@ -39,43 +40,24 @@ func Run(raw *checker.RawResults) ([]finding.Finding, string, error) {

for i := range r.Branches {
branch := &r.Branches[i]
if branch.BranchProtectionRule.EnforceAdmins != nil {
switch *branch.BranchProtectionRule.EnforceAdmins {
case true:
f, err := finding.NewWith(fs, Probe,
fmt.Sprintf("settings apply to administrators on branch '%s'", *branch.Name),
nil, finding.OutcomePositive)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
case false:
f, err := finding.NewWith(fs, Probe,
fmt.Sprintf("settings do not apply to administrators on branch '%s'", *branch.Name),
nil, finding.OutcomeNegative)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
}
} else {
f, err := finding.NewWith(fs, Probe,
fmt.Sprintf("unable to retrieve whether or not settings apply to administrators on branch '%s'", *branch.Name),
nil, finding.OutcomeNotAvailable)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
nilMsg := fmt.Sprintf("unable to retrieve whether or not settings apply to administrators on branch '%s'",
*branch.Name)
trueMsg := fmt.Sprintf("settings apply to administrators on branch '%s'", *branch.Name)
falseMsg := fmt.Sprintf("'allow deletion' disabled on branch '%s'", *branch.Name)

p := branch.BranchProtectionRule.EnforceAdmins
text, outcome, err := branchprotection.GetTextOutcomeFromBool(p, nilMsg, trueMsg, falseMsg)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}

Check warning on line 52 in probes/branchProtectionAppliesToAdmins/impl.go

View check run for this annotation

Codecov / codecov/patch

probes/branchProtectionAppliesToAdmins/impl.go#L51-L52

Added lines #L51 - L52 were not covered by tests
f, err := finding.NewWith(fs, Probe, text, nil, outcome)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}

Check warning on line 56 in probes/branchProtectionAppliesToAdmins/impl.go

View check run for this annotation

Codecov / codecov/patch

probes/branchProtectionAppliesToAdmins/impl.go#L55-L56

Added lines #L55 - L56 were not covered by tests
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
}
return findings, Probe, nil
}
53 changes: 17 additions & 36 deletions probes/dismissesStaleReviews/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/finding"
"github.com/ossf/scorecard/v4/probes/internal/utils/branchprotection"
"github.com/ossf/scorecard/v4/probes/internal/utils/uerror"
)

Expand All @@ -39,43 +40,23 @@ func Run(raw *checker.RawResults) ([]finding.Finding, string, error) {

for i := range r.Branches {
branch := &r.Branches[i]
if branch.BranchProtectionRule.RequiredPullRequestReviews.DismissStaleReviews != nil {
switch *branch.BranchProtectionRule.RequiredPullRequestReviews.DismissStaleReviews {
case true:
f, err := finding.NewWith(fs, Probe,
fmt.Sprintf("stale review dismissal enabled on branch '%s'", *branch.Name),
nil, finding.OutcomePositive)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
case false:
f, err := finding.NewWith(fs, Probe,
fmt.Sprintf("stale review dismissal disabled on branch '%s'", *branch.Name),
nil, finding.OutcomeNegative)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
}
} else {
f, err := finding.NewWith(fs, Probe,
fmt.Sprintf("unable to retrieve review dismissal on branch '%s'", *branch.Name),
nil, finding.OutcomeNotAvailable)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
nilMsg := fmt.Sprintf("unable to retrieve review dismissal on branch '%s'", *branch.Name)
trueMsg := fmt.Sprintf("stale review dismissal enabled on branch '%s'", *branch.Name)
falseMsg := fmt.Sprintf("stale review dismissal disabled on branch '%s'", *branch.Name)

p := branch.BranchProtectionRule.RequiredPullRequestReviews.DismissStaleReviews
text, outcome, err := branchprotection.GetTextOutcomeFromBool(p, nilMsg, trueMsg, falseMsg)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}

Check warning on line 51 in probes/dismissesStaleReviews/impl.go

View check run for this annotation

Codecov / codecov/patch

probes/dismissesStaleReviews/impl.go#L50-L51

Added lines #L50 - L51 were not covered by tests
f, err := finding.NewWith(fs, Probe, text, nil, outcome)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}

Check warning on line 55 in probes/dismissesStaleReviews/impl.go

View check run for this annotation

Codecov / codecov/patch

probes/dismissesStaleReviews/impl.go#L54-L55

Added lines #L54 - L55 were not covered by tests
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
}
return findings, Probe, nil
}
47 changes: 47 additions & 0 deletions probes/internal/utils/branchprotection/branchProtection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2023 OpenSSF Scorecard Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package branchprotection

import (
"errors"

"github.com/ossf/scorecard/v4/finding"
)

var errWrongValue = errors.New("wrong value, should not happen")

func GetTextOutcomeFromBool(b *bool, nilMsg, trueMsg, falseMsg string) (string, finding.Outcome, error) {
switch {
case b == nil:
return nilMsg, finding.OutcomeNotAvailable, nil
case *b:
return trueMsg, finding.OutcomePositive, nil
case !*b:
return falseMsg, finding.OutcomeNegative, nil
}
return "", finding.OutcomeNotApplicable, errWrongValue

Check warning on line 34 in probes/internal/utils/branchprotection/branchProtection.go

View check run for this annotation

Codecov / codecov/patch

probes/internal/utils/branchprotection/branchProtection.go#L34

Added line #L34 was not covered by tests
}

func Uint32LargerThan0(u32 *int32, nilMsg, trueMsg, falseMsg string) (string, finding.Outcome, error) {
switch {
case u32 == nil:
return nilMsg, finding.OutcomeNotAvailable, nil
case *u32 > 0:
return trueMsg, finding.OutcomePositive, nil
case *u32 == 0:
return falseMsg, finding.OutcomeNegative, nil
}
return "", finding.OutcomeNotApplicable, errWrongValue

Check warning on line 46 in probes/internal/utils/branchprotection/branchProtection.go

View check run for this annotation

Codecov / codecov/patch

probes/internal/utils/branchprotection/branchProtection.go#L46

Added line #L46 was not covered by tests
}
53 changes: 17 additions & 36 deletions probes/requiresApproversForPullRequests/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/finding"
"github.com/ossf/scorecard/v4/probes/internal/utils/branchprotection"
"github.com/ossf/scorecard/v4/probes/internal/utils/uerror"
)

Expand All @@ -39,43 +40,23 @@ func Run(raw *checker.RawResults) ([]finding.Finding, string, error) {

for i := range r.Branches {
branch := &r.Branches[i]
//nolint:nestif
if branch.BranchProtectionRule.RequiredPullRequestReviews.RequiredApprovingReviewCount != nil {
if *branch.BranchProtectionRule.RequiredPullRequestReviews.RequiredApprovingReviewCount > 0 {
f, err := finding.NewWith(fs, Probe,
fmt.Sprintf("required approving review count on branch '%s'", *branch.Name), nil,
finding.OutcomePositive)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
} else {
f, err := finding.NewWith(fs, Probe,
fmt.Sprintf("required approving review count on branch '%s'", *branch.Name), nil,
finding.OutcomeNegative)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
}
} else {
f, err := finding.NewWith(fs, Probe,
fmt.Sprintf("could not determine whether branch '%s' has required approving review count", *branch.Name),
nil, finding.OutcomeNotAvailable)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
nilMsg := fmt.Sprintf("could not determine whether branch '%s' has required approving review count", *branch.Name)
trueMsg := fmt.Sprintf("required approving review count on branch '%s'", *branch.Name)
falseMsg := fmt.Sprintf("required approving review count on branch '%s'", *branch.Name)

p := branch.BranchProtectionRule.RequiredPullRequestReviews.RequiredApprovingReviewCount
text, outcome, err := branchprotection.Uint32LargerThan0(p, nilMsg, trueMsg, falseMsg)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}

Check warning on line 51 in probes/requiresApproversForPullRequests/impl.go

View check run for this annotation

Codecov / codecov/patch

probes/requiresApproversForPullRequests/impl.go#L50-L51

Added lines #L50 - L51 were not covered by tests
f, err := finding.NewWith(fs, Probe, text, nil, outcome)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}

Check warning on line 55 in probes/requiresApproversForPullRequests/impl.go

View check run for this annotation

Codecov / codecov/patch

probes/requiresApproversForPullRequests/impl.go#L54-L55

Added lines #L54 - L55 were not covered by tests
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
}
return findings, Probe, nil
}
54 changes: 18 additions & 36 deletions probes/requiresLastPushApproval/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/finding"
"github.com/ossf/scorecard/v4/probes/internal/utils/branchprotection"
"github.com/ossf/scorecard/v4/probes/internal/utils/uerror"
)

Expand All @@ -39,43 +40,24 @@ func Run(raw *checker.RawResults) ([]finding.Finding, string, error) {

for i := range r.Branches {
branch := &r.Branches[i]
//nolint:nestif
if branch.BranchProtectionRule.RequireLastPushApproval != nil {
if *branch.BranchProtectionRule.RequireLastPushApproval {
f, err := finding.NewWith(fs, Probe,
fmt.Sprintf("'last push approval' enabled on branch '%s'", *branch.Name),
nil, finding.OutcomePositive)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
} else {
f, err := finding.NewWith(fs, Probe,
fmt.Sprintf("'last push approval' disabled on branch '%s'", *branch.Name),
nil, finding.OutcomeNegative)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
}
} else {
f, err := finding.NewWith(fs, Probe,
fmt.Sprintf("unable to retrieve whether 'last push approval' is required to merge on branch '%s'", *branch.Name),
nil, finding.OutcomeNotAvailable)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
nilMsg := fmt.Sprintf("unable to retrieve whether 'last push approval' is required to merge on branch '%s'",
*branch.Name)
trueMsg := fmt.Sprintf("'last push approval' enabled on branch '%s'", *branch.Name)
falseMsg := fmt.Sprintf("'last push approval' disabled on branch '%s'", *branch.Name)

p := branch.BranchProtectionRule.RequireLastPushApproval
text, outcome, err := branchprotection.GetTextOutcomeFromBool(p, nilMsg, trueMsg, falseMsg)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}

Check warning on line 52 in probes/requiresLastPushApproval/impl.go

View check run for this annotation

Codecov / codecov/patch

probes/requiresLastPushApproval/impl.go#L51-L52

Added lines #L51 - L52 were not covered by tests
f, err := finding.NewWith(fs, Probe, text, nil, outcome)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}

Check warning on line 56 in probes/requiresLastPushApproval/impl.go

View check run for this annotation

Codecov / codecov/patch

probes/requiresLastPushApproval/impl.go#L55-L56

Added lines #L55 - L56 were not covered by tests
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
}
return findings, Probe, nil
}
55 changes: 18 additions & 37 deletions probes/requiresUpToDateBranches/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/finding"
"github.com/ossf/scorecard/v4/probes/internal/utils/branchprotection"
"github.com/ossf/scorecard/v4/probes/internal/utils/uerror"
)

Expand All @@ -39,44 +40,24 @@ func Run(raw *checker.RawResults) ([]finding.Finding, string, error) {

for i := range r.Branches {
branch := &r.Branches[i]
//nolint:nestif
if branch.BranchProtectionRule.CheckRules.UpToDateBeforeMerge == nil {
f, err := finding.NewWith(fs, Probe,
fmt.Sprintf("unable to retrieve whether up-to-date branches are needed to merge on branch '%s'", *branch.Name),
nil, finding.OutcomeNotAvailable)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
} else {
// Note: `This setting will not take effect unless at least one status check is enabled`.
if *branch.BranchProtectionRule.CheckRules.UpToDateBeforeMerge {
f, err := finding.NewWith(fs, Probe,
fmt.Sprintf("status checks require up-to-date branches for '%s'", *branch.Name),
nil, finding.OutcomePositive)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
} else {
f, err := finding.NewWith(fs, Probe,
fmt.Sprintf("status checks do not require up-to-date branches for '%s'", *branch.Name),
nil, finding.OutcomeNegative)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
}
nilMsg := fmt.Sprintf("unable to retrieve whether up-to-date branches are needed to merge on branch '%s'",
*branch.Name)
trueMsg := fmt.Sprintf("status checks require up-to-date branches for '%s'", *branch.Name)
falseMsg := fmt.Sprintf("status checks do not require up-to-date branches for '%s'", *branch.Name)

p := branch.BranchProtectionRule.CheckRules.UpToDateBeforeMerge
text, outcome, err := branchprotection.GetTextOutcomeFromBool(p, nilMsg, trueMsg, falseMsg)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}

Check warning on line 52 in probes/requiresUpToDateBranches/impl.go

View check run for this annotation

Codecov / codecov/patch

probes/requiresUpToDateBranches/impl.go#L51-L52

Added lines #L51 - L52 were not covered by tests
f, err := finding.NewWith(fs, Probe, text, nil, outcome)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}

Check warning on line 56 in probes/requiresUpToDateBranches/impl.go

View check run for this annotation

Codecov / codecov/patch

probes/requiresUpToDateBranches/impl.go#L55-L56

Added lines #L55 - L56 were not covered by tests
f = f.WithValues(map[string]int{
*branch.Name: 1,
})
findings = append(findings, *f)
}
return findings, Probe, nil
}

0 comments on commit dd371e3

Please sign in to comment.