# ==================================================================================================================== # # Authors: # # Patrick Lehmann # # # # ==================================================================================================================== # # Copyright 2020-2023 The pyTooling Authors # # # # Licensed under the Apache License, Version 2.0 (the "License"); # # you may not use this file except in compliance with the License. # # You may obtain a copy of the License at # # # # http://www.apache.org/licenses/LICENSE-2.0 # # # # Unless required by applicable law or agreed to in writing, software # # distributed under the License is distributed on an "AS IS" BASIS, # # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # # See the License for the specific language governing permissions and # # limitations under the License. # # # # SPDX-License-Identifier: Apache-2.0 # # ==================================================================================================================== # name: Publish Code Coverage Results on: workflow_call: inputs: coverage_config: description: 'Path to the .coveragerc file. Use pyproject.toml by default.' required: false default: 'pyproject.toml' type: string coverage_sqlite_artifact: description: 'Name of the SQLite coverage artifact.' required: false default: '' type: string coverage_xml_artifact: description: 'Name of the XML coverage artifact.' required: false default: '' type: string coverage_json_artifact: description: 'Name of the JSON coverage artifact.' required: false default: '' type: string coverage_html_artifact: description: 'Name of the HTML coverage artifact.' required: false default: '' type: string secrets: codacy_token: description: 'Token to push result to codacy.' required: true jobs: PublishCoverageResults: name: 📊 Publish Code Coverage Results runs-on: ubuntu-latest if: always() steps: - name: ⏬ Checkout repository uses: actions/checkout@v4 - name: Download Artifacts uses: actions/download-artifact@v4 with: path: artifacts - name: 🔧 Install coverage and tomli run: | python -m pip install --disable-pip-version-check -U coverage[toml] tomli - name: 🔁 Extract configurations from pyproject.toml id: getVariables shell: python run: | from os import getenv from pathlib import Path from sys import version from textwrap import dedent print(f"Python: {version}") from tomli import load as tomli_load htmlDirectory = Path("htmlcov") xmlFile = Path("./coverage.xml") jsonFile = Path("./coverage.json") coverageRC = "${{ inputs.coverage_config }}".strip() # Read output paths from 'pyproject.toml' file if coverageRC == "pyproject.toml": pyProjectFile = Path("pyproject.toml") if pyProjectFile.exists(): with pyProjectFile.open("rb") as file: pyProjectSettings = tomli_load(file) htmlDirectory = Path(pyProjectSettings["tool"]["coverage"]["html"]["directory"]) xmlFile = Path(pyProjectSettings["tool"]["coverage"]["xml"]["output"]) jsonFile = Path(pyProjectSettings["tool"]["coverage"]["json"]["output"]) else: print(f"File '{pyProjectFile}' not found and no '.coveragerc' file specified.") # Read output paths from '.coveragerc' file elif len(coverageRC) > 0: coverageRCFile = Path(coverageRC) if coverageRCFile.exists(): with coverageRCFile.open("rb") as file: coverageRCSettings = tomli_load(file) htmlDirectory = Path(coverageRCSettings["html"]["directory"]) xmlFile = Path(coverageRCSettings["xml"]["output"]) jsonFile = Path(coverageRCSettings["json"]["output"]) else: print(f"File '{coverageRCFile}' not found.") # Write jobs to special file github_output = Path(getenv("GITHUB_OUTPUT")) print(f"GITHUB_OUTPUT: {github_output}") with github_output.open("a+", encoding="utf-8") as f: f.write(dedent(f"""\ coverage_report_html_directory={htmlDirectory.as_posix()} coverage_report_xml={xmlFile} coverage_report_json={jsonFile} """)) print(f"DEBUG:\n html={htmlDirectory}\n xml={xmlFile}\n json={jsonFile}") - name: Rename .coverage files and collect them all to coverage/ run: | mkdir -p coverage find . -type f -path "*artifacts*SQLite*.coverage" -exec sh -c 'cp -v $0 "coverage/$(basename $0).$(basename $(dirname $0))"' {} ';' tree -a coverage - name: Combine SQLite files (using Coverage.py) if: inputs.coverage_xml_artifact != '' run: coverage combine coverage/ - name: Report code coverage run: coverage report --rcfile=pyproject.toml --data-file=.coverage - name: Convert to XML format (Cobertura) if: inputs.coverage_xml_artifact != '' run: coverage xml --data-file=.coverage - name: Convert to JSON format if: inputs.coverage_json_artifact != '' run: coverage json --data-file=.coverage - name: Convert to HTML format if: inputs.coverage_html_artifact != '' run: | coverage html --data-file=.coverage -d report/coverage/html rm report/coverage/html/.gitignore tree -a report/coverage/html - name: 📤 Upload 'Coverage SQLite Database' artifact if: inputs.coverage_sqlite_artifact != '' continue-on-error: true uses: actions/upload-artifact@v4 with: name: ${{ inputs.coverage_sqlite_artifact }} path: .coverage if-no-files-found: error retention-days: 1 - name: 📤 Upload 'Coverage XML Report' artifact if: inputs.coverage_xml_artifact != '' continue-on-error: true uses: actions/upload-artifact@v4 with: name: ${{ inputs.coverage_xml_artifact }} path: ${{ steps.getVariables.outputs.coverage_report_xml }} if-no-files-found: error retention-days: 1 - name: 📤 Upload 'Coverage JSON Report' artifact if: inputs.coverage_json_artifact != '' continue-on-error: true uses: actions/upload-artifact@v4 with: name: ${{ inputs.coverage_json_artifact }} path: ${{ steps.getVariables.outputs.coverage_report_json }} if-no-files-found: error retention-days: 1 - name: 📤 Upload 'Coverage HTML Report' artifact if: inputs.coverage_html_artifact != '' continue-on-error: true uses: actions/upload-artifact@v4 with: name: ${{ inputs.coverage_html_artifact }} path: ${{ steps.getVariables.outputs.coverage_report_html_directory }} if-no-files-found: error retention-days: 1 - name: 📊 Publish code coverage at CodeCov if: inputs.CodeCov == true continue-on-error: true uses: codecov/codecov-action@v3 with: files: ${{ steps.getVariables.outputs.coverage_report_xml }} flags: unittests env_vars: PYTHON - name: 📉 Publish code coverage at Codacy if: inputs.Codacy == true continue-on-error: true uses: codacy/codacy-coverage-reporter-action@v1 with: project-token: ${{ secrets.codacy_token }} coverage-reports: ${{ steps.getVariables.outputs.coverage_report_xml }}