Skip to content

fix: skip github_team_members deletion when parent team is already gone#3461

Open
nitinjain999 wants to merge 2 commits into
integrations:mainfrom
nitinjain999:fix/skip-team-members-delete-on-team-gone
Open

fix: skip github_team_members deletion when parent team is already gone#3461
nitinjain999 wants to merge 2 commits into
integrations:mainfrom
nitinjain999:fix/skip-team-members-delete-on-team-gone

Conversation

@nitinjain999
Copy link
Copy Markdown

@nitinjain999 nitinjain999 commented May 30, 2026

Resolves #3411


Before the change?

When github_team and github_team_members are destroyed together, the team deletion removes all members server-side. The subsequent github_team_members destroy makes N individual RemoveTeamMembershipByID API calls that all return 404 and fail loudly.

Additionally, if a team is deleted outside of Terraform, the next terraform plan or terraform refresh would not remove github_team_members from state — it would either error or silently return an empty member list, causing a perpetual diff.

After the change?

Delete: resourceGithubTeamMembersDelete checks for a 404 at both points — the getTeamID/getTeamSlug REST lookup and the RemoveTeamMembershipByID call — and treats either as a no-op, returning cleanly. Destroying github_team + github_team_members together no longer errors.

Read: resourceGithubTeamMembersRead now removes the resource from state when the parent team is gone. It handles two paths:

  • getTeamSlug 404 (REST) — team identified by slug or numeric ID that no longer exists
  • GraphQL organization.team returning null (detected via DatabaseId == 0) — team gone after slug resolution succeeded

An acceptance test is added that deletes the team out-of-band via PreConfig, then runs Destroy: true to exercise the delete path.

Pull request checklist

  • Schema migrations have been created if needed — not applicable, no schema changes
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed — no doc changes needed, no schema or user-visible behaviour change

Does this introduce a breaking change?

  • Yes
  • No

@github-actions
Copy link
Copy Markdown

👋 Hi, and thank you for this contribution!

This repo is maintained by GitHub and community members on a best-effort basis. We'll get to this as soon as we can.

You can help us prioritize by joining the discussion on open issues and PRs, sharing details on the changes you need, and reviewing other contributions.


🤖 This is an automated message.

@github-actions github-actions Bot added the Type: Bug Something isn't working as documented label May 30, 2026
@nitinjain999 nitinjain999 marked this pull request as ready for review May 30, 2026 19:59
@nitinjain999 nitinjain999 force-pushed the fix/skip-team-members-delete-on-team-gone branch from 013f2fd to 99e1360 Compare May 30, 2026 20:02
When a github_team is destroyed alongside github_team_members, the team
deletion removes all members server-side. Subsequent per-member API calls
return 404 and fail loudly.

Handle 404 from both getTeamID (slug-based lookup) and
RemoveTeamMembershipByID (numeric ID path) by treating them as a no-op
and returning early, consistent with how other resources handle missing
parents.

Fixes integrations#3411
@nitinjain999 nitinjain999 force-pushed the fix/skip-team-members-delete-on-team-gone branch from 99e1360 to 8b8f5f4 Compare May 30, 2026 20:17
@nitinjain999
Copy link
Copy Markdown
Author

Tested against a real GitHub org (nitin-sv) using make testacc T=TestAccGithubTeamMembers.

The new subtest succeeds_when_parent_team_is_deleted_out-of-band_before_destroy passes — team deleted via API before destroy, Terraform completes cleanly with no 404 error.

The existing creates_a_team_&_members_configured_with_defaults subtest fails in this environment because the test user is an org owner, and GitHub silently promotes owners to maintainer regardless of the requested role. This is a pre-existing issue unrelated to this change and documented in the provider notes.

@stevehipwell stevehipwell added this to the v6.13.0 milestone Jun 3, 2026
@deiga deiga requested a review from Copilot June 3, 2026 18:08
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

These provider review instructions are being used.

This PR improves the github_team_members resource’s behavior when its parent GitHub team has been deleted (either by Terraform ordering during destroy or out-of-band), by treating “team not found” as a graceful no-op in Delete and removing the resource from state in Read to avoid perpetual diffs.

Changes:

  • Update resourceGithubTeamMembersRead to remove github_team_members from state when the parent team no longer exists (REST 404 during slug lookup, or GraphQL organization.team resolving to null).
  • Update resourceGithubTeamMembersDelete to treat “team not found” as a successful no-op and avoid failing on 404s.
  • Add an acceptance test that deletes the team out-of-band before running destroy.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
github/resource_github_team_members.go Adds 404-aware Read/Delete behavior to gracefully handle missing parent teams and prevent drift/perpetual diffs.
github/resource_github_team_members_test.go Adds an acceptance test scenario for out-of-band team deletion prior to destroy.

Comment thread github/resource_github_team_members.go
Comment thread github/resource_github_team_members_test.go
…coverage

- Delete: on 404 from RemoveTeamMembershipByID, verify whether the team
  itself is gone before short-circuiting; if team still exists the
  membership was removed out-of-band so skip that user and continue
  the loop rather than skipping all remaining members
- Test: add RefreshState step after out-of-band team deletion to assert
  github_team_members is removed from state by Read, covering the new
  getTeamSlug 404 and DatabaseId==0 guard paths
@nitinjain999
Copy link
Copy Markdown
Author

Addressed both Copilot review comments:

Delete logic (line 297): On a 404 from RemoveTeamMembershipByID, the code now calls GetTeamByID to distinguish between "team is gone" and "just this membership was removed out-of-band". If the team is gone it short-circuits; if the team still exists it skips that one user and continues the loop.

Test coverage (line 102): Added a RefreshState: true step that deletes the team out-of-band via PreConfig, then asserts the refresh removes github_team_members from state cleanly — covering both the getTeamSlug 404 and DatabaseId==0 guard paths in Read. Verified passing locally against nitin-sv.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Type: Bug Something isn't working as documented

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MAINT]: Skip github_team_members deletion when the associated github_team is deleted

4 participants