Skip to content

Commit

Permalink
Merge pull request #106 from ssciwr/codecov-api-v2
Browse files Browse the repository at this point in the history
Switch to CodeCov API v2
  • Loading branch information
dokempf authored Feb 8, 2024
2 parents 2a5158a + 532d2b0 commit 5298564
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 42 deletions.
48 changes: 10 additions & 38 deletions tests/test_deploy_bake.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,6 @@ def rtd_api_request(endpoint):
assert build["commit"] == sha


def codecov_api_verification(remote_url, token, sha):
def codecov_api_request(endpoint):
resp = requests.get('https://codecov.io/api/{}/{}'.format(remote_url, endpoint),
headers={'Authorization': 'token {}'.format(token)}
)
return resp.json()

# Poll the codecov.io API
commit = codecov_api_request('commit/{}'.format(sha))['commit']
while commit['state'] != 'complete':
time.sleep(5)
commit = codecov_api_request('commit/{}'.format(sha))['commit']

# Assert 100% coverage
assert commit['totals']['c'] == '100'


@pytest.mark.integrations
@pytest.mark.flaky(max_runs=3, min_passes=1, rerun_filter=wait_five_seconds)
@pytest.mark.timeout(120)
Expand All @@ -139,25 +122,14 @@ def test_codecovio_github_deploy():
repo = gh.get_repo('dokempf/test-gha-cookiecutter')
sha = repo.get_branch('main').commit.sha

codecov_api_verification(
'gh/dokempf/test-gha-cookiecutter',
os.getenv('CODECOV_GH_API_ACCESS_TOKEN'),
sha
)


@pytest.mark.integrations
@pytest.mark.flaky(max_runs=3, min_passes=1, rerun_filter=wait_five_seconds)
@pytest.mark.timeout(60)
def test_codecovio_gitlab_deploy():
# Authenticate with Gitlab API
gl = gitlab.Gitlab('https://gitlab.com', private_token=os.getenv("GL_API_ACCESS_TOKEN"))
gl.auth()
project = gl.projects.get('dokempf/test-gitlab-ci-cookiecutter-cpp-project')
sha = project.branches.get('main').commit['id']
# Ask CodeCov API for coverage information
resp = requests.get(
f'https://api.codecov.io/api/v2/github/dokempf/repos/test-gha-cookiecutter/totals',
headers={
'authorization': f'Bearer {os.getenv("CODECOV_GH_API_ACCESS_TOKEN")}'
},
params={"sha": sha}
).json()

codecov_api_verification(
'gl/dokempf/test-gitlab-ci-cookiecutter-cpp-project',
os.getenv('CODECOV_GL_API_ACCESS_TOKEN'),
sha
)
# Assert 100% coverage
assert resp['totals']['coverage'] == 100
9 changes: 8 additions & 1 deletion {{cookiecutter.project_slug}}/.github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,12 @@ jobs:
working-directory: ${{ "{{runner.workspace}}" }}
run: |
lcov --directory ./build{% if cookiecutter.header_only == "No" %}/src{% endif %} --capture --output-file coverage.info
bash <(curl --connect-timeout 10 --retry 5 -s https://codecov.io/bash) -f coverage.info || echo "Codecov did not collect coverage reports"
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
fail_ci_if_error: true
files: ${{ "{{runner.workspace}}" }}/coverage.info
env:
CODECOV_TOKEN: ${{ "{{ secrets.CODECOV_TOKEN }}" }}
{% endif %}
7 changes: 4 additions & 3 deletions {{cookiecutter.project_slug}}/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ The following tasks need to be done to get a fully working project:
{%- endif %}
{%- if cookiecutter.codecovio == "Yes" %}
* Enable the integration with `codecov.io` by heading to the [Codecov.io Website](https://codecov.io),
log in (e.g. with your Github credentials) and enable integration for your repository. This will
allow you to have automatic coverage reports on pull requests, but is not necessary to display
the coverage badge in the README.
log in (e.g. with your Github credentials) and enable integration for your repository. In order to do
so, you need to select it from the list of repositories (potentially re-syncing with GitHub) and head
to the Settings Tab. Within setting, get your token for this repository and put store it as a secret
called `CODECOV_TOKEN` for GitHub Actions.
{%- endif %}
{%- if cookiecutter.sonarcloud == "Yes" %}
* Enable the integration with `sonarcloud.io` by heading to the [Sonarcloud.io Website](https://sonarcloud.io),
Expand Down

0 comments on commit 5298564

Please sign in to comment.