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
18 changes: 5 additions & 13 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ func resourceGithubRepositoryCreate(ctx context.Context, d *schema.ResourceData,
return diag.FromErr(err)
}

d.SetId(*repo.Name)
d.SetId(repo.GetName())
}
} else if d.Get("fork").(string) == "true" {
// Handle repository forking
Expand Down Expand Up @@ -962,7 +962,7 @@ func resourceGithubRepositoryUpdate(ctx context.Context, d *schema.ResourceData,
if err != nil {
return diag.FromErr(err)
}
d.SetId(*repo.Name)
d.SetId(repo.GetName()) // It's possible that `repo.GetName()` is different from `repoName` if the repository is renamed

if d.HasChange("pages") && !d.IsNewResource() {
opts := expandPagesUpdate(d.Get("pages").([]any))
Expand All @@ -989,20 +989,12 @@ func resourceGithubRepositoryUpdate(ctx context.Context, d *schema.ResourceData,
}

if d.HasChange("topics") {
topics := repoReq.Topics
_, _, err = client.Repositories.ReplaceAllTopics(ctx, owner, *repo.Name, topics)
// GitHub API docs say that the ReplaceAllTopics endpoint should be used instead of updating the repository with the topics field
// https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#update-a-repository
_, _, err := client.Repositories.ReplaceAllTopics(ctx, owner, repo.GetName(), repoReq.Topics)
if err != nil {
return diag.FromErr(err)
}
d.SetId(*repo.Name)

if d.HasChange("topics") {
topics := repoReq.Topics
_, _, err = client.Repositories.ReplaceAllTopics(ctx, owner, *repo.Name, topics)
if err != nil {
return diag.FromErr(err)
}
}
}

if v, ok := d.GetOkExists("vulnerability_alerts"); ok { //nolint:staticcheck,SA1019 // We sometimes need to use GetOkExists for booleans
Expand Down
127 changes: 40 additions & 87 deletions github/resource_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,31 +383,34 @@ func TestAccGithubRepository(t *testing.T) {
})
})

t.Run("configures topics for a repository", func(t *testing.T) {
t.Run("configures_topics_for_a_repository", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
testRepoName := fmt.Sprintf("%stopic-%s", testResourcePrefix, randomID)
config := fmt.Sprintf(`
topicsBefore := `["terraform", "testing"]`
topicsAfter := `["terraform", "testing", "extra-topic"]`
config := `
resource "github_repository" "test" {
name = "%s"
description = "Terraform acceptance tests %[1]s"
topics = ["terraform", "testing"]
topics = %s
}
`, testRepoName)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "topics.#",
"2",
),
)
`

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnauthenticated(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
Config: fmt.Sprintf(config, testRepoName, topicsBefore),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("github_repository.test", "topics.#", "2"),
),
},
{
Config: fmt.Sprintf(config, testRepoName, topicsAfter),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("github_repository.test", "topics.#", "3"),
),
},
},
})
Expand All @@ -429,20 +432,15 @@ func TestAccGithubRepository(t *testing.T) {
}
`, testRepoName, testAccConf.testPublicTemplateRepositoryOwner, testAccConf.testPublicTemplateRepository)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "is_template",
"false",
),
)

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnauthenticated(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("github_repository.test", "is_template", "false"),
),
},
},
})
Expand All @@ -464,20 +462,15 @@ func TestAccGithubRepository(t *testing.T) {
}
`, testRepoName, testAccConf.owner, testAccConf.testOrgTemplateRepository)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "is_template",
"false",
),
)

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("github_repository.test", "is_template", "false"),
),
},
},
})
Expand All @@ -495,34 +488,23 @@ func TestAccGithubRepository(t *testing.T) {
}
`, testRepoName)

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

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnauthenticated(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: checks["before"],
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("github_repository.test", "archived", "false"),
),
},
{
Config: strings.Replace(config,
`archived = false`,
`archived = true`, 1),
Check: checks["after"],
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("github_repository.test", "archived", "true"),
),
},
},
})
Expand Down Expand Up @@ -853,40 +835,23 @@ resource "github_repository" "test" {
`, testRepoName, updatedMergeCommitTitle, updatedMergeCommitMessage),
}

checks := map[string]resource.TestCheckFunc{
"before": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "merge_commit_title",
mergeCommitTitle,
),
resource.TestCheckResourceAttr(
"github_repository.test", "merge_commit_message",
mergeCommitMessage,
),
),
"after": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "merge_commit_title",
updatedMergeCommitTitle,
),
resource.TestCheckResourceAttr(
"github_repository.test", "merge_commit_message",
updatedMergeCommitMessage,
),
),
}

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnauthenticated(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: configs["before"],
Check: checks["before"],
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("github_repository.test", "merge_commit_title", mergeCommitTitle),
resource.TestCheckResourceAttr("github_repository.test", "merge_commit_message", mergeCommitMessage),
),
},
{
Config: configs["after"],
Check: checks["after"],
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("github_repository.test", "merge_commit_title", updatedMergeCommitTitle),
resource.TestCheckResourceAttr("github_repository.test", "merge_commit_message", updatedMergeCommitMessage),
),
},
},
})
Expand Down Expand Up @@ -922,24 +887,12 @@ resource "github_repository" "test" {

checks := map[string]resource.TestCheckFunc{
"before": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "squash_merge_commit_title",
squashMergeCommitTitle,
),
resource.TestCheckResourceAttr(
"github_repository.test", "squash_merge_commit_message",
squashMergeCommitMessage,
),
resource.TestCheckResourceAttr("github_repository.test", "squash_merge_commit_title", squashMergeCommitTitle),
resource.TestCheckResourceAttr("github_repository.test", "squash_merge_commit_message", squashMergeCommitMessage),
),
"after": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "squash_merge_commit_title",
updatedSquashMergeCommitTitle,
),
resource.TestCheckResourceAttr(
"github_repository.test", "squash_merge_commit_message",
updatedSquashMergeCommitMessage,
),
resource.TestCheckResourceAttr("github_repository.test", "squash_merge_commit_title", updatedSquashMergeCommitTitle),
resource.TestCheckResourceAttr("github_repository.test", "squash_merge_commit_message", updatedSquashMergeCommitMessage),
),
}

Expand Down