forked from redis/RedisInsight
-
Notifications
You must be signed in to change notification settings - Fork 0
142 lines (124 loc) · 5.28 KB
/
code-coverage.yml
File metadata and controls
142 lines (124 loc) · 5.28 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
name: 'Code Coverage'
on:
workflow_call:
inputs:
type:
description: Type of report (unit or integration)
type: string
resource_name:
description: Resource name of coverage report
type: string
jobs:
coverage-unit:
runs-on: ubuntu-latest
name: Unit tests coverage
if: ${{ inputs.type == 'unit' }}
steps:
- uses: actions/checkout@v4
- name: Download Coverage Report
uses: actions/download-artifact@v4
with:
name: ${{ inputs.resource_name }}
path: report
- uses: jwalton/gh-find-current-pr@v1
id: findPr
- uses: ArtiomTr/jest-coverage-report-action@v2
with:
prnumber: ${{ steps.findPr.outputs.number }}
coverage-file: report/coverage/report.json
base-coverage-file: report/coverage/report.json
github-token: ${{ secrets.GITHUB_TOKEN }}
skip-step: all
custom-title: Code Coverage - ${{ inputs.resource_name == 'report-be' && 'Backend' || 'Frontend' }} unit tests
coverage-integration:
runs-on: ubuntu-latest
name: Integration tests coverage
if: ${{ inputs.type == 'integration' }}
steps:
- uses: actions/checkout@v4
- name: Download Coverage Report
uses: actions/download-artifact@v4
with:
name: ${{ inputs.resource_name }}
- name: Parse Coverage Summary
id: parse-coverage
run: |
# Extract coverage data from file.
# Example of processed row:
# Statements : 81.75% ( 16130/19730 )
# field '$3' = 81.75%, field '$5' = 16130
extract_coverage_data() {
local keyword=$1
local field=$2
awk "/$keyword/ {print $field}" integration-coverage.txt | tr -d '\n|%'
}
# Determine status based on percentage
get_status() {
if [ "$(echo "$1 < 50" | bc)" -eq 1 ]; then
echo "🔴"
elif [ "$(echo "$1 < 80" | bc)" -eq 1 ]; then
echo "🟡"
else
echo "🟢"
fi
}
# Extract coverage data from the summary
STATEMENTS_PERCENT=$(extract_coverage_data "Statements" '$3')
STATEMENTS_COVERED=$(extract_coverage_data "Statements" '$5')
STATEMENTS_STATUS=$(get_status $STATEMENTS_PERCENT)
BRANCHES_PERCENT=$(extract_coverage_data "Branches" '$3')
BRANCHES_COVERED=$(extract_coverage_data "Branches" '$5')
BRANCHES_STATUS=$(get_status $BRANCHES_PERCENT)
FUNCTIONS_PERCENT=$(extract_coverage_data "Functions" '$3')
FUNCTIONS_COVERED=$(extract_coverage_data "Functions" '$5')
FUNCTIONS_STATUS=$(get_status $FUNCTIONS_PERCENT)
LINES_PERCENT=$(extract_coverage_data "Lines" '$3')
LINES_COVERED=$(extract_coverage_data "Lines" '$5')
LINES_STATUS=$(get_status $LINES_PERCENT)
# Format as a Markdown table
echo "| Status | Category | Percentage | Covered / Total |" > coverage-table.md
echo "|-------------|-------------|-------------|-----------------|" >> coverage-table.md
echo "| $STATEMENTS_STATUS | Statements | ${STATEMENTS_PERCENT}% | ${STATEMENTS_COVERED} |" >> coverage-table.md
echo "| $BRANCHES_STATUS | Branches | ${BRANCHES_PERCENT}% | ${BRANCHES_COVERED} |" >> coverage-table.md
echo "| $FUNCTIONS_STATUS | Functions | ${FUNCTIONS_PERCENT}% | ${FUNCTIONS_COVERED} |" >> coverage-table.md
echo "| $LINES_STATUS | Lines | ${LINES_PERCENT}% | ${LINES_COVERED} |" >> coverage-table.md
- uses: jwalton/gh-find-current-pr@v1
id: findPr
continue-on-error: true
- name: Post or Update Coverage Summary Comment
if: ${{ steps.findPr.outputs.number != '' }}
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const table = fs.readFileSync('coverage-table.md', 'utf8');
const commentBody = `### Code Coverage - Integration Tests\n\n${table}`;
// Fetch existing comments on the pull request
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: process.env.RR_Number,
});
// Check if a comment with the same header already exists
const existingComment = comments.find(comment =>
comment.body.startsWith('### Code Coverage - Integration Tests')
);
if (existingComment) {
// Update the existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: commentBody,
});
} else {
// Create a new comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: process.env.RR_Number,
body: commentBody,
});
}
env:
RR_Number: ${{ steps.findPr.outputs.number }}