-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgoogle.ts
More file actions
52 lines (46 loc) · 1.56 KB
/
google.ts
File metadata and controls
52 lines (46 loc) · 1.56 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
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()
const userList = await service.users.list({
customer: 'my_customer',
maxResults: 250,
projection: 'custom',
query: 'isSuspended=false',
fields: 'users(customSchemas/Accounts/github(value),suspended,archived)',
customFieldMask: 'Accounts',
})
const githubAccounts = mod.formatUserList(userList.data.users)
return githubAccounts
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function formatUserList(users): Set<string> {
return new Set(
users
.filter((user) => !user.suspended && !user.archived)
.map((user) => user.customSchemas?.Accounts?.github?.map((account) => account.value?.toLowerCase()))
.flat()
.filter(Boolean),
)
}