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

Change coverage badge generation #465

Merged
merged 21 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
60 changes: 60 additions & 0 deletions .github/actions/coverage/generate-badge/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Coverage Badge Generation
description: Generates a coverage badge and uploades it to an artifact

inputs:
BADGE_ARTIFACT_NAME:
description: "Name of the Badge artifact"
required: true
COVERAGE_REPORT_ARTIFACT:
description: "Name of the XML coverage report artifact"
required: true
COVERAGE_REPORT_NAME:
description: "Name of the XML coverage report file"
required: true
LABEL:
description: "Badge label"
required: true
OUTPUT_FILE:
description: "Name of the output file"
required: true
RED_LIMIT:
description: "Percentage of the red/orange limit"
default: "50"
required: false
GREEN_LIMIT:
description: "Percentage of the orange/green limit"
default: "65"
required: false

runs:
using: "composite"
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-python@v5
with:
python-version: '3.8'

- name: Install requests
shell: bash
run: pip install requests

- name: Install pycobertura
shell: bash
run: pip install pycobertura

- name: Download line coverage reports
uses: actions/download-artifact@v4
with:
name: ${{ inputs.COVERAGE_REPORT_ARTIFACT }}

- name: Generate badge
shell: bash
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 }}"

- uses: actions/upload-artifact@v4
with:
name: ${{ inputs.BADGE_ARTIFACT_NAME }}
path: ${{ inputs.OUTPUT_FILE }}
55 changes: 55 additions & 0 deletions .github/actions/coverage/upload-badge/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Upload Badge
description: Uploads the generated badge to an specific branch

inputs:
BADGE_ARTIFACT_NAME:
description: "Name of the Badge artifact"
required: true
BADGE_FILE_NAME:
description: "Name of the Badge file"
required: true
BRANCH_NAME:
description: "Name of the branch where you want to add the badge"
required: true
github_token:
description: "Github token"
required: true

runs:
using: "composite"
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ inputs.BRANCH_NAME }}

- name: Download coverage badge
uses: actions/download-artifact@v4
with:
name: ${{ inputs.BADGE_ARTIFACT_NAME }}

- name: Verify Changed files
uses: tj-actions/verify-changed-files@v16
id: verify-changed-files
with:
files: ${{ inputs.BADGE_FILE_NAME }}

- name: Commit badge
if: steps.verify-changed-files.outputs.files_changed == 'true'
shell: bash
run: |
git config --local user.email "<>"
git config --local user.name "GitHubActions"
git add ${{ inputs.BADGE_FILE_NAME }}
git commit -m "Add/Update badge"
- name: Push badge commit
if: steps.verify-changed-files.outputs.files_changed == 'true'
uses: ad-m/github-push-action@master
with:
github_token: ${{ inputs.github_token }}
branch: ${{ inputs.BRANCH_NAME }} # Dedicated branch to store coverage badges

- uses: actions/checkout@v4 # we checkout to main so we have access the actions folder and we can execute the Post Upload
with:
fetch-depth: "0"
61 changes: 29 additions & 32 deletions .github/workflows/api-unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
name: coverage_api_xml
path: ./API/coverage_api.xml

coverage-badge:
generate-coverage-badge:
needs: api-unit-test
runs-on: ubuntu-latest
permissions:
Expand All @@ -81,41 +81,38 @@ jobs:
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: coverage_badges
fetch-depth: "0"

- uses: actions/setup-python@v5
- name: Generate Badge
uses: ./.github/actions/coverage/generate-badge
with:
python-version: '3.8'

- name: Install genbadge
run: pip install genbadge[coverage]
COVERAGE_REPORT_ARTIFACT: coverage_api_xml
COVERAGE_REPORT_NAME: coverage_api.xml
LABEL: "API coverage"
OUTPUT_FILE: api_coverage_badge.svg
RED_LIMIT: "50"
GREEN_LIMIT: "65"
BADGE_ARTIFACT_NAME: api_coverage_badge

upload-coverage-badge:
needs: generate-coverage-badge
runs-on: ubuntu-latest
permissions:
contents: write
defaults:
run:
working-directory: ./
if: github.ref == 'refs/heads/main' && github.event.head_commit.author.name != 'GitHubActions'

- name: Download line coverage reports
uses: actions/download-artifact@v4
steps:
- uses: actions/checkout@v4
with:
name: coverage_api_xml

- name: Generate badge
run: genbadge coverage -i coverage_api.xml -n "API coverage" -o api_coverage_badge.svg
fetch-depth: "0"

- name: Verify Changed files
uses: tj-actions/verify-changed-files@v16
id: verify-changed-files
with:
files: api_coverage_badge.svg

- name: Commit badge
if: steps.verify-changed-files.outputs.files_changed == 'true'
run: |
git config --local user.email "<>"
git config --local user.name "GitHubActions"
git add api_coverage_badge.svg
git commit -m "Add/Update badge"

- name: Push badge commit
if: steps.verify-changed-files.outputs.files_changed == 'true'
uses: ad-m/github-push-action@master
- name: Upload Badge
uses: ./.github/actions/coverage/upload-badge
with:
BADGE_ARTIFACT_NAME: api_coverage_badge
BADGE_FILE_NAME: api_coverage_badge.svg
BRANCH_NAME: coverage_badges
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: coverage_badges # Dedicated branch to store coverage badges
63 changes: 31 additions & 32 deletions .github/workflows/cli-unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ name: 🕵️‍♂️ CLI Unit Tests
jobs:
cli-unit-test:
runs-on: ubuntu-latest
# permissions:
# contents: write
defaults:
run:
working-directory: ./CLI
Expand Down Expand Up @@ -63,7 +65,7 @@ jobs:
name: coverage_cli_xml
path: ./CLI/coverage_cli.xml

coverage-badge:
generate-coverage-badge:
needs: cli-unit-test
runs-on: ubuntu-latest
permissions:
Expand All @@ -76,41 +78,38 @@ jobs:
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: coverage_badges
fetch-depth: "0"

- uses: actions/setup-python@v5
- name: Generate Badge
uses: ./.github/actions/coverage/generate-badge
with:
python-version: '3.8'

- name: Install genbadge
run: pip install genbadge[coverage]
COVERAGE_REPORT_ARTIFACT: coverage_cli_xml
COVERAGE_REPORT_NAME: coverage_cli.xml
LABEL: "CLI coverage"
OUTPUT_FILE: cli_coverage_badge.svg
RED_LIMIT: "50"
GREEN_LIMIT: "65"
BADGE_ARTIFACT_NAME: cli_coverage_badge

upload-coverage-badge:
needs: generate-coverage-badge
runs-on: ubuntu-latest
permissions:
contents: write
defaults:
run:
working-directory: ./
if: github.ref == 'refs/heads/main' && github.event.head_commit.author.name != 'GitHubActions'

- name: Download line coverage reports
uses: actions/download-artifact@v4
steps:
- uses: actions/checkout@v4
with:
name: coverage_cli_xml

- name: Generate badge
run: genbadge coverage -i coverage_cli.xml -n "CLI coverage" -o cli_coverage_badge.svg
fetch-depth: "0"

- name: Verify Changed files
uses: tj-actions/verify-changed-files@v16
id: verify-changed-files
with:
files: cli_coverage_badge.svg

- name: Commit badge
if: steps.verify-changed-files.outputs.files_changed == 'true'
run: |
git config --local user.email "<>"
git config --local user.name "GitHubActions"
git add cli_coverage_badge.svg
git commit -m "Add/Update CLI badge"

- name: Push badge commit
if: steps.verify-changed-files.outputs.files_changed == 'true'
uses: ad-m/github-push-action@master
- name: Upload Badge
uses: ./.github/actions/coverage/upload-badge
with:
BADGE_ARTIFACT_NAME: cli_coverage_badge
BADGE_FILE_NAME: cli_coverage_badge.svg
BRANCH_NAME: coverage_badges
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: coverage_badges # Dedicated branch to store coverage badges
39 changes: 39 additions & 0 deletions assets/badge_generator/ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Badge Generator
Script that generates a coverage badge. You can customize the following parameters:

- The left label that will appear in the badge
- The color range limits. By default, the badge will be red between 0-50%, orange between 50-65% and green if higher than 65
- The output file name

The coverage percentage be passed in two different ways:

- By giving the desired value using the flag `--coverage`
- By indicating the path to the XML coverage report using the flag `--input-report`

Installation
------------
```bash
# Generate python virtual environment and activate it
python3 -m venv venv
source venv/bin/activate

# Install requirements
pip install -r requirements.txt
```

Examples
------------

```bash
# Run help
python generate_badge.py --help

# Generate badge with default values and indicating the coverage percentage
python generate_badge.py --coverage 75

# Generate with xml report file
python generate_badge.py --input-report coverage.xml

# Generate badge with multiple parameters
python generate_badge.py --label "API coverage" --input-report coverage.xml --output coverage_api.svg --red-limit 55 --green-limit 70
```
Loading