fix en debugging for parse coverage report #87
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: .NET | |
on: | |
push: | |
branches: [ "Development" ] | |
pull_request: | |
branches: [ "Development" ] | |
env: | |
IMAGE_NAME: CargoHub | |
OWNER: "${{ github.repository_owner }}" | |
DOTNET_INSTALL_DIR: "./.dotnet" | |
jobs: | |
build: | |
runs-on: self-hosted | |
outputs: | |
BUILD_TIME: ${{ steps.build-time.outputs.time }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Setup .NET | |
uses: actions/setup-dotnet@v4 | |
with: | |
dotnet-version: 8.0.x | |
- name: Cache NuGet packages | |
uses: actions/cache@v3 | |
with: | |
path: ~/.nuget/packages | |
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }} | |
restore-keys: | | |
${{ runner.os }}-nuget- | |
- name: Restore dependencies | |
run: dotnet restore | |
- name: Add packages | |
run: | | |
dotnet add package Newtonsoft.Json | |
dotnet add package xunit | |
- name: Build | |
id: build-time | |
run: | | |
dotnet build --no-restore | |
echo "time=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT | |
test: | |
needs: build | |
runs-on: self-hosted | |
outputs: | |
TEST_TIME: ${{ steps.test-time.outputs.time }} | |
COVERAGE: ${{ steps.coverage.outputs.percentage }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Setup .NET Core | |
uses: actions/setup-dotnet@v4 | |
with: | |
dotnet-version: '8.0.x' | |
- name: Cache .NET build output | |
uses: actions/cache@v3 | |
with: | |
path: ~/.dotnet/ | |
key: ${{ runner.os }}-dotnet-${{ hashFiles('**/*.csproj') }} | |
restore-keys: | | |
${{ runner.os }}-dotnet- | |
- name: Build the application | |
run: dotnet build --configuration Release | |
- name: Run application | |
run: dotnet run --no-build --configuration Release --urls=${{ secrets.LOCALHOST }} & sleep 5 | |
- name: Setup Python | |
uses: actions/setup-python@v2 | |
with: | |
python-version: '3.x' | |
- name: Cache Python dependencies | |
uses: actions/cache@v3 | |
with: | |
path: ~/.cache/pip | |
key: ${{ runner.os }}-python-${{ hashFiles('**/requirements.txt') }} | |
restore-keys: | | |
${{ runner.os }}-python- | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
python -m pip install httpx pytest coverage | |
- name: Run integration tests | |
run: coverage run -m pytest | |
- name: Generate code coverage report | |
id: coverage | |
run: | | |
coverage xml -o coverage.xml | |
coverage report --format=markdown >> $GITHUB_STEP_SUMMARY | |
echo "percentage=$(coverage report | grep TOTAL | awk '{print $NF}')" >> $GITHUB_OUTPUT | |
coverage report --fail-under=90 | |
- name: Check for coverage file | |
run: | | |
if [ ! -f "coverage.xml" ]; then | |
echo "coverage.xml file not found" | |
exit 1 | |
fi | |
shell: bash | |
- name: Install ReportGenerator | |
run: | | |
dotnet tool install --global dotnet-reportgenerator-globaltool | |
echo "Installed tools:" | |
dotnet tool list --global | |
env: | |
DOTNET_CLI_TELEMETRY_OPTOUT: 1 | |
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1 | |
- name: Generate Coverage Report | |
run: | | |
DOTNET_TOOLS_DIR="$HOME/.dotnet/tools" | |
echo "DOTNET_TOOLS_DIR: $DOTNET_TOOLS_DIR" | |
export PATH="$PATH:$DOTNET_TOOLS_DIR" | |
which reportgenerator || echo "ReportGenerator not found in PATH" | |
$DOTNET_TOOLS_DIR/reportgenerator \ | |
-reports:"coverage.xml" \ | |
-targetdir:"coverage-report" \ | |
-reporttypes:Html | |
- name: Upload Coverage Report | |
uses: actions/upload-artifact@v4 | |
with: | |
name: coverage-report | |
path: coverage-report | |
close_pr_on_failure: | |
needs: [build, test] | |
runs-on: ubuntu-latest | |
if: failure() && github.event_name == 'pull_request' | |
steps: | |
- name: Close Pull Request | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{secrets.GITHUB_TOKEN}} | |
script: | | |
github.rest.pulls.update({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: context.issue.number, | |
state: 'closed' | |
}) | |
- name: Comment on Pull Request | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{secrets.GITHUB_TOKEN}} | |
script: | | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: 'This pull request has been automatically closed due to workflow failure. Please fix the issues and open a new pull request.' | |
}) | |
- name: Set test Completion time | |
id: test-time | |
run: | | |
echo "::set-output name=time::$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT | |
notify: | |
needs: [build, test] | |
runs-on: ubuntu-latest | |
steps: | |
- name: Download coverage report | |
uses: actions/download-artifact@v4 | |
with: | |
name: coverage-report | |
path: coverage-report | |
- name: Parse coverage report | |
id: parse-coverage | |
run: | | |
# Check if the coverage report file exists and is not empty | |
if [ ! -f "coverage-report/index.html" ]; then | |
echo "coverage-report/index.html file not found" | |
exit 1 | |
fi | |
# Print the file content for debugging | |
cat coverage-report/index.html | |
# Parse coverage values | |
COVERAGE=$(grep -oP '(?<=Line coverage:).*?(?=%)' coverage-report/index.html | tr -d ' ') | |
BRANCH_COVERAGE=$(grep -oP '(?<=Branch coverage:).*?(?=%)' coverage-report/index.html | tr -d ' ') | |
METHOD_COVERAGE=$(grep -oP '(?<=Method coverage:).*?(?=%)' coverage-report/index.html | tr -d ' ') | |
# If parsing fails, output a message | |
if [ -z "$COVERAGE" ]; then | |
echo "Line coverage not found" | |
exit 1 | |
fi | |
if [ -z "$BRANCH_COVERAGE" ]; then | |
echo "Branch coverage not found" | |
exit 1 | |
fi | |
if [ -z "$METHOD_COVERAGE" ]; then | |
echo "Method coverage not found" | |
exit 1 | |
fi | |
# Output parsed coverage values to GitHub Actions | |
echo "coverage=$COVERAGE" >> $GITHUB_OUTPUT | |
echo "branch_coverage=$BRANCH_COVERAGE" >> $GITHUB_OUTPUT | |
echo "method_coverage=$METHOD_COVERAGE" >> $GITHUB_OUTPUT | |
# Parse coverage for individual test files | |
FILE_COVERAGE=$(grep -oP '(?<=<tr class="line"></tr><td class="file">).*?(?=%)' coverage-report/index.html) | |
if [ -z "$FILE_COVERAGE" ]; then | |
echo "No file coverage data found" | |
exit 1 | |
fi | |
FILES=($FILE_COVERAGE) | |
for FILE in "${FILES[@]}"; do | |
echo "file_coverage=$FILE" >> $GITHUB_OUTPUT | |
done | |
shell: /usr/bin/bash -e {0} | |
env: | |
IMAGE_NAME: CargoHub | |
OWNER: safouuwa | |
DOTNET_INSTALL_DIR: ./.dotnet | |
- name: Determine coverage status | |
id: coverage-status | |
run: | | |
if (( $(echo "${{ steps.parse-coverage.outputs.coverage }} >= 90" | bc -l) )); then | |
echo "status=good" >> $GITHUB_OUTPUT | |
echo "gif=https://media.giphy.com/media/3o6ZtlGkjeschymLNm/giphy.gif" >> $GITHUB_OUTPUT | |
echo "checkmark=:white_check_mark:" >> $GITHUB_OUTPUT | |
else | |
echo "status=bad" >> $GITHUB_OUTPUT | |
echo "gif=https://media.giphy.com/media/l1J9EdzfOSgfyueLm/giphy.gif" >> $GITHUB_OUTPUT | |
echo "checkmark=:x:" >> $GITHUB_OUTPUT | |
fi | |
- name: Discord notification | |
env: | |
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }} | |
uses: Ilshidur/action-discord@master | |
with: | |
args: | | |
:rocket: Project Update Notification :rocket: | |
The project **${{ github.repository }}** has been updated. | |
:hammer_and_wrench: **Build Job** | |
- Completed at: ${{ needs.build.outputs.BUILD_TIME }} | |
:test_tube: **Test Job** | |
- Completed at: ${{ needs.test.outputs.TEST_TIME }} | |
:bar_chart: **Coverage Report** | |
- Overall Coverage: ${{ steps.parse-coverage.outputs.coverage }}% | |
- Branch Coverage: ${{ steps.parse-coverage.outputs.branch_coverage }}% | |
- Method Coverage: ${{ steps.parse-coverage.outputs.method_coverage }}% | |
:page_facing_up: **File Coverage:** | |
${{ steps.parse-coverage.outputs.file_coverage }} | |
${{ steps.coverage-status.outputs.gif }} | |
${{ steps.coverage-status.outputs.checkmark }} | |
:link: [View Commit](https://github.com/${{ github.repository }}/commit/${{ github.sha }}) | |
:octocat: [View Workflow Run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) | |
:chart_with_upwards_trend: [View Full Coverage Report](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) | |
Branch: ${{ github.ref_name }} | |
Event: ${{ github.event_name }} | |
- name: Upload coverage report to Discord | |
env: | |
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }} | |
run: | | |
curl -H "Content-Type: multipart/form-data" -X POST -F "file=@coverage-report/index.html" $DISCORD_WEBHOOK |