This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (67 loc) · 1.8 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
const core = require('@actions/core');
const github = require('@actions/github');
async function run() {
try {
const token = core.getInput('token');
const octokit = github.getOctokit(token)
const repo = github.context.repo
const files = new Map();
let commitIDs = [];
switch(github.context.eventName) {
case 'push':
commitIDs = getCommitsFromPush();
break;
case 'pull_request':
commitIDs = await getCommitsFromPullRequest(octokit);
break;
default:
core.warning("Unhandled event " + github.context.eventName);
return;
}
for (const id of commitIDs) {
const {data: commit} = await octokit.rest.repos.getCommit({
owner: repo.owner,
repo: repo.repo,
ref: id
});
for (const file of commit.files) {
add(files, 'all', file.filename)
add(files, file.status, file.filename)
}
}
let out = {};
for (const [k,v] of files) {
const values = Array.from(v)
out[k] = values;
core.setOutput(k, values);
}
core.info("Commits: ", commitIDs);
core.info("Output: ");
core.info(JSON.stringify(out, undefined, 2));
} catch (error) {
core.setFailed(error.message);
}
}
function getCommitsFromPush() {
return github.context.payload.commits.map(c => c.id);
}
async function getCommitsFromPullRequest(octokit) {
const {data: commits} = await octokit.rest.pulls.listCommits({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: github.context.payload.number
})
return commits.map(c => c.sha)
}
function add(map, key, value) {
if (map.has(key)) {
const s = map.get(key);
s.add(value)
map.set(key, s)
} else {
const s = new Set()
s.add(value)
map.set(key, s)
}
}
run();