def notify_controller(
id_element: NotifyIdOpt,
status: NotifyStatusOpt,
sharepoint_path: NotifySharepointPathOpt = DEFAULT_NOTIFY_SHAREPOINT_PATH,
) -> None:
"""
Sends the creator of the MS List item an email reporting the outcome of
a run: "success" (upload finished, awaiting review) from the main
download/build/upload pipeline, or "published"/"error" from either that
pipeline or the separate, approval-gated publish pipeline
(publish.Jenkinsfile).
On a failure status, also updates the request's Workflow State field to
"Error" -- notify is the common failure handler called from every step's
error path (see CLAUDE.md's "Microsoft List schema" section), so this is
the single place that write happens, rather than duplicating it in
download/build/upload/publish.
"""
site_id = get_site_id(
get_token_manager(),
read_secret(SecretName.SHAREPOINT_DOMAIN),
read_secret(SecretName.SITE_NAME),
)
list_id = get_list_id(
get_token_manager(), site_id, read_secret(SecretName.LIST_NAME)
)
logger.info("Getting access token...")
token = get_access_token(
read_secret(SecretName.TENANT_ID),
read_secret(SecretName.CLIENT_ID),
read_secret(SecretName.CLIENT_SECRET),
)
logger.info(f"Getting creator info for item ID {id_element}...")
to_email, name = get_creator_email(token, site_id, list_id, str(id_element))
logger.info(f"Sending email to: {to_email} ({name})")
if status == WorkflowStatus.SUCCESS:
subject = f"iMarina - Workflow ID {id_element} completed successfully"
body = build_success_body(name, str(id_element), str(sharepoint_path))
elif status == WorkflowStatus.PUBLISHED:
subject = f"iMarina - Workflow ID {id_element} published to iMarina"
body = build_published_body(name, str(id_element))
else:
subject = f"iMarina - Workflow ID {id_element} failed"
body = build_error_body(name, str(id_element))
try:
update_list_item_fields(
str(id_element), {FIELD_WORKFLOW_STATE: WorkflowState.ERROR}
)
except Exception:
# Best-effort bookkeeping: must not block the error email below.
logger.exception("Error updating Workflow State to Error")
smtp_user = read_secret(SecretName.SMTP_USERNAME)
send_email(
EmailContent(
to_email=to_email, subject=subject, body=body, from_email=smtp_user
),
smtp_user,
read_secret(SecretName.SMTP_PASSWORD),
read_secret(SecretName.SMTP_HOST),
int(read_secret(SecretName.SMTP_PORT)),
)
logger.info("Email sent. Process complete.")