Skip to content

Commit 1bb8034

Browse files
github-actions[bot]vmvarela
authored andcommitted
Fix enterprise teams tests and docs
1 parent 208e1a7 commit 1bb8034

10 files changed

+88
-14
lines changed

github/acc_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ type testAccConfig struct {
4343
token string
4444

4545
// Enterprise configuration
46-
enterpriseSlug string
46+
enterpriseSlug string
47+
enterpriseTestOrganization string
48+
enterpriseTestUsers string
4749

4850
// Global test configuration
4951
testPublicRepository string
@@ -144,6 +146,8 @@ func TestMain(m *testing.M) {
144146

145147
if config.authMode == enterprise {
146148
config.enterpriseSlug = os.Getenv("GITHUB_ENTERPRISE_SLUG")
149+
config.enterpriseTestOrganization = os.Getenv("ENTERPRISE_TEST_ORGANIZATION")
150+
config.enterpriseTestUsers = os.Getenv("ENTERPRISE_TEST_USERS")
147151

148152
if len(config.enterpriseSlug) == 0 {
149153
fmt.Println("GITHUB_ENTERPRISE_SLUG environment variable not set")
@@ -178,6 +182,19 @@ func getTestMeta() (*Owner, error) {
178182
return meta.(*Owner), nil
179183
}
180184

185+
func splitCommaSeparated(v string) []string {
186+
parts := strings.Split(v, ",")
187+
out := make([]string, 0, len(parts))
188+
for _, p := range parts {
189+
p = strings.TrimSpace(p)
190+
if p == "" {
191+
continue
192+
}
193+
out = append(out, p)
194+
}
195+
return out
196+
}
197+
181198
func configureSweepers() {
182199
resource.AddTestSweepers("repositories", &resource.Sweeper{
183200
Name: "repositories",

github/data_source_github_enterprise_team_membership.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func dataSourceGithubEnterpriseTeamMembership() *schema.Resource {
1313
return &schema.Resource{
14-
Description: "Gets information about a user's membership in a GitHub enterprise team.",
14+
Description: "Retrieves information about a user's membership in a GitHub enterprise team.",
1515
ReadContext: dataSourceGithubEnterpriseTeamMembershipRead,
1616

1717
Schema: map[string]*schema.Schema{

github/data_source_github_enterprise_team_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ func TestAccGithubEnterpriseTeamDataSource(t *testing.T) {
5555
func TestAccGithubEnterpriseTeamOrganizationsDataSource(t *testing.T) {
5656
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
5757

58+
if testAccConf.enterpriseTestOrganization == "" {
59+
t.Skip("Skipping because `ENTERPRISE_TEST_ORGANIZATION` is not set")
60+
}
61+
5862
config := fmt.Sprintf(`
5963
data "github_enterprise" "enterprise" {
6064
slug = "%s"
@@ -77,7 +81,7 @@ func TestAccGithubEnterpriseTeamOrganizationsDataSource(t *testing.T) {
7781
team_slug = github_enterprise_team.test.slug
7882
depends_on = [github_enterprise_team_organizations.assign]
7983
}
80-
`, testAccConf.enterpriseSlug, testResourcePrefix, randomID, testAccConf.owner)
84+
`, testAccConf.enterpriseSlug, testResourcePrefix, randomID, testAccConf.enterpriseTestOrganization)
8185

8286
resource.Test(t, resource.TestCase{
8387
PreCheck: func() { skipUnlessMode(t, enterprise) },
@@ -88,7 +92,7 @@ func TestAccGithubEnterpriseTeamOrganizationsDataSource(t *testing.T) {
8892
Check: resource.ComposeAggregateTestCheckFunc(
8993
resource.TestCheckResourceAttrSet("data.github_enterprise_team_organizations.test", "id"),
9094
resource.TestCheckResourceAttr("data.github_enterprise_team_organizations.test", "organization_slugs.#", "1"),
91-
resource.TestCheckTypeSetElemAttr("data.github_enterprise_team_organizations.test", "organization_slugs.*", testAccConf.owner),
95+
resource.TestCheckTypeSetElemAttr("data.github_enterprise_team_organizations.test", "organization_slugs.*", testAccConf.enterpriseTestOrganization),
9296
),
9397
},
9498
},
@@ -97,7 +101,16 @@ func TestAccGithubEnterpriseTeamOrganizationsDataSource(t *testing.T) {
97101

98102
func TestAccGithubEnterpriseTeamMembershipDataSource(t *testing.T) {
99103
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
100-
username := testAccConf.username
104+
105+
if testAccConf.enterpriseTestUsers == "" {
106+
t.Skip("Skipping because `ENTERPRISE_TEST_USERS` is not set")
107+
}
108+
109+
users := splitCommaSeparated(testAccConf.enterpriseTestUsers)
110+
if len(users) == 0 {
111+
t.Skip("Skipping because `ENTERPRISE_TEST_USERS` must contain at least one username")
112+
}
113+
username := users[0]
101114

102115
config := fmt.Sprintf(`
103116
data "github_enterprise" "enterprise" {

github/resource_github_enterprise_team.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,12 @@ func resourceGithubEnterpriseTeamCreate(ctx context.Context, d *schema.ResourceD
9090

9191
req := github.EnterpriseTeamCreateOrUpdateRequest{
9292
Name: name,
93-
Description: github.Ptr(description),
9493
OrganizationSelectionType: github.Ptr(orgSelection),
9594
GroupID: github.Ptr(groupID), // Empty string is valid for no group
9695
}
96+
if description != "" {
97+
req.Description = github.Ptr(description)
98+
}
9799

98100
ctx = context.WithValue(ctx, ctxId, d.Id())
99101
te, _, err := client.Enterprise.CreateTeam(ctx, enterpriseSlug, req)
@@ -210,10 +212,12 @@ func resourceGithubEnterpriseTeamUpdate(ctx context.Context, d *schema.ResourceD
210212

211213
req := github.EnterpriseTeamCreateOrUpdateRequest{
212214
Name: name,
213-
Description: github.Ptr(description),
214215
OrganizationSelectionType: github.Ptr(orgSelection),
215216
GroupID: github.Ptr(groupID), // Empty string clears the group
216217
}
218+
if description != "" {
219+
req.Description = github.Ptr(description)
220+
}
217221

218222
ctx = context.WithValue(ctx, ctxId, d.Id())
219223
te, _, err := client.Enterprise.UpdateTeam(ctx, enterpriseSlug, teamSlug, req)

github/resource_github_enterprise_team_membership.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ func resourceGithubEnterpriseTeamMembershipRead(ctx context.Context, d *schema.R
114114
if err != nil {
115115
return diag.FromErr(err)
116116
}
117+
if v, ok := d.GetOk("team_id"); ok {
118+
teamID := int64(v.(int))
119+
if teamID > 0 {
120+
team, findErr := findEnterpriseTeamByID(ctx, client, enterpriseSlug, teamID)
121+
if findErr != nil {
122+
return diag.FromErr(findErr)
123+
}
124+
if team == nil {
125+
d.SetId("")
126+
return nil
127+
}
128+
teamSlug = team.Slug
129+
}
130+
}
117131

118132
// Get the membership using the SDK
119133
user, resp, err := client.Enterprise.GetTeamMembership(ctx, enterpriseSlug, teamSlug, username)
@@ -158,6 +172,19 @@ func resourceGithubEnterpriseTeamMembershipDelete(ctx context.Context, d *schema
158172
if err != nil {
159173
return diag.FromErr(err)
160174
}
175+
if v, ok := d.GetOk("team_id"); ok {
176+
teamID := int64(v.(int))
177+
if teamID > 0 {
178+
team, findErr := findEnterpriseTeamByID(ctx, client, enterpriseSlug, teamID)
179+
if findErr != nil {
180+
return diag.FromErr(findErr)
181+
}
182+
if team == nil {
183+
return nil
184+
}
185+
teamSlug = team.Slug
186+
}
187+
}
161188

162189
// Remove the user from the team using the SDK
163190
resp, err := client.Enterprise.RemoveTeamMember(ctx, enterpriseSlug, teamSlug, username)

github/resource_github_enterprise_team_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ func TestAccGithubEnterpriseTeam(t *testing.T) {
6767
func TestAccGithubEnterpriseTeamOrganizations(t *testing.T) {
6868
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
6969

70+
if testAccConf.enterpriseTestOrganization == "" {
71+
t.Skip("Skipping because `ENTERPRISE_TEST_ORGANIZATION` is not set")
72+
}
73+
7074
config1 := fmt.Sprintf(`
7175
data "github_enterprise" "enterprise" {
7276
slug = "%s"
@@ -83,11 +87,11 @@ func TestAccGithubEnterpriseTeamOrganizations(t *testing.T) {
8387
team_slug = github_enterprise_team.test.slug
8488
organization_slugs = ["%s"]
8589
}
86-
`, testAccConf.enterpriseSlug, randomID, testAccConf.owner)
90+
`, testAccConf.enterpriseSlug, randomID, testAccConf.enterpriseTestOrganization)
8791

8892
check1 := resource.ComposeAggregateTestCheckFunc(
8993
resource.TestCheckResourceAttr("github_enterprise_team_organizations.test", "organization_slugs.#", "1"),
90-
resource.TestCheckTypeSetElemAttr("github_enterprise_team_organizations.test", "organization_slugs.*", testAccConf.owner),
94+
resource.TestCheckTypeSetElemAttr("github_enterprise_team_organizations.test", "organization_slugs.*", testAccConf.enterpriseTestOrganization),
9195
)
9296

9397
resource.Test(t, resource.TestCase{
@@ -139,7 +143,16 @@ func TestAccGithubEnterpriseTeamOrganizations_emptyOrganizations(t *testing.T) {
139143

140144
func TestAccGithubEnterpriseTeamMembership(t *testing.T) {
141145
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
142-
username := testAccConf.username
146+
147+
if testAccConf.enterpriseTestUsers == "" {
148+
t.Skip("Skipping because `ENTERPRISE_TEST_USERS` is not set")
149+
}
150+
151+
users := splitCommaSeparated(testAccConf.enterpriseTestUsers)
152+
if len(users) == 0 {
153+
t.Skip("Skipping because `ENTERPRISE_TEST_USERS` must contain at least one username")
154+
}
155+
username := users[0]
143156

144157
config := fmt.Sprintf(`
145158
data "github_enterprise" "enterprise" {

website/docs/d/enterprise_team_membership.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
layout: "github"
3-
page_title: "Github: github_enterprise_team_membership"
3+
page_title: "GitHub: github_enterprise_team_membership"
44
description: |-
55
Check if a user is a member of a GitHub enterprise team.
66
---

website/docs/d/enterprise_team_organizations.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
layout: "github"
3-
page_title: "Github: github_enterprise_team_organizations"
3+
page_title: "GitHub: github_enterprise_team_organizations"
44
description: |-
55
Get organizations assigned to a GitHub enterprise team.
66
---

website/docs/r/enterprise_team_membership.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
layout: "github"
3-
page_title: "Github: github_enterprise_team_membership"
3+
page_title: "GitHub: github_enterprise_team_membership"
44
description: |-
55
Manages membership in a GitHub enterprise team.
66
---

website/docs/r/enterprise_team_organizations.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
layout: "github"
3-
page_title: "Github: github_enterprise_team_organizations"
3+
page_title: "GitHub: github_enterprise_team_organizations"
44
description: |-
55
Manages organization assignments for a GitHub enterprise team.
66
---

0 commit comments

Comments
 (0)