Skip to content

Commit

Permalink
Stop processing prerequisites on first Pending
Browse files Browse the repository at this point in the history
Stops processing prerequisites in a rule, if for example "condition:
major > 9" evaluates to false (evaluating further would be unexpected).
  • Loading branch information
hluk committed Dec 6, 2024
1 parent 1777d1b commit ca8cc69
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 31 deletions.
36 changes: 13 additions & 23 deletions src/retasc/models/prerequisites/jira_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ def _report_jira_issue(issue: dict, jira_issue_id: str, context):
context.report.set("issue", issue["key"])


def _create_issue(fields, context, label: str, parent_issue_key: str | None = None):
def _create_issue(
fields, context, label: str, parent_issue_key: str | None = None
) -> dict:
context.report.set("create", json.dumps(fields))
_set_parent_issue(fields, parent_issue_key)
fields.setdefault("labels", []).extend([label, *context.jira_labels])
Expand Down Expand Up @@ -93,16 +95,13 @@ def _template_to_issue_data(template_data: dict, context, template: str) -> dict

def _update_issue(
jira_issue_id: str, template: str, context, parent_issue_key: str | None = None
) -> dict | None:
) -> dict:
"""
Create and update Jira issue if needed.
The issue is updated according to the template.
If the issue does not exist and none of the preceding prerequisites are in
Pending state, the issue is created.
Returns the managed Jira issue or None if it does not exist yet.
Returns the managed Jira issue.
"""
label = f"{context.config.jira_label_prefix}{jira_issue_id}"
issue = context.issues.pop(label, None)
Expand All @@ -125,14 +124,11 @@ def _update_issue(
)
return issue

if context.prerequisites_state > ReleaseRuleState.Pending:
issue = _create_issue(
fields, context, label=label, parent_issue_key=parent_issue_key
)
_report_jira_issue(issue, jira_issue_id, context)
return issue

return None
issue = _create_issue(
fields, context, label=label, parent_issue_key=parent_issue_key
)
_report_jira_issue(issue, jira_issue_id, context)
return issue


class JiraIssueTemplate(BaseModel):
Expand All @@ -144,13 +140,11 @@ class PrerequisiteJiraIssue(PrerequisiteBase):
"""
Prerequisite Jira issue.
The Jira issue is created when no preceding prerequisites are in Pending state.
The existing Jira issue is updated until it is resolved.
If the issue does not exist, the issue is created. Otherwise, it is updated
if it is not yet resolved.
After the Jira issue is resolved the prerequisite state is Completed,
otherwise InProgress if issue has been created or Pending if it does not
exist yet.
otherwise it is InProgress.
Root directory for templates files is indicated with "jira_template_path"
option in ReTaSC configuration, and "jira_fields" option declares supported
Expand Down Expand Up @@ -196,10 +190,6 @@ def update_state(self, context) -> ReleaseRuleState:
template parameter (dict key is jira_issue_id).
"""
issue = _update_issue(self.jira_issue_id, self.template, context)

if issue is None:
return ReleaseRuleState.Pending

if _is_resolved(issue):
return ReleaseRuleState.Completed

Expand Down
5 changes: 5 additions & 0 deletions src/retasc/models/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Rule(BaseModel):
- Pending, if some prerequisites are in Pending
- In-progress, if some prerequisites are in In-progress but none are Pending
- Completed, if all prerequisites are Completed
Prerequisites are processed only until the first one in Pending state.
"""

class Config:
Expand Down Expand Up @@ -47,6 +49,9 @@ def update_state(self, context) -> ReleaseRuleState:
- Completed, if all prerequisites are Completed
"""
for prereq in self.prerequisites:
if context.prerequisites_state == ReleaseRuleState.Pending:
break

with context.report.section(prereq.section_name()):
state = prereq.update_state(context)
if state != ReleaseRuleState.Completed:
Expand Down
16 changes: 8 additions & 8 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,14 @@ def test_run_rule_condition_failed(condition_expr, result, factory):
# prerequisites are Pending (all preceding conditions must pass).
if result:
issue_prereq = {
"create": '{"summary": "test"}',
"issue": "TEST-1",
"state": "InProgress",
"Jira('test_jira_template_1')": {
"create": '{"summary": "test"}',
"issue": "TEST-1",
"state": "InProgress",
}
}
else:
issue_prereq = {"state": "Pending"}
issue_prereq = {}

report = call_run()
assert report.data == {
Expand All @@ -331,7 +333,7 @@ def test_run_rule_condition_failed(condition_expr, result, factory):
"result": result,
**({} if result else {"state": "Pending"}),
},
"Jira('test_jira_template_1')": issue_prereq,
**issue_prereq,
"state": "InProgress" if result else "Pending",
}
}
Expand Down Expand Up @@ -400,9 +402,7 @@ def test_run_rule_schedule_params(condition_expr, result, mock_pp, factory):


def test_run_rule_jira_issue_unsupported_fields(factory):
jira_issue_prereq = factory.new_jira_issue_prerequisite(
"field_1: 1\n" "field_2: 2\n"
)
jira_issue_prereq = factory.new_jira_issue_prerequisite("field_1: 1\nfield_2: 2\n")
factory.new_rule(prerequisites=[jira_issue_prereq])
expected_error = (
f"Jira template {jira_issue_prereq.template!r} contains"
Expand Down

0 comments on commit ca8cc69

Please sign in to comment.