Skip to content

Commit

Permalink
fix: remove repository topic from state if it doesnt exist in GitHub …
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
felixlut and kfcampbell authored Jan 10, 2024
1 parent 2dddeb3 commit 9d24eb4
Showing 1 changed file with 14 additions and 0 deletions.
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

0 comments on commit 9d24eb4

Please sign in to comment.