From 07f70f41331555fff9c0faef29a455a25b84fdda Mon Sep 17 00:00:00 2001 From: Kevin Zhao Date: Tue, 3 Aug 2021 12:12:04 -0700 Subject: [PATCH] Handle repo rename edge case for team repository deletions --- github/resource_github_team_repository.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/github/resource_github_team_repository.go b/github/resource_github_team_repository.go index 4710452ca6..969b3f1921 100644 --- a/github/resource_github_team_repository.go +++ b/github/resource_github_team_repository.go @@ -210,6 +210,23 @@ func resourceGithubTeamRepositoryDelete(d *schema.ResourceData, meta interface{} log.Printf("[DEBUG] Deleting team repository association: %s (%s/%s)", teamIdString, orgName, repoName) - _, err = client.Teams.RemoveTeamRepoByID(ctx, orgId, teamId, orgName, repoName) + resp, err := client.Teams.RemoveTeamRepoByID(ctx, orgId, teamId, orgName, repoName) + + if resp.Response.StatusCode == 404 { + log.Printf("[DEBUG] Failed to find team %s to delete for repo: %s.", teamIdString, repoName) + repo, _, err := client.Repositories.Get(ctx, orgName, repoName) + if err != nil { + return err + } + newRepoName := repo.GetName() + if newRepoName != repoName { + log.Printf("[DEBUG] Repo name has changed %s -> %s. "+ + "Try deleting team repository again.", + repoName, newRepoName) + _, err := client.Teams.RemoveTeamRepoByID(ctx, orgId, teamId, orgName, newRepoName) + return err + } + } + return err }