Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#861: add code coverage comments to PRs #862

Merged
merged 22 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0de4fd3
#861: add code coverage comments to PRs
JoernBerkefeld Apr 17, 2023
9a94cb8
#861: fix path issue
JoernBerkefeld Apr 17, 2023
d6e0c52
#861: splitting off CodeQL
JoernBerkefeld Apr 17, 2023
c5ac57e
#861: add template file for coverage comments
JoernBerkefeld Apr 17, 2023
8d5fecc
#861: attempt with all coverage ymls
JoernBerkefeld Apr 17, 2023
fbb6683
#861: switch to coverage task for report and run npm ci instead of npm i
JoernBerkefeld Apr 17, 2023
304701e
#861: remove unneded code
JoernBerkefeld Apr 17, 2023
648c5eb
#861: remove configuration in favor of github default
JoernBerkefeld Apr 17, 2023
1ee45a9
#861: rename testing workflow to not clash with codeQL standards
JoernBerkefeld Apr 17, 2023
d193a62
#861: ensure coverage/ is created
JoernBerkefeld Apr 17, 2023
265b40d
#861: fix search_artifacts param of dawidd6/action-download-artifact@v2
JoernBerkefeld Apr 17, 2023
0e77a96
#861: upgrade actions/upload-artifact v2 to v3
JoernBerkefeld Apr 17, 2023
34d0d2c
#861: bump andstor/file-existence-action from v1 to 2
JoernBerkefeld Apr 17, 2023
68aaa46
#861: include actions/setup-node@v3
JoernBerkefeld Apr 17, 2023
e42def8
#861: disable custom fetch
JoernBerkefeld Apr 17, 2023
23143df
#861: fix path to coverage action
JoernBerkefeld Apr 17, 2023
270747f
#861: fix file references and install svelte on-the-fly
JoernBerkefeld Apr 17, 2023
8a4dfa2
Revert "#861: disable custom fetch"
JoernBerkefeld Apr 17, 2023
a53a022
#861: group report coverage preparation tasks + fix file path
JoernBerkefeld Apr 17, 2023
1950362
#861: split npm executions
JoernBerkefeld Apr 17, 2023
0f2e7d3
#861: correct order of code execution
JoernBerkefeld Apr 17, 2023
fe3a5f6
#861: handle develop and main branch separately AND fix coverate-base…
JoernBerkefeld Apr 17, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .coverage-comment-template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Coverage Report

Commit: [{{short_commit_sha}}]({{commit_link}})
Base: [{{base_ref}}@{{base_short_commit_sha}}]({{base_commit_link}})

| Type | Base | This PR |
| ------------------------- | ------------------------------------------ | ---------------------------------------------------------------------------------- |
| Total Statements Coverage | {{base_total_statements_coverage_percent}} | {{total_statements_coverage_percent}} ({{total_statements_coverage_percent_diff}}) |
| Total Branches Coverage | {{base_total_branches_coverage_percent}} | {{total_branches_coverage_percent}} ({{total_branches_coverage_percent_diff}}) |
| Total Functions Coverage | {{base_total_functions_coverage_percent}} | {{total_functions_coverage_percent}} ({{total_functions_coverage_percent_diff}}) |
| Total Lines Coverage | {{base_total_lines_coverage_percent}} | {{total_lines_coverage_percent}} ({{total_lines_coverage_percent_diff}}) |

<details>
<summary>Details (changed files)</summary>
{{changed_files_coverage_table}}
</details>
<details>
<summary>Details (all files)</summary>
{{files_coverage_table}}
</details>
178 changes: 178 additions & 0 deletions .coverage-comment-template.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<script>
export let total_statements_coverage_percent_raw;
export let total_branches_coverage_percent_raw;
export let total_functions_coverage_percent_raw;
export let total_lines_coverage_percent_raw;

export let base_total_statements_coverage_percent_raw;
export let base_total_branches_coverage_percent_raw;
export let base_total_functions_coverage_percent_raw;
export let base_total_lines_coverage_percent_raw;

export let commit_sha;
export let short_commit_sha;
export let commit_link;

export let base_commit_sha;
export let base_short_commit_sha;
export let base_commit_link;
export let base_ref;

export let changed_files_coverage_data;

const has_base_data = base_total_branches_coverage_percent_raw !== '?';
const summary_list = [
{
type: 'Total Statements Coverage',
percent: {
total: total_statements_coverage_percent_raw,
base: has_base_data ? base_total_statements_coverage_percent_raw: null,
diff: has_base_data ? (total_statements_coverage_percent_raw - base_total_statements_coverage_percent_raw) : null,
}
},
{
type: 'Total Branches Coverage',
percent: {
total: total_branches_coverage_percent_raw,
base: has_base_data ? base_total_branches_coverage_percent_raw: null,
diff: has_base_data ? (total_branches_coverage_percent_raw - base_total_branches_coverage_percent_raw) : null,
}
},
{
type: 'Total Functions Coverage',
percent: {
total: total_functions_coverage_percent_raw,
base: has_base_data ? base_total_functions_coverage_percent_raw: null,
diff: has_base_data ? (total_functions_coverage_percent_raw - base_total_functions_coverage_percent_raw) : null,
}
},
{
type: 'Total Lines Coverage',
percent: {
total: total_lines_coverage_percent_raw,
base: has_base_data ? base_total_lines_coverage_percent_raw: null,
diff: has_base_data ? (total_lines_coverage_percent_raw - base_total_lines_coverage_percent_raw) : null,
}
}];

const LETTER_LABEL = {
S: 'Statements',
B: 'Branches',
F: 'Functions',
L: 'Lines',
};

const COVERAGE_LEVEL_IMAGE = {
low: 'https://user-images.githubusercontent.com/11299391/159445221-fe3dc085-8c56-4e03-9642-219784c88fe7.svg',
medium:
'https://user-images.githubusercontent.com/11299391/159445212-f135c6d7-f354-4e8c-9a9f-28bb3ff1b7b5.svg',
high: 'https://user-images.githubusercontent.com/11299391/159445220-d88b3624-0814-4664-80c8-09f0f2b8e68b.svg',
};

function formatPercentDiff(percent) {
if (!Number.isFinite(percent)) {
return '';
}

const roundedPercent = `${Number(percent.toFixed(2))}%`;

if (percent >= 0) {
return '+' + roundedPercent;
}

return roundedPercent;
}

function getCoverageLevelImage(percent) {
// https://github.com/istanbuljs/istanbuljs/blob/c1559005b3bb318da01f505740adb0e782aaf14e/packages/istanbul-lib-report/lib/watermarks.js
if (percent >= 80) {
return COVERAGE_LEVEL_IMAGE.high;
} else if (percent >= 50) {
return COVERAGE_LEVEL_IMAGE.medium;
} else {
return COVERAGE_LEVEL_IMAGE.low;
}
}

function getFilePrefix() {
return `../blob/${commit_sha}/`;
}
</script>

<h2>Coverage Report</h2>

Commit: <a href="{commit_link}">{short_commit_sha}</a><br>
Base: <a href="{base_commit_link}">{base_ref}@{base_short_commit_sha}</a><br><br>

<table>
<thead>
<th>Type</th>
{#if has_base_data}
<th>Base</th>
{/if}
<th>This PR</th>
</thead>
<tbody>
{#each summary_list as {type, percent}}
<tr>
<td>{type}</td>
{#if has_base_data}
<td>
{#if Number.isFinite(percent.base)}
<img src="{getCoverageLevelImage(percent.base)}" alt="">&nbsp;{percent.base}%
{:else}
-
{/if}
</td>
{/if}
<td>
{#if Number.isFinite(percent.total)}
<img src="{getCoverageLevelImage(percent.total)}" alt="">&nbsp;{percent.total}%
{#if has_base_data}
&nbsp;({formatPercentDiff(percent.diff)})
{/if}
{:else}
-
{/if}
</td>
</tr>
{/each}
</tbody>
</table>

<details>
<summary>Details (changed files):</summary><br>
<table>
<thead>
<th>File</th>
<th>Statements</th>
<th>Branches</th>
<th>Functions</th>
<th>Lines</th>
</thead>
<tbody>
{#each changed_files_coverage_data as [file, data]}
{@const percents = [
data.statements.pct,
data.branches.pct,
data.functions.pct,
data.lines.pct
]}
<tr>
<td>
<a href="{getFilePrefix()}{file}">{file}</a>
</td>
{#each percents as percent}
<td>
{#if Number.isFinite(percent)}
<img src="{getCoverageLevelImage(percent)}" alt="">&nbsp;{percent}%
{:else}
-
{/if}
</td>
{/each}
</tr>
{/each}
</tbody>
</table>
</details>
59 changes: 0 additions & 59 deletions .github/workflows/code-analysis.yml

This file was deleted.

36 changes: 36 additions & 0 deletions .github/workflows/code-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Custom tests for mcdev
#
name: 'Code Testing'

on:
push:
branches: [main, develop, hotfix]
pull_request:
# The branches below must be a subset of the branches above
branches: [main, develop, hotfix]

jobs:
testAndCoverage:
name: lint & test
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- uses: actions/setup-node@v3
with:
node-version: 16
registry-url: https://registry.npmjs.org/

- run: npm ci --ignore-scripts

- run: npm run lint

# Assuming code passes, run tests
- name: Run mcdev-tests
run: npm run test
55 changes: 55 additions & 0 deletions .github/workflows/coverage-base-update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Update coverage comment
on:
pull_request:
types: [edited]

jobs:
hello_world_job:
runs-on: ubuntu-latest
name: Test and report
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 1000

- name: Fetch base for git diff
run: git fetch origin ${{ github.event.pull_request.base.ref }} --depth=1000

- name: Download artifact
uses: dawidd6/action-download-artifact@v2
continue-on-error: true
with:
workflow: ${{ github.event.pull_request.base.ref == 'main' && 'coverage-main-branch.yml' || github.event.pull_request.base.ref == 'develop' && 'coverage-develop-branch.yml' || 'coverage.yml' }}
workflow_conclusion: success
commit: ${{github.event.pull_request.base.sha}}
name: 'test-coverage-output'
path: base-artifacts
search_artifacts: true

- name: Download artifact
uses: dawidd6/action-download-artifact@v2
continue-on-error: true
with:
workflow: 'coverage.yml'
workflow_conclusion: success
commit: ${{github.event.pull_request.head.sha}}
name: 'test-coverage-output'
path: artifacts
search_artifacts: true

- name: Check file existence
id: check_files
uses: andstor/file-existence-action@v2
with:
files: 'base-artifacts/coverage-summary.json, artifacts/coverage-summary.json'

- name: File exists
uses: sidx1024/report-nyc-coverage-github-action@v1.2.6
if: steps.check_files.outputs.files_exists == 'true'
# Only runs if all of the files exists
with:
coverage_file: 'artifacts/coverage-summary.json'
base_coverage_file: 'base-artifacts/coverage-summary.json'
comment_template_file: '.coverage-comment-template.svelte'
35 changes: 35 additions & 0 deletions .github/workflows/coverage-develop-branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Test coverage for develop branch

on:
push:
branches:
- develop

jobs:
hello_world_job:
runs-on: ubuntu-latest
name: Test and report
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 1000

- name: Fetch base
run: git fetch origin ${{ github.event.pull_request.base.ref }} --depth=1000

- uses: actions/setup-node@v3
with:
node-version: 16

- name: Run tests
run: |
npm ci --ignore-scripts
npm run coverage

- name: Upload coverage artifact
uses: actions/upload-artifact@v3
with:
name: test-coverage-output
path: coverage
Loading