Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/acrcssc/azext_acrcssc/helper/_taskoperations.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,20 @@
def create_update_continuous_patch_v1(cmd, registry, cssc_config_file, schedule, dryrun, run_immediately, is_create_workflow=True):
logger.debug(f"Entering continuousPatchV1_creation {cssc_config_file} {dryrun} {run_immediately}")
resource_group = parse_resource_id(registry.id)[RESOURCE_GROUP]

next_date = None
schedule_cron_expression = None

if schedule is not None:
schedule_cron_expression = convert_timespan_to_cron(schedule)
logger.debug(f"converted schedule to cron expression: {schedule_cron_expression}")
logger.debug(f"Converted schedule to cron expression: {schedule_cron_expression}")

cssc_tasks_exists = check_continuous_task_exists(cmd, registry)
if is_create_workflow:
if cssc_tasks_exists:
raise AzCLIError(f"{CONTINUOUS_PATCHING_WORKFLOW_NAME} workflow task already exists. Use 'az acr supply-chain workflow update' command to perform updates.")
_create_cssc_workflow(cmd, registry, schedule_cron_expression, resource_group, dryrun)
workflow = _create_cssc_workflow(cmd, registry, schedule_cron_expression, resource_group, dryrun)
next_date = workflow["nextOccurrence"]["value"]
else:
if not cssc_tasks_exists:
raise AzCLIError(f"{CONTINUOUS_PATCHING_WORKFLOW_NAME} workflow task does not exist. Use 'az acr supply-chain workflow create' command to create {CONTINUOUS_PATCHING_WORKFLOW_NAME} workflow.")
Expand All @@ -67,13 +72,21 @@ def create_update_continuous_patch_v1(cmd, registry, cssc_config_file, schedule,
_eval_trigger_run(cmd, registry, resource_group, run_immediately)

# on 'update' schedule is optional
if schedule is None:
if next_date is None:
task = get_task(cmd, registry, CONTINUOUSPATCH_TASK_SCANREGISTRY_NAME)
trigger = task.trigger
if trigger and trigger.timer_triggers:
schedule_cron_expression = trigger.timer_triggers[0].schedule
# Note (transteven): I think additional_properties is needed here because nextOccurrence isn't officially in ARM Manifest yet
next_date = trigger.timer_triggers[0].additional_properties["nextOccurrence"]
Copy link

Copilot AI Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accessing 'additional_properties["nextOccurrence"]' without checking if the key exists can raise a KeyError. Add a check to ensure the key exists before accessing it.

Suggested change
next_date = trigger.timer_triggers[0].additional_properties["nextOccurrence"]
if 'nextOccurrence' in trigger.timer_triggers[0].additional_properties: next_date = trigger.timer_triggers[0].additional_properties['nextOccurrence']

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valid since the Task API needs to be released and referenced on the client


computed_next_date = get_next_date(schedule_cron_expression)
if next_date is None:
logger.debug(f"Computed next occurrence from schedule")
next_date = computed_next_date
else:
logger.debug(f"Found next occurrence from task output: {next_date}, computed would be: {computed_next_date}")

next_date = get_next_date(schedule_cron_expression)
print(f"Continuous Patching workflow scheduled to run next at: {next_date} UTC")


Expand All @@ -89,7 +102,7 @@ def _create_cssc_workflow(cmd, registry, schedule_cron_expression, resource_grou
param_name = CONTINUOUSPATCH_TASK_DEFINITION[task]["parameter_name"]
parameters[param_name] = encoded_task

validate_and_deploy_template(
return validate_and_deploy_template(
cmd.cli_ctx,
registry,
resource_group,
Expand All @@ -104,7 +117,7 @@ def _create_cssc_workflow(cmd, registry, schedule_cron_expression, resource_grou

def _update_cssc_workflow(cmd, registry, schedule_cron_expression, resource_group, dry_run):
if schedule_cron_expression is not None:
_update_task_schedule(cmd, registry, schedule_cron_expression, resource_group, dry_run)
return _update_task_schedule(cmd, registry, schedule_cron_expression, resource_group, dry_run)


def _eval_trigger_run(cmd, registry, resource_group, run_immediately):
Expand Down Expand Up @@ -342,9 +355,10 @@ def _update_task_schedule(cmd, registry, cron_expression, resource_group_name, d
logger.debug("Dry run, skipping the update of the task schedule")
return None
try:
acr_task_client.begin_update(resource_group_name, registry.name,
update_task = acr_task_client.begin_update(resource_group_name, registry.name,
CONTINUOUSPATCH_TASK_SCANREGISTRY_NAME,
taskUpdateParameters)
LongRunningOperation(cmd.cli_ctx)(update_task)
print("Schedule has been successfully updated.")
except Exception as exception:
raise AzCLIError(f"Failed to update the task schedule: {exception}")
Expand Down Expand Up @@ -426,7 +440,7 @@ def _transform_task_list(tasks):
transformed_obj["schedule"] = transform_cron_to_schedule(trigger.timer_triggers[0].schedule)

# add a 'nextOccurrence' field to the task, only for the scheduling task
transformed_obj["nextOccurrence"] = get_next_date(task.trigger.timer_triggers[0].schedule)
transformed_obj["nextOccurrence"] = task.trigger.timer_triggers[0].additional_properties["nextOccurrence"]
transformed.append(transformed_obj)

return transformed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,11 @@
"[resourceId('Microsoft.ContainerRegistry/registries/tasks', parameters('AcrName'), 'cssc-trigger-workflow')]"
]
}
]
],
"outputs": {
"nextOccurrence": {
"type": "string",
"value": "[reference(resourceId('Microsoft.ContainerRegistry/registries/tasks', parameters('AcrName'), 'cssc-trigger-workflow')).trigger.timerTriggers[0]['nextOccurrence']]"
}
}
}