Skip to content

Commit ea3d4de

Browse files
authored
Change coverage badge generation (#465)
* test(api) change api workflow * test(api) change api and cli workflow * test(api) test cli * test(api) test cli * test(api) test cli * test(api) test cli * test(api) create coverage github actions * test(api) create coverage github actions * test(api) create coverage github actions * test(api) create coverage github actions * test(api) create coverage github actions * test(api) create coverage github actions * test(api) create coverage github actions * test(api) create coverage github actions * test(api) create coverage github actions * test(api) create coverage github actions * test(api) create coverage github actions * test(api) use coverage github actions in cli * test(api) use coverage github actions in cli * test(api) use coverage github actions in cli * test(api) add Readme to badge generator script
1 parent 5a52fd0 commit ea3d4de

File tree

7 files changed

+335
-64
lines changed

7 files changed

+335
-64
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Coverage Badge Generation
2+
description: Generates a coverage badge and uploades it to an artifact
3+
4+
inputs:
5+
BADGE_ARTIFACT_NAME:
6+
description: "Name of the Badge artifact"
7+
required: true
8+
COVERAGE_REPORT_ARTIFACT:
9+
description: "Name of the XML coverage report artifact"
10+
required: true
11+
COVERAGE_REPORT_NAME:
12+
description: "Name of the XML coverage report file"
13+
required: true
14+
LABEL:
15+
description: "Badge label"
16+
required: true
17+
OUTPUT_FILE:
18+
description: "Name of the output file"
19+
required: true
20+
RED_LIMIT:
21+
description: "Percentage of the red/orange limit"
22+
default: "50"
23+
required: false
24+
GREEN_LIMIT:
25+
description: "Percentage of the orange/green limit"
26+
default: "65"
27+
required: false
28+
29+
runs:
30+
using: "composite"
31+
steps:
32+
- uses: actions/checkout@v4
33+
with:
34+
fetch-depth: 0
35+
36+
- uses: actions/setup-python@v5
37+
with:
38+
python-version: '3.8'
39+
40+
- name: Install requests
41+
shell: bash
42+
run: pip install requests
43+
44+
- name: Install pycobertura
45+
shell: bash
46+
run: pip install pycobertura
47+
48+
- name: Download line coverage reports
49+
uses: actions/download-artifact@v4
50+
with:
51+
name: ${{ inputs.COVERAGE_REPORT_ARTIFACT }}
52+
53+
- name: Generate badge
54+
shell: bash
55+
run: python assets/badge_generator/generate_badge.py --label "${{ inputs.LABEL }}" --output "${{ inputs.OUTPUT_FILE }}" --input-report "${{ inputs.COVERAGE_REPORT_NAME }}" --red-limit "${{ inputs.RED_LIMIT }}" --green-limit "${{ inputs.GREEN_LIMIT }}"
56+
57+
- uses: actions/upload-artifact@v4
58+
with:
59+
name: ${{ inputs.BADGE_ARTIFACT_NAME }}
60+
path: ${{ inputs.OUTPUT_FILE }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Upload Badge
2+
description: Uploads the generated badge to an specific branch
3+
4+
inputs:
5+
BADGE_ARTIFACT_NAME:
6+
description: "Name of the Badge artifact"
7+
required: true
8+
BADGE_FILE_NAME:
9+
description: "Name of the Badge file"
10+
required: true
11+
BRANCH_NAME:
12+
description: "Name of the branch where you want to add the badge"
13+
required: true
14+
github_token:
15+
description: "Github token"
16+
required: true
17+
18+
runs:
19+
using: "composite"
20+
steps:
21+
- uses: actions/checkout@v4
22+
with:
23+
fetch-depth: 0
24+
ref: ${{ inputs.BRANCH_NAME }}
25+
26+
- name: Download coverage badge
27+
uses: actions/download-artifact@v4
28+
with:
29+
name: ${{ inputs.BADGE_ARTIFACT_NAME }}
30+
31+
- name: Verify Changed files
32+
uses: tj-actions/verify-changed-files@v16
33+
id: verify-changed-files
34+
with:
35+
files: ${{ inputs.BADGE_FILE_NAME }}
36+
37+
- name: Commit badge
38+
if: steps.verify-changed-files.outputs.files_changed == 'true'
39+
shell: bash
40+
run: |
41+
git config --local user.email "<>"
42+
git config --local user.name "GitHubActions"
43+
git add ${{ inputs.BADGE_FILE_NAME }}
44+
git commit -m "Add/Update badge"
45+
46+
- name: Push badge commit
47+
if: steps.verify-changed-files.outputs.files_changed == 'true'
48+
uses: ad-m/github-push-action@master
49+
with:
50+
github_token: ${{ inputs.github_token }}
51+
branch: ${{ inputs.BRANCH_NAME }} # Dedicated branch to store coverage badges
52+
53+
- uses: actions/checkout@v4 # we checkout to main so we have access the actions folder and we can execute the Post Upload
54+
with:
55+
fetch-depth: "0"

.github/workflows/api-unit-test.yml

+29-32
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ jobs:
6868
name: coverage_api_xml
6969
path: ./API/coverage_api.xml
7070

71-
coverage-badge:
71+
generate-coverage-badge:
7272
needs: api-unit-test
7373
runs-on: ubuntu-latest
7474
permissions:
@@ -81,41 +81,38 @@ jobs:
8181
steps:
8282
- uses: actions/checkout@v4
8383
with:
84-
fetch-depth: 0
85-
ref: coverage_badges
84+
fetch-depth: "0"
8685

87-
- uses: actions/setup-python@v5
86+
- name: Generate Badge
87+
uses: ./.github/actions/coverage/generate-badge
8888
with:
89-
python-version: '3.8'
90-
91-
- name: Install genbadge
92-
run: pip install genbadge[coverage]
89+
COVERAGE_REPORT_ARTIFACT: coverage_api_xml
90+
COVERAGE_REPORT_NAME: coverage_api.xml
91+
LABEL: "API coverage"
92+
OUTPUT_FILE: api_coverage_badge.svg
93+
RED_LIMIT: "50"
94+
GREEN_LIMIT: "65"
95+
BADGE_ARTIFACT_NAME: api_coverage_badge
96+
97+
upload-coverage-badge:
98+
needs: generate-coverage-badge
99+
runs-on: ubuntu-latest
100+
permissions:
101+
contents: write
102+
defaults:
103+
run:
104+
working-directory: ./
105+
if: github.ref == 'refs/heads/main' && github.event.head_commit.author.name != 'GitHubActions'
93106

94-
- name: Download line coverage reports
95-
uses: actions/download-artifact@v4
107+
steps:
108+
- uses: actions/checkout@v4
96109
with:
97-
name: coverage_api_xml
98-
99-
- name: Generate badge
100-
run: genbadge coverage -i coverage_api.xml -n "API coverage" -o api_coverage_badge.svg
110+
fetch-depth: "0"
101111

102-
- name: Verify Changed files
103-
uses: tj-actions/verify-changed-files@v16
104-
id: verify-changed-files
105-
with:
106-
files: api_coverage_badge.svg
107-
108-
- name: Commit badge
109-
if: steps.verify-changed-files.outputs.files_changed == 'true'
110-
run: |
111-
git config --local user.email "<>"
112-
git config --local user.name "GitHubActions"
113-
git add api_coverage_badge.svg
114-
git commit -m "Add/Update badge"
115-
116-
- name: Push badge commit
117-
if: steps.verify-changed-files.outputs.files_changed == 'true'
118-
uses: ad-m/github-push-action@master
112+
- name: Upload Badge
113+
uses: ./.github/actions/coverage/upload-badge
119114
with:
115+
BADGE_ARTIFACT_NAME: api_coverage_badge
116+
BADGE_FILE_NAME: api_coverage_badge.svg
117+
BRANCH_NAME: coverage_badges
120118
github_token: ${{ secrets.GITHUB_TOKEN }}
121-
branch: coverage_badges # Dedicated branch to store coverage badges

.github/workflows/cli-unit-test.yml

+31-32
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ name: 🕵️‍♂️ CLI Unit Tests
1111
jobs:
1212
cli-unit-test:
1313
runs-on: ubuntu-latest
14+
# permissions:
15+
# contents: write
1416
defaults:
1517
run:
1618
working-directory: ./CLI
@@ -63,7 +65,7 @@ jobs:
6365
name: coverage_cli_xml
6466
path: ./CLI/coverage_cli.xml
6567

66-
coverage-badge:
68+
generate-coverage-badge:
6769
needs: cli-unit-test
6870
runs-on: ubuntu-latest
6971
permissions:
@@ -76,41 +78,38 @@ jobs:
7678
steps:
7779
- uses: actions/checkout@v4
7880
with:
79-
fetch-depth: 0
80-
ref: coverage_badges
81+
fetch-depth: "0"
8182

82-
- uses: actions/setup-python@v5
83+
- name: Generate Badge
84+
uses: ./.github/actions/coverage/generate-badge
8385
with:
84-
python-version: '3.8'
85-
86-
- name: Install genbadge
87-
run: pip install genbadge[coverage]
86+
COVERAGE_REPORT_ARTIFACT: coverage_cli_xml
87+
COVERAGE_REPORT_NAME: coverage_cli.xml
88+
LABEL: "CLI coverage"
89+
OUTPUT_FILE: cli_coverage_badge.svg
90+
RED_LIMIT: "50"
91+
GREEN_LIMIT: "65"
92+
BADGE_ARTIFACT_NAME: cli_coverage_badge
93+
94+
upload-coverage-badge:
95+
needs: generate-coverage-badge
96+
runs-on: ubuntu-latest
97+
permissions:
98+
contents: write
99+
defaults:
100+
run:
101+
working-directory: ./
102+
if: github.ref == 'refs/heads/main' && github.event.head_commit.author.name != 'GitHubActions'
88103

89-
- name: Download line coverage reports
90-
uses: actions/download-artifact@v4
104+
steps:
105+
- uses: actions/checkout@v4
91106
with:
92-
name: coverage_cli_xml
93-
94-
- name: Generate badge
95-
run: genbadge coverage -i coverage_cli.xml -n "CLI coverage" -o cli_coverage_badge.svg
107+
fetch-depth: "0"
96108

97-
- name: Verify Changed files
98-
uses: tj-actions/verify-changed-files@v16
99-
id: verify-changed-files
100-
with:
101-
files: cli_coverage_badge.svg
102-
103-
- name: Commit badge
104-
if: steps.verify-changed-files.outputs.files_changed == 'true'
105-
run: |
106-
git config --local user.email "<>"
107-
git config --local user.name "GitHubActions"
108-
git add cli_coverage_badge.svg
109-
git commit -m "Add/Update CLI badge"
110-
111-
- name: Push badge commit
112-
if: steps.verify-changed-files.outputs.files_changed == 'true'
113-
uses: ad-m/github-push-action@master
109+
- name: Upload Badge
110+
uses: ./.github/actions/coverage/upload-badge
114111
with:
112+
BADGE_ARTIFACT_NAME: cli_coverage_badge
113+
BADGE_FILE_NAME: cli_coverage_badge.svg
114+
BRANCH_NAME: coverage_badges
115115
github_token: ${{ secrets.GITHUB_TOKEN }}
116-
branch: coverage_badges # Dedicated branch to store coverage badges

assets/badge_generator/ReadMe.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Badge Generator
2+
Script that generates a coverage badge. You can customize the following parameters:
3+
4+
- The left label that will appear in the badge
5+
- The color range limits. By default, the badge will be red between 0-50%, orange between 50-65% and green if higher than 65
6+
- The output file name
7+
8+
The coverage percentage be passed in two different ways:
9+
10+
- By giving the desired value using the flag `--coverage`
11+
- By indicating the path to the XML coverage report using the flag `--input-report`
12+
13+
Installation
14+
------------
15+
```bash
16+
# Generate python virtual environment and activate it
17+
python3 -m venv venv
18+
source venv/bin/activate
19+
20+
# Install requirements
21+
pip install -r requirements.txt
22+
```
23+
24+
Examples
25+
------------
26+
27+
```bash
28+
# Run help
29+
python generate_badge.py --help
30+
31+
# Generate badge with default values and indicating the coverage percentage
32+
python generate_badge.py --coverage 75
33+
34+
# Generate with xml report file
35+
python generate_badge.py --input-report coverage.xml
36+
37+
# Generate badge with multiple parameters
38+
python generate_badge.py --label "API coverage" --input-report coverage.xml --output coverage_api.svg --red-limit 55 --green-limit 70
39+
```

0 commit comments

Comments
 (0)