From b42fa11adcced2e0c56a3742b7ac898b4ce50a11 Mon Sep 17 00:00:00 2001 From: Paul Tyng Date: Wed, 5 Feb 2020 12:37:50 -0500 Subject: [PATCH] Handle repo 404 gracefully fixes #343 --- github/resource_github_repository_collaborator.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/github/resource_github_repository_collaborator.go b/github/resource_github_repository_collaborator.go index 4dbe201d34..f6127b231d 100644 --- a/github/resource_github_repository_collaborator.go +++ b/github/resource_github_repository_collaborator.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log" + "net/http" "strings" "github.com/google/go-github/v28/github" @@ -97,8 +98,19 @@ func resourceGithubRepositoryCollaboratorRead(d *schema.ResourceData, meta inter // First, check if the user has been invited but has not yet accepted invitation, err := findRepoInvitation(client, ctx, orgName, repoName, username) if err != nil { + if ghErr, ok := err.(*github.ErrorResponse); ok { + if ghErr.Response.StatusCode == http.StatusNotFound { + // this short circuits the rest of the code because if the + // repo is 404, no reason to try to list existing collaborators + log.Printf("[WARN] Removing repository collaborator %s/%s %s from state because it no longer exists in GitHub", + orgName, repoName, username) + d.SetId("") + return nil + } + } return err - } else if invitation != nil { + } + if invitation != nil { username = *invitation.Invitee.Login log.Printf("[DEBUG] Found invitation for %q", username)