-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathindex.js
More file actions
153 lines (131 loc) · 4.59 KB
/
index.js
File metadata and controls
153 lines (131 loc) · 4.59 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
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
const core = require('@actions/core');
const {DefaultArtifactClient} = require('@actions/artifact')
const crypto = require("crypto");
const fs = require('fs');
async function run() {
try {
const configString = core.getInput('config');
let config = {};
if (configString) {
config = JSON.parse(configString);
}
if (!config.hasOwnProperty('create_artifact')) {
config['create_artifact'] = true;
}
if (!config.hasOwnProperty('enabled')) {
config['enabled'] = true;
}
if (!config.hasOwnProperty('debug')) {
config['debug'] = false;
}
if (!config.enabled)
return;
const debug = core.getInput('debug').toUpperCase() === 'TRUE' || config.debug || process.env.RUNNER_DEBUG;
if (debug) {
// for the bash script
core.exportVariable('RUNNER_DEBUG', 1);
}
const hosts = new Set();
hosts.add(process.env.GITHUB_SERVER_URL.split('/')[2].toLowerCase());
hosts.add(process.env.GITHUB_API_URL.split('/')[2].toLowerCase());
if (process.env.ACTIONS_ID_TOKEN_REQUEST_URL) {
hosts.add(process.env.ACTIONS_ID_TOKEN_REQUEST_URL.split('/')[2].toLowerCase());
}
if (!!core.getState('isPost')) {
let rootDir = '';
if (process.env.RUNNER_OS === 'Linux') {
rootDir = '/home/mitmproxyuser';
} else if (process.env.RUNNER_OS === 'macOS') {
rootDir = '/Users/mitmproxyuser';
}
const debugLog = `${rootDir}/debug.log`;
if (fs.existsSync(debugLog)) {
// using core.info instead of core.debug to print even if the runner itself doesn't run in debug mode
core.info(fs.readFileSync(debugLog, 'utf8'));
}
const data = fs.readFileSync(`${rootDir}/out.txt`, 'utf8');
if (debug)
console.log(`logged: ${data}`);
const errorLog = `${rootDir}/error.log`;
if (fs.existsSync(errorLog)) {
core.setFailed(fs.readFileSync(errorLog, 'utf8'));
process.exit(1);
}
const results = JSON.parse(`[${data.trim().replace(/\r?\n|\r/g, ',')}]`);
let permissions = new Map();
let wasUnknown = false;
for (const result of results) {
if (!hosts.has(result.host.toLowerCase()))
continue;
for (const p of result.permissions) {
const kind = Object.keys(p)[0];
const perm = p[kind];
if (kind === 'unknown') {
core.warning(`The github token was used to call ${result.method} ${result.host}${result.path} but the permission is unknown. Please report this to the action author.`);
wasUnknown = true;
continue;
}
if (permissions.has(kind)) {
if (perm === "write") {
permissions.set(kind, perm)
}
} else {
permissions.set(kind, perm)
}
}
}
let summary = 'permissions:';
if (permissions.size === 0) {
summary += ' {}'
} else {
summary += '\n'
for (const [kind, perm] of permissions) {
summary += ` ${kind}: ${perm}\n`;
}
}
if (wasUnknown) {
summary += "\nAt least one call wasn't recognized. Please check the logs and report this to the action author.";
}
core.summary
.addRaw('#### Minimal required permissions:\n')
.addCodeBlock(summary, 'yaml')
.write();
if (config.create_artifact) {
const tempDirectory = process.env['RUNNER_TEMP'];
fs.writeFileSync(`${tempDirectory}/permissions`, JSON.stringify(Object.fromEntries(permissions)));
await new DefaultArtifactClient().uploadArtifact(
`${process.env['GITHUB_JOB']}-permissions-${crypto.randomBytes(16).toString("hex")}`,
[`${tempDirectory}/permissions`],
tempDirectory,
{ continueOnError: false }
);
}
}
else {
core.saveState('isPost', true)
const { spawn } = require('child_process');
bashArgs = ['-e', 'setup.sh', Array.from(hosts).join(",")];
if (debug)
bashArgs.unshift('-v');
const command = spawn('bash', bashArgs, { cwd: `${__dirname}/..` })
command.stdout.on('data', output => {
console.log(output.toString())
if (output.toString().includes('--all done--')) {
process.exit(0)
}
})
command.stderr.on('data', output => {
console.log(`stderr: ${output.toString()}`)
})
command.on('exit', code => {
if (code !== 0) {
core.setFailed(`Exited with code ${code}`);
process.exit(code);
}
})
}
} catch (error) {
core.setFailed(error.message);
}
}
run();