diff --git a/github/resource_github_repository.go b/github/resource_github_repository.go index 87ec7f34d0..46c8f7e4b5 100644 --- a/github/resource_github_repository.go +++ b/github/resource_github_repository.go @@ -1036,10 +1036,9 @@ func resourceGithubRepositoryDelete(ctx context.Context, d *schema.ResourceData, log.Printf("[DEBUG] Repository already archived, nothing to do on delete: %s/%s", owner, repoName) return nil } else { - if err := d.Set("archived", true); err != nil { - return diag.FromErr(err) + repoReq := &github.Repository{ + Archived: new(true), } - repoReq := resourceGithubRepositoryObject(d) log.Printf("[DEBUG] Archiving repository on delete: %s/%s", owner, repoName) _, _, err := client.Repositories.Edit(ctx, owner, repoName, repoReq) return diag.FromErr(err) diff --git a/github/resource_github_repository_test.go b/github/resource_github_repository_test.go index de8ba18464..fea8b70353 100644 --- a/github/resource_github_repository_test.go +++ b/github/resource_github_repository_test.go @@ -514,7 +514,6 @@ resource "github_repository" "test" { name = "%s" auto_init = true archive_on_destroy = true - archived = %s visibility = "%s" } ` @@ -522,19 +521,23 @@ resource "github_repository" "test" { resource.Test(t, resource.TestCase{ PreCheck: func() { skipUnauthenticated(t) }, ProviderFactories: providerFactories, + CheckDestroy: func(_ *terraform.State) error { + repository, _, err := testAccConf.meta.v3client.Repositories.Get(t.Context(), testAccConf.meta.name, testRepoName) + if err != nil { + return fmt.Errorf("failed to get repository %q after destroy: %w", testRepoName, err) + } + if !repository.GetArchived() { + return fmt.Errorf("repository %q was not archived on destroy", testRepoName) + } + return nil + }, Steps: []resource.TestStep{ { - Config: fmt.Sprintf(config, testRepoName, "false", testAccConf.testRepositoryVisibility), + Config: fmt.Sprintf(config, testRepoName, testAccConf.testRepositoryVisibility), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("github_repository.test", "archived", "false"), ), }, - { - Config: fmt.Sprintf(config, testRepoName, "true", testAccConf.testRepositoryVisibility), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("github_repository.test", "archived", "true"), - ), - }, }, }) }) @@ -1631,6 +1634,44 @@ resource "github_repository" "private" { }) } +func TestGithubRepositoryDeleteArchivesWithMinimalPayload(t *testing.T) { + t.Parallel() + + ts := githubApiMock([]*mockResponse{ + { + ExpectedUri: "/repos/owner/repo", + ExpectedMethod: "PATCH", + ExpectedBody: []byte(`{"archived":true} +`), + StatusCode: 200, + ResponseBody: `{"name":"repo","archived":true}`, + }, + }) + defer ts.Close() + + client := mustCreateTestGitHubClient(t, ts.URL) + meta := &Owner{name: "owner", v3client: client} + + d := schema.TestResourceDataRaw(t, resourceGithubRepository().Schema, map[string]any{ + "name": "repo", + "archive_on_destroy": true, + "archived": false, + "security_and_analysis": []any{ + map[string]any{ + "advanced_security": []any{ + map[string]any{"status": "enabled"}, + }, + }, + }, + }) + d.SetId("repo") + + diags := resourceGithubRepositoryDelete(t.Context(), d, meta) + if diags.HasError() { + t.Fatalf("expected no error, got: %v", diags) + } +} + func Test_expandPages(t *testing.T) { t.Parallel()