forked from mydea/ember-cli-code-coverage-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
97 lines (71 loc) · 2.89 KB
/
Copy pathindex.js
File metadata and controls
97 lines (71 loc) · 2.89 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
const { getInput, setFailed } = require('@actions/core');
const { exec } = require('@actions/exec');
const { getOctokit, context }= require('@actions/github');
const fs = require('fs');
const { buildOutput } = require('./lib/build-output');
let octokit;
const repoInfo = context.repo;
async function run() {
try {
let myToken = getInput('repo-token', { required: true });
let testCommand = getInput('test-command', { required: true });
let coverageFilePath = getInput('coverage-file', { required: true });
let coverageIndicator = getInput('coverage-indicator', { required: true });
let workingDirectory = getInput('working-directory', { required: true });
let messageFormat = getInput('message', { required: true });
octokit = getOctokit(myToken);
let pullRequest = await getPullRequest();
let testCoverage = await getTestCoverage({ testCommand, coverageFilePath, coverageIndicator, workingDirectory });
console.log(`
New test coverage: ${testCoverage}%
`);
await exec(`git fetch origin ${pullRequest.base.ref}`);
await exec(`git checkout origin/${pullRequest.base.ref}`);
// This could fail, e.g. if no test coverage existed before
let testCoverageBefore;
try {
testCoverageBefore = await getTestCoverage({testCommand, coverageFilePath, coverageIndicator, workingDirectory});
} catch (error) {
testCoverageBefore = 0;
}
console.log(`
Previous test coverage: ${testCoverageBefore}%
`);
let body = buildOutput(messageFormat,{ testCoverage, testCoverageBefore });
try {
await octokit.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequest.number,
body,
});
} catch (e) {
console.log(`Could not create a comment automatically. This could be because github does not allow writing from actions on a fork.
See https://github.community/t5/GitHub-Actions/Actions-not-working-correctly-for-forks/td-p/35545 for more information.`);
console.log(`Copy and paste the following into a comment yourself if you want to still show the diff:
${body}`);
}
} catch (error) {
setFailed(error.message);
}
}
async function getTestCoverage({ testCommand, coverageFilePath, coverageIndicator, workingDirectory }) {
await exec(testCommand,[],{cwd:workingDirectory});
let coverageFile = fs.readFileSync(coverageFilePath, 'utf-8');
let coverageSummary = JSON.parse(coverageFile);
return coverageSummary.total[coverageIndicator].pct;
}
async function getPullRequest() {
let pr = context.payload.pull_request;
if (!pr) {
console.log('Could not get pull request number from context, exiting');
return;
}
const { data: pullRequest } = await octokit.rest.pulls.get({
owner: pr.base.repo.owner.login,
repo: pr.base.repo.name,
pull_number: pr.number
});
return pullRequest;
}
run();