Skip to content

Commit

Permalink
Ignore draft schedule tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Jan 8, 2025
1 parent 471a60f commit e7f559a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 14 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ formatted and adhere to the expected schema (see the section below).

Example of validating rule files with CLI:

```
```bash
export RETASC_CONFIG=examples/config.yaml

# validate all rules in a path recursively
retasc validate-rules examples/rules

Expand Down
2 changes: 2 additions & 0 deletions src/retasc/models/prerequisites/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class PrerequisiteSchedule(PrerequisiteBase):
- schedule_task - name of the schedule task
- start_date - the schedule task's start_date
- end_date - the schedule task's end_date
- schedule_task_is_draft - if schedule task is marked as draft
"""

schedule_task: str = Field(
Expand All @@ -36,6 +37,7 @@ def _params(self, context) -> dict:
"schedule_task": schedule_task,
"start_date": task.start_date,
"end_date": task.end_date,
"schedule_task_is_draft": task.is_draft,
}
)
return local_params
Expand Down
11 changes: 11 additions & 0 deletions src/retasc/models/prerequisites/target_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class PrerequisiteTargetDate(PrerequisiteBase):
The prerequisite state is Completed only if the target_date evaluates to a
date in the past or it is today. Otherwise, the state is Pending.
Additionally, if ignore_drafts is true (the default value), the
prerequisite state is always Pending if the scheduled item is draft (i.e.
schedule_task_is_draft template parameter is true).
Adds the following template parameters:
- target_date - the evaluated target date
"""
Expand All @@ -28,12 +32,19 @@ class PrerequisiteTargetDate(PrerequisiteBase):
- "today"
""").strip(),
)
ignore_drafts: bool = Field(
description="Ignore draft scheduled items if true (the default).", default=True
)

def update_state(self, context) -> ReleaseRuleState:
"""
Return Completed if target date is earlier than today,
otherwise return Pending.
"""
if self.ignore_drafts and context.template.params["schedule_task_is_draft"]:
context.report.set("schedule_task_is_draft", True)
return ReleaseRuleState.Pending

target_date = context.template.evaluate(self.target_date)
context.template.params["target_date"] = target_date
today = context.template.env.globals["today"]
Expand Down
6 changes: 5 additions & 1 deletion src/retasc/product_pages_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class ProductPagesScheduleTask:
start_date: date
end_date: date
is_draft: bool = False


class ProductPagesApi:
Expand Down Expand Up @@ -49,13 +50,16 @@ def release_schedules(
:return: dict with schedule name as key and start date as value
"""
url = f"{self.api_url}/releases/{release_short_name}/schedule-tasks"
res = self.session.get(url, params={"fields": "name,date_start,date_finish"})
res = self.session.get(
url, params={"fields": "name,date_start,date_finish,draft"}
)
res.raise_for_status()
data = res.json()
return {
item["name"]: ProductPagesScheduleTask(
start_date=date.fromisoformat(item["date_start"]),
end_date=date.fromisoformat(item["date_finish"]),
is_draft=item["draft"],
)
for item in data
}
25 changes: 23 additions & 2 deletions tests/test_product_pages_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,24 @@ def test_active_releases(pp_api, requests_mock):

def test_release_schedules(pp_api, requests_mock):
schedules = [
{"name": "task1", "date_start": "2024-10-01", "date_finish": "2024-10-02"},
{"name": "task2", "date_start": "2024-11-20", "date_finish": "2024-11-21"},
{
"name": "task1",
"date_start": "2024-10-01",
"date_finish": "2024-10-02",
"draft": False,
},
{
"name": "task2",
"date_start": "2024-11-20",
"date_finish": "2024-11-21",
"draft": False,
},
{
"name": "task3",
"date_start": "2024-11-21",
"date_finish": "2024-11-22",
"draft": True,
},
]
requests_mock.get(
f"{PP_URL}/releases/example_product/schedule-tasks",
Expand All @@ -43,4 +59,9 @@ def test_release_schedules(pp_api, requests_mock):
start_date=date(2024, 11, 20),
end_date=date(2024, 11, 21),
),
"task3": ProductPagesScheduleTask(
start_date=date(2024, 11, 21),
end_date=date(2024, 11, 22),
is_draft=True,
),
}
23 changes: 13 additions & 10 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,23 +394,26 @@ def test_run_rule_condition_failed(condition_expr, result, factory):


@mark.parametrize(
("target_date", "result"),
("target_date", "is_draft", "result"),
(
("start_date", True),
("start_date - 1|weeks", True),
("start_date + 1|weeks", True),
("end_date - 1|weeks", True),
("end_date + 1|weeks", False),
("today", True),
("today + 1|days", False),
("today + 1|weeks", False),
("start_date", False, True),
("start_date", True, False),
("start_date - 1|weeks", False, True),
("start_date + 1|weeks", False, True),
("end_date - 1|weeks", False, True),
("end_date + 1|weeks", False, False),
("today", False, True),
("today", True, False),
("today + 1|days", False, False),
("today + 1|weeks", False, False),
),
)
def test_run_rule_schedule_target_date(target_date, result, mock_pp, factory):
def test_run_rule_schedule_target_date(target_date, is_draft, result, mock_pp, factory):
mock_pp.release_schedules.return_value = {
"TASK": ProductPagesScheduleTask(
start_date=date(1990, 1, 1),
end_date=datetime.now(UTC).date(),
is_draft=is_draft,
),
}
rule = factory.new_rule(
Expand Down

0 comments on commit e7f559a

Please sign in to comment.