Skip to content

Commit

Permalink
feat(support): support Github Actions CI (TheKevJames#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturKlauser committed Dec 3, 2019
1 parent ee95017 commit a74de5e
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 9 deletions.
9 changes: 9 additions & 0 deletions coveralls/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ def load_config_from_circle():
pr = os.environ.get('CI_PULL_REQUEST', '').split('/')[-1] or None
return 'circle-ci', os.environ.get('CIRCLE_BUILD_NUM'), pr

@staticmethod
def load_config_from_github():
pr = None
if os.environ.get('GITHUB_REF', '').startswith('refs/pull/'):
pr = os.environ.get('GITHUB_REF', '//').split('/')[2]
return 'github', None, pr

@staticmethod
def load_config_from_jenkins():
pr = os.environ.get('CI_PULL_REQUEST', '').split('/')[-1] or None
Expand Down Expand Up @@ -110,6 +117,8 @@ def load_config_from_ci_environment(self):
elif os.environ.get('CIRCLECI'):
self._token_required = False
name, job, pr = self.load_config_from_circle()
elif os.environ.get('GITHUB_ACTIONS'):
name, job, pr = self.load_config_from_github()
elif os.environ.get('JENKINS_HOME'):
name, job, pr = self.load_config_from_jenkins()
elif os.environ.get('TRAVIS'):
Expand Down
32 changes: 24 additions & 8 deletions coveralls/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,29 @@ def gitlog(fmt):
return unicode(glog) # pylint: disable=undefined-variable


def git_branch():
branch = None
if os.environ.get('GITHUB_ACTIONS'):
github_ref = os.environ.get('GITHUB_REF')
if github_ref.startswith('refs/heads/'):
# E.g. in push events.
branch = github_ref.split('/', 2)[-1]
else:
# E.g. in pull_request events.
branch = os.environ.get('GITHUB_HEAD_REF')
else:
branch = (os.environ.get('APPVEYOR_REPO_BRANCH')
or os.environ.get('BUILDKITE_BRANCH')
or os.environ.get('CI_BRANCH')
or os.environ.get('CIRCLE_BRANCH')
or os.environ.get('GIT_BRANCH')
or os.environ.get('TRAVIS_BRANCH')
or os.environ.get('BRANCH_NAME')
or run_command('git', 'rev-parse', '--abbrev-ref', 'HEAD'))

return branch


def git_info():
"""
A hash of Git data that can be used to display more information to users.
Expand All @@ -56,14 +79,7 @@ def git_info():
}
"""
try:
branch = (os.environ.get('APPVEYOR_REPO_BRANCH')
or os.environ.get('BUILDKITE_BRANCH')
or os.environ.get('CI_BRANCH')
or os.environ.get('CIRCLE_BRANCH')
or os.environ.get('GIT_BRANCH')
or os.environ.get('TRAVIS_BRANCH')
or os.environ.get('BRANCH_NAME')
or run_command('git', 'rev-parse', '--abbrev-ref', 'HEAD'))
branch = git_branch()
head = {
'id': gitlog('%H'),
'author_name': gitlog('%aN'),
Expand Down
2 changes: 1 addition & 1 deletion docs/usage/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Configuration
=============

coveralls-python often works without any outside configuration by examining the environment it is being run in. Special handling has been added for AppVeyor, BuildKite, CircleCI, Jenkins, and TravisCI to make coveralls-python as close to "plug and play" as possible.
coveralls-python often works without any outside configuration by examining the environment it is being run in. Special handling has been added for AppVeyor, BuildKite, CircleCI, Github, Jenkins, and TravisCI to make coveralls-python as close to "plug and play" as possible.

Most often, you will simply need to run coveralls-python with no additional options after you have run your coverage suite::

Expand Down
12 changes: 12 additions & 0 deletions docs/usage/tox.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ All variables:
- ``CIRCLE_BRANCH``
- ``CI_PULL_REQUEST``

Github
---------
::

passenv = GITHUB_*

All variables:

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

Jenkins
-------
::
Expand Down
20 changes: 20 additions & 0 deletions tests/api/configuration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ 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)
def test_github_no_config(self):
cover = Coveralls(repo_token='xxx')
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)
def test_github_no_config_no_pr(self):
cover = Coveralls(repo_token='xxx')
assert cover.config['service_name'] == 'github'
assert 'service_pull_request' not in cover.config
assert 'service_job_id' not in cover.config

@mock.patch.dict(
os.environ,
{'JENKINS_HOME': '/var/lib/jenkins',
Expand Down
21 changes: 21 additions & 0 deletions tests/git_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,24 @@ def test_gitlog_envvars(self):
'branch': 'master',
},
}


class GitInfoTestBranch(GitTest):
@mock.patch.dict(os.environ, {
'GITHUB_ACTIONS': 'true',
'GITHUB_REF': 'refs/pull/1234/merge',
'GITHUB_HEAD_REF': 'fixup-branch'
}, clear=True)
def test_gitinfo_github_pr(self):
git_info = coveralls.git.git_info()
print('git_info', git_info)
assert git_info['git']['branch'] == 'fixup-branch'

@mock.patch.dict(os.environ, {
'GITHUB_ACTIONS': 'true',
'GITHUB_REF': 'refs/heads/master',
'GITHUB_HEAD_REF': ''
}, clear=True)
def test_gitinfo_github_nopr(self):
git_info = coveralls.git.git_info()
assert git_info['git']['branch'] == 'master'

0 comments on commit a74de5e

Please sign in to comment.