|
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,56 @@ 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 | + # Do not bother update the field if empty on create or if another field is updated. |
| 211 | + if (context.operation == Operation.CREATE and not context.bug.milestone) or ( |
| 212 | + context.operation == Operation.UPDATE |
| 213 | + and "milestone" not in context.event.changed_fields() |
| 214 | + ): |
| 215 | + return (StepStatus.NOOP, context) |
| 216 | + |
| 217 | + if context.bug.milestone: |
| 218 | + m = re.match(r"Branch (\d+)", context.bug.milestone) |
| 219 | + if m is None: |
| 220 | + # Milestone not supported (eg. "Future") |
| 221 | + return (StepStatus.NOOP, context) |
| 222 | + |
| 223 | + milestone_version = m.group(1) |
| 224 | + jira_project_versions = { |
| 225 | + v["name"] |
| 226 | + for v in jira_service.client.get_project_versions(context.jira.project) |
| 227 | + } |
| 228 | + potential_fix_versions = { |
| 229 | + f"Fx{milestone_version}", |
| 230 | + f"Release {milestone_version}", |
| 231 | + } |
| 232 | + jira_fix_versions = list( |
| 233 | + jira_project_versions.intersection(potential_fix_versions) |
| 234 | + ) |
| 235 | + if not jira_fix_versions: |
| 236 | + logger.info( |
| 237 | + "Bug %s milestone %r has no matching version in project.", |
| 238 | + context.bug.id, |
| 239 | + context.bug.milestone, |
| 240 | + extra=context.update( |
| 241 | + operation=Operation.IGNORE, |
| 242 | + ).model_dump(), |
| 243 | + ) |
| 244 | + return (StepStatus.INCOMPLETE, context) |
| 245 | + |
| 246 | + else: |
| 247 | + jira_fix_versions = [] |
| 248 | + |
| 249 | + resp = jira_service.update_issue_field(context, "fixVersions", jira_fix_versions) |
| 250 | + context.append_responses(resp) |
| 251 | + return (StepStatus.SUCCESS, context) |
| 252 | + |
| 253 | + |
203 | 254 | def _maybe_update_issue_mapped_field(
|
204 | 255 | source_field: str,
|
205 | 256 | context: ActionContext,
|
|
0 commit comments