Skip to content

Fix #553: don't retry 4XX errors in Jira client #558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion jbi/services/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
ServiceHealth = dict[str, bool]


def instrument(prefix: str, exceptions: Sequence[Type[Exception]]):
def instrument(prefix: str, exceptions: Sequence[Type[Exception]], **backoff_params):
"""This decorator wraps a function such that it increments a counter every
time it is called and times its execution. It retries the function if the
specified exceptions are raised.
Expand All @@ -32,6 +32,7 @@ def decorator(func):
backoff.expo,
exceptions,
max_tries=settings.max_retries + 1,
**backoff_params,
)
def wrapper(*args, **kwargs):
# Increment the call counter.
Expand Down
11 changes: 11 additions & 0 deletions jbi/services/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,23 @@

JIRA_DESCRIPTION_CHAR_LIMIT = 32767


def fatal_code(exc):
"""Do not retry 4XX errors, mark them as fatal."""
try:
return 400 <= exc.response.status_code < 500
except AttributeError:
# `ApiError` or `ConnectionError` won't have response attribute.
return False


instrumented_method = instrument(
prefix="jira",
exceptions=(
atlassian_errors.ApiError,
requests_exceptions.RequestException,
),
giveup=fatal_code,
)


Expand Down
16 changes: 16 additions & 0 deletions tests/unit/services/test_jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ def test_jira_retries_failing_connections_in_health_check(
assert len(mocked_responses.calls) == 4


@pytest.mark.no_mocked_jira
def test_jira_does_not_retry_4XX(mocked_responses, context_create_example):
url = f"{get_settings().jira_base_url}rest/api/2/issue"
mocked_responses.add(
responses.POST,
url,
json={},
status=400,
)

with pytest.raises(requests.HTTPError):
jira.create_jira_issue(context=context_create_example, description="")

assert len(mocked_responses.calls) == 1


@pytest.mark.parametrize(
"jira_components, project_components, expected_result",
[
Expand Down