@@ -53,14 +53,15 @@ import {
5353} from "api/functions/entraId.js" ;
5454import { SecretsManagerClient } from "@aws-sdk/client-secrets-manager" ;
5555import { getRoleCredentials } from "api/functions/sts.js" ;
56- import { SQSClient } from "@aws-sdk/client-sqs" ;
56+ import { SendMessageCommand , SQSClient } from "@aws-sdk/client-sqs" ;
5757import { sendSqsMessagesInBatches } from "api/functions/sqs.js" ;
5858import { retryDynamoTransactionWithBackoff } from "api/utils.js" ;
5959import {
6060 assignIdpGroupsToTeam ,
6161 createGithubTeam ,
6262} from "api/functions/github.js" ;
6363import { SKIP_EXTERNAL_ORG_LEAD_UPDATE } from "common/overrides.js" ;
64+ import { AvailableSQSFunctions , SQSPayload } from "common/types/sqsMessage.js" ;
6465
6566export const CLIENT_HTTP_CACHE_POLICY = `public, max-age=${ ORG_DATA_CACHED_DURATION } , stale-while-revalidate=${ Math . floor ( ORG_DATA_CACHED_DURATION * 1.1 ) } , stale-if-error=3600` ;
6667
@@ -413,12 +414,10 @@ const organizationsPlugin: FastifyPluginAsync = async (fastify, _options) => {
413414 ? ( unmarshall ( metadataResponse . Item ) . leadsEntraGroupId as string )
414415 : undefined ;
415416
416- let githubTeamId = metadataResponse . Item
417+ const githubTeamId = metadataResponse . Item
417418 ? ( unmarshall ( metadataResponse . Item ) . githubTeamId as number )
418419 : undefined ;
419420
420- let createdGithubTeam = false ;
421-
422421 const entraIdToken = await getEntraIdToken ( {
423422 clients,
424423 clientId : fastify . environmentConfig . AadValidClientId ,
@@ -428,8 +427,6 @@ const organizationsPlugin: FastifyPluginAsync = async (fastify, _options) => {
428427
429428 const shouldCreateNewEntraGroup =
430429 ! entraGroupId && ! shouldSkipEnhancedActions ;
431- const shouldCreateNewGithubGroup =
432- ! githubTeamId && ! shouldSkipEnhancedActions ;
433430 const grpDisplayName = `${ request . params . orgId } Admin` ;
434431 const orgInfo = getOrgByName ( request . params . orgId ) ;
435432 if ( ! orgInfo ) {
@@ -524,65 +521,6 @@ const organizationsPlugin: FastifyPluginAsync = async (fastify, _options) => {
524521 }
525522 }
526523
527- // Create GitHub team if needed
528- if ( shouldCreateNewGithubGroup ) {
529- request . log . info (
530- `No GitHub team exists for ${ request . params . orgId } . Creating new team...` ,
531- ) ;
532- const suffix = fastify . environmentConfig . GroupEmailSuffix ;
533- githubTeamId = await createGithubTeam ( {
534- orgId : fastify . environmentConfig . GithubOrgName ,
535- githubToken : fastify . secretConfig . github_pat ,
536- parentTeamId : fastify . environmentConfig . ExecGithubTeam ,
537- name : `${ grpShortName } ${ suffix === "" ? "" : `-${ suffix } ` } ` ,
538- description : grpDisplayName ,
539- logger : request . log ,
540- } ) ;
541- request . log . info (
542- `Created GitHub team "${ githubTeamId } " for ${ request . params . orgId } leads.` ,
543- ) ;
544- createdGithubTeam = true ;
545-
546- // Store GitHub team ID immediately
547- const logStatement = buildAuditLogTransactPut ( {
548- entry : {
549- module : Modules . ORG_INFO ,
550- message : `Created GitHub team "${ githubTeamId } " for organization leads.` ,
551- actor : request . username ! ,
552- target : request . params . orgId ,
553- } ,
554- } ) ;
555-
556- const storeGithubIdOperation = async ( ) => {
557- const commandTransaction = new TransactWriteItemsCommand ( {
558- TransactItems : [
559- ...( logStatement ? [ logStatement ] : [ ] ) ,
560- {
561- Update : {
562- TableName : genericConfig . SigInfoTableName ,
563- Key : marshall ( {
564- primaryKey : `DEFINE#${ request . params . orgId } ` ,
565- entryId : "0" ,
566- } ) ,
567- UpdateExpression :
568- "SET leadsGithubTeamId = :githubTeamId, updatedAt = :updatedAt" ,
569- ExpressionAttributeValues : marshall ( {
570- ":githubTeamId" : githubTeamId ,
571- ":updatedAt" : new Date ( ) . toISOString ( ) ,
572- } ) ,
573- } ,
574- } ,
575- ] ,
576- } ) ;
577- return await clients . dynamoClient . send ( commandTransaction ) ;
578- } ;
579-
580- await retryDynamoTransactionWithBackoff (
581- storeGithubIdOperation ,
582- request . log ,
583- `Store GitHub team ID for ${ request . params . orgId } ` ,
584- ) ;
585- }
586524 const commonArgs = {
587525 orgId : request . params . orgId ,
588526 actorUsername : request . username ! ,
@@ -628,36 +566,37 @@ const organizationsPlugin: FastifyPluginAsync = async (fastify, _options) => {
628566 . map ( ( r ) => r . value )
629567 . filter ( ( p ) : p is SQSMessage => p !== null ) ;
630568
569+ if ( ! fastify . sqsClient ) {
570+ fastify . sqsClient = new SQSClient ( {
571+ region : genericConfig . AwsRegion ,
572+ } ) ;
573+ }
574+
575+ // Queue creating GitHub team if needed
576+ if ( ! githubTeamId ) {
577+ const sqsPayload : SQSPayload < AvailableSQSFunctions . CreateOrgGithubTeam > =
578+ {
579+ function : AvailableSQSFunctions . CreateOrgGithubTeam ,
580+ metadata : {
581+ initiator : request . username ! ,
582+ reqId : request . id ,
583+ } ,
584+ payload : {
585+ orgName : request . params . orgId ,
586+ githubTeamDescription : grpDisplayName ,
587+ githubTeamName : grpShortName ,
588+ } ,
589+ } ;
590+ sqsPayloads . push ( sqsPayload ) ;
591+ }
631592 if ( sqsPayloads . length > 0 ) {
632- if ( ! fastify . sqsClient ) {
633- fastify . sqsClient = new SQSClient ( {
634- region : genericConfig . AwsRegion ,
635- } ) ;
636- }
637593 await sendSqsMessagesInBatches ( {
638594 sqsClient : fastify . sqsClient ,
639595 queueUrl : fastify . environmentConfig . SqsQueueUrl ,
640596 logger : request . log ,
641597 sqsPayloads,
642598 } ) ;
643599 }
644-
645- if (
646- createdGithubTeam &&
647- githubTeamId &&
648- fastify . environmentConfig . GithubIdpSyncEnabled
649- ) {
650- request . log . info ( "Setting up IDP sync for Github team!" ) ;
651- await assignIdpGroupsToTeam ( {
652- githubToken : fastify . secretConfig . github_pat ,
653- teamId : githubTeamId ,
654- logger : request . log ,
655- groupsToSync : [ entraGroupId ] . filter ( ( x ) : x is string => ! ! x ) ,
656- orgId : fastify . environmentConfig . GithubOrgId ,
657- orgName : fastify . environmentConfig . GithubOrgName ,
658- } ) ;
659- }
660-
661600 return reply . status ( 201 ) . send ( ) ;
662601 } ,
663602 ) ;
0 commit comments