diff --git a/openedx_webhooks/info.py b/openedx_webhooks/info.py index be0cd8e3..9e011def 100644 --- a/openedx_webhooks/info.py +++ b/openedx_webhooks/info.py @@ -157,7 +157,10 @@ def get_jira_server_info(jira_nick: str) -> JiraServer: Given a Jira nickname, get the JiraServer info about it. """ jira_info = get_jira_info() - jira_server = jira_info[jira_nick.lower()] + try: + jira_server = jira_info[jira_nick.lower()] + except KeyError: + raise KeyError(f"No Jira server configured with nick {jira_nick!r}") return jira_server @@ -355,4 +358,7 @@ def jira_details_for_pr(jira_nick: str, pr: PrDict) -> tuple[str, str]: details.update(repo_info) break - return details["project"], details["type"] + try: + return details["project"], details["type"] + except KeyError: + raise ValueError(f"No Jira project mapping for {repo_name!r}: {details=}") diff --git a/tests/fake_jira.py b/tests/fake_jira.py index 7d475ee3..efd555ef 100644 --- a/tests/fake_jira.py +++ b/tests/fake_jira.py @@ -3,7 +3,7 @@ import dataclasses import itertools from dataclasses import dataclass, field -from typing import Dict, List, Optional, Set +from typing import Dict, Optional, Set from . import faker diff --git a/tests/repo_data/openedx/openedx-webhooks-data/test-jira-info.yaml b/tests/repo_data/openedx/openedx-webhooks-data/test-jira-info.yaml index ad6fc5e5..1d90719e 100644 --- a/tests/repo_data/openedx/openedx-webhooks-data/test-jira-info.yaml +++ b/tests/repo_data/openedx/openedx-webhooks-data/test-jira-info.yaml @@ -6,6 +6,12 @@ Test1: token: asdasdasdasdasd mapping: https://raw.githubusercontent.com/test1/dot-github/HEAD/jira-mapping.yaml description: the private Test1 Jira +Test2: + server: https://test2.atlassian.net + email: jira-user@test2.com + token: asdasdasdasdasd + mapping: https://raw.githubusercontent.com/test2/dot-github/HEAD/jira-mapping.yaml + description: the private Test2 Jira AnotherOrg: server: https://anotherorg.atlassian.net email: jira-user@anotherorg.com diff --git a/tests/repo_data/test2/dot-github/jira-mapping.yaml b/tests/repo_data/test2/dot-github/jira-mapping.yaml new file mode 100644 index 00000000..43dafa2f --- /dev/null +++ b/tests/repo_data/test2/dot-github/jira-mapping.yaml @@ -0,0 +1,6 @@ +# Mapping from repo to Jira. +defaults: + type: Task +repos: + - name: nedbat/* + project: NEDBAT diff --git a/tests/test_pull_request_opened.py b/tests/test_pull_request_opened.py index b1f2df0e..6efc2358 100644 --- a/tests/test_pull_request_opened.py +++ b/tests/test_pull_request_opened.py @@ -416,7 +416,7 @@ def test_jira_labelling_later(fake_github, fake_jira, fake_jira2): jira_issue = fake_jira2.issues[jira_id.key] assert jira_issue.summary == "Yet another PR" -def test_bad_jira_labelling(fake_github, fake_jira, fake_jira2): +def test_bad_jira_labelling_no_server(fake_github, fake_jira, fake_jira2): # What if the jira: label doesn't match one of our configured servers? pr = fake_github.make_pull_request("openedx", user="nedbat", title="Ned's PR") pr.set_labels(["jira:bogus"]) @@ -424,5 +424,14 @@ def test_bad_jira_labelling(fake_github, fake_jira, fake_jira2): pull_request_changed(pr.as_json()) assert len(exc_info.value.exceptions) == 1 exc = exc_info.value.exceptions[0] - assert isinstance(exc, KeyError) - assert exc.args == ("bogus",) + assert exc.args == ("No Jira server configured with nick 'bogus'",) + +def test_bad_jira_labelling_no_repo_map(fake_github, fake_jira, fake_jira2): + # What if the jira: label is good, but the repo has no mapping to a project? + pr = fake_github.make_pull_request("openedx", user="nedbat", title="Ned's PR") + pr.set_labels(["jira:test2"]) + with pytest.raises(ExceptionGroup) as exc_info: + pull_request_changed(pr.as_json()) + assert len(exc_info.value.exceptions) == 1 + exc = exc_info.value.exceptions[0] + assert exc.args[0].startswith("No Jira project mapping for 'openedx/a-repo':")