forked from selfagency/merge-coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
108 lines (98 loc) · 3.78 KB
/
action.yml
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
name: 'Merge coverage reports'
author: 'selfagency'
description: 'Merge Istanbul coverage reports'
branding:
icon: 'git-merge'
color: 'green'
inputs:
source:
description: 'Path to source code'
required: true
coverage-reports:
description: 'Path to coverage reports'
required: true
output-folder:
description: 'Where to output merged reports'
required: true
formats:
description: 'Comma-separated list of formats to output'
required: false
artifacts:
description: 'Whether to create artifacts'
required: false
default: 'true'
outputs:
report:
description: 'Coverage report'
value: steps.merge-coverage.outputs.report
json-summary:
description: 'JSON coverage summary'
value: steps.merge-coverage.outputs.json
runs:
using: 'composite'
steps:
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
- name: Configure reporters
id: configure-reporters
uses: actions/github-script@v6
if: ${{ !steps.merge-coverage.outputs.skip }}
with:
script: |
const reporters = '${{ inputs.formats }}'.split(',');
if (reporters.length) {
let reporterConfig = '';
reporters.forEach(reporter => {
reporterConfig += `--reporter ${reporter} `;
});
core.setOutput('reporters', reporterConfig);
}
- name: Install dependencies
shell: bash
if: ${{ steps.cache.outputs.cache-hit != 'true' }}
run: npm i -g nyc
- name: Collect coverage reports
uses: actions/github-script@v6
with:
script: |
const {readdir, writeFile, copyFile, mkdir} = require('fs').promises;
await mkdir(`${{ github.workspace }}/coverage`);
const reports = await readdir('${{ inputs.coverage-reports }}', { withFileTypes: true });
const reportDirs = reports.filter(dirent => dirent.isDirectory()).map(dirent => dirent.name);
for (const dir of reportDirs) {
await copyFile(`${{ inputs.coverage-reports }}/${dir}/coverage-final.json`, `${{ github.workspace }}/coverage/${dir}.json`)
};
const reportFiles = reports.filter(dirent => dirent.isFile() && dirent.name.includes('.json')).map(dirent => dirent.name);
for (const file of reportFiles) {
await copyFile(`${{ inputs.coverage-reports }}/${file}`, `${{ github.workspace }}/coverage/${file}.json`)
};
- name: Merge coverage
shell: bash
run: |
nyc merge ${{ github.workspace }}/coverage ${{ github.workspace }}/coverage/coverage-final.json
- name: Output coverage report
shell: bash
working-directory: ${{ inputs.source }}
run: |
nyc report -t ${{ github.workspace }}/coverage --reporter text --reporter json --reporter json-summary ${{ steps.configure-reporters.outputs.reporters }} --report-dir ${{ inputs.output-folder }} | tee ${{ inputs.output-folder }}/coverage-final.txt
report=$(cat ${{ inputs.output-folder }}/coverage-final.txt)
report="${report//'%'/'%25'}"
report="${report//$'\n'/'%0A'}"
report="${report//$'\r'/'%0D'}"
echo -e "::set-output name=report::${report}"
echo -e "::notice::Code Coverage Report%0A%0A${report}"
json=$(cat ${{ inputs.output-folder }}/coverage-summary.json)
json="${json//'%'/'%25'}"
json="${json//$'\n'/'%0A'}"
json="${json//$'\r'/'%0D'}"
echo -e "::set-output name=json::${json}"
- name: Upload coverage reports
uses: actions/upload-artifact@v3
if: ${{ inputs.artifacts == 'true' }}
with:
name: coverage-reports
path: |
${{ inputs.output-folder }}
${{ github.workspace }}/coverage