-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
215 lines (179 loc) · 6.84 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const core = require('@actions/core');
const exec = require('@actions/exec');
const github = require('@actions/github');
const { arrayCompare } = require('./src/lib/utils');
const PullRequest = require('./src/pullRequest');
const Comment = require('./src/comment');
const CommentError = require('./src/errors/comment.error');
const Analyzer = require('./src/analyzer');
const Reporter = require('./src/reporter');
const Decorator = require('./src/decorator');
const document = require('./src/document');
// command
// RUNNER_TOOL_CACHE="/tmp" GITHUB_REF=refs/heads/master INPUT_FRAMEWORK=mocha INPUT_TESTS="example/mocha/**.js" node index.js
const mainRepoPath = process.env.GITHUB_WORKSPACE;
// most @actions toolkit packages have async methods
async function run() {
const repoUrl = process.env.GITHUB_REPOSITORY;
const [owner, repo] = repoUrl.split('/');
const octokit = new github.GitHub(core.getInput('token', { required: true }));
const nodiff = core.getInput('nodiff');
const framework = core.getInput('framework', { required: true });
const pattern = core.getInput('tests', { required: true });
const apiKey = core.getInput('testomatio-key');
const ghPat = core.getInput('github-pat');
const enableDocumentation = core.getInput('enable-documentation');
const wikiFile = core.getInput('wiki-doc-name') || 'Tests';
/* prettier-ignore */
const docBranch = core.getInput('documentation-branch') || (await octokit.repos.get({ owner, repo })).data.default_branch;
const pullRequest = new PullRequest(core.getInput('token', { required: true }));
const analyzer = new Analyzer(framework, mainRepoPath, opts);
if (core.getInput('typescript')) analyzer.withTypeScript();
if (!core.getInput('documentation-branch')) {
console.log(`Using default branch ${docBranch}`);
}
try {
if (!mainRepoPath) {
throw new Error('Repository was not fetched, please enable add `actions/checkout` step before');
}
analyzer.analyze(pattern);
const allTests = analyzer.getDecorator();
const stats = analyzer.getStats();
if (apiKey) {
const reporter = new Reporter(apiKey);
reporter.addTests(allTests.getDecorator().getTests());
reporter.send(); // async call
}
let pr;
try {
if (!nodiff) {
pr = await pullRequest.fetch();
}
} catch (err) {
pr = null;
}
const baseStats = await analyzeBase(pr);
const diff = arrayCompare(baseStats.tests, stats.tests);
diff.missing = diff.missing.filter(t => !stats.skipped.includes(Object.values(t)[0])); // remove skipped tests from missing
const skippedDiff = arrayCompare(baseStats.skipped, stats.skipped);
console.log(`Added ${diff.added.length} tests, removed ${diff.missing.length} tests`);
console.log(`Total ${stats.tests.length} tests`);
if (!pr && enableDocumentation && process.env.GITHUB_REF.endsWith(docBranch)) {
console.log('Documentation enabled, Going to create Wiki');
await createTestDocWikiPage(allTests);
}
if (!pr) return;
const comment = new Comment();
comment.writeSummary(stats.tests.length, stats.files.length, framework);
const commentOnEmpty = core.getInput('comment-on-empty');
const commentOnSkipped = core.getInput('comment-on-skipped');
const closeOnEmpty = core.getInput('close-on-empty');
const closeOnSkipped = core.getInput('close-on-skipped');
/* prettier-ignore */
const isEmpty = !diff.added.length && !diff.missing.length && !skippedDiff.added.length && !skippedDiff.missing.length;
if (commentOnEmpty && commentOnEmpty !== 'true' && isEmpty) {
comment.write(commentOnEmpty);
}
if (commentOnSkipped && commentOnSkipped !== 'true' && skippedDiff.added.length) {
comment.write(commentOnSkipped);
}
comment.writeDiff(diff);
comment.writeSkippedDiff(skippedDiff);
comment.writeSkipped(allTests.getSkippedMarkdownList());
if (allTests.count() < 300) {
comment.writeTests(allTests.getMarkdownList());
} else {
comment.writeSuites(allTests.getSuitesMarkdownList());
}
if (isEmpty && !commentOnEmpty) {
console.log('No tests changed, comment not shown');
} else {
await pullRequest.addComment(comment);
}
if (isEmpty && closeOnEmpty) {
await pullRequest.close();
}
if (skippedDiff.added.length && closeOnSkipped) {
await pullRequest.close();
}
// add label
if (core.getInput('has-tests-label')) {
let title = core.getInput('has-tests-label');
title = title === 'true' ? '✔️ has tests' : title;
if (diff.added.length) {
await pullRequest.addLabel(title);
} else {
await pullRequest.removeLabel(title);
}
}
if (core.getInput('no-tests-label')) {
let title = core.getInput('no-tests-label');
title = title === 'true' ? '❌ no tests' : title;
if (diff.added.length) {
await pullRequest.removeLabel(title);
} else {
await pullRequest.addLabel(title);
}
}
} catch (error) {
if (error instanceof CommentError) {
pullRequest.addComment(error.getComment());
}
core.setFailed(error.message);
console.error(error);
}
async function analyzeBase(pr) {
if (!pr) {
return analyzer.getEmptyStats();
}
try {
console.log('Comparing with', pr.base.sha);
await exec.exec('git', ['checkout', pr.base.sha], {
cwd: mainRepoPath,
stdio: 'inherit',
});
analyzer.analyze(pattern);
await exec.exec('git', ['switch', pr.head.ref], {
cwd: mainRepoPath,
stdio: 'inherit',
});
return analyzer.getStats();
} catch (err) {
console.error("Can't calculate base test files");
console.error(err);
return analyzer.getEmptyStats();
}
}
/**
*
* @param {*} pr
* @param {Decorator} decorator
*/
async function createTestDocWikiPage(decorator) {
try {
await exec.exec('git', ['clone', `https://${ghPat}@github.com/${process.env.GITHUB_REPOSITORY}.wiki.git`]);
const isFileUpdated = document.createTestDoc(`${repo}.wiki/${wikiFile}.md`, decorator);
if (isFileUpdated) {
await setTestomatioUserInGit();
await exec.exec('git', ['add', '.'], {
cwd: `${process.cwd()}/${repo}.wiki`,
});
await exec.exec('git', ['commit', '-am', 'Update test docs'], {
cwd: `${process.cwd()}/${repo}.wiki`,
});
await exec.exec('git', ['push', 'origin', 'master'], {
cwd: `${process.cwd()}/${repo}.wiki`,
stdio: 'inherit',
});
}
} catch (err) {
console.error("Can't create test doc PR");
console.error(err);
}
}
async function setTestomatioUserInGit() {
await exec.exec('git', ['config', '--global', 'user.email', '[email protected]']);
await exec.exec('git', ['config', '--global', 'user.name', 'testomatio']);
}
}
run();