-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·252 lines (226 loc) · 7 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
#!/usr/bin/env node
import Reporting from './utils/reporting.js'
import chalk from 'chalk'
import meow from 'meow'
const {dim, blue, bold, red, yellow} = chalk
const cli = meow(
`
${bold('Usage')}
${blue(`action-reporting`)} ${yellow(`[options]`)}
${bold('Required options')} ${dim(`[one of]`)}
${yellow(`--enterprise`)}, ${yellow(`-e`)} GitHub Enterprise (Cloud|Server) account slug ${dim(
'(e.g. enterprise)',
)}.
${yellow(`--owner`)}, ${yellow(`-o`)} GitHub organization/user login ${dim('(e.g. owner)')}.
${dim(
`If ${yellow(`--owner`)} is a user, results for the authenticated user (${yellow(
`--token`,
)}) will be returned.`,
)}
${yellow(`--repository`)}, ${yellow(`-r`)} GitHub repository name with owner ${dim('(e.g. owner/repo)')}.
${bold('Additional options')}
${yellow(`--token`)}, ${yellow(`-t`)} GitHub Personal Access Token (PAT) ${dim('(default GITHUB_TOKEN)')}.
${yellow(`--hostname`)} GitHub Enterprise Server ${bold('hostname')} ${dim('(default api.github.com)')}.
${dim(`For example: ${yellow('github.example.com')}`)}
${bold('Report options')}
${yellow(`--all`)} Report all below.
${yellow(`--listeners`)} Report ${bold('on')} listeners used.
${yellow(`--permissions`)} Report ${bold('permissions')} values for GITHUB_TOKEN.
${yellow(`--runs-on`)} Report ${bold('runs-on')} values.
${yellow(`--secrets`)} Report ${bold('secrets')} used.
${yellow(`--uses`)} Report ${bold('uses')} values.
${yellow(`--exclude`)} Exclude GitHub Actions created by GitHub.
${dim(
`From https://github.com/actions and https://github.com/github organizations.
Only applies to ${yellow(`--uses`)}.`,
)}
${yellow(`--unique`)} List unique GitHub Actions.
${dim(
`Possible values are ${yellow('true')}, ${yellow('false')} and ${yellow('both')}.
Only applies to ${yellow(`--uses`)}.`,
)}
${dim(`Will create an additional ${bold('*-unique.{csv,json,md}')} report file.`)}
${yellow(`--vars`)} Report ${bold('vars')} used.
${bold('Output options')}
${yellow(`--csv`)} Path to save CSV output ${dim('(e.g. /path/to/reports/report.csv)')}.
${yellow(`--json`)} Path to save JSON output ${dim('(e.g. /path/to/reports/report.json)')}.
${yellow(`--md`)} Path to save markdown output ${dim('(e.g. /path/to/reports/report.md)')}.
${bold('Helper options')}
${yellow(`--help`)}, ${yellow(`-h`)} Print action-reporting help.
${yellow(`--version`)}, ${yellow(`-v`)} Print action-reporting version.`,
{
booleanDefault: undefined,
description: false,
hardRejection: false,
allowUnknownFlags: false,
importMeta: import.meta,
inferType: false,
input: [],
flags: {
help: {
type: 'boolean',
shortFlag: 'h',
},
version: {
type: 'boolean',
shortFlag: 'v',
},
enterprise: {
type: 'string',
shortFlag: 'e',
},
owner: {
type: 'string',
shortFlag: 'o',
isMultiple: false,
},
repository: {
type: 'string',
shortFlag: 'r',
isMultiple: false,
},
token: {
type: 'string',
shortFlag: 't',
default: process.env.GITHUB_TOKEN || '',
},
// reports
all: {
type: 'boolean',
default: false,
},
listeners: {
type: 'boolean',
default: false,
},
permissions: {
type: 'boolean',
default: false,
},
runsOn: {
type: 'boolean',
default: false,
},
secrets: {
type: 'boolean',
default: false,
},
uses: {
type: 'boolean',
default: false,
},
exclude: {
type: 'boolean',
default: false,
},
unique: {
default: false,
},
vars: {
type: 'boolean',
default: false,
},
hostname: {
type: 'string',
},
// outputs
csv: {
type: 'string',
},
md: {
type: 'string',
},
json: {
type: 'string',
},
},
},
)
// action
;(async () => {
try {
// Get options/flags
const {help, version, enterprise, owner, repository, token, all, unique: _unique, exclude, hostname} = cli.flags
// Get report options/flags
let {listeners, permissions, runsOn, secrets, uses, vars} = cli.flags
// Get output options/flags
const {csv, md, json} = cli.flags
help && cli.showHelp(0)
version && cli.showVersion(0)
if (!token) {
throw new Error('GitHub Personal Access Token (PAT) not provided')
}
if (!(enterprise || owner || repository)) {
throw new Error('no options provided')
}
if ((enterprise && owner) || (enterprise && repository) || (owner && repository)) {
throw new Error('can only use one of: enterprise, owner, repository')
}
if (csv === '') {
throw new Error('please provide a valid path for the CSV output')
}
if (md === '') {
throw new Error('please provide a valid path for the markdown output')
}
if (json === '') {
throw new Error('please provide a valid path for the JSON output')
}
let uniqueFlag = _unique === 'both' ? 'both' : _unique === 'true'
if (![true, false, 'both'].includes(uniqueFlag)) {
throw new Error('please provide a valid value for unique: true, false, both')
}
if (all) {
listeners = true
permissions = true
runsOn = true
secrets = true
uses = true
vars = true
// if all is true, create unique report by default
uniqueFlag = 'both'
}
const report = new Reporting({
token,
enterprise,
owner,
repository,
flags: {
getListeners: listeners,
getPermissions: permissions,
getRunsOn: runsOn,
getSecrets: secrets,
getUses: uses,
isUnique: uniqueFlag,
isExcluded: exclude,
getVars: vars,
},
outputs: {
csvPath: csv,
mdPath: md,
jsonPath: json,
},
hostname,
})
// get report
await report.get()
// create and save CSV
if (csv) {
await report.saveCsv()
await report.saveCsvUnique()
}
// create and save markdown
if (md) {
await report.saveMarkdown()
await report.saveMarkdownUnique()
}
// create and save JSON
if (json) {
await report.saveJSON()
await report.saveJSONUnique()
}
} catch (error) {
console.error(`\n ${red('ERROR: %s')}`, error.message)
console.error(error.stack)
cli.showHelp(1)
}
})()