forked from lots0logs/gh-action-get-changed-files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
175 lines (129 loc) · 4.55 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
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
// External Dependencies
const fs = require('fs');
const github = require('@actions/github');
const core = require('@actions/core');
const context = github.context;
const repo = context.payload.repository;
const owner = repo.owner;
const FILES = new Set();
const FILES_ADDED = new Set();
const FILES_MODIFIED = new Set();
const FILES_REMOVED = new Set();
const FILES_RENAMED = new Set();
const gh = github.getOctokit(core.getInput('token'));
const args = { owner: owner.name || owner.login, repo: repo.name };
function debug(msg, obj = null) {
core.debug(formatLogMessage(msg, obj));
}
function fetchCommitData(commit) {
args.ref = commit.id || commit.sha;
debug('Calling gh.repos.getCommit() with args', args)
return gh.repos.getCommit(args);
}
function formatLogMessage(msg, obj = null) {
return obj ? `${msg}: ${toJSON(obj)}` : msg;
}
async function getCommits() {
let commits;
debug('Getting commits...');
switch(context.eventName) {
case 'push':
commits = context.payload.commits;
break;
case 'pull_request':
const url = context.payload.pull_request.commits_url;
commits = await gh.paginate(`GET ${url}`, args);
break;
default:
info('You are using this action on an event for which it has not been tested. Only the "push" and "pull_request" events are officially supported.');
commits = [];
break;
}
return commits;
}
function info(msg, obj = null) {
core.info(formatLogMessage(msg, obj));
}
function isAdded(file) {
return 'added' === file.status;
}
function isModified(file) {
return 'modified' === file.status;
}
function isRemoved(file) {
return 'removed' === file.status;
}
function isRenamed(file) {
return 'renamed' === file.status;
}
async function outputResults() {
debug('FILES', Array.from(FILES.values()));
core.setOutput('all', toJSON(Array.from(FILES.values()), 0));
core.setOutput('added', toJSON(Array.from(FILES_ADDED.values()), 0));
core.setOutput('modified', toJSON(Array.from(FILES_MODIFIED.values()), 0));
core.setOutput('removed', toJSON(Array.from(FILES_REMOVED.values()), 0));
core.setOutput('renamed', toJSON(Array.from(FILES_RENAMED.values()), 0));
fs.writeFileSync(`${process.env.HOME}/files.json`, toJSON(Array.from(FILES.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_added.json`, toJSON(Array.from(FILES_ADDED.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_modified.json`, toJSON(Array.from(FILES_MODIFIED.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_removed.json`, toJSON(Array.from(FILES_REMOVED.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_renamed.json`, toJSON(Array.from(FILES_RENAMED.values())), 'utf-8');
// Backwards Compatability
core.setOutput('deleted', toJSON(Array.from(FILES_REMOVED.values()), 0));
fs.writeFileSync(`${process.env.HOME}/files_deleted.json`, toJSON(Array.from(FILES_REMOVED.values())), 'utf-8');
}
async function processCommitData(result) {
debug('Processing API Response', result);
if (! result || ! result.data) {
return;
}
result.data.files.forEach(file => {
(isAdded(file) || isModified(file) || isRenamed(file)) && FILES.add(file.filename);
if (isAdded(file)) {
FILES_ADDED.add(file.filename);
FILES_REMOVED.delete(file.filename);
return; // continue
}
if (isRemoved(file)) {
if (! FILES_ADDED.has(file.filename)) {
FILES_REMOVED.add(file.filename);
}
FILES_ADDED.delete(file.filename);
FILES_MODIFIED.delete(file.filename);
return; // continue;
}
if (isModified(file)) {
FILES_MODIFIED.add(file.filename);
return; // continue;
}
if (isRenamed(file)) {
processRenamedFile(file.previous_filename, file.filename);
}
});
}
function processRenamedFile(prev_file, new_file) {
FILES.delete(prev_file) && FILES.add(new_file);
FILES_ADDED.delete(prev_file) && FILES_ADDED.add(new_file);
FILES_MODIFIED.delete(prev_file) && FILES_MODIFIED.add(new_file);
FILES_RENAMED.add(new_file);
}
function toJSON(value, pretty=true) {
return pretty
? JSON.stringify(value, null, 4)
: JSON.stringify(value);
}
debug('context', context);
debug('args', args);
getCommits().then(commits => {
// Exclude merge commits
commits = commits.filter(c => ! c.parents || 1 === c.parents.length);
if ('push' === context.eventName) {
commits = commits.filter(c => c.distinct);
}
debug('All Commits', commits);
Promise.all(commits.map(fetchCommitData))
.then(data => Promise.all(data.map(processCommitData)))
.then(outputResults)
.then(() => process.exitCode = 0)
.catch(err => core.error(err) && (process.exitCode = 1));
});