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

Add Apps to actor types in branch protection #615

Merged
merged 1 commit into from
Dec 18, 2020
Merged
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
Add Apps to actor types in branch protection
Adding Github Apps to actor types in the branch protection resource.

NOTE: Apps as an actor type is only available in push restrictions.
patrickmarabeas committed Nov 28, 2020
commit 9f105b29cd61d994c49f1345738bd0e37ee30074
34 changes: 28 additions & 6 deletions github/util_v4_branch_protection.go
Original file line number Diff line number Diff line change
@@ -13,23 +13,31 @@ type Actor struct {
Name githubv4.String
}

type ActorTypes struct {
type DismissalActorTypes struct {
Actor struct {
Team Actor `graphql:"... on Team"`
User Actor `graphql:"... on User"`
}
}

type PushActorTypes struct {
Actor struct {
App Actor `graphql:"... on App"`
Team Actor `graphql:"... on Team"`
User Actor `graphql:"... on User"`
}
}

type BranchProtectionRule struct {
Repository struct {
ID githubv4.String
Name githubv4.String
}
PushAllowances struct {
Nodes []ActorTypes
Nodes []PushActorTypes
} `graphql:"pushAllowances(first: 100)"`
ReviewDismissalAllowances struct {
Nodes []ActorTypes
Nodes []DismissalActorTypes
} `graphql:"reviewDismissalAllowances(first: 100)"`
DismissesStaleReviews githubv4.Boolean
ID githubv4.ID
@@ -163,7 +171,21 @@ func branchProtectionResourceData(d *schema.ResourceData, meta interface{}) (Bra
return data, nil
}

func setActorIDs(actors []ActorTypes) []string {
func setDismissalActorIDs(actors []DismissalActorTypes) []string {
pushActors := make([]string, 0, len(actors))
for _, a := range actors {
if a.Actor.Team != (Actor{}) {
pushActors = append(pushActors, a.Actor.Team.ID.(string))
}
if a.Actor.User != (Actor{}) {
pushActors = append(pushActors, a.Actor.Team.ID.(string))
}
}

return pushActors
}

func setPushActorIDs(actors []PushActorTypes) []string {
pushActors := make([]string, 0, len(actors))
for _, a := range actors {
if a.Actor.Team != (Actor{}) {
@@ -183,7 +205,7 @@ func setApprovingReviews(protection BranchProtectionRule) interface{} {
}

dismissalAllowances := protection.ReviewDismissalAllowances.Nodes
dismissalActors := setActorIDs(dismissalAllowances)
dismissalActors := setDismissalActorIDs(dismissalAllowances)
approvalReviews := []interface{}{
map[string]interface{}{
PROTECTION_REQUIRED_APPROVING_REVIEW_COUNT: protection.RequiredApprovingReviewCount,
@@ -216,7 +238,7 @@ func setPushes(protection BranchProtectionRule) []string {
return nil
}
pushAllowances := protection.PushAllowances.Nodes
pushActors := setActorIDs(pushAllowances)
pushActors := setPushActorIDs(pushAllowances)

return pushActors
}