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

🐛 Fix nil-ptr dereference #1269

Merged
merged 1 commit into from
Nov 15, 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
21 changes: 18 additions & 3 deletions checks/branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,21 @@ func (b branchMap) getBranchByName(name string) (*clients.BranchRef, error) {
func getBranchMapFrom(branches []*clients.BranchRef) branchMap {
ret := make(branchMap)
for _, branch := range branches {
if branch.Name != nil && *branch.Name != "" {
ret[*branch.Name] = branch
branchName := getBranchName(branch)
if branchName != "" {
ret[branchName] = branch
}
}
return ret
}

func getBranchName(branch *clients.BranchRef) string {
if branch == nil || branch.Name == nil {
return ""
}
return *branch.Name
}

// BranchProtection runs Branch-Protection check.
func BranchProtection(c *checker.CheckRequest) checker.CheckResult {
// Checks branch protection on both release and development branch.
Expand Down Expand Up @@ -142,7 +150,10 @@ func checkReleaseAndDevBranchProtection(
if err != nil {
return checker.CreateRuntimeErrorResult(CheckBranchProtection, err)
}
checkBranches[*defaultBranch.Name] = true
defaultBranchName := getBranchName(defaultBranch)
if defaultBranchName != "" {
checkBranches[defaultBranchName] = true
}

var scores []int
// Check protections on all the branches.
Expand All @@ -164,6 +175,10 @@ func checkReleaseAndDevBranchProtection(
scores = append(scores, score)
}

if len(scores) == 0 {
return checker.CreateInconclusiveResult(CheckBranchProtection, "unable to detect any development/release branches")
}

score := checker.AggregateScores(scores...)
switch score {
case checker.MinResultScore:
Expand Down
56 changes: 55 additions & 1 deletion checks/branch_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import (

func getBranch(branches []*clients.BranchRef, name string) *clients.BranchRef {
for _, branch := range branches {
if *branch.Name == name {
branchName := getBranchName(branch)
if branchName == name {
return branch
}
}
Expand Down Expand Up @@ -68,6 +69,59 @@ func TestReleaseAndDevBranchProtected(t *testing.T) {
releases []string
nonadmin bool
}{
{
name: "Nil release and main branch names",
expected: scut.TestReturn{
Error: nil,
Score: -1,
NumberOfWarn: 0,
NumberOfInfo: 0,
NumberOfDebug: 0,
},
defaultBranch: main,
branches: []*clients.BranchRef{
{
Name: nil,
Protected: &trueVal,
BranchProtectionRule: clients.BranchProtectionRule{
RequiredStatusChecks: clients.StatusChecksRule{
Strict: &trueVal,
Contexts: []string{"foo"},
},
RequiredPullRequestReviews: clients.PullRequestReviewRule{
DismissStaleReviews: &trueVal,
RequireCodeOwnerReviews: &trueVal,
RequiredApprovingReviewCount: &oneVal,
},
EnforceAdmins: &trueVal,
RequireLinearHistory: &trueVal,
AllowForcePushes: &falseVal,
AllowDeletions: &falseVal,
},
},
{
Name: nil,
Protected: &trueVal,
BranchProtectionRule: clients.BranchProtectionRule{
RequiredStatusChecks: clients.StatusChecksRule{
Strict: &falseVal,
Contexts: nil,
},
RequiredPullRequestReviews: clients.PullRequestReviewRule{
DismissStaleReviews: &falseVal,
RequireCodeOwnerReviews: &falseVal,
RequiredApprovingReviewCount: &zeroVal,
},
EnforceAdmins: &falseVal,
RequireLinearHistory: &falseVal,
AllowForcePushes: &falseVal,
AllowDeletions: &falseVal,
},
},
nil,
},
releases: []string{},
},
{
name: "Only development branch",
expected: scut.TestReturn{
Expand Down