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

Enable the use of different actors in rule sets #2588

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 6 additions & 6 deletions github/resource_github_organization_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
Schema: map[string]*schema.Schema{
"actor_id": {
Type: schema.TypeInt,
Required: true,
Description: "The ID of the actor that can bypass a ruleset. When `actor_type` is `OrganizationAdmin`, this should be set to `1`.",
Optional: true,
Default: nil,
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.",
},
"actor_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"RepositoryRole", "Team", "Integration", "OrganizationAdmin"}, false),
Description: "The type of actor that can bypass a ruleset. Can be one of: `RepositoryRole`, `Team`, `Integration`, `OrganizationAdmin`.",
Type: schema.TypeString,
Required: true,
Description: "The type of actor that can bypass a ruleset. See https://docs.github.com/en/rest/orgs/rules for more information",
},
"bypass_mode": {
Type: schema.TypeString,
Expand Down
83 changes: 83 additions & 0 deletions github/resource_github_organization_ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,87 @@ func TestGithubOrganizationRulesets(t *testing.T) {

})

t.Run("Creates and updates organization using bypasses", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_organization_ruleset" "test" {
name = "test-%s"
target = "branch"
enforcement = "active"

conditions {
ref_name {
include = ["~ALL"]
exclude = []
}
}

rules {
creation = true
update = true
deletion = true
required_linear_history = true
required_signatures = false
pull_request {
required_approving_review_count = 2
required_review_thread_resolution = true
require_code_owner_review = true
dismiss_stale_reviews_on_push = true
require_last_push_approval = true
}

bypass_actors {
actor_type = "DeployKey"
bypass_mode = "always"
}

bypass_actors {
actor_id = 5
actor_type = "RepositoryRole"
bypass_mode = "always"
}

bypass_actors {
actor_id = 0
actor_type = "OrganizationAdmin"
bypass_mode = "always"
}
}
}
`, randomID)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_organization_ruleset.test", "bypass_actors.0.actor_type",
"0",
),
resource.TestCheckResourceAttr(
"github_organization_ruleset.test", "bypass_actors.1.actor_type",
"5",
),
resource.TestCheckResourceAttr(
"github_organization_ruleset.test", "bypass_actors.2.actor_type",
"0",
),
)

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 enterprise account", func(t *testing.T) {
testCase(t, enterprise)
})

})

}
9 changes: 5 additions & 4 deletions github/resource_github_repository_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ func resourceGithubRepositoryRuleset() *schema.Resource {
Schema: map[string]*schema.Schema{
"actor_id": {
Type: schema.TypeInt,
Required: true,
Description: "The ID of the actor that can bypass a ruleset. When `actor_type` is `OrganizationAdmin`, this should be set to `1`.",
Optional: true,
Default: nil,
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.",
},
"actor_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"RepositoryRole", "Team", "Integration", "OrganizationAdmin"}, false),
Description: "The type of actor that can bypass a ruleset. Can be one of: `RepositoryRole`, `Team`, `Integration`, `OrganizationAdmin`.",
Description: "The type of actor that can bypass a ruleset. See https://docs.github.com/en/rest/repos/rules for more information.",
},
"bypass_mode": {
Type: schema.TypeString,
Expand Down Expand Up @@ -604,7 +605,7 @@ func resourceGithubRepositoryRulesetUpdate(d *schema.ResourceData, meta interfac

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

ruleset, _, err := client.Repositories.UpdateRuleset(ctx, owner, repoName, rulesetID, rulesetReq)
ruleset, _, err := client.Repositories.UpdateRulesetNoBypassActor(ctx, owner, repoName, rulesetID, rulesetReq)
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion github/respository_rules_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ func expandBypassActors(input []interface{}) []*github.BypassActor {
inputMap := v.(map[string]interface{})
actor := &github.BypassActor{}
if v, ok := inputMap["actor_id"].(int); ok {
actor.ActorID = github.Int64(int64(v))
if v == 0 {
actor.ActorID = nil
} else {
actor.ActorID = github.Int64(int64(v))
}
}

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