From 9d24eb42d25ae8cb93aee90a65ba26be89afb92d Mon Sep 17 00:00:00 2001 From: Felix Luthman <34520175+felixlut@users.noreply.github.com> Date: Thu, 11 Jan 2024 00:46:47 +0100 Subject: [PATCH] fix: remove repository topic from state if it doesnt exist in GitHub anymore (#1918) * remove repository topic if they cannot be found in GitHub anymore * Fix build error by bumping package version in offending file --------- Co-authored-by: Keegan Campbell --- github/resource_github_repository_topics.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/github/resource_github_repository_topics.go b/github/resource_github_repository_topics.go index e481042830..2d84370be6 100644 --- a/github/resource_github_repository_topics.go +++ b/github/resource_github_repository_topics.go @@ -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" ) @@ -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 }