Skip to content

Commit

Permalink
Add cli option to finish parallel build
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoRoth committed Jul 5, 2020
1 parent bdf03ed commit 6697680
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 11 deletions.
29 changes: 29 additions & 0 deletions coveralls/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,35 @@ def wear(self, dry_run=False):
except Exception as e:
raise CoverallsException('Could not submit coverage: {}'.format(e))

def parallel_finish(self):
payload = {
'payload': {
'status': 'done'
}
}
if self.config.get('repo_token'):
payload['repo_token'] = self.config['repo_token']
if self.config.get('service_number'):
payload['payload']['build_num'] = self.config['service_number']

endpoint = '{}/webhook'.format(self._coveralls_host.rstrip('/'))
verify = not bool(os.environ.get('COVERALLS_SKIP_SSL_VERIFY'))
response = requests.post(endpoint, json=payload, verify=verify)
try:
response.raise_for_status()
response = response.json()
except Exception as e:
raise CoverallsException('Parallel finish failed: {}'.format(e))

if 'error' in response:
e = response['error']
raise CoverallsException('Parallel finish failed: {}'.format(e))

if 'done' not in response or not response['done']:
raise CoverallsException('Parallel finish failed')

return response

def create_report(self):
"""Generate json dumped report for coveralls api."""
data = self.create_data()
Expand Down
7 changes: 7 additions & 0 deletions coveralls/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
--rcfile=<file> Specify configuration file. [default: .coveragerc]
--output=<file> Write report to file. Doesn't send anything.
--merge=<file> Merge report from file when submitting.
--finish Finish parallel jobs.
-h --help Display this help.
-v --verbose Print extra info, always enabled when debugging.
Expand Down Expand Up @@ -74,6 +75,12 @@ def main(argv=None):
coverallz.save_report(options['--output'])
return

if options['--finish']:
log.info('Finishing parallel jobs...')
coverallz.parallel_finish()
log.info('Done')
return

log.info('Submitting coverage to coveralls.io...')
result = coverallz.wear()

Expand Down
18 changes: 8 additions & 10 deletions docs/usage/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Coveralls natively supports jobs running on Github Actions. You can directly pas
run: |
coveralls

For parallel builds you have to specify a unique flag-name for every step/job. You can use the official coveralls action to finalize the build::
For parallel builds you have to add a final step to let coveralls know the parallel build is finished::

jobs:
test:
Expand All @@ -74,20 +74,18 @@ For parallel builds you have to specify a unique flag-name for every step/job. Y
- name: Checkout
uses: actions/checkout@v2
- name: Test
run: |
./run_tests.sh ${{ matrix.test-name }}
coveralls
run: ./run_tests.sh ${{ matrix.test-name }}
- name: Upload Coverage
run: coveralls
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COVERALLS_PARALLEL: true
COVERALLS_FLAG_NAME: test-${{ matrix.test-env }}
coveralls:
name: Coveralls
name: Finish Coveralls
needs: test
runs-on: ubuntu-latest
steps:
- name: Finished
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true
run: pip install coveralls && coveralls --finish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
'docopt>=0.6.1',
'requests>=1.0.0',
],
tests_require=['mock', 'pytest'],
tests_require=['mock', 'responses', 'pytest'],
extras_require={
'yaml': ['PyYAML>=3.10'],
},
Expand Down
54 changes: 54 additions & 0 deletions tests/cli_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import mock
import responses

import coveralls.cli
from coveralls.exception import CoverallsException
Expand All @@ -27,6 +28,59 @@ def test_debug_no_token(mock_wear, mock_log):
mock_log.assert_has_calls([mock.call('Testing coveralls-python...')])


@mock.patch.dict(
os.environ,
{'GITHUB_ACTIONS': 'true',
'GITHUB_TOKEN': 'xxx',
'GITHUB_RUN_ID': '123456789',
'GITHUB_RUN_NUMBER': '123'},
clear=True)
@mock.patch.object(coveralls.cli.log, 'info')
@responses.activate
def test_finish(mock_log):
responses.add(responses.POST, 'https://coveralls.io/webhook',
json={'done': True}, status=200)
expected_json = {
'repo_token': 'xxx',
'payload': {
'status': 'done',
'build_num': '123'
}
}

coveralls.cli.main(argv=['--finish'])

mock_log.assert_has_calls(
[mock.call('Finishing parallel jobs...')
mock.call('Done')])
assert len(responses.calls) == 1
assert responses.calls[0].request.json == expected_json


@mock.patch.dict(os.environ, clear=True)
@mock.patch.object(coveralls.cli.log, 'exception')
@responses.activate
def test_finish_exception(mock_log):
responses.add(responses.POST, 'https://coveralls.io/webhook',
json={'error': 'Mocked'}, status=200)
expected_json = {
'payload': {
'status': 'done'
}
}

try:
coveralls.cli.main(argv=['--finish'])
assert 0 == 1 # Should never reach this line
except SystemExit:
pass

mock_log.assert_called_with(CoverallsException(
'Parallel finish failed: Mocked'))
assert len(responses.calls) == 1
assert responses.calls[0].request.json == expected_json


@mock.patch.object(coveralls.cli.log, 'info')
@mock.patch.object(coveralls.Coveralls, 'wear')
@mock.patch.dict(os.environ, {'TRAVIS': 'True'}, clear=True)
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ passenv = *
usedevelop = true
deps =
mock
responses
pytest
pyyaml: PyYAML>=3.10,<5.3
cov41: coverage>=4.1,<5.0
Expand Down

0 comments on commit 6697680

Please sign in to comment.