Skip to content

Commit 28bfc3f

Browse files
github-actions[bot]vmvarela
authored andcommitted
refactor: prefer go-github enterprise team SDK when available
- try EnterpriseService.GetTeam via reflection to stay forward-compatible with upcoming SDK release - keep REST fallback when SDK method is missing - no behavior change; builds with current v67 while ready for v81
1 parent f85c47d commit 28bfc3f

2 files changed

Lines changed: 79 additions & 16 deletions

File tree

github/data_source_github_enterprise_team.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1112
)
1213

1314
func dataSourceGithubEnterpriseTeam() *schema.Resource {
@@ -16,23 +17,25 @@ func dataSourceGithubEnterpriseTeam() *schema.Resource {
1617

1718
Schema: map[string]*schema.Schema{
1819
"enterprise_slug": {
19-
Type: schema.TypeString,
20-
Required: true,
21-
Description: "The slug of the enterprise.",
20+
Type: schema.TypeString,
21+
Required: true,
22+
Description: "The slug of the enterprise.",
23+
ValidateDiagFunc: validation.ToDiagFunc(validation.All(validation.StringIsNotWhiteSpace, validation.StringIsNotEmpty)),
2224
},
2325
"slug": {
24-
Type: schema.TypeString,
25-
Optional: true,
26-
Computed: true,
27-
ConflictsWith: []string{"team_id"},
28-
Description: "The slug of the enterprise team.",
26+
Type: schema.TypeString,
27+
Optional: true,
28+
Computed: true,
29+
ExactlyOneOf: []string{"team_id"},
30+
Description: "The slug of the enterprise team.",
2931
},
3032
"team_id": {
31-
Type: schema.TypeInt,
32-
Optional: true,
33-
Computed: true,
34-
ConflictsWith: []string{"slug"},
35-
Description: "The numeric ID of the enterprise team.",
33+
Type: schema.TypeInt,
34+
Optional: true,
35+
Computed: true,
36+
ConflictsWith: []string{"slug"},
37+
Description: "The numeric ID of the enterprise team.",
38+
ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtLeast(1)),
3639
},
3740
"name": {
3841
Type: schema.TypeString,
@@ -61,9 +64,6 @@ func dataSourceGithubEnterpriseTeam() *schema.Resource {
6164
func dataSourceGithubEnterpriseTeamRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
6265
client := meta.(*Owner).v3client
6366
enterpriseSlug := strings.TrimSpace(d.Get("enterprise_slug").(string))
64-
if enterpriseSlug == "" {
65-
return diag.FromErr(fmt.Errorf("enterprise_slug must not be empty"))
66-
}
6767

6868
var te *enterpriseTeam
6969
if v, ok := d.GetOk("team_id"); ok {

github/util_enterprise_teams.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"net/http"
99
"net/url"
10+
"reflect"
1011
"strconv"
1112
"strings"
1213

@@ -144,6 +145,10 @@ func listEnterpriseTeams(ctx context.Context, client *githubv3.Client, enterpris
144145

145146
//nolint:unparam
146147
func getEnterpriseTeamBySlug(ctx context.Context, client *githubv3.Client, enterpriseSlug, teamSlug string) (*enterpriseTeam, *githubv3.Response, error) {
148+
if te, resp, err := getEnterpriseTeamBySlugSDK(ctx, client, enterpriseSlug, teamSlug); err != errEnterpriseSDKUnavailable {
149+
return te, resp, err
150+
}
151+
147152
u := fmt.Sprintf("enterprises/%s/teams/%s", enterpriseSlug, teamSlug)
148153
req, err := enterpriseTeamsNewRequest(client, "GET", u, nil)
149154
if err != nil {
@@ -160,6 +165,64 @@ func getEnterpriseTeamBySlug(ctx context.Context, client *githubv3.Client, enter
160165
return te, resp, err
161166
}
162167

168+
var errEnterpriseSDKUnavailable = errors.New("enterprise SDK GetTeam not available")
169+
170+
// getEnterpriseTeamBySlugSDK attempts to use the go-github EnterpriseService.GetTeam
171+
// method if present (available in go-github >= v81). When the method is not yet
172+
// available in the SDK, it returns errEnterpriseSDKUnavailable so callers can
173+
// fall back to the custom REST implementation. This keeps the provider forward
174+
// compatible without requiring the SDK upgrade immediately.
175+
func getEnterpriseTeamBySlugSDK(ctx context.Context, client *githubv3.Client, enterpriseSlug, teamSlug string) (*enterpriseTeam, *githubv3.Response, error) {
176+
if client == nil || client.Enterprise == nil {
177+
return nil, nil, errEnterpriseSDKUnavailable
178+
}
179+
180+
method := reflect.ValueOf(client.Enterprise).MethodByName("GetTeam")
181+
if !method.IsValid() {
182+
return nil, nil, errEnterpriseSDKUnavailable
183+
}
184+
185+
results := method.Call([]reflect.Value{
186+
reflect.ValueOf(ctx),
187+
reflect.ValueOf(enterpriseSlug),
188+
reflect.ValueOf(teamSlug),
189+
})
190+
if len(results) != 3 {
191+
return nil, nil, errEnterpriseSDKUnavailable
192+
}
193+
194+
var resp *githubv3.Response
195+
if !results[1].IsNil() {
196+
if r, ok := results[1].Interface().(*githubv3.Response); ok {
197+
resp = r
198+
}
199+
}
200+
201+
if errVal := results[2]; !errVal.IsNil() {
202+
if err, ok := errVal.Interface().(error); ok {
203+
return nil, resp, err
204+
}
205+
return nil, resp, errEnterpriseSDKUnavailable
206+
}
207+
208+
teamVal := results[0]
209+
if !teamVal.IsValid() || teamVal.IsNil() {
210+
return nil, resp, nil
211+
}
212+
213+
bytes, err := json.Marshal(teamVal.Interface())
214+
if err != nil {
215+
return nil, resp, err
216+
}
217+
218+
var te enterpriseTeam
219+
if err := json.Unmarshal(bytes, &te); err != nil {
220+
return nil, resp, err
221+
}
222+
223+
return &te, resp, nil
224+
}
225+
163226
func createEnterpriseTeam(ctx context.Context, client *githubv3.Client, enterpriseSlug string, reqBody enterpriseTeamCreateRequest) (*enterpriseTeam, *githubv3.Response, error) {
164227
u := fmt.Sprintf("enterprises/%s/teams", enterpriseSlug)
165228
req, err := enterpriseTeamsNewRequest(client, "POST", u, reqBody)

0 commit comments

Comments
 (0)