This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaction.js
177 lines (147 loc) · 5.3 KB
/
action.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
import {context, getOctokit} from '@actions/github'
import {getBooleanInput, getInput, setFailed, setOutput} from '@actions/core'
import {join, parse} from 'path'
import FindActionUses from '@stoe/action-uses-cli/utils/action-uses'
// action
;(async () => {
try {
const token = getInput('token', {required: true})
const enterprise = getInput('enterprise', {required: false}) || null
const owner = getInput('owner', {required: false}) || null
const csv = getInput('csv', {required: false}) || ''
const md = getInput('md', {required: false}) || ''
const exclude = getBooleanInput('exclude', {required: false}) || false
const _unique = getInput('unique', {required: false}) || false
const pushToRepo = getBooleanInput('push_results_to_repo', {required: false}) || false
if (!(enterprise || owner)) {
throw new Error('Please provide a valid value: enterprise or owner')
}
if (enterprise && owner) {
throw new Error('Can only use one of: enterprise, owner')
}
const 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 (csv !== '') {
const csvPath = join(process.env.GITHUB_WORKSPACE, csv)
const {dir: csvDir} = parse(csvPath)
if (csvDir.indexOf(process.env.GITHUB_WORKSPACE) < 0) {
throw new Error(`${csv} is not an allowed path`)
}
}
if (md !== '') {
const mdPath = join(process.env.GITHUB_WORKSPACE, md)
const {dir: mdDir} = parse(mdPath)
if (mdDir.indexOf(process.env.GITHUB_WORKSPACE) < 0) {
throw new Error(`${md} is not an allowed path`)
}
}
const fau = new FindActionUses(token, enterprise, owner, null, csv, md, uniqueFlag, exclude)
const {actions, unique} = await fau.getActionUses(uniqueFlag)
const octokit = await getOctokit(token)
const commitOptions = pushToRepo
? {
...context.repo,
committer: {
name: 'github-actions[bot]',
email: '41898282+github-actions[bot]@users.noreply.github.com'
}
}
: {}
// Create and save CSV
if (csv !== '') {
const csvResult = await fau.saveCsv({actions, unique}, uniqueFlag)
if (csvResult !== false) {
const {csv: csvOut, csvUnique: csvUniqueOut} = csvResult
const csvPathUnique = `${csv.replace('.csv', '-unique.csv')}`
if (pushToRepo) {
await pushFileToRepo(octokit, {
...commitOptions,
path: csv,
message: `Save/Update GitHub Actions usage report (csv)`,
content: Buffer.from(csvOut).toString('base64')
})
if (uniqueFlag === 'both') {
await pushFileToRepo(octokit, {
...commitOptions,
path: csvPathUnique,
message: `Save/Update GitHub Actions usage report (csv)`,
content: Buffer.from(csvUniqueOut).toString('base64')
})
}
}
setOutput('csv_result', csvOut)
if (uniqueFlag === 'both') {
setOutput('csv_resul_unique', csvUniqueOut)
}
}
}
// Create and save markdown
if (md !== '') {
const mdResult = await fau.saveMarkdown({actions, unique}, uniqueFlag)
if (mdResult !== false) {
const {md: mdOut, mdUnique: mdUniqueOut} = mdResult
const mdPathUnique = `${md.replace('.md', '-unique.md')}`
if (pushToRepo) {
await pushFileToRepo(octokit, {
...commitOptions,
path: md,
message: `Save/Update GitHub Actions usage report (md)`,
content: Buffer.from(mdOut).toString('base64')
})
if (uniqueFlag === 'both') {
await pushFileToRepo(octokit, {
...commitOptions,
path: mdPathUnique,
message: `Save/Update GitHub Actions usage report (md)`,
content: Buffer.from(mdUniqueOut).toString('base64')
})
}
}
setOutput('md_result', mdOut)
if (uniqueFlag === 'both') {
setOutput('md_result_unique', mdUniqueOut)
}
}
}
setOutput('json_result', JSON.stringify(actions))
} catch (error) {
setFailed(error.message)
}
})()
/**
* @private
*
* @param {import('@octokit/core').Octokit} octokit
* @param {object} options
* @param {string} options.owner
* @param {string} options.repo
* @param {string} options.path
* @param {string} options.message
* @param {string} options.content
*/
const pushFileToRepo = async (octokit, options) => {
try {
const {data} = await octokit.rest.repos.getContent({
owner: options.owner,
repo: options.repo,
path: options.path
})
if (data && data.sha) {
options.sha = data.sha
}
const base = Buffer.from(data.content, 'base64')
const head = Buffer.from(options.content, 'base64')
// 0 if they are equal
if (Buffer.compare(base, head) === 0) {
console.log(`no change detected for ${options.path}`)
// exit without updating the file
return
}
} catch (error) {
// do nothing
}
// https://docs.github.com/en/rest/reference/repos#create-or-update-file-contents
await octokit.rest.repos.createOrUpdateFileContents(options)
}