Skip to content

Commit 841e82d

Browse files
committed
Fix #501: add 'maybe_update_issue_milestone' step
1 parent 56327ca commit 841e82d

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

jbi/bugzilla/models.py

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class Bug(BaseModel, frozen=True):
7474
creator: Optional[str] = None
7575
assigned_to: Optional[str] = None
7676
comment: Optional[WebhookComment] = None
77+
milestone: Optional[str] = None
7778
# Custom field Firefox for story points
7879
cf_fx_points: Optional[str] = None
7980

jbi/steps.py

+60
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from __future__ import annotations
1212

1313
import logging
14+
import re
1415
from enum import Enum, auto
1516
from typing import TYPE_CHECKING, Iterable, Optional
1617

@@ -200,6 +201,65 @@ def maybe_assign_jira_user(
200201
return (StepStatus.NOOP, context)
201202

202203

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+
203263
def _maybe_update_issue_mapped_field(
204264
source_field: str,
205265
context: ActionContext,

tests/unit/test_steps.py

+35
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,41 @@ def test_change_to_unknown_resolution_with_resolution_map(
728728
]
729729

730730

731+
def test_maybe_update_issue_milestone_empty_create():
732+
# NOOP when not set on create event
733+
pass
734+
735+
736+
def test_maybe_update_issue_milestone_unsupported_milestone():
737+
# NOOP when milestone is "Future"
738+
pass
739+
740+
741+
def test_maybe_update_issue_milestone_unknown_create():
742+
# Test log message when milestone unknown in project
743+
pass
744+
745+
746+
def test_maybe_update_issue_milestone_set_create():
747+
# Test both fix versions are known.
748+
pass
749+
750+
751+
def test_maybe_update_issue_milestone_empty_update():
752+
# Test set to empty list when empty on update.
753+
pass
754+
755+
756+
def test_maybe_update_issue_milestone_unknown_update():
757+
# Test log message when milestone unknown in project
758+
pass
759+
760+
761+
def test_maybe_update_issue_milestone_set_update():
762+
# Test both fix versions are known.
763+
pass
764+
765+
731766
def test_update_issue_priority(
732767
action_context_factory,
733768
mocked_jira,

0 commit comments

Comments
 (0)