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
132 lines (110 loc) · 3.59 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
import {context, getOctokit} from '@actions/github'
import {getBooleanInput, getInput, setFailed, setOutput} from '@actions/core'
import {join, parse} from 'path'
import ActionPermissions from '@stoe/action-permissions-cli/utils/action-permissions'
// 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 pushToRepo = getBooleanInput('push_results_to_repo', {required: false}) || false
if (!(enterprise || owner)) {
throw new Error('One of enterprise, owner is required')
}
if (enterprise && owner) {
throw new Error('Can only use one of enterprise, owner')
}
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 fap = new ActionPermissions(token, enterprise, owner, null, csv, md)
const actions = await fap.getActionPermissionsUse()
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 csvOut = await fap.saveCsv(actions)
if (pushToRepo) {
await pushFileToRepo(octokit, {
...commitOptions,
path: csv,
message: `Save/Update GitHub Actions permissions report (csv)`,
content: Buffer.from(csvOut).toString('base64')
})
}
setOutput('csv_result', csvOut)
}
// Create and save markdown
if (md !== '') {
const mdOut = await fap.saveMarkdown(actions)
if (pushToRepo) {
await pushFileToRepo(octokit, {
...commitOptions,
path: md,
message: `Save/Update GitHub Actions permissions report (md)`,
content: Buffer.from(mdOut).toString('base64')
})
}
setOutput('md_result', mdOut)
}
setOutput('json_result', JSON.stringify(actions))
} catch (error) {
setFailed(error.message)
}
})()
/**
* @private
*
* @param {object} oktokit
* @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
}
await octokit.rest.repos.createOrUpdateFileContents(options)
}