|
11 | 11 | from __future__ import annotations
|
12 | 12 |
|
13 | 13 | import logging
|
| 14 | +import re |
14 | 15 | from enum import Enum, auto
|
15 | 16 | from typing import TYPE_CHECKING, Iterable, Optional
|
16 | 17 |
|
@@ -200,6 +201,65 @@ def maybe_assign_jira_user(
|
200 | 201 | return (StepStatus.NOOP, context)
|
201 | 202 |
|
202 | 203 |
|
| 204 | +def maybe_update_issue_milestone( |
| 205 | + context: ActionContext, *, parameters: ActionParams, jira_service: JiraService |
| 206 | +) -> StepResult: |
| 207 | + """ |
| 208 | + Update the Jira issue 'Fix Version' field. |
| 209 | + """ |
| 210 | + if context.bug.milestone: |
| 211 | + m = re.match(r"Branch (\d+)", context.bug.milestone) |
| 212 | + if m is None: |
| 213 | + # Milestone not supported (eg. "Future") |
| 214 | + return (StepStatus.NOOP, context) |
| 215 | + |
| 216 | + milestone_version = m.group(1) |
| 217 | + jira_project_versions = { |
| 218 | + v["name"] |
| 219 | + for v in jira_service.client.get_project_versions(context.jira.project) |
| 220 | + } |
| 221 | + potential_fix_versions = { |
| 222 | + f"Fx{milestone_version}", |
| 223 | + f"Release {milestone_version}", |
| 224 | + } |
| 225 | + jira_fix_versions = list( |
| 226 | + jira_project_versions.intersection(potential_fix_versions) |
| 227 | + ) |
| 228 | + if not jira_fix_versions: |
| 229 | + logger.info( |
| 230 | + "Bug %s milestone %r has no matching version in project.", |
| 231 | + context.bug.id, |
| 232 | + context.bug.milestone, |
| 233 | + extra=context.update( |
| 234 | + operation=Operation.IGNORE, |
| 235 | + ).model_dump(), |
| 236 | + ) |
| 237 | + return (StepStatus.INCOMPLETE, context) |
| 238 | + |
| 239 | + else: |
| 240 | + jira_fix_versions = [] |
| 241 | + |
| 242 | + if context.operation == Operation.CREATE: |
| 243 | + if jira_fix_versions: |
| 244 | + resp = jira_service.update_issue_field( |
| 245 | + context, "fixVersions", jira_fix_versions |
| 246 | + ) |
| 247 | + context.append_responses(resp) |
| 248 | + return (StepStatus.SUCCESS, context) |
| 249 | + |
| 250 | + if context.operation == Operation.UPDATE: |
| 251 | + changed_fields = context.event.changed_fields() |
| 252 | + |
| 253 | + if "milestone" in changed_fields: |
| 254 | + resp = jira_service.update_issue_field( |
| 255 | + context, "fixVersions", jira_fix_versions |
| 256 | + ) |
| 257 | + context.append_responses(resp) |
| 258 | + return (StepStatus.SUCCESS, context) |
| 259 | + |
| 260 | + return (StepStatus.NOOP, context) |
| 261 | + |
| 262 | + |
203 | 263 | def _maybe_update_issue_mapped_field(
|
204 | 264 | source_field: str,
|
205 | 265 | context: ActionContext,
|
|
0 commit comments