From d984d80aa0e08d9282ed46c66ff9f198a0f0f1e6 Mon Sep 17 00:00:00 2001 From: Lukas Holecek Date: Wed, 4 Dec 2024 16:11:26 +0100 Subject: [PATCH] Stop processing prerequisites on first Pending Stops processing prerequisites in a rule, if for example "condition: major > 9" evaluates to false (evaluating further would be unexpected). --- src/retasc/models/prerequisites/jira_issue.py | 36 +++++++------------ src/retasc/models/rule.py | 5 +++ tests/test_run.py | 16 ++++----- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/src/retasc/models/prerequisites/jira_issue.py b/src/retasc/models/prerequisites/jira_issue.py index 920fae6..03487a5 100644 --- a/src/retasc/models/prerequisites/jira_issue.py +++ b/src/retasc/models/prerequisites/jira_issue.py @@ -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]) @@ -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) @@ -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): @@ -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 @@ -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 diff --git a/src/retasc/models/rule.py b/src/retasc/models/rule.py index 69de093..2757dfd 100644 --- a/src/retasc/models/rule.py +++ b/src/retasc/models/rule.py @@ -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: @@ -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: diff --git a/tests/test_run.py b/tests/test_run.py index 09d3759..86563ac 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -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 == { @@ -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", } } @@ -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"