@@ -4,7 +4,7 @@ import { Config } from "../config";
44import { GitHubClient , GitHubId , GitHubTeamId , InstalledClient , Org , OrgInvite , OrgRoles , Response } from "./gitHubTypes" ;
55import { AppConfig } from "./appConfig" ;
66import yaml from "js-yaml" ;
7- import { throttling } from "@octokit/plugin-throttling" ;
7+ import { throttling } from "@octokit/plugin-throttling" ;
88import { AsyncReturnType } from "../utility" ;
99import { Log , LoggerToUse } from "../logging" ;
1010import { GitHubClientCache } from "./gitHubCache" ;
@@ -36,7 +36,7 @@ async function GetOrgClient(installationId: number): Promise<InstalledClient> {
3636 // TODO: look further into this... it seems like it would be best if
3737 // installation client was generated from the original client, and not
3838 // created fresh.
39- const MyOctokit = Octokit . plugin ( throttling ) ;
39+ const MyOctokit = Octokit . plugin ( throttling ) ;
4040
4141 const installedOctokit = new MyOctokit ( {
4242 authStrategy : createAppAuth ,
@@ -45,7 +45,7 @@ async function GetOrgClient(installationId: number): Promise<InstalledClient> {
4545 privateKey : Config ( ) . GitHub . PrivateKey ,
4646 installationId
4747 } ,
48- throttle : {
48+ throttle : {
4949 onRateLimit : ( retryAfter , options , octokit , retryCount ) => {
5050 octokit . log . warn (
5151 `Request quota exhausted for request ${ options . method } ${ options . url } `
@@ -56,7 +56,7 @@ async function GetOrgClient(installationId: number): Promise<InstalledClient> {
5656 octokit . log . info ( `Retrying after ${ retryAfter } seconds!` ) ;
5757 return true ;
5858 }
59- } ,
59+ } ,
6060 onSecondaryRateLimit : ( retryAfter , options , octokit ) => {
6161 // does not retry, only logs a warning
6262 octokit . log . warn (
@@ -74,7 +74,7 @@ async function GetOrgClient(installationId: number): Promise<InstalledClient> {
7474 // TODO: keep an eye on this as there is a good
7575 // chance login will be removed considering it
7676 // is already not present on the type...
77- login :string
77+ login : string
7878 }
7979
8080 // HACK: gross typing nonsense
@@ -192,18 +192,18 @@ async function GetAppConfig(client: Octokit): Promise<AppConfig> {
192192 }
193193
194194 type RawAppConfig = {
195- GitHubIdAppend ?:string
196- SecurityManagerTeams ?:string [ ]
195+ GitHubIdAppend ?: string
196+ SecurityManagerTeams ?: string [ ]
197197 Description ?: {
198198 ShortLink : string
199199 }
200- TeamsToIgnore ?:string [ ]
200+ TeamsToIgnore ?: string [ ]
201201 }
202202
203203 const configuration = yaml . load ( Buffer . from ( contentData . content , 'base64' ) . toString ( ) ) as RawAppConfig ;
204204
205205 return {
206- Description : configuration . Description ?? { ShortLink :"https://github.com/cloudpups/github-teams-user-sync" } ,
206+ Description : configuration . Description ?? { ShortLink : "https://github.com/cloudpups/github-teams-user-sync" } ,
207207 SecurityManagerTeams : configuration . SecurityManagerTeams ?? [ ] ,
208208 TeamsToIgnore : configuration . TeamsToIgnore ?? [ ] ,
209209 GitHubIdAppend : configuration . GitHubIdAppend ?? ""
@@ -230,6 +230,46 @@ class InstalledGitHubClient implements InstalledClient {
230230 this . orgName = orgName ;
231231 }
232232
233+ async AddTeamsToCopilotSubscription ( teamNames : string [ ] ) : Response < string [ ] > {
234+ // Such logic should not generally go in a facade, though the convenience
235+ // and lack of actual problems makes this violation of pattern more "okay."
236+ if ( teamNames . length < 1 ) {
237+ return {
238+ // Should be "no op"
239+ successful : true ,
240+ data : [ ]
241+ }
242+ }
243+
244+ try {
245+ const response = await this . gitHubClient . request ( "POST /orgs/{org}/copilot/billing/selected_teams" , {
246+ org : this . orgName ,
247+ selected_teams : teamNames ,
248+ headers : {
249+ 'X-GitHub-Api-Version' : '2022-11-28'
250+ }
251+ } ) ;
252+
253+ if ( response . status < 200 || response . status > 299 ) {
254+ return {
255+ successful : false
256+ }
257+ }
258+
259+ return {
260+ successful : true ,
261+ data : teamNames
262+ }
263+ }
264+ catch ( e ) {
265+ console . log ( e ) ;
266+ // TODO: actually catch exception and investigate...
267+ return {
268+ successful : false
269+ }
270+ }
271+ }
272+
233273 async ListPendingInvitesForTeam ( teamName : string ) : Response < OrgInvite [ ] > {
234274 const safeName = MakeTeamNameSafeAndApiFriendly ( teamName ) ;
235275
0 commit comments