Skip to content

Commit c8e8f67

Browse files
committed
Remove duplicate code
Signed-off-by: Timo Sand <[email protected]>
1 parent 15fff78 commit c8e8f67

2 files changed

Lines changed: 21 additions & 18 deletions

File tree

github/resource_github_repository.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -998,19 +998,13 @@ func resourceGithubRepositoryUpdate(ctx context.Context, d *schema.ResourceData,
998998

999999
if d.HasChange("topics") {
10001000
topics := repoReq.Topics
1001-
_, _, err = client.Repositories.ReplaceAllTopics(ctx, owner, *repo.Name, topics)
1001+
// GitHub API docs say that the ReplaceAllTopics endpoint should be used instead of updating the repository with the topics field
1002+
// https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#update-a-repository
1003+
_, _, err := client.Repositories.ReplaceAllTopics(ctx, owner, *repo.Name, topics)
10021004
if err != nil {
10031005
return diag.FromErr(err)
10041006
}
10051007
d.SetId(*repo.Name)
1006-
1007-
if d.HasChange("topics") {
1008-
topics := repoReq.Topics
1009-
_, _, err = client.Repositories.ReplaceAllTopics(ctx, owner, *repo.Name, topics)
1010-
if err != nil {
1011-
return diag.FromErr(err)
1012-
}
1013-
}
10141008
}
10151009

10161010
if d.HasChange("vulnerability_alerts") {

github/resource_github_repository_test.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ func TestAccGithubRepository(t *testing.T) {
382382
})
383383
})
384384

385-
t.Run("configures topics for a repository", func(t *testing.T) {
385+
t.Run("configures_topics_for_a_repository", func(t *testing.T) {
386386
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
387387
testRepoName := fmt.Sprintf("%stopic-%s", testResourcePrefix, randomID)
388388
config := fmt.Sprintf(`
@@ -393,20 +393,29 @@ func TestAccGithubRepository(t *testing.T) {
393393
}
394394
`, testRepoName)
395395

396-
check := resource.ComposeTestCheckFunc(
397-
resource.TestCheckResourceAttr(
398-
"github_repository.test", "topics.#",
399-
"2",
400-
),
401-
)
402-
403396
resource.Test(t, resource.TestCase{
404397
PreCheck: func() { skipUnauthenticated(t) },
405398
ProviderFactories: providerFactories,
406399
Steps: []resource.TestStep{
407400
{
408401
Config: config,
409-
Check: check,
402+
Check: resource.ComposeTestCheckFunc(
403+
resource.TestCheckResourceAttr(
404+
"github_repository.test", "topics.#",
405+
"2",
406+
),
407+
),
408+
},
409+
{
410+
Config: strings.Replace(config,
411+
`topics = ["terraform", "testing"]`,
412+
`topics = ["terraform", "testing", "extra-topic"]`, 1),
413+
Check: resource.ComposeTestCheckFunc(
414+
resource.TestCheckResourceAttr(
415+
"github_repository.test", "topics.#",
416+
"3",
417+
),
418+
),
410419
},
411420
},
412421
})

0 commit comments

Comments
 (0)