Skip to content

Commit 5c81985

Browse files
committed
Update github function to use the numerical team ID
1 parent 8700f05 commit 5c81985

File tree

2 files changed

+18
-115
lines changed

2 files changed

+18
-115
lines changed

src/api/functions/github.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -234,20 +234,13 @@ export async function assignIdpGroupsToTeam({
234234
return;
235235
}
236236

237-
const teamSlug = await resolveTeamIdToSlug({
238-
octokit,
239-
orgId,
240-
teamId,
241-
logger,
242-
});
243-
244237
try {
245-
logger.info(`Checking team sync availability for team ${teamSlug}`);
238+
logger.info(`Checking team sync availability for team ${teamId}`);
246239
await octokit.request(
247-
"GET /orgs/{org}/teams/{team_slug}/team-sync/group-mappings",
240+
"GET /organizations/{org}/team/{team_id}/team-sync/group-mappings",
248241
{
249242
org: orgId,
250-
team_slug: teamSlug,
243+
team_id: teamId,
251244
headers: {
252245
"X-GitHub-Api-Version": "2022-11-28",
253246
},
@@ -257,7 +250,7 @@ export async function assignIdpGroupsToTeam({
257250
} catch (checkError: any) {
258251
if (checkError.status === 404) {
259252
logger.warn(
260-
`Team sync is not available for team ${teamSlug}. This could mean:
253+
`Team sync is not available for team ${teamId}. This could mean:
261254
1. The organization doesn't have SAML SSO properly configured
262255
2. Team sync feature is not enabled for this organization
263256
3. The team was just created and sync isn't ready yet`,
@@ -289,20 +282,20 @@ export async function assignIdpGroupsToTeam({
289282
idpGroups.push(idpGroup);
290283
}
291284

292-
logger.info(`Mapping ${idpGroups.length} IdP group(s) to team ${teamSlug}`);
285+
logger.info(`Mapping ${idpGroups.length} IdP group(s) to team ${teamId}`);
293286
await octokit.request(
294-
"PATCH /orgs/{org}/teams/{team_slug}/team-sync/group-mappings",
287+
"PATCH /organizations/{org}/team/{team_id}/team-sync/group-mappings",
295288
{
296289
org: orgId,
297-
team_slug: teamSlug,
290+
team_id: teamId,
298291
groups: idpGroups,
299292
headers: {
300293
"X-GitHub-Api-Version": "2022-11-28",
301294
},
302295
},
303296
);
304297

305-
logger.info(`Successfully mapped IdP groups to team ${teamSlug}`);
298+
logger.info(`Successfully mapped IdP groups to team ${teamId}`);
306299
} catch (e: any) {
307300
if (e instanceof BaseError) {
308301
throw e;

tests/unit/functions/github.test.ts

Lines changed: 10 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ describe("assignIdpGroupsToTeam", () => {
204204
teamId: 123,
205205
groupsToSync: ["group-1", "group-2"],
206206
logger: mockLogger,
207-
orgId: "test-org",
207+
orgId: 456,
208+
orgName: "test-org",
208209
};
209210

210211
let mockOctokit: any;
@@ -256,11 +257,6 @@ describe("assignIdpGroupsToTeam", () => {
256257
},
257258
];
258259

259-
// Mock team ID to slug resolution
260-
mockOctokit.request.mockResolvedValueOnce({
261-
data: { slug: "test-team" },
262-
});
263-
264260
// Mock team sync availability check
265261
mockOctokit.request.mockResolvedValueOnce({});
266262

@@ -279,18 +275,18 @@ describe("assignIdpGroupsToTeam", () => {
279275
await assignIdpGroupsToTeam(defaultInputs);
280276

281277
expect(mockOctokit.request).toHaveBeenCalledWith(
282-
"PATCH /orgs/{org}/teams/{team_slug}/team-sync/group-mappings",
278+
"PATCH /organizations/{org}/team/{team_id}/team-sync/group-mappings",
283279
{
284-
org: "test-org",
285-
team_slug: "test-team",
280+
org: 456,
281+
team_id: 123,
286282
groups: mockGroups,
287283
headers: {
288284
"X-GitHub-Api-Version": "2022-11-28",
289285
},
290286
}
291287
);
292288
expect(mockLogger.info).toHaveBeenCalledWith(
293-
"Successfully mapped IdP groups to team test-team"
289+
"Successfully mapped IdP groups to team 123"
294290
);
295291
});
296292

@@ -301,11 +297,6 @@ describe("assignIdpGroupsToTeam", () => {
301297
group_description: "First group",
302298
};
303299

304-
// Mock team ID to slug resolution
305-
mockOctokit.request.mockResolvedValueOnce({
306-
data: { slug: "test-team" },
307-
});
308-
309300
// Mock team sync availability check
310301
mockOctokit.request.mockResolvedValueOnce({});
311302

@@ -328,11 +319,6 @@ describe("assignIdpGroupsToTeam", () => {
328319
});
329320

330321
it("should throw GithubError if IdP group not found after max retries", async () => {
331-
// Mock team ID to slug resolution
332-
mockOctokit.request.mockResolvedValueOnce({
333-
data: { slug: "test-team" },
334-
});
335-
336322
// Mock team sync availability check
337323
mockOctokit.request.mockResolvedValueOnce({});
338324

@@ -345,7 +331,7 @@ describe("assignIdpGroupsToTeam", () => {
345331
expect(mockLogger.error).toHaveBeenCalledWith(
346332
"Failed to find IdP group with ID group-1 after 5 retries"
347333
);
348-
expect(mockOctokit.request).toHaveBeenCalledTimes(7); // 1 for slug resolution + 1 for sync check + 5 retries for first group
334+
expect(mockOctokit.request).toHaveBeenCalledTimes(6); // 1 for sync check + 5 retries for first group
349335
});
350336

351337
it("should handle IdP groups without description", async () => {
@@ -355,11 +341,6 @@ describe("assignIdpGroupsToTeam", () => {
355341
group_description: undefined,
356342
};
357343

358-
// Mock team ID to slug resolution
359-
mockOctokit.request.mockResolvedValueOnce({
360-
data: { slug: "test-team" },
361-
});
362-
363344
// Mock team sync availability check
364345
mockOctokit.request.mockResolvedValueOnce({});
365346

@@ -373,7 +354,7 @@ describe("assignIdpGroupsToTeam", () => {
373354
});
374355

375356
expect(mockOctokit.request).toHaveBeenCalledWith(
376-
"PATCH /orgs/{org}/teams/{team_slug}/team-sync/group-mappings",
357+
"PATCH /organizations/{org}/team/{team_id}/team-sync/group-mappings",
377358
expect.objectContaining({
378359
groups: [
379360
{
@@ -396,11 +377,6 @@ describe("assignIdpGroupsToTeam", () => {
396377
});
397378

398379
it("should wrap non-BaseError exceptions in GithubError", async () => {
399-
// Mock team ID to slug resolution
400-
mockOctokit.request.mockResolvedValueOnce({
401-
data: { slug: "test-team" },
402-
});
403-
404380
// Mock team sync availability check
405381
mockOctokit.request.mockResolvedValueOnce({});
406382

@@ -421,68 +397,7 @@ describe("assignIdpGroupsToTeam", () => {
421397
);
422398
});
423399

424-
it("should resolve team ID to slug", async () => {
425-
const mockGroup = {
426-
group_id: "group-1",
427-
group_name: "Group One",
428-
group_description: "First group",
429-
};
430-
431-
// Mock team ID to slug resolution
432-
mockOctokit.request.mockResolvedValueOnce({
433-
data: { slug: "my-team-slug" },
434-
});
435-
436-
// Mock team sync availability check
437-
mockOctokit.request.mockResolvedValueOnce({});
438-
439-
// Mock IdP group search
440-
mockOctokit.request.mockResolvedValueOnce({
441-
data: { groups: [mockGroup] },
442-
});
443-
444-
// Mock PATCH request
445-
mockOctokit.request.mockResolvedValueOnce({});
446-
447-
await assignIdpGroupsToTeam({
448-
...defaultInputs,
449-
groupsToSync: ["group-1"],
450-
});
451-
452-
expect(mockOctokit.request).toHaveBeenCalledWith(
453-
"GET /organizations/{org}/team/{team_id}",
454-
{
455-
org: "test-org",
456-
team_id: 123,
457-
headers: {
458-
"X-GitHub-Api-Version": "2022-11-28",
459-
},
460-
}
461-
);
462-
expect(mockLogger.info).toHaveBeenCalledWith(
463-
"Resolved team ID 123 to slug: my-team-slug"
464-
);
465-
});
466-
467-
it("should throw GithubError if team slug resolution fails", async () => {
468-
// Mock team ID to slug resolution failure
469-
mockOctokit.request.mockRejectedValueOnce(new Error("Team not found"));
470-
471-
await expect(assignIdpGroupsToTeam(defaultInputs)).rejects.toThrow(
472-
GithubError
473-
);
474-
expect(mockLogger.error).toHaveBeenCalledWith(
475-
"Failed to resolve team ID 123 to slug:",
476-
expect.any(Error)
477-
);
478-
});
479-
480400
it("should exit gracefully if team sync is not available (404)", async () => {
481-
// Mock team ID to slug resolution
482-
mockOctokit.request.mockResolvedValueOnce({
483-
data: { slug: "test-team" },
484-
});
485-
486401
// Mock team sync availability check returning 404
487402
mockOctokit.request.mockRejectedValueOnce({
488403
status: 404,
@@ -492,11 +407,11 @@ describe("assignIdpGroupsToTeam", () => {
492407
await assignIdpGroupsToTeam(defaultInputs);
493408

494409
expect(mockLogger.warn).toHaveBeenCalledWith(
495-
expect.stringContaining("Team sync is not available for team test-team")
410+
expect.stringContaining("Team sync is not available for team 123")
496411
);
497412
expect(mockLogger.warn).toHaveBeenCalledWith("Skipping IdP group assignment");
498413
// Should not attempt to search for groups or patch
499-
expect(mockOctokit.request).toHaveBeenCalledTimes(2); // Only slug resolution and sync check
414+
expect(mockOctokit.request).toHaveBeenCalledTimes(1); // Only sync check
500415
});
501416

502417
it("should exit gracefully if PATCH returns 404", async () => {
@@ -506,11 +421,6 @@ describe("assignIdpGroupsToTeam", () => {
506421
group_description: "First group",
507422
};
508423

509-
// Mock team ID to slug resolution
510-
mockOctokit.request.mockResolvedValueOnce({
511-
data: { slug: "test-team" },
512-
});
513-
514424
// Mock team sync availability check
515425
mockOctokit.request.mockResolvedValueOnce({});
516426

0 commit comments

Comments
 (0)