Skip to content

Commit

Permalink
chore: drop codecov
Browse files Browse the repository at this point in the history
  • Loading branch information
percevalw committed May 15, 2024
1 parent e733991 commit 3f62657
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 113 deletions.
143 changes: 143 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
name: Coverage

on:
workflow_call:
inputs:
base-branch:
required: true
type: string
badge-template:
required: false
type: string
coverage-branch:
required: false
type: string
default: coverage
coverage-report:
required: false
type: string
default: coverage.txt
coverage-badge:
required: false
type: string
default: coverage.svg
coverage-data-pattern:
required: true
type: string

jobs:
pull-request-coverage:
name: Check Pull Request Coverage
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
env:
GITHUB_PR_NUMBER: ${{github.event.pull_request.number}}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
# Use latest Python, so it understands all syntax.
python-version: "3.7"

- uses: actions/download-artifact@v4
with:
pattern: coverage-data-*
merge-multiple: true

- name: Combine coverage
run: |
python -Im pip install --upgrade "git+https://github.com/percevalw/coveragepy.git#egg=coverage[toml]"
python -Im coverage combine
- name: Compare coverage
if: github.event_name == 'pull_request'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Get the main branch, so that we can run `git diff main`
git fetch origin ${{ inputs.base-branch }}:${{ inputs.base-branch }} --depth=1
# We share store main coverage data in a separate branch, so we fetch it too
git fetch origin ${{ inputs.coverage-branch }}:${{ inputs.coverage-branch }} --depth=1 || true
# Get the coverage data from the coverage branch
git show ${{ inputs.coverage-branch }}:${{ inputs.coverage-report }} > /tmp/main-coverage.txt || true
# Report and write to summary.
echo '## Coverage Report' > /tmp/report.md
if [ -f /tmp/main-coverage.txt ]; then
coverage report --skip-covered --skip-empty --show-missing --format=diff --base-coverage-report=/tmp/main-coverage.txt --base-revision=${{ inputs.base-branch }} --fail-under=base >> /tmp/report.md || status=$?
else
coverage report --skip-covered --skip-empty --show-missing --format=diff --base-revision=main >> /tmp/report.md || status=$?
fi
cat /tmp/report.md >> $GITHUB_STEP_SUMMARY
COMMENT_BODY_JSON=$(jq -Rs . <<< $(cat /tmp/report.md))
HEADER="Authorization: token $GITHUB_TOKEN"
PR_COMMENTS_URL="https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments"
# Fetch existing comments to find if one from this workflow already exists
COMMENTS=$(curl -s -H "$HEADER" "$PR_COMMENTS_URL")
COMMENT_ID=$(echo "$COMMENTS" | jq -r '.[] | select(.user.login == "github-actions[bot]" and (.body | startswith("## Coverage Report"))) | .id')
# Check if we have a comment ID, if so, update it, otherwise create a new one
if [[ "$COMMENT_ID" ]]; then
# Update existing comment
curl -s -X PATCH -H "$HEADER" -H "Content-Type: application/json" -d "{\"body\": $COMMENT_BODY_JSON}" "https://api.github.com/repos/${{ github.repository }}/issues/comments/$COMMENT_ID"
else
# Post new comment
curl -s -X POST -H "$HEADER" -H "Content-Type: application/json" -d "{\"body\": $COMMENT_BODY_JSON}" "$PR_COMMENTS_URL"
fi
if [ $status -ne 0 ]; then
exit $status
fi
main-coverage:
name: Update Base Coverage
if: github.ref == inputs.base-branch
runs-on: ubuntu-latest
env:
GITHUB_PR_NUMBER: ${{github.event.pull_request.number}}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
# Use latest Python, so it understands all syntax.
python-version: "3.7"

- uses: actions/download-artifact@v4
with:
pattern: ${{ inputs.coverage-data-pattern }}
merge-multiple: true

- name: Set up Git
run: |
git config user.name ${{ github.actor }}
git config user.email ${{ github.actor }}@users.noreply.github.com
- uses: actions/checkout@v4
with:
repository: aphp/foldedtensor
sparse-checkout: |
.github/workflows/generate_badge.py
- name: Combine coverage and generate the report and the badge
run: |
python -Im pip install --upgrade "git+https://github.com/percevalw/coveragepy.git[toml]"
python -Im coverage combine
coverage report --show-missing > /tmp/main-coverage.txt
# Generate the coverage badge
python .github/workflows/generate_badge.py -r /tmp/main-coverage.txt -t ${{ inputs.badge-template.svg }} > /tmp/coverage.svg
git fetch origin ${{ inputs.coverage-branch }}:${{ inputs.coverage-branch }} --depth=1 || true
git checkout ${{ inputs.coverage-branch }} || git checkout --orphan ${{ inputs.coverage-branch }}
git reset --hard
cp /tmp/main-coverage.txt ${{ inputs.coverage-report }}
cp /tmp/coverage.svg ${{ inputs.coverage-badge }}
git add ${{ inputs.coverage-report }} ${{ inputs.coverage-badge }}
git commit -m "Update main coverage"
git push origin ${{ inputs.coverage-branch }}
6 changes: 4 additions & 2 deletions .github/workflows/generate_badge.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</g>
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110">
<text x="315" y="140" transform="scale(.1)" fill="#fff" textLength="510">coverage</text>
<text x="835" y="140" transform="scale(.1)" fill="#fff" textLength="370">{COVERAGE}</text>
<text x="835" y="140" transform="scale(.1)" fill="#fff" textLength="{WIDTH}">{COVERAGE}</text>
</g>
</svg>""" # noqa: E501

Expand Down Expand Up @@ -55,8 +55,9 @@ def make_badge(
else DEFAULT_TEMPLATE
)
badge = badge_template.format(
COVERAGE=f"{coverage_str}%",
COVERAGE=f"{int(coverage)}%",
COLOR=f"#{''.join(f'{int(c * 255):02x}' for c in rgb)}",
WIDTH="250" if coverage < 100 else "330",
)
return badge

Expand Down Expand Up @@ -92,6 +93,7 @@ def make_badge(
"--badge-template-path",
type=str,
default=None,
nargs="?",
)
args = parser.parse_args()
badge = make_badge(
Expand Down
119 changes: 9 additions & 110 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,117 +61,16 @@ jobs:
path: .coverage.*
if-no-files-found: ignore

pull-request-coverage:
name: Check Pull Request Coverage
if: github.event_name == 'pull_request'
Coverage:
name: Compute coverage
needs: Pytest
runs-on: ubuntu-latest
env:
GITHUB_PR_NUMBER: ${{github.event.pull_request.number}}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
# Use latest Python, so it understands all syntax.
python-version: "3.7"

- uses: actions/download-artifact@v4
with:
pattern: coverage-data-*
merge-multiple: true

- name: Combine coverage
run: |
python -Im pip install --upgrade "git+https://github.com/percevalw/coveragepy.git#egg=coverage[toml]"
python -Im coverage combine
- name: Compare coverage
if: github.event_name == 'pull_request'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Get the main branch, so that we can run `git diff main`
git fetch origin main:main --depth=1
# We share store main coverage data in a separate branch, so we fetch it too
git fetch origin coverage:coverage --depth=1 || true
# Get the coverage data from the coverage branch
git checkout coverage -- main-coverage.txt || true
# Report and write to summary.
echo '## Coverage Report' > report.md
if [ -f main-coverage.txt ]; then
coverage report --skip-covered --skip-empty --show-missing --format=diff --base-coverage-report=main-coverage.txt --base-revision=main --fail-under=base >> report.md || status=$?
else
coverage report --skip-covered --skip-empty --show-missing --format=diff --base-revision=main >> report.md || status=$?
fi
cat report.md >> $GITHUB_STEP_SUMMARY
COMMENT_BODY_JSON=$(jq -Rs . <<< $(cat report.md))
HEADER="Authorization: token $GITHUB_TOKEN"
PR_COMMENTS_URL="https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments"
# Fetch existing comments to find if one from this workflow already exists
COMMENTS=$(curl -s -H "$HEADER" "$PR_COMMENTS_URL")
COMMENT_ID=$(echo "$COMMENTS" | jq -r '.[] | select(.user.login == "github-actions[bot]" and (.body | startswith("## Coverage Report"))) | .id')
# Check if we have a comment ID, if so, update it, otherwise create a new one
if [[ "$COMMENT_ID" ]]; then
# Update existing comment
curl -s -X PATCH -H "$HEADER" -H "Content-Type: application/json" -d "{\"body\": $COMMENT_BODY_JSON}" "https://api.github.com/repos/${{ github.repository }}/issues/comments/$COMMENT_ID"
else
# Post new comment
curl -s -X POST -H "$HEADER" -H "Content-Type: application/json" -d "{\"body\": $COMMENT_BODY_JSON}" "$PR_COMMENTS_URL"
fi
if [ $status -ne 0 ]; then
exit $status
fi
main-coverage:
name: Update Base Coverage
#if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
needs: Pytest
runs-on: ubuntu-latest
env:
GITHUB_PR_NUMBER: ${{github.event.pull_request.number}}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
# Use latest Python, so it understands all syntax.
python-version: "3.7"

- uses: actions/download-artifact@v4
with:
pattern: coverage-data-*
merge-multiple: true

- name: Set up Git
run: |
git config user.name ${{ github.actor }}
git config user.email ${{ github.actor }}@users.noreply.github.com
- name: Combine coverage and generate the report and the badge
run: |
python -Im pip install --upgrade "git+https://github.com/percevalw/coveragepy.git#egg=coverage[toml]"
python -Im coverage combine
coverage report --show-missing > /tmp/main-coverage.txt
# Generate the coverage badge (color = #4c1)
python .github/workflows/generate_badge.py -r /tmp/main-coverage.txt -t .github/badge-template.svg > /tmp/coverage.svg
git fetch origin coverage:coverage --depth=1 || true
git checkout coverage || git checkout --orphan coverage
git reset --hard
cp /tmp/main-coverage.txt main-coverage.txt
cp /tmp/coverage.svg coverage.svg
git add main-coverage.txt coverage.svg
git commit -m "Update main coverage"
git push origin coverage
uses: ./github/workflows/coverage.yml
with:
base-branch: main
coverage-data-pattern: coverage-data-*.xml
coverage-report: coverage.txt
coverage-badge: coverage.svg
coverage-branch: coverage

Installation:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

![Tests](https://img.shields.io/github/actions/workflow/status/aphp/foldedtensor/tests.yml?branch=main&label=tests&style=flat-square)
[![PyPI](https://img.shields.io/pypi/v/foldedtensor?color=blue&style=flat-square)](https://pypi.org/project/foldedtensor/)
[![Codecov](https://img.shields.io/codecov/c/github/aphp/foldedtensor?logo=codecov&style=flat-square)](https://codecov.io/gh/aphp/foldedtensor)
[![Coverage](https://raw.githubusercontent.com/aphp/foldedtensor/coverage/coverage.svg)](https://raw.githubusercontent.com/aphp/foldedtensor/coverage/main-coverage.txt)
[![License](https://img.shields.io/github/license/aphp/foldedtensor?color=x&style=flat-square)](https://github.com/aphp/foldedtensor/blob/main/LICENSE)

# FoldedTensor: PyTorch extension for handling deeply nested sequences of variable length
Expand Down

0 comments on commit 3f62657

Please sign in to comment.