Skip to content

feat: Refactor github_team_members - #3499

Merged
stevehipwell merged 8 commits into
integrations:mainfrom
stevehipwell:refactor-team-members
Jun 22, 2026
Merged

stevehipwell merged 8 commits into
integrations:mainfrom
stevehipwell:refactor-team-members

Conversation

@stevehipwell

@stevehipwell stevehipwell commented Jun 19, 2026 •

Copy link
Copy Markdown
Collaborator

Resolves #2731


Before the change?

  • The github_team_members resource had behavioural issues

After the change?

  • github_team_members has been refactored to follow the latest best practices
    • Docs updated
    • Improved test coverage
    • Set resource ID consistently to team ID
    • Use slug APIs
    • Import sets full state
  • Added team_slug to github_team_members and deprecated users setting team_id (this will be made computed in the next major version with it's type switched to int)
  • Fixed github_team_members team membership reconcile loop to not remove members when modifying membership and to handle maintainers first so as to not accidentally promote a non-maintainer

Pull request checklist

  • Schema migrations have been created if needed (example)
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)

Does this introduce a breaking change?

Please see our docs on breaking changes to help!

  • 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.

@stevehipwell
stevehipwell force-pushed the refactor-team-members branch from cc72363 to 8b21f72 Compare June 22, 2026 08:50
@stevehipwell
stevehipwell marked this pull request as ready for review June 22, 2026 08:50
@stevehipwell
stevehipwell force-pushed the refactor-team-members branch 2 times, most recently from ee9878d to 0fec66c Compare June 22, 2026 09:04
@stevehipwell stevehipwell self-assigned this Jun 22, 2026
@stevehipwell stevehipwell added Type: Bug Something isn't working as documented Type: Maintenance Any dependency, housekeeping, and clean up Issue or PR labels Jun 22, 2026
@stevehipwell stevehipwell added this to the v6.13.0 milestone Jun 22, 2026
@stevehipwell
stevehipwell requested review from Copilot and deiga June 22, 2026 10:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 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_members CRUD + reconcile loop to update roles in place and add team_slug, deprecating team_id, with a v0→v1 state migration and dynamic rename-safe ForceNew.
  • 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→Release spelling fix in acc_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.

Comment thread github/resource_github_team_members.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 29 out of 29 changed files in this pull request and generated 1 comment.

Comment thread github/acc_helpers_test.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 29 out of 29 changed files in this pull request and generated 1 comment.

Comment thread github/util_diff.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 29 out of 29 changed files in this pull request and generated 1 comment.

Comment thread github/acc_test.go Resolved
@stevehipwell
stevehipwell force-pushed the refactor-team-members branch from b48b263 to fc4bf8e Compare June 22, 2026 13:48

@robert-crandall robert-crandall left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 deiga left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would it make sense to add unit tests to the new util_* functions?

@stevehipwell

Copy link
Copy Markdown
Collaborator Author

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.

@stevehipwell
stevehipwell merged commit ee3ebd7 into integrations:main Jun 22, 2026
10 checks passed
@stevehipwell
stevehipwell deleted the refactor-team-members branch June 22, 2026 16:08
robert-crandall added a commit that referenced this pull request Jun 22, 2026
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]>
robert-crandall added a commit that referenced this pull request Jun 23, 2026
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]>
stevehipwell pushed a commit that referenced this pull request Jun 23, 2026
…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]>
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 Type: Maintenance Any dependency, housekeeping, and clean up Issue or PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: changing team role has side effects

4 participants