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

fix: do not update/create rulesets on archived repository #2460

Open
wants to merge 3 commits 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
13 changes: 12 additions & 1 deletion github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,13 @@ func resourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) erro
d.Set("merge_commit_title", repo.GetMergeCommitTitle())
d.Set("squash_merge_commit_message", repo.GetSquashMergeCommitMessage())
d.Set("squash_merge_commit_title", repo.GetSquashMergeCommitTitle())

// Get repository rulesets
rulesets, _, err := client.Repositories.GetAllRulesets(ctx, owner, repoName, true)
if err != nil {
return fmt.Errorf("error getting repository rulesets: %v", err)
}
d.Set("rulesets", rulesets)
}

if repo.GetHasPages() {
Expand Down Expand Up @@ -746,7 +753,6 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er
}

client := meta.(*Owner).v3client

repoReq := resourceGithubRepositoryObject(d)

// handle visibility updates separately from other fields
Expand All @@ -757,6 +763,11 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er
log.Print("[DEBUG] No security_and_analysis update required. Removing this field from the payload.")
}

// Skip ruleset operations if repository is archived
if d.Get("archived").(bool) {
log.Printf("[DEBUG] Repository is archived, skipping ruleset operations")
}

// The documentation for `default_branch` states: "This can only be set
// after a repository has already been created". However, for backwards
// compatibility we need to allow terraform configurations that set
Expand Down
71 changes: 71 additions & 0 deletions github/resource_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,77 @@ func TestAccGithubRepositories(t *testing.T) {

})

t.Run("skips ruleset operations for archived repositories", func(t *testing.T) {
config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-archive-ruleset-%[1]s"
description = "Terraform acceptance tests %[1]s"
archived = false
}

resource "github_repository_ruleset" "test" {
name = "test"
repository = github_repository.test.name
target = "branch"
enforcement = "active"

rules {
creation = true
}
}
`, randomID)

checks := map[string]resource.TestCheckFunc{
"before": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "archived",
"false",
),
resource.TestCheckResourceAttr(
"github_repository_ruleset.test", "name",
"test",
),
),
"after": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "archived",
"true",
),
),
}

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: checks["before"],
},
{
Config: strings.Replace(config,
`archived = false`,
`archived = true`, 1),
Check: checks["after"],
},
},
})
}

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) {
testCase(t, individual)
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})
})

}
func TestAccGithubRepositoryPages(t *testing.T) {

Expand Down