Skip to content

Commit 45bd8d5

Browse files
committed
github_repository: Adds PATCH call in Create to set archived value
...and skips further updates to the repository Signed-off-by: Timo Sand <[email protected]>
1 parent 8727da5 commit 45bd8d5

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

github/resource_github_repository.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
"github.com/google/go-github/v81/github"
13+
"github.com/hashicorp/terraform-plugin-log/tflog"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1415
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1516
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -756,6 +757,24 @@ func resourceGithubRepositoryCreate(ctx context.Context, d *schema.ResourceData,
756757
d.SetId(repo.GetName())
757758
}
758759

760+
archived := d.Get("archived").(bool)
761+
if archived {
762+
tflog.Debug(ctx, "Repository archived, skipping modifying topics, pages, visibility, and vulnerability alerts", map[string]any{
763+
"owner": meta.(*Owner).name,
764+
"repository": d.Id(),
765+
"archived": archived,
766+
})
767+
// Archived is not set on Repository.Create, so we need to PATCH the repository to set it
768+
repo, _, err := client.Repositories.Edit(ctx, owner, repoName, &github.Repository{
769+
Archived: github.Ptr(archived),
770+
})
771+
if err != nil {
772+
return diag.FromErr(err)
773+
}
774+
d.SetId(repo.GetName())
775+
return resourceGithubRepositoryRead(ctx, d, meta)
776+
}
777+
759778
topics := repoReq.Topics
760779
if len(topics) > 0 {
761780
_, _, err := client.Repositories.ReplaceAllTopics(ctx, owner, repoName, topics)

github/resource_github_repository_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,7 @@ func TestAccGithubRepository(t *testing.T) {
209209
},
210210
})
211211
})
212-
213-
t.Run("creates and archives repositories without error", func(t *testing.T) {
212+
t.Run("creates_repositories_as_archived_without_error", func(t *testing.T) {
214213
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
215214
testRepoName := fmt.Sprintf("%screate-and-archive-%s", testResourcePrefix, randomID)
216215
config := fmt.Sprintf(`

0 commit comments

Comments
 (0)