Skip to content
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
23 changes: 20 additions & 3 deletions src/retasc/jira_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,31 @@ def __init__(
# value to be an int.
self.jira.timeout = timeout # type: ignore

@property
def cloud(self) -> bool:
"""Whether this client is connected to Jira Cloud."""
return self.jira.cloud

def _get_user_identifier(self, user: dict) -> str | None:
"""
Extract user identifier from a Jira user dict.

Jira Cloud uses "accountId", Jira Server uses "key".
"""
field = "accountId" if self.cloud else "key"
value = user.get(field)
return value if isinstance(value, str) else None

@cached_property
def current_user_key(self) -> str:
"""
Get the current user's account ID.
Get the current user's identifier.
"""
user = self.jira.myself()
if isinstance(user, dict) and isinstance(user.get("key"), str):
return user["key"]
if isinstance(user, dict):
key = self._get_user_identifier(user)
if key is not None:
return key

raise RuntimeError(f"Unexpected response: {user!r}")

Expand Down
2 changes: 1 addition & 1 deletion src/retasc/models/prerequisites/jira_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def _skip_user_modified_fields(issue: dict, to_update: dict, context) -> dict:
data = context.jira.get_issue(issue["key"], fields=[], expand="changelog")
changelog = data.get("changelog", {}).get("histories", [])
field_authors = {
item["field"]: change.get("author", {}).get("key")
item["field"]: context.jira._get_user_identifier(change.get("author", {}))
for change in changelog
for item in change.get("items", [])
}
Expand Down
29 changes: 29 additions & 0 deletions tests/test_jira_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ def jira_api():
return JiraClient(JIRA_URL, token="DUMMY-TOKEN", session=Session())


@fixture
def jira_cloud_api():
return JiraClient(JIRA_URL, token="DUMMY-TOKEN", session=Session(), cloud=True)


@fixture
def dryrun_jira_api():
return DryRunJiraClient(JIRA_URL, token="DUMMY-TOKEN", session=Session())
Expand All @@ -37,6 +42,14 @@ def test_current_user_key(jira_api, requests_mock):
assert jira_api.current_user_key == "retasc-bot"


def test_current_user_key_cloud(jira_cloud_api, requests_mock):
requests_mock.get(
f"{JIRA_URL}/rest/api/2/myself",
json={"accountId": "abc123", "displayName": "retasc-bot"},
)
assert jira_cloud_api.current_user_key == "abc123"


def test_create_issue(jira_api, requests_mock):
requests_mock.post(
f"{JIRA_URL}/rest/api/2/issue?updateHistory=false", json=TEST_RES
Expand Down Expand Up @@ -85,6 +98,14 @@ def test_search_issues_fields(jira_api, requests_mock):
assert requests_mock.request_history[0].qs["fields"] == ["a,b"]


def test_search_issues_cloud(jira_cloud_api, requests_mock):
requests_mock.get(f"{JIRA_URL}/rest/api/2/search/jql", json=SEARCH_LIST)
issues = jira_cloud_api.search_issues(JQL)
assert issues == [{"id": "10000", "key": ISSUE_KEY}]
assert len(requests_mock.request_history) == 1
assert requests_mock.request_history[0].qs["jql"] == [JQL.lower()]


def test_unexpected_response_create_issue(jira_api, requests_mock):
requests_mock.post(f"{JIRA_URL}/rest/api/2/issue", json=[])
with raises(RuntimeError, match=r"Unexpected response: \[\]"):
Expand All @@ -103,6 +124,14 @@ def test_unexpected_response_current_user_key(jira_api, requests_mock):
jira_api.current_user_key


def test_unexpected_response_current_user_key_missing_field(jira_api, requests_mock):
requests_mock.get(
f"{JIRA_URL}/rest/api/2/myself", json={"displayName": "retasc-bot"}
)
with raises(RuntimeError, match=r"Unexpected response"):
jira_api.current_user_key


def test_timeout(requests_mock):
"""
The default timeout in atlassian.Jira API is 75 seconds for both the
Expand Down
Loading