Skip to content

Commit 50af55e

Browse files
committed
🐛 Fixes service_job_id KeyError with coveralls==2.2.0
It seems like `coveralls==2.2.0` requires `service_job_id` to internally resubmit payload on 422 Unprocessable Entry error, refs: https://github.com/coveralls-clients/coveralls-python/pull/241/files#r532248047
1 parent bb2d6b0 commit 50af55e

3 files changed

Lines changed: 46 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Change Log
22

33
## [Unreleased]
4+
- Improves exception logging
5+
- Fixes KeyError with `coveralls==2.2.0`
46
- Fixes entrypoint assertion error ([dfm](https://github.com/dfm)), refs #5
57
- Fixes parallel mode ([johanneswilm](https://github.com/johanneswilm)), refs #8
6-
- Improves exception logging
78

89
## [v20200413]
910
- Made `github-token` parameter optional

src/entrypoint.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,18 @@ def run_coveralls(repo_token, parallel=False, flag_name=False, base_path=False):
4646
# for some reasons the "service_name" can be one or the other
4747
# (depending on where it's ran from?)
4848
service_names = ("github", "github-actions")
49+
# sets `service_job_id` key so it exists, refs:
50+
# https://github.com/coveralls-clients/coveralls-python/pull/241/files#r532248047
51+
service_job_id = None
4952
result = None
5053
if base_path and os.path.exists(base_path):
5154
os.chdir(base_path)
5255
for service_name in service_names:
5356
log.info(f"Trying submitting coverage with service_name: {service_name}...")
5457
with patch_os_environ(repo_token, parallel, flag_name):
55-
coveralls = Coveralls(service_name=service_name)
58+
coveralls = Coveralls(
59+
service_name=service_name, service_job_id=service_job_id
60+
)
5661
try:
5762
result = coveralls.wear()
5863
break

tests/test_entrypoint.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pytest
55
from coveralls.api import CoverallsException
6+
from requests.models import Response
67

78
import entrypoint
89

@@ -23,11 +24,12 @@ def patch_sys_argv(argv):
2324
return mock.patch("sys.argv", argv)
2425

2526

26-
def patch_requests_post(json_response=None):
27-
new_mock = mock.Mock()
28-
if json_response:
29-
new_mock.return_value.json.return_value = json_response
30-
return mock.patch("entrypoint.requests.post", new_mock)
27+
def patch_requests_post(json_response=mock.Mock(), status_code=200):
28+
response = Response()
29+
response.status_code = status_code
30+
response.json = lambda: json_response
31+
m_post = mock.Mock(return_value=response)
32+
return mock.patch("entrypoint.requests.post", m_post)
3133

3234

3335
class TestEntryPoint:
@@ -147,6 +149,37 @@ def test_run_coveralls_wear_error_twice(self):
147149
entrypoint.run_coveralls(repo_token="TOKEN")
148150
assert ex_info.value.args == (entrypoint.ExitCode.FAILURE,)
149151

152+
def test_status_code_422(self):
153+
"""
154+
Makes sure the coveralls package retries on "422 Unprocessable Entry" error
155+
rather than crashing while trying to access the `service_job_id` key, refs:
156+
https://github.com/coveralls-clients/coveralls-python/pull/241/files#r532248047
157+
"""
158+
status_code = 422
159+
with patch_requests_post(status_code=status_code) as m_post, pytest.raises(
160+
SystemExit
161+
), patch_log() as m_log:
162+
entrypoint.run_coveralls(repo_token="TOKEN")
163+
# coveralls package will retry once per service we call it with
164+
assert m_post.call_count == 4
165+
assert m_log.error.call_args_list == [
166+
mock.call("Failed to submit coverage", exc_info=None)
167+
]
168+
assert m_log.warning.call_args_list == [
169+
mock.call(
170+
"Failed submitting coverage with service_name: github",
171+
exc_info=CoverallsException(
172+
"Could not submit coverage: 422 Client Error: None for url: None"
173+
),
174+
),
175+
mock.call(
176+
"Failed submitting coverage with service_name: github-actions",
177+
exc_info=CoverallsException(
178+
"Could not submit coverage: 422 Client Error: None for url: None"
179+
),
180+
),
181+
]
182+
150183
def test_post_webhook(self):
151184
"""
152185
Tests different uses cases:

0 commit comments

Comments
 (0)