Skip to content

Commit

Permalink
🐛 Check GitHub RequiredPullRequestReviews before access
Browse files Browse the repository at this point in the history
Fixes #mondoohq/cnspec#289

Signed-off-by: Christian Zunker <[email protected]>
  • Loading branch information
czunker committed Jan 27, 2023
1 parent 9d6b209 commit bfff73f
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions resources/packs/github/github_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,29 +448,36 @@ func (g *mqlGithubBranch) GetProtectionRules() (interface{}, error) {

var ghDismissalRestrictions *githubDismissalRestrictions

if branchProtection.RequiredPullRequestReviews.DismissalRestrictions != nil {
ghDismissalRestrictions = &githubDismissalRestrictions{
Users: []string{},
Teams: []string{},
}
rprr, err := core.JsonToDict(githubRequiredPullRequestReviews{})
if err != nil {
return nil, err
}
if branchProtection.RequiredPullRequestReviews != nil {

for i := range branchProtection.RequiredPullRequestReviews.DismissalRestrictions.Teams {
ghDismissalRestrictions.Teams = append(ghDismissalRestrictions.Teams, branchProtection.RequiredPullRequestReviews.DismissalRestrictions.Teams[i].GetName())
}
for i := range branchProtection.RequiredPullRequestReviews.DismissalRestrictions.Users {
ghDismissalRestrictions.Users = append(ghDismissalRestrictions.Users, branchProtection.RequiredPullRequestReviews.DismissalRestrictions.Users[i].GetLogin())
if branchProtection.RequiredPullRequestReviews.DismissalRestrictions != nil {
ghDismissalRestrictions = &githubDismissalRestrictions{
Users: []string{},
Teams: []string{},
}

for i := range branchProtection.RequiredPullRequestReviews.DismissalRestrictions.Teams {
ghDismissalRestrictions.Teams = append(ghDismissalRestrictions.Teams, branchProtection.RequiredPullRequestReviews.DismissalRestrictions.Teams[i].GetName())
}
for i := range branchProtection.RequiredPullRequestReviews.DismissalRestrictions.Users {
ghDismissalRestrictions.Users = append(ghDismissalRestrictions.Users, branchProtection.RequiredPullRequestReviews.DismissalRestrictions.Users[i].GetLogin())
}
}
}

// we use a separate struct to ensure that the output is proper camelCase
rprr, err := core.JsonToDict(githubRequiredPullRequestReviews{
DismissStaleReviews: branchProtection.RequiredPullRequestReviews.DismissStaleReviews,
RequireCodeOwnerReviews: branchProtection.RequiredPullRequestReviews.RequireCodeOwnerReviews,
RequiredApprovingReviewCount: branchProtection.RequiredPullRequestReviews.RequiredApprovingReviewCount,
DismissalRestrictions: ghDismissalRestrictions,
})
if err != nil {
return nil, err
// we use a separate struct to ensure that the output is proper camelCase
rprr, err = core.JsonToDict(githubRequiredPullRequestReviews{
DismissStaleReviews: branchProtection.RequiredPullRequestReviews.DismissStaleReviews,
RequireCodeOwnerReviews: branchProtection.RequiredPullRequestReviews.RequireCodeOwnerReviews,
RequiredApprovingReviewCount: branchProtection.RequiredPullRequestReviews.RequiredApprovingReviewCount,
DismissalRestrictions: ghDismissalRestrictions,
})
if err != nil {
return nil, err
}
}

ea, err := core.JsonToDict(branchProtection.EnforceAdmins)
Expand Down

0 comments on commit bfff73f

Please sign in to comment.