Skip to content

Commit 553785b

Browse files
authored
feat(branch-protection): Push/Reviewer actors can be specified by name (#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
1 parent 9d58e1d commit 553785b

File tree

5 files changed

+295
-49
lines changed

5 files changed

+295
-49
lines changed

github/resource_github_branch_protection.go

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,27 @@ func resourceGithubBranchProtectionCreate(d *schema.ResourceData, meta interface
154154
if err != nil {
155155
return err
156156
}
157+
158+
var reviewIds, pushIds, bypassIds []string
159+
reviewIds, err = getActorIds(data.ReviewDismissalActorIDs, meta)
160+
if err != nil {
161+
return err
162+
}
163+
164+
pushIds, err = getActorIds(data.PushActorIDs, meta)
165+
if err != nil {
166+
return err
167+
}
168+
169+
bypassIds, err = getActorIds(data.BypassPullRequestActorIDs, meta)
170+
if err != nil {
171+
return err
172+
}
173+
174+
data.PushActorIDs = pushIds
175+
data.ReviewDismissalActorIDs = reviewIds
176+
data.BypassPullRequestActorIDs = bypassIds
177+
157178
input := githubv4.CreateBranchProtectionRuleInput{
158179
AllowsDeletions: githubv4.NewBoolean(githubv4.Boolean(data.AllowsDeletions)),
159180
AllowsForcePushes: githubv4.NewBoolean(githubv4.Boolean(data.AllowsForcePushes)),
@@ -255,7 +276,11 @@ func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta interface{}
255276
log.Printf("[DEBUG] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_REQUIRES_CONVERSATION_RESOLUTION, protection.Repository.Name, protection.Pattern, d.Id())
256277
}
257278

258-
approvingReviews := setApprovingReviews(protection)
279+
approvingReviews, err := setApprovingReviews(protection, d, meta)
280+
if err != nil {
281+
return err
282+
}
283+
259284
err = d.Set(PROTECTION_REQUIRES_APPROVING_REVIEWS, approvingReviews)
260285
if err != nil {
261286
log.Printf("[DEBUG] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_REQUIRES_APPROVING_REVIEWS, protection.Repository.Name, protection.Pattern, d.Id())
@@ -267,7 +292,10 @@ func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta interface{}
267292
log.Printf("[DEBUG] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_REQUIRES_STATUS_CHECKS, protection.Repository.Name, protection.Pattern, d.Id())
268293
}
269294

270-
restrictsPushes := setPushes(protection)
295+
restrictsPushes, err := setPushes(protection, d, meta)
296+
if err != nil {
297+
return err
298+
}
271299
err = d.Set(PROTECTION_RESTRICTS_PUSHES, restrictsPushes)
272300
if err != nil {
273301
log.Printf("[DEBUG] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_RESTRICTS_PUSHES, protection.Repository.Name, protection.Pattern, d.Id())
@@ -288,6 +316,27 @@ func resourceGithubBranchProtectionUpdate(d *schema.ResourceData, meta interface
288316
if err != nil {
289317
return err
290318
}
319+
320+
var reviewIds, pushIds, bypassIds []string
321+
reviewIds, err = getActorIds(data.ReviewDismissalActorIDs, meta)
322+
if err != nil {
323+
return err
324+
}
325+
326+
pushIds, err = getActorIds(data.PushActorIDs, meta)
327+
if err != nil {
328+
return err
329+
}
330+
331+
bypassIds, err = getActorIds(data.BypassPullRequestActorIDs, meta)
332+
if err != nil {
333+
return err
334+
}
335+
336+
data.PushActorIDs = pushIds
337+
data.ReviewDismissalActorIDs = reviewIds
338+
data.BypassPullRequestActorIDs = bypassIds
339+
291340
input := githubv4.UpdateBranchProtectionRuleInput{
292341
BranchProtectionRuleID: d.Id(),
293342
AllowsDeletions: githubv4.NewBoolean(githubv4.Boolean(data.AllowsDeletions)),

github/resource_github_branch_protection_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,60 @@ func TestAccGithubBranchProtection(t *testing.T) {
380380

381381
})
382382

383+
t.Run("configures branch push restrictions with username", func(t *testing.T) {
384+
385+
user := fmt.Sprintf("/%s", testOwnerFunc())
386+
config := fmt.Sprintf(`
387+
resource "github_repository" "test" {
388+
name = "tf-acc-test-%s"
389+
auto_init = true
390+
}
391+
392+
resource "github_branch_protection" "test" {
393+
394+
repository_id = github_repository.test.name
395+
pattern = "main"
396+
397+
push_restrictions = [
398+
"%s",
399+
]
400+
401+
}
402+
`, randomID, user)
403+
404+
check := resource.ComposeAggregateTestCheckFunc(
405+
resource.TestCheckResourceAttr(
406+
"github_branch_protection.test", "push_restrictions.#", "1",
407+
),
408+
)
409+
410+
testCase := func(t *testing.T, mode string) {
411+
resource.Test(t, resource.TestCase{
412+
PreCheck: func() { skipUnlessMode(t, mode) },
413+
Providers: testAccProviders,
414+
Steps: []resource.TestStep{
415+
{
416+
Config: config,
417+
Check: check,
418+
},
419+
},
420+
})
421+
}
422+
423+
t.Run("with an anonymous account", func(t *testing.T) {
424+
t.Skip("anonymous account not supported for this operation")
425+
})
426+
427+
t.Run("with an individual account", func(t *testing.T) {
428+
t.Skip("individual account not supported for this operation")
429+
})
430+
431+
t.Run("with an organization account", func(t *testing.T) {
432+
testCase(t, organization)
433+
})
434+
435+
})
436+
383437
t.Run("configures force pushes and deletions", func(t *testing.T) {
384438

385439
config := fmt.Sprintf(`

github/resource_github_repository_environment.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,13 @@ func resourceGithubRepositoryEnvironmentRead(d *schema.ResourceData, meta interf
134134
for _, r := range pr.Reviewers {
135135
switch *r.Type {
136136
case "Team":
137-
teams = append(teams, *r.Reviewer.(*github.Team).ID)
137+
if r.Reviewer.(*github.Team).ID != nil {
138+
teams = append(teams, *r.Reviewer.(*github.Team).ID)
139+
}
138140
case "User":
139-
users = append(users, *r.Reviewer.(*github.User).ID)
141+
if r.Reviewer.(*github.User).ID != nil {
142+
users = append(users, *r.Reviewer.(*github.User).ID)
143+
}
140144
}
141145
}
142146
d.Set("reviewers", []interface{}{

0 commit comments

Comments
 (0)