Skip to content

Commit b697b32

Browse files
KrumTypawelangelow
authored andcommitted
RI-7038: Update Github flow to show code coverage reports to each PR (#4555)
* RI-7038: add code coverage summary for FE tests * temp: trigger code change * update workflow * add jest coverage report * update workflows * update workflow * update workflow * update workflow file * update workflow * update workflow * update workflow * update workflow * update workflow * update workflow * update workflows * update code coverage title * remove comment * add integration tests code coverage * fix workflow * update integration workflow * update integration workflow * debug integration workflow * update workflow * remove debug section * update integration tests coverage markdown * remove dep install for jest test coverage * update integration flow and formatting * refactor workflows * update workflow * revert temp code change * RI-7038: apply review suggestions
1 parent bb21ed0 commit b697b32

File tree

5 files changed

+174
-3
lines changed

5 files changed

+174
-3
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
name: 'Code Coverage'
2+
on:
3+
workflow_call:
4+
inputs:
5+
type:
6+
description: Type of report (unit or integration)
7+
type: string
8+
resource_name:
9+
description: Resource name of coverage report
10+
type: string
11+
12+
jobs:
13+
coverage-unit:
14+
runs-on: ubuntu-latest
15+
name: Unit tests coverage
16+
if: ${{ inputs.type == 'unit' }}
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- name: Download Coverage Report
21+
uses: actions/download-artifact@v4
22+
with:
23+
name: ${{ inputs.resource_name }}
24+
path: report
25+
26+
- uses: jwalton/gh-find-current-pr@v1
27+
id: findPr
28+
29+
- uses: ArtiomTr/jest-coverage-report-action@v2
30+
with:
31+
prnumber: ${{ steps.findPr.outputs.number }}
32+
coverage-file: report/coverage/report.json
33+
base-coverage-file: report/coverage/report.json
34+
github-token: ${{ secrets.GITHUB_TOKEN }}
35+
skip-step: all
36+
custom-title: Code Coverage - ${{ inputs.resource_name == 'report-be' && 'Backend' || 'Frontend' }} unit tests
37+
38+
coverage-integration:
39+
runs-on: ubuntu-latest
40+
name: Integration tests coverage
41+
if: ${{ inputs.type == 'integration' }}
42+
steps:
43+
- uses: actions/checkout@v4
44+
45+
- name: Download Coverage Report
46+
uses: actions/download-artifact@v4
47+
with:
48+
name: ${{ inputs.resource_name }}
49+
50+
- name: Parse Coverage Summary
51+
id: parse-coverage
52+
run: |
53+
# Extract coverage data from file.
54+
# Example of processed row:
55+
# Statements : 81.75% ( 16130/19730 )
56+
# field '$3' = 81.75%, field '$5' = 16130
57+
extract_coverage_data() {
58+
local keyword=$1
59+
local field=$2
60+
awk "/$keyword/ {print $field}" integration-coverage.txt | tr -d '\n|%'
61+
}
62+
63+
# Determine status based on percentage
64+
get_status() {
65+
if [ "$(echo "$1 < 50" | bc)" -eq 1 ]; then
66+
echo "🔴"
67+
elif [ "$(echo "$1 < 80" | bc)" -eq 1 ]; then
68+
echo "🟡"
69+
else
70+
echo "🟢"
71+
fi
72+
}
73+
74+
# Extract coverage data from the summary
75+
STATEMENTS_PERCENT=$(extract_coverage_data "Statements" '$3')
76+
STATEMENTS_COVERED=$(extract_coverage_data "Statements" '$5')
77+
STATEMENTS_STATUS=$(get_status $STATEMENTS_PERCENT)
78+
79+
BRANCHES_PERCENT=$(extract_coverage_data "Branches" '$3')
80+
BRANCHES_COVERED=$(extract_coverage_data "Branches" '$5')
81+
BRANCHES_STATUS=$(get_status $BRANCHES_PERCENT)
82+
83+
FUNCTIONS_PERCENT=$(extract_coverage_data "Functions" '$3')
84+
FUNCTIONS_COVERED=$(extract_coverage_data "Functions" '$5')
85+
FUNCTIONS_STATUS=$(get_status $FUNCTIONS_PERCENT)
86+
87+
LINES_PERCENT=$(extract_coverage_data "Lines" '$3')
88+
LINES_COVERED=$(extract_coverage_data "Lines" '$5')
89+
LINES_STATUS=$(get_status $LINES_PERCENT)
90+
91+
# Format as a Markdown table
92+
echo "| Status | Category | Percentage | Covered / Total |" > coverage-table.md
93+
echo "|-------------|-------------|-------------|-----------------|" >> coverage-table.md
94+
echo "| $STATEMENTS_STATUS | Statements | ${STATEMENTS_PERCENT}% | ${STATEMENTS_COVERED} |" >> coverage-table.md
95+
echo "| $BRANCHES_STATUS | Branches | ${BRANCHES_PERCENT}% | ${BRANCHES_COVERED} |" >> coverage-table.md
96+
echo "| $FUNCTIONS_STATUS | Functions | ${FUNCTIONS_PERCENT}% | ${FUNCTIONS_COVERED} |" >> coverage-table.md
97+
echo "| $LINES_STATUS | Lines | ${LINES_PERCENT}% | ${LINES_COVERED} |" >> coverage-table.md
98+
99+
- uses: jwalton/gh-find-current-pr@v1
100+
id: findPr
101+
102+
- name: Post or Update Coverage Summary Comment
103+
uses: actions/github-script@v7
104+
with:
105+
script: |
106+
const fs = require('fs');
107+
const table = fs.readFileSync('coverage-table.md', 'utf8');
108+
const commentBody = `### Code Coverage - Integration Tests\n\n${table}`;
109+
110+
// Fetch existing comments on the pull request
111+
const { data: comments } = await github.rest.issues.listComments({
112+
owner: context.repo.owner,
113+
repo: context.repo.repo,
114+
issue_number: process.env.RR_Number,
115+
});
116+
117+
// Check if a comment with the same header already exists
118+
const existingComment = comments.find(comment =>
119+
comment.body.startsWith('### Code Coverage - Integration Tests')
120+
);
121+
122+
if (existingComment) {
123+
// Update the existing comment
124+
await github.rest.issues.updateComment({
125+
owner: context.repo.owner,
126+
repo: context.repo.repo,
127+
comment_id: existingComment.id,
128+
body: commentBody,
129+
});
130+
} else {
131+
// Create a new comment
132+
await github.rest.issues.createComment({
133+
owner: context.repo.owner,
134+
repo: context.repo.repo,
135+
issue_number: process.env.RR_Number,
136+
body: commentBody,
137+
});
138+
}
139+
env:
140+
RR_Number: ${{ steps.findPr.outputs.number }}

.github/workflows/tests-integration.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,14 @@ jobs:
191191
sudo mkdir -p /usr/src/app
192192
sudo cp -a ./redisinsight/api/. /usr/src/app/
193193
sudo cp -R ./coverages /usr/src/app && sudo chmod 777 -R /usr/src/app
194-
cd /usr/src/app && npx nyc report -t ./coverages -r text -r text-summary
194+
cd /usr/src/app && npx nyc report -t ./coverages -r text -r text-summary > integration-coverage.txt
195+
cp integration-coverage.txt $GITHUB_WORKSPACE/integration-coverage.txt
196+
197+
- name: Upload integration-coverage as artifact
198+
uses: actions/upload-artifact@v4
199+
with:
200+
name: integration-coverage
201+
path: integration-coverage.txt
195202

196203
- name: Delete Artifact
197204
uses: actions/github-script@v7

.github/workflows/tests.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,28 @@ jobs:
9494
uses: ./.github/workflows/tests-frontend.yml
9595
secrets: inherit
9696

97+
frontend-tests-coverage:
98+
needs: frontend-tests
99+
uses: ./.github/workflows/code-coverage.yml
100+
secrets: inherit
101+
with:
102+
resource_name: report-fe
103+
type: unit
104+
97105
backend-tests:
98106
needs: changes
99107
if: inputs.group_tests == 'all' || inputs.group_tests == 'without_e2e' || startsWith(github.ref_name, 'be/') || startsWith(github.ref_name, 'fe-be/') || startsWith(github.ref_name, 'feature/') || startsWith(github.ref_name, 'bugfix/') || startsWith(github.ref_name, 'ric/')
100108
uses: ./.github/workflows/tests-backend.yml
101109
secrets: inherit
102110

111+
backend-tests-coverage:
112+
needs: backend-tests
113+
uses: ./.github/workflows/code-coverage.yml
114+
secrets: inherit
115+
with:
116+
resource_name: report-be
117+
type: unit
118+
103119
integration-tests:
104120
needs: changes
105121
if: inputs.group_tests == 'all' || inputs.group_tests == 'without_e2e' || startsWith(github.ref_name, 'be/') || startsWith(github.ref_name, 'fe-be/') || startsWith(github.ref_name, 'feature/') || startsWith(github.ref_name, 'bugfix/') || startsWith(github.ref_name, 'ric/')
@@ -110,6 +126,14 @@ jobs:
110126
redis_client: ${{ inputs.redis_client || '' }}
111127
debug: ${{ inputs.debug || false }}
112128

129+
integration-tests-coverage:
130+
needs: integration-tests
131+
uses: ./.github/workflows/code-coverage.yml
132+
secrets: inherit
133+
with:
134+
resource_name: integration-coverage
135+
type: integration
136+
113137
# # E2E Approve
114138
e2e-approve:
115139
runs-on: ubuntu-latest

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"test:api": "yarn --cwd redisinsight/api test",
4949
"test:api:integration": "yarn --cwd redisinsight/api test:api",
5050
"test:watch": "jest ./redisinsight/ui --watch -w 1",
51-
"test:cov": "cross-env NODE_OPTIONS='' jest ./redisinsight/ui --silent --coverage --no-cache --forceExit -w 3",
51+
"test:cov": "cross-env NODE_OPTIONS='' jest ./redisinsight/ui --testLocationInResults --json --outputFile=\"report/coverage/report.json\" --silent --coverage --no-cache --forceExit -w 3",
5252
"test:cov:unit": "jest ./redisinsight/ui --group=-component --coverage -w 1",
5353
"test:cov:component": "jest ./redisinsight/ui --group=component --coverage -w 1",
5454
"type-check:ui": "tsc --project redisinsight/ui --noEmit"

redisinsight/api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"start:prod": "cross-env NODE_ENV=production node dist/src/main",
3030
"test": "cross-env NODE_ENV=test ./node_modules/.bin/jest -w 1",
3131
"test:watch": "cross-env NODE_ENV=test jest --watch -w 1",
32-
"test:cov": "cross-env NODE_ENV=test ./node_modules/.bin/jest --forceExit --coverage --runInBand",
32+
"test:cov": "cross-env NODE_ENV=test ./node_modules/.bin/jest --testLocationInResults --json --outputFile=\"report/coverage/report.json\" --forceExit --coverage --runInBand",
3333
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand -w 1",
3434
"test:e2e": "jest --config ./test/jest-e2e.json -w 1",
3535
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js -d ./config/ormconfig.ts",

0 commit comments

Comments
 (0)