@@ -94,6 +94,37 @@ async function findIdpGroupWithRetry({
9494 return null ;
9595}
9696
97+ async function resolveTeamIdToSlug ( {
98+ octokit,
99+ orgId,
100+ teamId,
101+ logger,
102+ } : {
103+ octokit : Octokit ;
104+ orgId : string ;
105+ teamId : number ;
106+ logger : ValidLoggers ;
107+ } ) : Promise < string > {
108+ try {
109+ logger . info ( `Resolving team ID ${ teamId } to slug` ) ;
110+ const response = await octokit . request ( "GET /orgs/{org}/teams/{team_id}" , {
111+ org : orgId ,
112+ team_id : teamId ,
113+ headers : {
114+ "X-GitHub-Api-Version" : "2022-11-28" ,
115+ } ,
116+ } ) ;
117+ const slug = response . data . slug ;
118+ logger . info ( `Resolved team ID ${ teamId } to slug: ${ slug } ` ) ;
119+ return slug ;
120+ } catch ( error ) {
121+ logger . error ( `Failed to resolve team ID ${ teamId } to slug:` , error ) ;
122+ throw new GithubError ( {
123+ message : `Failed to resolve team ID ${ teamId } to slug` ,
124+ } ) ;
125+ }
126+ }
127+
97128export async function createGithubTeam ( {
98129 githubToken,
99130 orgId,
@@ -160,7 +191,6 @@ export async function createGithubTeam({
160191 ) ;
161192 } catch ( removeError ) {
162193 logger . warn ( `Failed to remove user from team ${ newTeamId } :` , removeError ) ;
163- // Don't throw here - team was created successfully
164194 }
165195
166196 return newTeamId ;
@@ -199,7 +229,40 @@ export async function assignIdpGroupsToTeam({
199229 return ;
200230 }
201231
202- // Search for IdP groups with retry logic
232+ const teamSlug = await resolveTeamIdToSlug ( {
233+ octokit,
234+ orgId,
235+ teamId,
236+ logger,
237+ } ) ;
238+
239+ try {
240+ logger . info ( `Checking team sync availability for team ${ teamSlug } ` ) ;
241+ await octokit . request (
242+ "GET /orgs/{org}/teams/{team_slug}/team-sync/group-mappings" ,
243+ {
244+ org : orgId ,
245+ team_slug : teamSlug ,
246+ headers : {
247+ "X-GitHub-Api-Version" : "2022-11-28" ,
248+ } ,
249+ } ,
250+ ) ;
251+ logger . info ( "Team sync is available for this team" ) ;
252+ } catch ( checkError : any ) {
253+ if ( checkError . status === 404 ) {
254+ logger . warn (
255+ `Team sync is not available for team ${ teamSlug } . This could mean:
256+ 1. The organization doesn't have SAML SSO properly configured
257+ 2. Team sync feature is not enabled for this organization
258+ 3. The team was just created and sync isn't ready yet` ,
259+ ) ;
260+ logger . warn ( "Skipping IdP group assignment" ) ;
261+ return ;
262+ }
263+ throw checkError ;
264+ }
265+
203266 const idpGroups = [ ] ;
204267 for ( const groupId of groupsToSync ) {
205268 const idpGroup = await findIdpGroupWithRetry ( {
@@ -221,25 +284,32 @@ export async function assignIdpGroupsToTeam({
221284 idpGroups . push ( idpGroup ) ;
222285 }
223286
224- // Add IdP group mappings to team
225- logger . info ( `Mapping ${ idpGroups . length } IdP group(s) to team ${ teamId } ` ) ;
287+ logger . info ( `Mapping ${ idpGroups . length } IdP group(s) to team ${ teamSlug } ` ) ;
226288 await octokit . request (
227- "PATCH /orgs/{org}/teams/{team_id }/team-sync/group-mappings" ,
289+ "PATCH /orgs/{org}/teams/{team_slug }/team-sync/group-mappings" ,
228290 {
229291 org : orgId ,
230- team_id : teamId ,
292+ team_slug : teamSlug ,
231293 groups : idpGroups ,
232294 headers : {
233295 "X-GitHub-Api-Version" : "2022-11-28" ,
234296 } ,
235297 } ,
236298 ) ;
237299
238- logger . info ( `Successfully mapped IdP groups to team ${ teamId } ` ) ;
239- } catch ( e ) {
300+ logger . info ( `Successfully mapped IdP groups to team ${ teamSlug } ` ) ;
301+ } catch ( e : any ) {
240302 if ( e instanceof BaseError ) {
241303 throw e ;
242304 }
305+
306+ if ( e . status === 404 ) {
307+ logger . warn (
308+ `Team sync endpoint not available for team ${ teamId } . IdP groups were not assigned.` ,
309+ ) ;
310+ return ;
311+ }
312+
243313 logger . error ( `Failed to assign IdP groups to team ${ teamId } ` ) ;
244314 logger . error ( e ) ;
245315 throw new GithubError ( {
0 commit comments