Skip to content

Commit 8b2eac9

Browse files
languitarmorfien101nickfloyd
authored
fix: allow missing types of rule bypasses (#2726)
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. Signed-off-by: Johannes Wienke <[email protected]> Co-authored-by: Randy Coburn <[email protected]> Co-authored-by: Nick Floyd <[email protected]>
1 parent d9effad commit 8b2eac9

File tree

5 files changed

+101
-13
lines changed

5 files changed

+101
-13
lines changed

github/resource_github_organization_ruleset.go

Lines changed: 6 additions & 6 deletions
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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,87 @@ func TestGithubOrganizationRulesets(t *testing.T) {
295295

296296
})
297297

298+
t.Run("Creates and updates organization using bypasses", func(t *testing.T) {
299+
300+
config := fmt.Sprintf(`
301+
resource "github_organization_ruleset" "test" {
302+
name = "test-%s"
303+
target = "branch"
304+
enforcement = "active"
305+
306+
conditions {
307+
ref_name {
308+
include = ["~ALL"]
309+
exclude = []
310+
}
311+
}
312+
313+
rules {
314+
creation = true
315+
update = true
316+
deletion = true
317+
required_linear_history = true
318+
required_signatures = false
319+
pull_request {
320+
required_approving_review_count = 2
321+
required_review_thread_resolution = true
322+
require_code_owner_review = true
323+
dismiss_stale_reviews_on_push = true
324+
require_last_push_approval = true
325+
}
326+
327+
bypass_actors {
328+
actor_type = "DeployKey"
329+
bypass_mode = "always"
330+
}
331+
332+
bypass_actors {
333+
actor_id = 5
334+
actor_type = "RepositoryRole"
335+
bypass_mode = "always"
336+
}
337+
338+
bypass_actors {
339+
actor_id = 0
340+
actor_type = "OrganizationAdmin"
341+
bypass_mode = "always"
342+
}
343+
}
344+
}
345+
`, randomID)
346+
347+
check := resource.ComposeTestCheckFunc(
348+
resource.TestCheckResourceAttr(
349+
"github_organization_ruleset.test", "bypass_actors.0.actor_type",
350+
"0",
351+
),
352+
resource.TestCheckResourceAttr(
353+
"github_organization_ruleset.test", "bypass_actors.1.actor_type",
354+
"5",
355+
),
356+
resource.TestCheckResourceAttr(
357+
"github_organization_ruleset.test", "bypass_actors.2.actor_type",
358+
"0",
359+
),
360+
)
361+
362+
testCase := func(t *testing.T, mode string) {
363+
resource.Test(t, resource.TestCase{
364+
PreCheck: func() { skipUnlessMode(t, mode) },
365+
Providers: testAccProviders,
366+
Steps: []resource.TestStep{
367+
{
368+
Config: config,
369+
Check: check,
370+
},
371+
},
372+
})
373+
}
374+
375+
t.Run("with an enterprise account", func(t *testing.T) {
376+
testCase(t, enterprise)
377+
})
378+
379+
})
380+
298381
}

github/resource_github_repository_ruleset.go

Lines changed: 5 additions & 4 deletions
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,
66-
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`.",
67+
ValidateFunc: validation.StringInSlice([]string{"RepositoryRole", "Team", "Integration", "OrganizationAdmin", "DeployKey"}, false),
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,

github/respository_rules_utils.go

Lines changed: 5 additions & 1 deletion
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 {

website/docs/r/repository_ruleset.html.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ The `rules` block supports the following:
256256

257257
#### bypass_actors ####
258258

259-
* `actor_id` - (Required) (Number) The ID of the actor that can bypass a ruleset. If `actor_type` is `Integration`, `actor_id` is a GitHub App ID. App ID can be obtained by following instructions from the [Get an App API docs](https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#get-an-app)
259+
* `actor_id` - (Number) The ID of the actor that can bypass a ruleset. If `actor_type` is `Integration`, `actor_id` is a GitHub App ID. App ID can be obtained by following instructions from the [Get an App API docs](https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#get-an-app)
260260

261-
* `actor_type` (String) The type of actor that can bypass a ruleset. Can be one of: `RepositoryRole`, `Team`, `Integration`, `OrganizationAdmin`.
261+
* `actor_type` (String) The type of actor that can bypass a ruleset. Can be one of: `RepositoryRole`, `Team`, `Integration`, `OrganizationAdmin`, `DeployKey`.
262262

263263
* `bypass_mode` - (Optional) (String) When the specified actor can bypass the ruleset. pull_request means that an actor can only bypass rules on pull requests. Can be one of: `always`, `pull_request`, `exempt`.
264264

0 commit comments

Comments
 (0)