Skip to content

Commit

Permalink
fix: Return False if upstream codelist check fails in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
rebkwok committed Nov 13, 2023
1 parent b87bef3 commit d9172c9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
29 changes: 21 additions & 8 deletions opensafely/codelists.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,29 @@ def check_upstream(codelists_dir=None):
# if any codelists in codelists.csv don't match the expected pattern. These should all
# be fixable by `opensafely codelists update`.
if "error" in response["data"]:
exit_with_prompt(
error_message = (
f"Error checking upstream codelists: {response['data']['error']}\n"
)
if response["data"]["added"] or response["data"]["removed"]:
exit_with_prompt(
elif response["data"]["added"] or response["data"]["removed"]:
error_message = (
"Codelists have been added or removed\n\n"
"For details, run:\n\n opensafely codelists check\n"
)
changed = "\n ".join(response["data"]["changed"])
exit_with_prompt(
f"Some codelists are out of date\nCodelists affected:\n {changed}\n"
)
else:
changed = "\n ".join(response["data"]["changed"])
error_message = (
f"Some codelists are out of date\nCodelists affected:\n {changed}\n"
)

# If we're running in CI, we don't want to fail the entire action due to out of
# date upstream codelists, as users may have valid reasons for not wanting to update
# them (i.e. if they have already run jobs that use the backend database). In this
# case, just print the error message instead of exiting.
if os.environ.get("GITHUB_WORKFLOW"):
print(error_message)
return False
exit_with_prompt(error_message)

print("Codelists OK")
return True

Expand Down Expand Up @@ -210,14 +221,16 @@ def check():
)

try:
return check_upstream(codelists_dir)
upstream_check = check_upstream(codelists_dir)
except requests.exceptions.ConnectionError:
print(
f"Local codelists OK; could not contact {OPENCODELISTS_BASE_URL} for upstream check,"
"try again later"
)
return True

return upstream_check


def make_temporary_manifest(codelists_dir):
with tempfile.TemporaryDirectory() as tmpdir:
Expand Down
32 changes: 32 additions & 0 deletions tests/test_codelists.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def _mock(response):
return _mock


@pytest.fixture(autouse=True)
def not_ci(monkeypatch):
if "GITHUB_WORKFLOW" in os.environ:
monkeypatch.delenv("GITHUB_WORKFLOW")


def test_codelists_update(tmp_path, requests_mock):
codelist_dir = tmp_path / "codelists"
codelist_dir.mkdir()
Expand Down Expand Up @@ -227,3 +233,29 @@ def test_codelists_check_upstream_with_changes(codelists_path, mock_check, respo
mock_check(response={"status": "error", "data": response})
with pytest.raises(SystemExit):
codelists.check_upstream(codelists_path / "codelists")


def test_codelists_check_with_upstream_changes(codelists_path, mock_check):
mock_check(
response={
"status": "error",
"data": {"added": [], "removed": [], "changed": ["org/foo/123"]},
}
)
os.chdir(codelists_path)
with pytest.raises(SystemExit):
codelists.check()


def test_codelists_check_with_upstream_changes_in_CI(
codelists_path, mock_check, monkeypatch
):
monkeypatch.setenv("GITHUB_WORKFLOW", "test")
mock_check(
response={
"status": "error",
"data": {"added": [], "removed": [], "changed": ["org/foo/123"]},
}
)
os.chdir(codelists_path)
assert codelists.check() is False

0 comments on commit d9172c9

Please sign in to comment.