Skip to content

Commit

Permalink
feat(branch-protection): Push/Reviewer actors can be specified by name (
Browse files Browse the repository at this point in the history
#1020)

* feat(branch-protection): Push/Reviewer actors can be specified by name

feat: Added getNodeIDv4

feat(branch-protection): Added prefix requirement for teams/users

The / prefix defines a user, the orgname/ prefix defines a team. (Ex.: testorg/team)

* test(branch-protection): Added push restriction test with username

* doc(branch-protection): Updated documentation to mention actor names

* feat(branch-protection): updated to latest version

* fix: env nil pointer dereference

includes a nil check on branch protection resources

* fix(branch-protection): added error handling

Also removed loop labels
  • Loading branch information
dion-gionet authored Oct 12, 2022
1 parent 9d58e1d commit 553785b
Show file tree
Hide file tree
Showing 5 changed files with 295 additions and 49 deletions.
53 changes: 51 additions & 2 deletions github/resource_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,27 @@ func resourceGithubBranchProtectionCreate(d *schema.ResourceData, meta interface
if err != nil {
return err
}

var reviewIds, pushIds, bypassIds []string
reviewIds, err = getActorIds(data.ReviewDismissalActorIDs, meta)
if err != nil {
return err
}

pushIds, err = getActorIds(data.PushActorIDs, meta)
if err != nil {
return err
}

bypassIds, err = getActorIds(data.BypassPullRequestActorIDs, meta)
if err != nil {
return err
}

data.PushActorIDs = pushIds
data.ReviewDismissalActorIDs = reviewIds
data.BypassPullRequestActorIDs = bypassIds

input := githubv4.CreateBranchProtectionRuleInput{
AllowsDeletions: githubv4.NewBoolean(githubv4.Boolean(data.AllowsDeletions)),
AllowsForcePushes: githubv4.NewBoolean(githubv4.Boolean(data.AllowsForcePushes)),
Expand Down Expand Up @@ -255,7 +276,11 @@ func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta interface{}
log.Printf("[DEBUG] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_REQUIRES_CONVERSATION_RESOLUTION, protection.Repository.Name, protection.Pattern, d.Id())
}

approvingReviews := setApprovingReviews(protection)
approvingReviews, err := setApprovingReviews(protection, d, meta)
if err != nil {
return err
}

err = d.Set(PROTECTION_REQUIRES_APPROVING_REVIEWS, approvingReviews)
if err != nil {
log.Printf("[DEBUG] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_REQUIRES_APPROVING_REVIEWS, protection.Repository.Name, protection.Pattern, d.Id())
Expand All @@ -267,7 +292,10 @@ func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta interface{}
log.Printf("[DEBUG] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_REQUIRES_STATUS_CHECKS, protection.Repository.Name, protection.Pattern, d.Id())
}

restrictsPushes := setPushes(protection)
restrictsPushes, err := setPushes(protection, d, meta)
if err != nil {
return err
}
err = d.Set(PROTECTION_RESTRICTS_PUSHES, restrictsPushes)
if err != nil {
log.Printf("[DEBUG] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_RESTRICTS_PUSHES, protection.Repository.Name, protection.Pattern, d.Id())
Expand All @@ -288,6 +316,27 @@ func resourceGithubBranchProtectionUpdate(d *schema.ResourceData, meta interface
if err != nil {
return err
}

var reviewIds, pushIds, bypassIds []string
reviewIds, err = getActorIds(data.ReviewDismissalActorIDs, meta)
if err != nil {
return err
}

pushIds, err = getActorIds(data.PushActorIDs, meta)
if err != nil {
return err
}

bypassIds, err = getActorIds(data.BypassPullRequestActorIDs, meta)
if err != nil {
return err
}

data.PushActorIDs = pushIds
data.ReviewDismissalActorIDs = reviewIds
data.BypassPullRequestActorIDs = bypassIds

input := githubv4.UpdateBranchProtectionRuleInput{
BranchProtectionRuleID: d.Id(),
AllowsDeletions: githubv4.NewBoolean(githubv4.Boolean(data.AllowsDeletions)),
Expand Down
54 changes: 54 additions & 0 deletions github/resource_github_branch_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,60 @@ func TestAccGithubBranchProtection(t *testing.T) {

})

t.Run("configures branch push restrictions with username", func(t *testing.T) {

user := fmt.Sprintf("/%s", testOwnerFunc())
config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-%s"
auto_init = true
}
resource "github_branch_protection" "test" {
repository_id = github_repository.test.name
pattern = "main"
push_restrictions = [
"%s",
]
}
`, randomID, user)

check := resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"github_branch_protection.test", "push_restrictions.#", "1",
),
)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
t.Skip("individual account not supported for this operation")
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})

})

t.Run("configures force pushes and deletions", func(t *testing.T) {

config := fmt.Sprintf(`
Expand Down
8 changes: 6 additions & 2 deletions github/resource_github_repository_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,13 @@ func resourceGithubRepositoryEnvironmentRead(d *schema.ResourceData, meta interf
for _, r := range pr.Reviewers {
switch *r.Type {
case "Team":
teams = append(teams, *r.Reviewer.(*github.Team).ID)
if r.Reviewer.(*github.Team).ID != nil {
teams = append(teams, *r.Reviewer.(*github.Team).ID)
}
case "User":
users = append(users, *r.Reviewer.(*github.User).ID)
if r.Reviewer.(*github.User).ID != nil {
users = append(users, *r.Reviewer.(*github.User).ID)
}
}
}
d.Set("reviewers", []interface{}{
Expand Down
Loading

0 comments on commit 553785b

Please sign in to comment.