Skip to content

Commit 089c6ff

Browse files
Lukas Holecekhluk
authored andcommitted
Fix Jira Cloud compatibility for search API and user identifiers
Jira Cloud has removed the /rest/api/2/search endpoint (HTTP 410). The atlassian-python-api library already supports the replacement /rest/api/3/search/jql endpoint when cloud=True is set. Additionally, Jira Cloud uses "accountId" instead of "key" for user identification. Update current_user_key and changelog field author extraction to use the correct identifier based on the cloud setting. To activate, set "jira_cloud: true" in the config file. JIRA: RHELWF-13538 Assisted-by: Claude Opus 4.6 (Anthropic)
1 parent aa251bc commit 089c6ff

3 files changed

Lines changed: 42 additions & 4 deletions

File tree

src/retasc/jira_client.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,31 @@ def __init__(
3939
# value to be an int.
4040
self.jira.timeout = timeout # type: ignore
4141

42+
@property
43+
def cloud(self) -> bool:
44+
"""Whether this client is connected to Jira Cloud."""
45+
return self.jira.cloud
46+
47+
def _get_user_identifier(self, user: dict) -> str | None:
48+
"""
49+
Extract user identifier from a Jira user dict.
50+
51+
Jira Cloud uses "accountId", Jira Server uses "key".
52+
"""
53+
field = "accountId" if self.cloud else "key"
54+
value = user.get(field)
55+
return value if isinstance(value, str) else None
56+
4257
@cached_property
4358
def current_user_key(self) -> str:
4459
"""
45-
Get the current user's account ID.
60+
Get the current user's identifier.
4661
"""
4762
user = self.jira.myself()
48-
if isinstance(user, dict) and isinstance(user.get("key"), str):
49-
return user["key"]
63+
if isinstance(user, dict):
64+
key = self._get_user_identifier(user)
65+
if key is not None:
66+
return key
5067

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

src/retasc/models/prerequisites/jira_issue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def _skip_user_modified_fields(issue: dict, to_update: dict, context) -> dict:
8585
data = context.jira.get_issue(issue["key"], fields=[], expand="changelog")
8686
changelog = data.get("changelog", {}).get("histories", [])
8787
field_authors = {
88-
item["field"]: change.get("author", {}).get("key")
88+
item["field"]: context.jira._get_user_identifier(change.get("author", {}))
8989
for change in changelog
9090
for item in change.get("items", [])
9191
}

tests/test_jira_client.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ def jira_api():
2727
return JiraClient(JIRA_URL, token="DUMMY-TOKEN", session=Session())
2828

2929

30+
@fixture
31+
def jira_cloud_api():
32+
return JiraClient(JIRA_URL, token="DUMMY-TOKEN", session=Session(), cloud=True)
33+
34+
3035
@fixture
3136
def dryrun_jira_api():
3237
return DryRunJiraClient(JIRA_URL, token="DUMMY-TOKEN", session=Session())
@@ -37,6 +42,14 @@ def test_current_user_key(jira_api, requests_mock):
3742
assert jira_api.current_user_key == "retasc-bot"
3843

3944

45+
def test_current_user_key_cloud(jira_cloud_api, requests_mock):
46+
requests_mock.get(
47+
f"{JIRA_URL}/rest/api/2/myself",
48+
json={"accountId": "abc123", "displayName": "retasc-bot"},
49+
)
50+
assert jira_cloud_api.current_user_key == "abc123"
51+
52+
4053
def test_create_issue(jira_api, requests_mock):
4154
requests_mock.post(
4255
f"{JIRA_URL}/rest/api/2/issue?updateHistory=false", json=TEST_RES
@@ -85,6 +98,14 @@ def test_search_issues_fields(jira_api, requests_mock):
8598
assert requests_mock.request_history[0].qs["fields"] == ["a,b"]
8699

87100

101+
def test_search_issues_cloud(jira_cloud_api, requests_mock):
102+
requests_mock.get(f"{JIRA_URL}/rest/api/2/search/jql", json=SEARCH_LIST)
103+
issues = jira_cloud_api.search_issues(JQL)
104+
assert issues == [{"id": "10000", "key": ISSUE_KEY}]
105+
assert len(requests_mock.request_history) == 1
106+
assert requests_mock.request_history[0].qs["jql"] == [JQL.lower()]
107+
108+
88109
def test_unexpected_response_create_issue(jira_api, requests_mock):
89110
requests_mock.post(f"{JIRA_URL}/rest/api/2/issue", json=[])
90111
with raises(RuntimeError, match=r"Unexpected response: \[\]"):

0 commit comments

Comments
 (0)