-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgoogle.ts
More file actions
57 lines (52 loc) · 1.71 KB
/
google.ts
File metadata and controls
57 lines (52 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { google } from 'googleapis'
import * as mod from './google'
import { config } from './config'
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export async function googleAuth() {
const privatekey = config.googleCredentials
const jwtClient = new google.auth.JWT(
privatekey.client_email,
null,
privatekey.private_key,
['https://www.googleapis.com/auth/admin.directory.user.readonly'],
config.googleEmailAddress,
)
await jwtClient.authorize()
return jwtClient
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export async function getAdminService() {
return google.admin({
version: 'directory_v1',
auth: await googleAuth(),
})
}
export async function getGithubUsersFromGoogle(): Promise<Set<string>> {
const service = await mod.getAdminService()
let githubAccounts = new Set<string>()
let pageToken = null
do {
const userList = await service.users.list({
customer: 'my_customer',
maxResults: 250,
projection: 'custom',
fields: 'users(customSchemas/Accounts/github(value)),nextPageToken',
customFieldMask: 'Accounts',
pageToken: pageToken,
})
pageToken = userList.data.nextPageToken
githubAccounts = new Set([...githubAccounts, ...formatUserList(userList.data.users)])
} while (pageToken != null)
return githubAccounts
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function formatUserList(users: any[]): Set<string> {
return new Set(
users
.map((user) =>
user.customSchemas?.Accounts?.github?.map((account: { value: string }) => account.value?.toLowerCase()),
)
.flat()
.filter(Boolean),
)
}