From 27f3db12327b63efff80417a2a1a3d36a233a06e Mon Sep 17 00:00:00 2001 From: Ori Rawlings Date: Fri, 20 Mar 2026 17:04:30 -0500 Subject: [PATCH] feat: Add `github_team_external_groups` data source Fixes #3295 --- ...data_source_github_team_external_groups.go | 82 +++++++++++++++++++ ...source_github_team_external_groups_test.go | 62 ++++++++++++++ github/provider.go | 1 + website/docs/d/external_groups.html.markdown | 10 +-- .../docs/d/team_external_groups.html.markdown | 33 ++++++++ website/github.erb | 3 + 6 files changed, 184 insertions(+), 7 deletions(-) create mode 100644 github/data_source_github_team_external_groups.go create mode 100644 github/data_source_github_team_external_groups_test.go create mode 100644 website/docs/d/team_external_groups.html.markdown diff --git a/github/data_source_github_team_external_groups.go b/github/data_source_github_team_external_groups.go new file mode 100644 index 0000000000..52208dd4f5 --- /dev/null +++ b/github/data_source_github_team_external_groups.go @@ -0,0 +1,82 @@ +package github + +import ( + "context" + "encoding/json" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceGithubTeamExternalGroups() *schema.Resource { + return &schema.Resource{ + Description: "Retrieve external groups for a specific GitHub team.", + ReadContext: dataSourceGithubTeamExternalGroupsRead, + Schema: map[string]*schema.Schema{ + "slug": { + Type: schema.TypeString, + Required: true, + Description: "The slug of the GitHub team.", + }, + "external_groups": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "group_id": { + Type: schema.TypeInt, + Computed: true, + }, + "group_name": { + Type: schema.TypeString, + Computed: true, + }, + "updated_at": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func dataSourceGithubTeamExternalGroupsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { + err := checkOrganization(meta) + if err != nil { + return diag.FromErr(err) + } + client := meta.(*Owner).v3client + orgName := meta.(*Owner).name + slug := d.Get("slug").(string) + + externalGroups, _, err := client.Teams.ListExternalGroupsForTeamBySlug(ctx, orgName, slug) + if err != nil { + return diag.FromErr(err) + } + + // convert to JSON in order to marshal to format we can return + jsonGroups, err := json.Marshal(externalGroups.Groups) + if err != nil { + return diag.FromErr(err) + } + + groupsState := make([]map[string]any, 0) + err = json.Unmarshal(jsonGroups, &groupsState) + if err != nil { + return diag.FromErr(err) + } + + if err := d.Set("external_groups", groupsState); err != nil { + return diag.FromErr(err) + } + + id, err := buildID(orgName, slug) + if err != nil { + return diag.FromErr(err) + } + d.SetId(id) + + return nil +} diff --git a/github/data_source_github_team_external_groups_test.go b/github/data_source_github_team_external_groups_test.go new file mode 100644 index 0000000000..9ced8a827c --- /dev/null +++ b/github/data_source_github_team_external_groups_test.go @@ -0,0 +1,62 @@ +package github + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/acctest" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/knownvalue" + "github.com/hashicorp/terraform-plugin-testing/statecheck" + "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" +) + +func TestAccGithubTeamExternalGroupsDataSource(t *testing.T) { + + t.Run("errors when querying a non-existing team", func(t *testing.T) { + config := ` + data "github_team_external_groups" "test" { + slug = "non-existing-team-slug" + } + ` + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + ExpectError: regexp.MustCompile(`Not Found`), + }, + }, + }) + }) + + t.Run("returns empty list for team without external groups", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + teamName := fmt.Sprintf("%steam-%s", testResourcePrefix, randomID) + config := fmt.Sprintf(` + resource "github_team" "test" { + name = "%s" + } + + data "github_team_external_groups" "test" { + slug = github_team.test.slug + } + `, teamName) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("data.github_team_external_groups.test", tfjsonpath.New("external_groups"), knownvalue.ListSizeExact(0)), + }, + }, + }, + }) + }) +} diff --git a/github/provider.go b/github/provider.go index 2d5d6dc33a..1c061e9548 100644 --- a/github/provider.go +++ b/github/provider.go @@ -289,6 +289,7 @@ func Provider() *schema.Provider { "github_rest_api": dataSourceGithubRestApi(), "github_ssh_keys": dataSourceGithubSshKeys(), "github_team": dataSourceGithubTeam(), + "github_team_external_groups": dataSourceGithubTeamExternalGroups(), "github_tree": dataSourceGithubTree(), "github_user": dataSourceGithubUser(), "github_user_external_identity": dataSourceGithubUserExternalIdentity(), diff --git a/website/docs/d/external_groups.html.markdown b/website/docs/d/external_groups.html.markdown index 0560dd00cd..4f2c9d3e3b 100644 --- a/website/docs/d/external_groups.html.markdown +++ b/website/docs/d/external_groups.html.markdown @@ -30,11 +30,7 @@ N/A. This resource will retrieve all the external groups belonging to an organiz ## Attributes Reference * `external_groups` - an array of external groups belonging to the organization. Each group consists of the fields documented below. - -___ - - - * `group_id` - the ID of the group. - * `group_name` - the name of the group. - * `updated_at` - the date the group was last updated. + * `group_id` - the ID of the group. + * `group_name` - the name of the group. + * `updated_at` - the date the group was last updated. diff --git a/website/docs/d/team_external_groups.html.markdown b/website/docs/d/team_external_groups.html.markdown new file mode 100644 index 0000000000..eb3cfbf34a --- /dev/null +++ b/website/docs/d/team_external_groups.html.markdown @@ -0,0 +1,33 @@ +--- +layout: "github" +page_title: "GitHub: github_team_external_groups" +description: |- + Retrieve external groups for a specific GitHub team. +--- + +# github\_team\_external\_groups + +Use this data source to retrieve external groups for a specific GitHub team. + +## Example Usage + +```hcl +data "github_team_external_groups" "example" { + slug = "example" +} + +output "groups" { + value = data.github_team_external_groups.example.external_groups +} +``` + +## Argument Reference + +* `slug` - (Required) The slug of the GitHub team. + +## Attributes Reference + +* `external_groups` - An array of external groups for the team. Each group consists of the fields documented below. + * `group_id` - The ID of the external group. + * `group_name` - The name of the external group. + * `updated_at` - The date the group was last updated. diff --git a/website/github.erb b/website/github.erb index 997536b42f..093acf6bc4 100644 --- a/website/github.erb +++ b/website/github.erb @@ -205,6 +205,9 @@
  • github_team
  • +
  • + github_team_external_groups +
  • github_user