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
17 changes: 17 additions & 0 deletions jbi/jira/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ def raise_for_status(self, *args, **kwargs):
create_issue = instrumented_method(Jira.create_issue)
get_project = instrumented_method(Jira.get_project)

@instrumented_method
def get_issue_transitions_with_fields(self, issue_key: str) -> list[dict]:
"""Get available transitions for an issue with field metadata.

This uses expand=transitions.fields to get information about which
fields are available on each transition screen.

Args:
issue_key: The issue key (e.g., "SYNC-5055")

Returns:
List of transition objects with field metadata
"""
url = f"rest/api/2/issue/{issue_key}/transitions"
response = self.get(url, params={"expand": "transitions.fields"})
return response.get("transitions", []) if response else []

@instrumented_method
def paginated_projects(
self,
Expand Down
21 changes: 18 additions & 3 deletions jbi/jira/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,24 @@ def update_issue_status(self, context: ActionContext, jira_status: str):

kwargs: dict[str, Any] = {}
if jira_status == "Cancelled":
kwargs["fields"] = {
"resolution": {"name": "Invalid"},
}
# Check if resolution field is available on the transition screen
transitions = self.client.get_issue_transitions_with_fields(issue_key)
target_transition = next(
(t for t in transitions if t.get("to", {}).get("name") == jira_status),
None,
)

if target_transition and "resolution" in target_transition.get("fields", {}):
kwargs["fields"] = {
"resolution": {"name": "Invalid"},
}
else:
logger.info(
"Resolution field not available on transition screen for %s, skipping",
issue_key,
extra=context.model_dump(),
)

kwargs["update"] = {
"comment": [{"add": {"body": "Issue was cancelled."}}],
}
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/jira/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,25 @@ def test_update_issue_status_adds_comment_and_resolution_when_cancelled(
context = action_context_factory(jira__issue="JBI-234")
url = f"{settings.jira_base_url}rest/api/2/issue/JBI-234/transitions"

# Mock GET transitions with expand=transitions.fields (for checking resolution availability)
mocked_responses.add(
responses.GET,
url,
match=[
responses.matchers.query_param_matcher({"expand": "transitions.fields"})
],
json={
"transitions": [
{
"name": "foo",
"id": 42,
"to": {"name": "Cancelled"},
"fields": {"resolution": {"required": True}},
}
]
},
)
# Mock GET transitions (for set_issue_status to find transition ID)
mocked_responses.add(
responses.GET,
url,
Expand Down
Loading