Skip to content

Commit

Permalink
feat(github): add service_number for github actions
Browse files Browse the repository at this point in the history
Required for supporting parallel builds.

Co-Authored-By: Kevin James <KevinJames@thekev.in>
  • Loading branch information
bboe and TheKevJames committed Feb 12, 2020
1 parent 6064a81 commit 9f93bd8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
12 changes: 10 additions & 2 deletions coveralls/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ def __init__(self, token_required=True, service_name=None, **kwargs):
name, job, pr = self.load_config_from_ci_environment()
self.config['service_name'] = self.config.get('service_name', name)
if job:
self.config['service_job_id'] = job
# N.B. Github Actions uses a different chunk of the Coveralls
# config when running parallel builds, ie. `service_number` instead
# of `service_job_id`.
if name.startswith('github'):
self.config['service_number'] = job
else:
self.config['service_job_id'] = job
if pr:
self.config['service_pull_request'] = pr

Expand Down Expand Up @@ -85,10 +91,12 @@ def load_config_from_circle():

@staticmethod
def load_config_from_github():
service_number = os.environ.get('GITHUB_SHA')
pr = None
if os.environ.get('GITHUB_REF', '').startswith('refs/pull/'):
pr = os.environ.get('GITHUB_REF', '//').split('/')[2]
return 'github-actions', None, pr
service_number += "-PR-{0}".format(pr)
return 'github', service_number, pr

@staticmethod
def load_config_from_jenkins():
Expand Down
1 change: 1 addition & 0 deletions docs/usage/tox.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ All variables:

- ``GITHUB_ACTIONS``
- ``GITHUB_REF``
- ``GITHUB_SHA``
- ``GITHUB_HEAD_REF``

Jenkins
Expand Down
26 changes: 16 additions & 10 deletions tests/api/configuration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,29 @@ def test_circleci_no_config(self):
assert cover.config['service_job_id'] == '888'
assert cover.config['service_pull_request'] == '9999'

@mock.patch.dict(os.environ, {'GITHUB_ACTIONS': 'true',
'GITHUB_REF': 'refs/pull/1234/merge',
'GITHUB_HEAD_REF': 'fixup-branch'},
clear=True)
@mock.patch.dict(
os.environ,
{'GITHUB_ACTIONS': 'true',
'GITHUB_REF': 'refs/pull/1234/merge',
'GITHUB_SHA': 'bb0e00166b28f49db04d6a8b8cb4bddb5afa529f',
'GITHUB_HEAD_REF': 'fixup-branch'},
clear=True)
def test_github_no_config(self):
cover = Coveralls(repo_token='xxx')
assert cover.config['service_name'] == 'github-actions'
assert cover.config['service_name'] == 'github'
assert cover.config['service_pull_request'] == '1234'
assert 'service_job_id' not in cover.config

@mock.patch.dict(os.environ, {'GITHUB_ACTIONS': 'true',
'GITHUB_REF': 'refs/heads/master',
'GITHUB_HEAD_REF': ''},
clear=True)
@mock.patch.dict(
os.environ,
{'GITHUB_ACTIONS': 'true',
'GITHUB_REF': 'refs/heads/master',
'GITHUB_SHA': 'bb0e00166b28f49db04d6a8b8cb4bddb5afa529f',
'GITHUB_HEAD_REF': ''},
clear=True)
def test_github_no_config_no_pr(self):
cover = Coveralls(repo_token='xxx')
assert cover.config['service_name'] == 'github-actions'
assert cover.config['service_name'] == 'github'
assert 'service_pull_request' not in cover.config
assert 'service_job_id' not in cover.config

Expand Down
2 changes: 2 additions & 0 deletions tests/git_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class GitInfoTestBranch(GitTest):
@mock.patch.dict(os.environ, {
'GITHUB_ACTIONS': 'true',
'GITHUB_REF': 'refs/pull/1234/merge',
'GITHUB_SHA': 'bb0e00166b28f49db04d6a8b8cb4bddb5afa529f',
'GITHUB_HEAD_REF': 'fixup-branch'
}, clear=True)
def test_gitinfo_github_pr(self):
Expand All @@ -133,6 +134,7 @@ def test_gitinfo_github_pr(self):
@mock.patch.dict(os.environ, {
'GITHUB_ACTIONS': 'true',
'GITHUB_REF': 'refs/heads/master',
'GITHUB_SHA': 'bb0e00166b28f49db04d6a8b8cb4bddb5afa529f',
'GITHUB_HEAD_REF': ''
}, clear=True)
def test_gitinfo_github_nopr(self):
Expand Down

0 comments on commit 9f93bd8

Please sign in to comment.