Skip to content
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

fix: more specific exceptions for jira labelling #272

Merged
merged 1 commit into from
Oct 31, 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
10 changes: 8 additions & 2 deletions openedx_webhooks/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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=}")
2 changes: 1 addition & 1 deletion tests/fake_jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]
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: [email protected]
Expand Down
6 changes: 6 additions & 0 deletions tests/repo_data/test2/dot-github/jira-mapping.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Mapping from repo to Jira.
defaults:
type: Task
repos:
- name: nedbat/*
project: NEDBAT
15 changes: 12 additions & 3 deletions tests/test_pull_request_opened.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,13 +416,22 @@ 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"])
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 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':")
Loading