-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
327 lines (288 loc) · 9.08 KB
/
index.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
const core = require('@actions/core')
const github = require('@actions/github')
const { GitHub } = require('@actions/github/lib/utils')
const { createAppAuth } = require('@octokit/auth-app')
const { stringify } = require('csv-stringify/sync')
const { orderBy } = require('natural-orderby')
const token = core.getInput('token', { required: false })
const eventPayload = require(process.env.GITHUB_EVENT_PATH)
const owner = eventPayload.repository.owner.login
const repo = eventPayload.repository.name
const appId = core.getInput('appid', { required: false })
const privateKey = core.getInput('privatekey', { required: false })
const installationId = core.getInput('installationid', { required: false })
const org = core.getInput('org', { required: false }) || eventPayload.organization.login
const committerName = core.getInput('committer-name', { required: false }) || 'github-actions'
const committerEmail = core.getInput('committer-email', { required: false }) || 'github-actions@github.com'
const singleReport = core.getInput('single-report', { required: false }) || 'false'
const sortColumn = core.getInput('sort', { required: false }) || 'userName'
const sortOrder = core.getInput('sort-order', { required: false }) || 'asc'
const jsonExport = core.getInput('json', { required: false }) || 'false'
let octokit = null
// GitHub App authentication
if (appId && privateKey && installationId) {
octokit = new GitHub({
authStrategy: createAppAuth,
auth: {
appId: appId,
privateKey: privateKey,
installationId: installationId
}
})
} else {
octokit = github.getOctokit(token)
}
// Orchestrator
;(async () => {
try {
const emailArray = []
await getSamlId(emailArray)
await csvReport(emailArray)
if (jsonExport === 'true') {
await jsonReport(emailArray)
}
} catch (error) {
core.setFailed(error.message)
}
})()
// Retrieve organization SSO status
async function getSamlId(emailArray) {
try {
const query = /* GraphQL */ `
query ($org: String!) {
organization(login: $org) {
samlIdentityProvider {
id
}
}
}
`
dataJSON = await octokit.graphql({
query,
org: org
})
if (dataJSON.organization.samlIdentityProvider) {
await ssoEmail(emailArray)
} else {
await dotcomEmail(emailArray)
}
} catch (error) {
core.setFailed(error.message)
}
}
// Retrieve all members of a SSO enabled organization
async function ssoEmail(emailArray) {
try {
let endCursor = null
const query = /* GraphQL */ `
query ($org: String!, $cursorID: String) {
organization(login: $org) {
samlIdentityProvider {
externalIdentities(first: 100, after: $cursorID) {
edges {
node {
samlIdentity {
nameId
}
user {
login
name
email
createdAt
updatedAt
organizationVerifiedDomainEmails(login: $org)
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
}
`
let hasNextPage = false
let dataJSON = null
do {
dataJSON = await octokit.graphql({
query,
org: org,
cursorID: endCursor
})
const emails = dataJSON.organization.samlIdentityProvider.externalIdentities.edges
hasNextPage = dataJSON.organization.samlIdentityProvider.externalIdentities.pageInfo.hasNextPage
for (const email of emails) {
if (hasNextPage) {
endCursor = dataJSON.organization.samlIdentityProvider.externalIdentities.pageInfo.endCursor
} else {
endCursor = null
}
if (!email.node.user) continue
const userName = email.node.user.login
const fullName = email.node.user.name
const ssoEmail = email.node.samlIdentity.nameId
const publicEmail = email.node.user.email
const verifiedEmail = email.node.user.organizationVerifiedDomainEmails ? email.node.user.organizationVerifiedDomainEmails.join(', ') : ''
const updatedAt = email.node.user.updatedAt.slice(0, 10)
const createdAt = email.node.user.createdAt.slice(0, 10)
emailArray.push({ userName, fullName, ssoEmail, publicEmail, verifiedEmail, updatedAt, createdAt })
console.log(`${userName}`)
}
} while (hasNextPage)
} catch (error) {
core.setFailed(error.message)
}
}
// Retrieve all members of a SSO-disabled organization
async function dotcomEmail(emailArray) {
try {
let endCursor = null
const query = /* GraphQL */ `
query ($org: String!, $cursorID: String) {
organization(login: $org) {
membersWithRole(first: 100, after: $cursorID) {
edges {
node {
login
name
email
organizationVerifiedDomainEmails(login: $org)
updatedAt
createdAt
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
`
let hasNextPage = false
let dataJSON = null
do {
dataJSON = await octokit.graphql({
query,
org: org,
cursorID: endCursor
})
const emails = dataJSON.organization.membersWithRole.edges
hasNextPage = dataJSON.organization.membersWithRole.pageInfo.hasNextPage
for (const email of emails) {
if (hasNextPage) {
endCursor = dataJSON.organization.membersWithRole.pageInfo.endCursor
} else {
endCursor = null
}
if (!email.node.login) continue
const userName = email.node.login
const fullName = email.node.name
const publicEmail = email.node.email
const verifiedEmail = email.node.organizationVerifiedDomainEmails ? email.node.organizationVerifiedDomainEmails.join(', ') : ''
const updatedAt = email.node.updatedAt.slice(0, 10)
const createdAt = email.node.createdAt.slice(0, 10)
console.log(`${userName}`)
emailArray.push({ userName, fullName, publicEmail, verifiedEmail, updatedAt, createdAt })
}
} while (hasNextPage)
} catch (error) {
core.setFailed(error.message)
}
}
async function csvReport(emailArray) {
try {
// Add columns to array result
const columns = {
userName: 'Username',
fullName: 'Full name',
publicEmail: 'Public email',
verifiedEmail: 'Verified email',
ssoEmail: 'SSO email',
updatedAt: 'Updated',
createdAt: 'Created'
}
// Sort array by column
const sortArray = orderBy(emailArray, [sortColumn], [sortOrder])
// Convert array to csv
const csv = stringify(sortArray, {
header: true,
columns: columns
})
// Prepare path/filename, set repo/org context and commit name/email/message parameters
const reportPath = { path: `reports/${org}-member-email-report.csv` }
const opts = {
owner,
repo,
message: `${new Date().toISOString().slice(0, 10)} Member email report`,
content: Buffer.from(csv).toString('base64'),
committer: {
name: committerName,
email: committerEmail
}
}
// try to get the sha, if the file already exists
try {
const { data } = await octokit.rest.repos.getContent({
owner,
repo,
...reportPath
})
if (data && data.sha) {
reportPath.sha = data.sha
}
} catch (error) {}
// push csv report to repo
await octokit.rest.repos.createOrUpdateFileContents({
...opts,
...reportPath
})
// push optional single csv report to repo
if (singleReport === 'true') {
const singlePath = { path: `reports/single/${org}-${new Date().toISOString().substring(0, 19) + 'Z'}.csv` }
await octokit.rest.repos.createOrUpdateFileContents({
...opts,
...singlePath
})
}
} catch (error) {
core.setFailed(error.message)
}
}
// Generate an optional JSON report
async function jsonReport(emailArray) {
try {
// Prepare path/filename, set repo/org context and commit name/email/message parameters
const reportPath = { path: `reports/${org}-member-email-report.json` }
const opts = {
owner,
repo,
message: `${new Date().toISOString().slice(0, 10)} Member email report`,
content: Buffer.from(JSON.stringify(emailArray, null, 2)).toString('base64'),
committer: {
name: committerName,
email: committerEmail
}
}
// try to get the sha, if the file already exists
try {
const { data } = await octokit.rest.repos.getContent({
owner,
repo,
...reportPath
})
if (data && data.sha) {
reportPath.sha = data.sha
}
} catch (err) {}
// push json report to repo
await octokit.rest.repos.createOrUpdateFileContents({
...opts,
...reportPath
})
} catch (error) {
core.setFailed(error.message)
}
}