Skip to content

Commit

Permalink
fix - Handle nil structs in branch protection (#124)
Browse files Browse the repository at this point in the history
Handle structs that could be nil while checking for branch protection.
  • Loading branch information
naveensrinivasan committed Jan 7, 2021
1 parent 9d4e5c0 commit 7b740ce
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions checks/branch_protected.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,44 +47,56 @@ func IsBranchProtected(protection *github.Protection, c checker.Checker) checker
totalChecks := 6
totalSuccess := 0

if protection.AllowForcePushes.Enabled {
c.Logf("!! branch protection AllowForcePushes enabled")
} else {
totalSuccess++
if protection.GetAllowForcePushes() != nil {
if protection.AllowForcePushes.Enabled {
c.Logf("!! branch protection AllowForcePushes enabled")
} else {
totalSuccess++
}
}

if protection.AllowDeletions.Enabled {
c.Logf("!! branch protection AllowDeletions enabled")
} else {
totalSuccess++
if protection.GetAllowDeletions() != nil {
if protection.AllowDeletions.Enabled {
c.Logf("!! branch protection AllowDeletions enabled")
} else {
totalSuccess++
}
}

if !protection.EnforceAdmins.Enabled {
c.Logf("!! branch protection EnforceAdmins not enabled")
} else {
totalSuccess++
if protection.GetEnforceAdmins() != nil {
if !protection.EnforceAdmins.Enabled {
c.Logf("!! branch protection EnforceAdmins not enabled")
} else {
totalSuccess++
}
}

if !protection.RequireLinearHistory.Enabled {
c.Logf("!! branch protection require linear history not enabled")
} else {
totalSuccess++
if protection.GetRequireLinearHistory() != nil {
if !protection.RequireLinearHistory.Enabled {
c.Logf("!! branch protection require linear history not enabled")
} else {
totalSuccess++
}
}

if !protection.RequiredStatusChecks.Strict {
c.Logf("!! branch protection require status checks to pass before merging not enabled")
} else {
if len(protection.RequiredStatusChecks.Contexts) == 0 {
c.Logf("!! branch protection require status checks to pass before merging has no specific status to check for")
if protection.GetRequiredStatusChecks() != nil {
if !protection.RequiredStatusChecks.Strict {
c.Logf("!! branch protection require status checks to pass before merging not enabled")
} else {
totalSuccess++
if len(protection.RequiredStatusChecks.Contexts) == 0 {
c.Logf("!! branch protection require status checks to pass before merging has no specific status to check for")
} else {
totalSuccess++
}
}
}

if protection.RequiredPullRequestReviews.RequiredApprovingReviewCount < 1 {
c.Logf("!! branch protection require pullrequest before merging not enabled")
} else {
totalSuccess++
if protection.GetRequiredPullRequestReviews() != nil {
if protection.RequiredPullRequestReviews.RequiredApprovingReviewCount < 1 {
c.Logf("!! branch protection require pullrequest before merging not enabled")
} else {
totalSuccess++
}
}

return checker.ProportionalResult(totalSuccess, totalChecks, 1.0)
Expand Down

0 comments on commit 7b740ce

Please sign in to comment.