Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove repository topic from state if it doesnt exist in GitHub anymore #1918

Merged
merged 4 commits into from
Jan 10, 2024
Merged
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
14 changes: 14 additions & 0 deletions github/resource_github_repository_topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package github

import (
"context"
"log"
"net/http"
"regexp"

"github.com/google/go-github/v57/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
Expand Down Expand Up @@ -68,6 +71,17 @@ func resourceGithubRepositoryTopicsRead(d *schema.ResourceData, meta interface{}

topics, _, err := client.Repositories.ListAllTopics(ctx, owner, repoName)
if err != nil {
if ghErr, ok := err.(*github.ErrorResponse); ok {
if ghErr.Response.StatusCode == http.StatusNotModified {
return nil
}
if ghErr.Response.StatusCode == http.StatusNotFound {
log.Printf("[INFO] Removing topics from repository %s/%s from state because it no longer exists in GitHub",
owner, repoName)
d.SetId("")
return nil
}
}
return err
}

Expand Down