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

🌱 refactor: reorder functions to enhance readability #3880

Closed
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
234 changes: 117 additions & 117 deletions checks/evaluation/branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,64 +235,79 @@
return name, nil
}

func sumUpScoreForTier(t tier, scoresData []levelScore) int {
sum := 0
for i := range scoresData {
score := scoresData[i]
switch t {
case Tier1:
sum += score.scores.basic
case Tier2:
sum += score.scores.review + score.scores.adminReview
case Tier3:
sum += score.scores.context
case Tier4:
sum += score.scores.thoroughReview + score.scores.codeownerReview
case Tier5:
sum += score.scores.adminThoroughReview
}
func deleteAndForcePushProtection(f *finding.Finding, doLogging bool, dl checker.DetailLogger) (int, int) {
var score, max int
logWithoutDebug(f, doLogging, dl)
if f.Outcome == finding.OutcomePositive {
score++
}
return sum
max++

return score, max
}

func logWithDebug(f *finding.Finding, doLogging bool, dl checker.DetailLogger) {
switch f.Outcome {
case finding.OutcomeNotAvailable:
debug(dl, doLogging, f.Message)
case finding.OutcomePositive:
info(dl, doLogging, f.Message)
case finding.OutcomeNegative:
warn(dl, doLogging, f.Message)
default:
// To satisfy linter
func adminThoroughReviewProtection(f *finding.Finding, doLogging bool, dl checker.DetailLogger) (int, int) {
var score, max int

logWithDebug(f, doLogging, dl)
if f.Outcome == finding.OutcomePositive {
score++
}
if f.Outcome != finding.OutcomeNotAvailable {
max++
}
return score, max
}

func logWithoutDebug(f *finding.Finding, doLogging bool, dl checker.DetailLogger) {
switch f.Outcome {
case finding.OutcomePositive:
info(dl, doLogging, f.Message)
case finding.OutcomeNegative:
func nonAdminThoroughReviewProtection(f *finding.Finding, doLogging bool, dl checker.DetailLogger) (int, int) {
var score, max int
if f.Outcome == finding.OutcomePositive {
noOfRequiredReviews, _ := strconv.Atoi(f.Values["numberOfRequiredReviewers"]) //nolint:errcheck
if noOfRequiredReviews >= minReviews {
info(dl, doLogging, f.Message)
score++

Check warning on line 268 in checks/evaluation/branch_protection.go

View check run for this annotation

Codecov / codecov/patch

checks/evaluation/branch_protection.go#L267-L268

Added lines #L267 - L268 were not covered by tests
} else {
warn(dl, doLogging, f.Message)
}
} else if f.Outcome == finding.OutcomeNegative {
warn(dl, doLogging, f.Message)
default:
// To satisfy linter
}
max++
return score, max
}

func logInfoOrWarn(f *finding.Finding, doLogging bool, dl checker.DetailLogger) {
switch f.Outcome {
case finding.OutcomePositive:
func codeownerBranchProtection(f *finding.Finding, doLogging bool, dl checker.DetailLogger) (int, int) {
var score, max int
if f.Outcome == finding.OutcomePositive {
info(dl, doLogging, f.Message)
default:
score++
} else {
warn(dl, doLogging, f.Message)
}
max++
return score, max
}

func normalizeScore(score, max, level int) float64 {
if max == 0 {
return float64(level)
func adminReviewProtection(f *finding.Finding, doLogging bool, dl checker.DetailLogger) (int, int) {
var score, max int
if f.Outcome == finding.OutcomePositive {
score++
}
return float64(score*level) / float64(max)
logWithDebug(f, doLogging, dl)
if f.Outcome != finding.OutcomeNotAvailable {
max++
}
return score, max
}

func nonAdminContextProtection(f *finding.Finding, doLogging bool, dl checker.DetailLogger) (int, int) {
var score, max int
logInfoOrWarn(f, doLogging, dl)
if f.Outcome == finding.OutcomePositive {
score++
}
max++
return score, max
}

func computeFinalScore(scores []levelScore) (int, error) {
Expand Down Expand Up @@ -351,6 +366,66 @@
return int(score), nil
}

func sumUpScoreForTier(t tier, scoresData []levelScore) int {
sum := 0
for i := range scoresData {
score := scoresData[i]
switch t {
case Tier1:
sum += score.scores.basic
case Tier2:
sum += score.scores.review + score.scores.adminReview
case Tier3:
sum += score.scores.context
case Tier4:
sum += score.scores.thoroughReview + score.scores.codeownerReview
case Tier5:
sum += score.scores.adminThoroughReview

Check warning on line 383 in checks/evaluation/branch_protection.go

View check run for this annotation

Codecov / codecov/patch

checks/evaluation/branch_protection.go#L382-L383

Added lines #L382 - L383 were not covered by tests
}
}
return sum
}

func normalizeScore(score, max, level int) float64 {
if max == 0 {
return float64(level)
}

Check warning on line 392 in checks/evaluation/branch_protection.go

View check run for this annotation

Codecov / codecov/patch

checks/evaluation/branch_protection.go#L391-L392

Added lines #L391 - L392 were not covered by tests
return float64(score*level) / float64(max)
}

func logWithDebug(f *finding.Finding, doLogging bool, dl checker.DetailLogger) {
switch f.Outcome {
case finding.OutcomeNotAvailable:
debug(dl, doLogging, f.Message)
case finding.OutcomePositive:
info(dl, doLogging, f.Message)
case finding.OutcomeNegative:
warn(dl, doLogging, f.Message)
default:

Check warning on line 404 in checks/evaluation/branch_protection.go

View check run for this annotation

Codecov / codecov/patch

checks/evaluation/branch_protection.go#L404

Added line #L404 was not covered by tests
// To satisfy linter
}
}

func logWithoutDebug(f *finding.Finding, doLogging bool, dl checker.DetailLogger) {
switch f.Outcome {
case finding.OutcomePositive:
info(dl, doLogging, f.Message)
case finding.OutcomeNegative:
warn(dl, doLogging, f.Message)
default:
// To satisfy linter
}
}

func logInfoOrWarn(f *finding.Finding, doLogging bool, dl checker.DetailLogger) {
switch f.Outcome {
case finding.OutcomePositive:
info(dl, doLogging, f.Message)
default:
warn(dl, doLogging, f.Message)
}
}

func info(dl checker.DetailLogger, doLogging bool, desc string, args ...interface{}) {
if !doLogging {
return
Expand Down Expand Up @@ -380,78 +455,3 @@
Text: fmt.Sprintf(desc, args...),
})
}

func deleteAndForcePushProtection(f *finding.Finding, doLogging bool, dl checker.DetailLogger) (int, int) {
var score, max int
logWithoutDebug(f, doLogging, dl)
if f.Outcome == finding.OutcomePositive {
score++
}
max++

return score, max
}

func nonAdminContextProtection(f *finding.Finding, doLogging bool, dl checker.DetailLogger) (int, int) {
var score, max int
logInfoOrWarn(f, doLogging, dl)
if f.Outcome == finding.OutcomePositive {
score++
}
max++
return score, max
}

func adminReviewProtection(f *finding.Finding, doLogging bool, dl checker.DetailLogger) (int, int) {
var score, max int
if f.Outcome == finding.OutcomePositive {
score++
}
logWithDebug(f, doLogging, dl)
if f.Outcome != finding.OutcomeNotAvailable {
max++
}
return score, max
}

func adminThoroughReviewProtection(f *finding.Finding, doLogging bool, dl checker.DetailLogger) (int, int) {
var score, max int

logWithDebug(f, doLogging, dl)
if f.Outcome == finding.OutcomePositive {
score++
}
if f.Outcome != finding.OutcomeNotAvailable {
max++
}
return score, max
}

func nonAdminThoroughReviewProtection(f *finding.Finding, doLogging bool, dl checker.DetailLogger) (int, int) {
var score, max int
if f.Outcome == finding.OutcomePositive {
noOfRequiredReviews, _ := strconv.Atoi(f.Values["numberOfRequiredReviewers"]) //nolint:errcheck
if noOfRequiredReviews >= minReviews {
info(dl, doLogging, f.Message)
score++
} else {
warn(dl, doLogging, f.Message)
}
} else if f.Outcome == finding.OutcomeNegative {
warn(dl, doLogging, f.Message)
}
max++
return score, max
}

func codeownerBranchProtection(f *finding.Finding, doLogging bool, dl checker.DetailLogger) (int, int) {
var score, max int
if f.Outcome == finding.OutcomePositive {
info(dl, doLogging, f.Message)
score++
} else {
warn(dl, doLogging, f.Message)
}
max++
return score, max
}
Loading