-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
107 lines (93 loc) · 3.85 KB
/
index.js
File metadata and controls
107 lines (93 loc) · 3.85 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
98
99
100
101
102
103
104
105
106
107
const core = require('@actions/core');
const {Octokit} = require('@octokit/rest');
const fetch = require("node-fetch");
const {Toolkit} = require('actions-toolkit');
const getDiffWithLineNumbers = require('./git_diff');
const coverageReportToJs = require('./lcov-to-json');
const findUncoveredCodeInPR = require('./analyze');
const createAnnotations = require('./annotations');
const createOrUpdateCheck = require('./check-run');
Toolkit.run(async (tools) => {
try {
const githubToken = core.getInput('token');
const octokit = new Octokit({
previews: ['antiope'],
auth: githubToken,
request: {fetch}
});
const tools = new Toolkit({
token: githubToken,
});
let createData = {
started_at: new Date().toISOString(),
status: 'in_progress',
name: 'Test Coverage Annotate',
};
const eventType = core.getInput('action-type');
console.log('eventType:', eventType);
let PR;
if (eventType === 'workflow_dispatch') {
PR = await octokit.pulls.get({
owner: tools.context.repo.owner,
repo: tools.context.repo.repo,
pull_number: tools.context.payload.inputs.pr_number,
});
PR = PR.data;
} else {
PR = tools.context.payload.pull_request;
};
const response = await createOrUpdateCheck(createData, 'create', tools, PR);
let check_id = response.data.id;
console.log(`Check Successfully Created`, check_id);
let prData = await getDiffWithLineNumbers('HEAD^1');
const coverageReportPath = core.getInput('coverage-info-path');
const noOfCoverageFiles = core.getInput('total-coverage-files');
let coverageJSON = await coverageReportToJs(coverageReportPath, noOfCoverageFiles);
let typesToCover = core.getInput('annotation-type');
typesToCover = typesToCover.split(',').map(item => item.trim());
let untestedLinesOfFiles = await findUncoveredCodeInPR(prData, coverageJSON, typesToCover);
// Create appropriate annotations for uncovered code in files changed by the pull request and not covered with tests
const coverageType = core.getInput('annotation-coverage');
const annotations = createAnnotations(untestedLinesOfFiles, coverageType);
let totalFiles = Object.keys(untestedLinesOfFiles).length;
let totalWarnings = annotations.length;
let updateData = {
check_run_id: check_id,
output: {
title: 'Test Coverage Annotate🔎',
}
};
if (!annotations.length) {
updateData['output'].summary = 'All Good! We found No Uncovered Lines of Code in your Pull Request.🚀';
} else {
let summary = `### Found a Total of ${totalWarnings} Instances of Uncovered Code in ${totalFiles} Files!⚠️\n\n`;
summary += 'File Name | No. of Warnings\n';
summary += '--------- | ---------------\n';
Object.entries(untestedLinesOfFiles).forEach(([filename, untestedStuffArray]) => {
summary += `${filename} | ${untestedStuffArray.length}\n`;
});
updateData['output'].summary = summary;
};
let leftAnnotations = [...annotations];
while (leftAnnotations.length > 0) {
let toProcess = leftAnnotations.splice(0, 50);
updateData.output.annotations = toProcess;
await createOrUpdateCheck(updateData, 'update', tools, PR);
console.log(`Check Successfully Updated.`);
};
// finally close the Check
let completeData = {
conclusion: 'success',
status: 'completed',
completed_at: new Date().toISOString()
}
completeData = { ...updateData, ...completeData };
delete completeData['output'].annotations;
await createOrUpdateCheck(completeData, 'update', tools, PR);
console.log(`Check Successfully Closed`);
} catch (error) {
tools.exit.failure(error.message);
}
// If we got this far things were a success
tools.exit.success('PR Scan and Warn Analysis completed successfully!')
});