Repository navigation
feat: Refactor github_team_members - #3499
Conversation
|
👋 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. |
cc72363 to
8b21f72
Compare
ee9878d to
0fec66c
Compare
There was a problem hiding this comment.
Pull request overview
These provider review instructions are being used.
This PR refactors the github_team_members resource to fix issue #2731, where changing a member's role (e.g. member → maintainer) caused the provider to remove and re-add the user (briefly revoking access and unassigning PRs). The reconcile logic is rewritten to call AddTeamMembershipBySlug to update roles in place (rather than remove + re-add), to process maintainers first (so demotions don't trigger GitHub's auto-promotion of the last maintainer), and to only remove members not in the desired set. It also introduces a team_slug attribute, deprecates user-set team_id (now Optional/Computed with dynamic ForceNew via diffLegacyTeam/diffTeam), switches the resource ID to the numeric team ID, adds a v0→v1 state migration, makes import populate full state, and refreshes docs/examples. A large set of test env vars are renamed (testOrgUser → testOrgUser1, testExternalUser → testExternalUser1, plus a testPublicRelaseAsset* → testPublicReleaseAsset* typo fix), touching many test files.
Changes:
- Rewrote
github_team_membersCRUD + reconcile loop to update roles in place and addteam_slug, deprecatingteam_id, with a v0→v1 state migration and dynamic rename-safeForceNew. - Added/rewrote acceptance tests (modern
statecheck/plancheck), migration unit tests, and team test helpers; rewrote docs/template/examples. - Repo-wide rename of test configuration env vars and a
Relase→Releasespelling fix inacc_test.go.
Reviewed changes
Copilot reviewed 29 out of 29 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| github/resource_github_team_members.go | Core refactor: schema (team_slug/deprecated team_id), CRUD, customdiff, and updateTeamMembers reconcile (contains the case-handling bug). |
| github/resource_github_team_members_migration.go | New v0→v1 state upgrader resolving team ID/slug. |
| github/resource_github_team_members_migration_test.go | Unit tests for the state upgrader. |
| github/util_diff.go | New diffLegacyTeam/reworked diffTeam for rename-safe replacement logic. |
| github/util_user.go | New userMember/userMembers types, flatten, and newUserMembers. |
| github/util_team.go | New lookupTeamSlug helper. |
| github/resource_github_team_members_test.go | Rewritten acceptance tests using statecheck/plancheck. |
| github/acc_helpers_test.go | New team test helpers (mustCreateTestTeam, rename/delete/add member). |
| github/acc_test.go | Renamed test env-var fields, new skip helpers, commented-out unused OrgUser3 helper. |
| github/*_test.go (membership, collaborator(s), users, etc.) | Mechanical updates to renamed test config fields. |
| docs/resources/team_members.md, templates/resources/team_members.md.tmpl | Regenerated docs/template with new schema, notes, and import guidance. |
| examples/resources/github_team_members/* | New example, import shell, and import-by-id config; old team_members/example_1.tf removed. |
| CONTRIBUTING.md | Documented renamed test env vars. |
Key finding: updateTeamMembers keys the want/lookup maps inconsistently — currentMembers logins are lowercased (from getTeamMembers) while wantMembers logins keep the config casing. For any managed user whose GitHub login has uppercase letters, the member is added and then immediately removed in the same apply, causing data loss and a perpetual diff (re-triggering the very churn #2731 fixes). A fix is suggested inline. Note this is a high-risk change overall: it alters schema semantics (team_id Required→Optional/Computed/Deprecated), the resource ID format, and includes a state migration plus dynamic ForceNew, none of which I can validate via acceptance tests here.
7225a19 to
45a5b82
Compare
Signed-off-by: Steve Hipwell <[email protected]>
b48b263 to
fc4bf8e
Compare
robert-crandall
left a comment
There was a problem hiding this comment.
Really nice work on this - the maintainer-first reconcile and the authoritative membership fix are clearly thought through, and I appreciate the thorough acceptance coverage (rename, delete, out-of-band members, and the slug/id migration paths). The deprecation + state upgrader keep it backward compatible too. Thanks for taking the time to do this properly!
deiga
left a comment
There was a problem hiding this comment.
Would it make sense to add unit tests to the new util_* functions?
@deiga they're on my list. They currently get tested via the acceptance tests and I'm pretty sure we could come up with a better API once we've got a few more resources uisng the pattern. So my thoughts are that if we hold off a bit and focus on the resources we can revisit the utils later and get a better turn on the time investment. |
The Read path (getTeamMembers) used the REST list-team-members endpoint, which always includes members inherited from child teams and has no immediate-only option. For organizations with nested teams this surfaced inherited members as drift, and broke the github_team_members member set that is meant to be authoritative. Switch getTeamMembers to the GraphQL API with membership:IMMEDIATE so only direct members are returned. A null team node is mapped to a 404 github.ErrorResponse so the existing Read/Delete handling still removes the resource from state when the team is deleted out-of-band. getTeamMembers and updateTeamMembers now take *Owner so they can reach the v4 (GraphQL) client for reads while still using the v3 client for membership add/remove. Fixes the team_with_child_team regression test added in #3499. Co-authored-by: Copilot <[email protected]>
The Read path (getTeamMembers) used the REST list-team-members endpoint, which always includes members inherited from child teams and has no immediate-only option. For organizations with nested teams this surfaced inherited members as drift, and broke the github_team_members member set that is meant to be authoritative. Switch getTeamMembers to the GraphQL API with membership:IMMEDIATE so only direct members are returned. A null team node is mapped to a 404 github.ErrorResponse so the existing Read/Delete handling still removes the resource from state when the team is deleted out-of-band. getTeamMembers and updateTeamMembers now take *Owner so they can reach the v4 (GraphQL) client for reads while still using the v3 client for membership add/remove. Fixes the team_with_child_team regression test added in #3499. Co-authored-by: Copilot <[email protected]>
…3498) The Read path (getTeamMembers) used the REST list-team-members endpoint, which always includes members inherited from child teams and has no immediate-only option. For organizations with nested teams this surfaced inherited members as drift, and broke the github_team_members member set that is meant to be authoritative. Switch getTeamMembers to the GraphQL API with membership:IMMEDIATE so only direct members are returned. A null team node is mapped to a 404 github.ErrorResponse so the existing Read/Delete handling still removes the resource from state when the team is deleted out-of-band. getTeamMembers and updateTeamMembers now take *Owner so they can reach the v4 (GraphQL) client for reads while still using the v3 client for membership add/remove. Fixes the team_with_child_team regression test added in #3499. Co-authored-by: Copilot <[email protected]>
Resolves #2731
Before the change?
github_team_membersresource had behavioural issuesAfter the change?
github_team_membershas been refactored to follow the latest best practicesteam_slugtogithub_team_membersand deprecated users settingteam_id(this will be made computed in the next major version with it's type switched toint)github_team_membersteam membership reconcile loop to not remove members when modifying membership and to handle maintainers first so as to not accidentally promote a non-maintainerPull request checklist
Does this introduce a breaking change?
Please see our docs on breaking changes to help!