Skip to content
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
5 changes: 2 additions & 3 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
57 changes: 49 additions & 8 deletions github/resource_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,27 +514,30 @@ resource "github_repository" "test" {
name = "%s"
auto_init = true
archive_on_destroy = true
archived = %s
visibility = "%s"
}
`

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"),
),
},
},
})
})
Expand Down Expand Up @@ -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()

Expand Down
Loading