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

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

Merged
merged 6 commits into from
Oct 12, 2022
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
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