|
| 1 | +# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | +import logging |
| 3 | + |
| 4 | +from retasc.models.prerequisites.exceptions import PrerequisiteUpdateStateError |
| 5 | + |
| 6 | +logger = logging.getLogger(__name__) |
| 7 | + |
| 8 | + |
| 9 | +def _find_transition(transitions: list[dict], status: str) -> dict | None: |
| 10 | + """Find a transition targeting the given status.""" |
| 11 | + return next((t for t in transitions if t["to"] == status), None) |
| 12 | + |
| 13 | + |
| 14 | +def _find_next_transition( |
| 15 | + available: list[dict], |
| 16 | + desired_status: str, |
| 17 | + remaining: list[str], |
| 18 | +) -> tuple[dict | None, list[str]]: |
| 19 | + """ |
| 20 | + Pick the next transition to execute. |
| 21 | +
|
| 22 | + Tries the target status first; if unavailable, picks the latest |
| 23 | + available intermediate from remaining (searching from the end). |
| 24 | +
|
| 25 | + Returns a (transition, new_remaining) tuple, where new_remaining is |
| 26 | + the slice of the list after the chosen intermediate (or empty when |
| 27 | + the target status is reached directly). transition is None when no |
| 28 | + suitable transition is available. |
| 29 | + """ |
| 30 | + transition = _find_transition(available, desired_status) |
| 31 | + if transition is not None: |
| 32 | + return transition, [] |
| 33 | + |
| 34 | + for i, status in enumerate(reversed(remaining)): |
| 35 | + transition = _find_transition(available, status) |
| 36 | + if transition is not None: |
| 37 | + return transition, remaining[len(remaining) - i :] |
| 38 | + |
| 39 | + return None, remaining |
| 40 | + |
| 41 | + |
| 42 | +def update_issue_status( |
| 43 | + issue: dict, |
| 44 | + status_template: str, |
| 45 | + transitions: list[str], |
| 46 | + context, |
| 47 | +) -> None: |
| 48 | + """ |
| 49 | + Transition a Jira issue to the desired status if needed. |
| 50 | +
|
| 51 | + At each step the algorithm first attempts a direct transition to the |
| 52 | + target status. If that is not available, it picks the latest |
| 53 | + (rightmost) status from the remaining intermediates that is offered |
| 54 | + as a Jira transition, executes it, and repeats. Entries at or |
| 55 | + before the issue's current status are skipped automatically. |
| 56 | +
|
| 57 | + :param issue: The Jira issue dict. |
| 58 | + :param status_template: Jinja2 template for the desired status. |
| 59 | + :param transitions: Ordered list of intermediate statuses to try |
| 60 | + when the target is not directly reachable. |
| 61 | + :param context: The execution context. |
| 62 | + :raises PrerequisiteUpdateStateError: If the desired status is unreachable. |
| 63 | + """ |
| 64 | + desired_status = context.template.render(status_template) |
| 65 | + current_status = issue["fields"].get("status", {}).get("name", "") |
| 66 | + remaining = ( |
| 67 | + transitions[transitions.index(current_status) + 1 :] |
| 68 | + if current_status in transitions |
| 69 | + else transitions |
| 70 | + ) |
| 71 | + |
| 72 | + visited = [current_status] |
| 73 | + while current_status != desired_status: |
| 74 | + available = context.jira.get_issue_transitions(issue["key"]) |
| 75 | + transition, remaining = _find_next_transition( |
| 76 | + available, desired_status, remaining |
| 77 | + ) |
| 78 | + if transition is None: |
| 79 | + available_names = [t["to"] for t in available] |
| 80 | + raise PrerequisiteUpdateStateError( |
| 81 | + f"Cannot transition issue {issue['key']} to {desired_status!r};" |
| 82 | + f" available transitions: {available_names}" |
| 83 | + ) |
| 84 | + |
| 85 | + current_status = transition["to"] |
| 86 | + visited.append(current_status) |
| 87 | + logger.info( |
| 88 | + "Transitioning %r to %r via %r", |
| 89 | + issue["key"], |
| 90 | + current_status, |
| 91 | + transition["name"], |
| 92 | + ) |
| 93 | + context.jira.transition_issue(issue["key"], transition["id"]) |
| 94 | + |
| 95 | + context.report.set("status_transitions", visited) |
0 commit comments