generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
69 lines (63 loc) · 2.18 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
const core = require('@actions/core');
const exec = require('@actions/exec');
const process = require('process');
const utils = require('./utils');
async function run() {
try {
if (process.env["GITHUB_ACTIONS"]) {
// console.log("Running inside a github action");
}
// To test this from your dev box, use "env INPUT_GOMODS='<test_pattern>' node index.js"
const filePatterns = core
.getInput('gomods')
.split("\n")
.map(s => s.trim())
.filter(s => s !== "");
let dirs = await utils.findDirectories(filePatterns);
await gomodTidy(dirs);
let diffs = await gitDiffFiles();
core.startGroup('Diff Files');
for (const f of diffs) {
console.log(` ${f}`);
}
core.endGroup();
core.startGroup('Full diff');
await exec.exec('git', ['diff']);
core.endGroup();
const gosum_only = core.getInput('gosum_only').toLowerCase();
const gomodsum_only = core.getInput('gomodsum_only').toLowerCase();
let enabled = (s) => { return s === 'true' || s === 'enabled'; };
utils.checkModifiedFiles(diffs, enabled(gosum_only), enabled(gomodsum_only));
core.setOutput('changedfiles', diffs.join("\n"));
} catch (error) {
core.setFailed(error.message);
}
}
run();
async function gomodTidy(dirs) {
let p = [];
for (const d of dirs) {
core.debug(`${d}: go mod tidy`);
p.push(exec.exec('go', ['mod', 'tidy'], { cwd: d, silent: true }));
}
return await Promise.all(p);
}
// Run git status in each directory - to see what files have changed
// \todo check behaviour with git submodules
async function gitDiffFiles() {
core.debug(`git diff --name-only`);
let myOutput = '';
const options = {
silent: true,
listeners: {
stdout: data => {
myOutput += data.toString();
}
}
};
await exec.exec('git', ['diff', '--name-only'], options);
// break up the filenames in output by line
const diffs = myOutput.split("\n").filter(x => x.trim());
core.debug(`diffs=${diffs}`);
return diffs;
}