Skip to content

Commit dedde03

Browse files
committed
Leave it to the API to determine if the values for actor_types are valid of not. These change over time and we would need to carry a list in code to verify this.
Do not require an actor_id on rule bypasses. Things like DeployKey do not have an id.
1 parent d63ad78 commit dedde03

4 files changed

+99
-11
lines changed

github/resource_github_organization_ruleset.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
5252
Schema: map[string]*schema.Schema{
5353
"actor_id": {
5454
Type: schema.TypeInt,
55-
Required: true,
56-
Description: "The ID of the actor that can bypass a ruleset. When `actor_type` is `OrganizationAdmin`, this should be set to `1`.",
55+
Optional: true,
56+
Default: nil,
57+
Description: "The ID of the actor that can bypass a ruleset. When `actor_type` is `OrganizationAdmin`, this should be set to `1`. Some resources such as DeployKey do not have an ID and this should be omitted.",
5758
},
5859
"actor_type": {
59-
Type: schema.TypeString,
60-
Required: true,
61-
ValidateFunc: validation.StringInSlice([]string{"RepositoryRole", "Team", "Integration", "OrganizationAdmin"}, false),
62-
Description: "The type of actor that can bypass a ruleset. Can be one of: `RepositoryRole`, `Team`, `Integration`, `OrganizationAdmin`.",
60+
Type: schema.TypeString,
61+
Required: true,
62+
Description: "The type of actor that can bypass a ruleset. See https://docs.github.com/en/rest/orgs/rules for more information",
6363
},
6464
"bypass_mode": {
6565
Type: schema.TypeString,

github/resource_github_organization_ruleset_test.go

+83
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,87 @@ func TestGithubOrganizationRulesets(t *testing.T) {
260260

261261
})
262262

263+
t.Run("Creates and updates organization using bypasses", func(t *testing.T) {
264+
265+
config := fmt.Sprintf(`
266+
resource "github_organization_ruleset" "test" {
267+
name = "test-%s"
268+
target = "branch"
269+
enforcement = "active"
270+
271+
conditions {
272+
ref_name {
273+
include = ["~ALL"]
274+
exclude = []
275+
}
276+
}
277+
278+
rules {
279+
creation = true
280+
update = true
281+
deletion = true
282+
required_linear_history = true
283+
required_signatures = false
284+
pull_request {
285+
required_approving_review_count = 2
286+
required_review_thread_resolution = true
287+
require_code_owner_review = true
288+
dismiss_stale_reviews_on_push = true
289+
require_last_push_approval = true
290+
}
291+
292+
bypass_actors {
293+
actor_type = "DeployKey"
294+
bypass_mode = "always"
295+
}
296+
297+
bypass_actors {
298+
actor_id = 5
299+
actor_type = "RepositoryRole"
300+
bypass_mode = "always"
301+
}
302+
303+
bypass_actors {
304+
actor_id = 0
305+
actor_type = "OrganizationAdmin"
306+
bypass_mode = "always"
307+
}
308+
}
309+
}
310+
`, randomID)
311+
312+
check := resource.ComposeTestCheckFunc(
313+
resource.TestCheckResourceAttr(
314+
"github_organization_ruleset.test", "bypass_actors.0.actor_type",
315+
"0",
316+
),
317+
resource.TestCheckResourceAttr(
318+
"github_organization_ruleset.test", "bypass_actors.1.actor_type",
319+
"5",
320+
),
321+
resource.TestCheckResourceAttr(
322+
"github_organization_ruleset.test", "bypass_actors.2.actor_type",
323+
"0",
324+
),
325+
)
326+
327+
testCase := func(t *testing.T, mode string) {
328+
resource.Test(t, resource.TestCase{
329+
PreCheck: func() { skipUnlessMode(t, mode) },
330+
Providers: testAccProviders,
331+
Steps: []resource.TestStep{
332+
{
333+
Config: config,
334+
Check: check,
335+
},
336+
},
337+
})
338+
}
339+
340+
t.Run("with an enterprise account", func(t *testing.T) {
341+
testCase(t, enterprise)
342+
})
343+
344+
})
345+
263346
}

github/resource_github_repository_ruleset.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@ func resourceGithubRepositoryRuleset() *schema.Resource {
5757
Schema: map[string]*schema.Schema{
5858
"actor_id": {
5959
Type: schema.TypeInt,
60-
Required: true,
61-
Description: "The ID of the actor that can bypass a ruleset. When `actor_type` is `OrganizationAdmin`, this should be set to `1`.",
60+
Optional: true,
61+
Default: nil,
62+
Description: "The ID of the actor that can bypass a ruleset. When `actor_type` is `OrganizationAdmin`, this should be set to `1`. Some resources such as DeployKey do not have an ID and this should be omitted.",
6263
},
6364
"actor_type": {
6465
Type: schema.TypeString,
6566
Required: true,
6667
ValidateFunc: validation.StringInSlice([]string{"RepositoryRole", "Team", "Integration", "OrganizationAdmin"}, false),
67-
Description: "The type of actor that can bypass a ruleset. Can be one of: `RepositoryRole`, `Team`, `Integration`, `OrganizationAdmin`.",
68+
Description: "The type of actor that can bypass a ruleset. See https://docs.github.com/en/rest/repos/rules for more information.",
6869
},
6970
"bypass_mode": {
7071
Type: schema.TypeString,
@@ -604,7 +605,7 @@ func resourceGithubRepositoryRulesetUpdate(d *schema.ResourceData, meta interfac
604605

605606
ctx := context.WithValue(context.Background(), ctxId, rulesetID)
606607

607-
ruleset, _, err := client.Repositories.UpdateRuleset(ctx, owner, repoName, rulesetID, rulesetReq)
608+
ruleset, _, err := client.Repositories.UpdateRulesetNoBypassActor(ctx, owner, repoName, rulesetID, rulesetReq)
608609
if err != nil {
609610
return err
610611
}

github/respository_rules_utils.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ func expandBypassActors(input []interface{}) []*github.BypassActor {
4444
inputMap := v.(map[string]interface{})
4545
actor := &github.BypassActor{}
4646
if v, ok := inputMap["actor_id"].(int); ok {
47-
actor.ActorID = github.Int64(int64(v))
47+
if v == 0 {
48+
actor.ActorID = nil
49+
} else {
50+
actor.ActorID = github.Int64(int64(v))
51+
}
4852
}
4953

5054
if v, ok := inputMap["actor_type"].(string); ok {

0 commit comments

Comments
 (0)