Skip to content

Commit 4fdbe3f

Browse files
Merge pull request #629 from NirajC-Microsoft/dev
chore: Bicep Parameter validation email format change
2 parents a05687e + b98f69b commit 4fdbe3f

2 files changed

Lines changed: 281 additions & 15 deletions

File tree

.github/workflows/validate-bicep-params.yml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,16 @@ jobs:
3434
- name: Validate infra/ parameters
3535
id: validate_infra
3636
continue-on-error: true
37+
env:
38+
ACCELERATOR_NAME: ${{ env.accelerator_name }}
3739
run: |
3840
set +e
39-
python Deployment/validate_bicep_params.py --dir infra --strict --no-color --json-output infra_results.json 2>&1 | tee infra_output.txt
41+
RUN_URL="https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
42+
python Deployment/validate_bicep_params.py --dir infra --strict --no-color \
43+
--json-output infra_results.json \
44+
--html-output email_body.html \
45+
--accelerator-name "${ACCELERATOR_NAME}" \
46+
--run-url "${RUN_URL}" 2>&1 | tee infra_output.txt
4047
EXIT_CODE=${PIPESTATUS[0]}
4148
set -e
4249
echo "## Infra Param Validation" >> "$GITHUB_STEP_SUMMARY"
@@ -61,24 +68,21 @@ jobs:
6168
name: bicep-validation-results
6269
path: |
6370
infra_results.json
71+
email_body.html
6472
retention-days: 30
6573

6674
- name: Send schedule notification on failure
6775
if: github.event_name == 'schedule' && steps.result.outputs.status == 'failure'
6876
env:
6977
LOGICAPP_URL: ${{ secrets.EMAILNOTIFICATION_LOGICAPP_URL_TA }}
70-
GITHUB_REPOSITORY: ${{ github.repository }}
71-
GITHUB_RUN_ID: ${{ github.run_id }}
7278
ACCELERATOR_NAME: ${{ env.accelerator_name }}
7379
run: |
74-
RUN_URL="https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
75-
INFRA_OUTPUT=$(sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g' infra_output.txt)
80+
EMAIL_BODY=$(cat email_body.html)
7681
7782
jq -n \
7883
--arg name "${ACCELERATOR_NAME}" \
79-
--arg infra "$INFRA_OUTPUT" \
80-
--arg url "$RUN_URL" \
81-
'{subject: ("Bicep Parameter Validation Report - " + $name + " - Issues Detected"), body: ("<p>Dear Team,</p><p>The scheduled <strong>Bicep Parameter Validation</strong> for <strong>" + $name + "</strong> has detected parameter mapping errors.</p><p><strong>infra/ Results:</strong></p><pre>" + $infra + "</pre><p><strong>Run URL:</strong> <a href=\"" + $url + "\">" + $url + "</a></p><p>Please fix the parameter mapping issues at your earliest convenience.</p><p>Best regards,<br>Your Automation Team</p>")}' \
84+
--arg body "$EMAIL_BODY" \
85+
'{subject: ("Bicep Parameter Validation Report - " + $name + " - Issues Detected"), body: $body}' \
8286
| curl -X POST "${LOGICAPP_URL}" \
8387
-H "Content-Type: application/json" \
8488
-d @- || echo "Failed to send notification"
@@ -87,18 +91,14 @@ jobs:
8791
if: github.event_name == 'schedule' && steps.result.outputs.status == 'success'
8892
env:
8993
LOGICAPP_URL: ${{ secrets.EMAILNOTIFICATION_LOGICAPP_URL_TA }}
90-
GITHUB_REPOSITORY: ${{ github.repository }}
91-
GITHUB_RUN_ID: ${{ github.run_id }}
9294
ACCELERATOR_NAME: ${{ env.accelerator_name }}
9395
run: |
94-
RUN_URL="https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
95-
INFRA_OUTPUT=$(sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g' infra_output.txt)
96+
EMAIL_BODY=$(cat email_body.html)
9697
9798
jq -n \
9899
--arg name "${ACCELERATOR_NAME}" \
99-
--arg infra "$INFRA_OUTPUT" \
100-
--arg url "$RUN_URL" \
101-
'{subject: ("Bicep Parameter Validation Report - " + $name + " - Passed"), body: ("<p>Dear Team,</p><p>The scheduled <strong>Bicep Parameter Validation</strong> for <strong>" + $name + "</strong> has completed successfully. All parameter mappings are valid.</p><p><strong>infra/ Results:</strong></p><pre>" + $infra + "</pre><p><strong>Run URL:</strong> <a href=\"" + $url + "\">" + $url + "</a></p><p>Best regards,<br>Your Automation Team</p>")}' \
100+
--arg body "$EMAIL_BODY" \
101+
'{subject: ("Bicep Parameter Validation Report - " + $name + " - Passed"), body: $body}' \
102102
| curl -X POST "${LOGICAPP_URL}" \
103103
-H "Content-Type: application/json" \
104104
-d @- || echo "Failed to send notification"

Deployment/validate_bicep_params.py

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from __future__ import annotations
3030

3131
import argparse
32+
import html
3233
import json
3334
import re
3435
import sys
@@ -295,6 +296,241 @@ def discover_pairs(infra_dir: Path) -> list[tuple[Path, Path]]:
295296
return pairs
296297

297298

299+
# ---------------------------------------------------------------------------
300+
# HTML email report
301+
# ---------------------------------------------------------------------------
302+
303+
def _html_escape(text: str) -> str:
304+
"""Escape HTML special characters."""
305+
return html.escape(text, quote=True)
306+
307+
308+
def generate_html_report(
309+
results: list[ValidationResult],
310+
*,
311+
accelerator_name: str = "",
312+
run_url: str = "",
313+
scan_dir: str = "",
314+
) -> str:
315+
"""Build a structured HTML email body from validation results."""
316+
total_errors = sum(
317+
1 for r in results for i in r.issues if i.severity == "ERROR"
318+
)
319+
total_warnings = sum(
320+
1 for r in results for i in r.issues if i.severity == "WARNING"
321+
)
322+
has_errors = total_errors > 0
323+
overall_status = "Issues Detected" if has_errors else "Passed"
324+
status_color = "#D32F2F" if has_errors else "#2E7D32"
325+
status_bg = "#FFEBEE" if has_errors else "#E8F5E9"
326+
status_icon = "&#10060;" if has_errors else "&#9989;"
327+
328+
parts: list[str] = []
329+
330+
# --- Document wrapper (Outlook-compatible, no gradient/border-radius/box-shadow) ---
331+
parts.append(
332+
'<!DOCTYPE html><html><head><meta charset="utf-8"></head>'
333+
'<body style="margin:0;padding:0;font-family:Segoe UI,Helvetica,Arial,sans-serif;'
334+
'background-color:#ffffff;">'
335+
'<table role="presentation" width="100%" cellpadding="0" cellspacing="0"'
336+
' style="background-color:#ffffff;">'
337+
'<tr><td align="center" style="padding:0;">'
338+
'<table role="presentation" width="100%" cellpadding="0" cellspacing="0"'
339+
' style="max-width:680px;background-color:#ffffff;">'
340+
)
341+
342+
# --- Header banner (solid color, Outlook-safe) ---
343+
parts.append(
344+
f'<tr><td style="background-color:#0078D4;padding:20px 24px;color:#ffffff;">'
345+
f'<h1 style="margin:0 0 4px 0;font-size:20px;font-weight:600;color:#ffffff;">'
346+
f'Bicep Parameter Validation Report</h1>'
347+
f'<p style="margin:0;font-size:13px;color:#ffffff;">'
348+
f'{_html_escape(accelerator_name) if accelerator_name else "Accelerator"}'
349+
f' &mdash; Automated Check</p>'
350+
f'</td></tr>'
351+
)
352+
353+
# --- Summary card ---
354+
parts.append(
355+
f'<tr><td style="padding:16px 24px 12px 24px;">'
356+
f'<table role="presentation" width="100%" cellpadding="0" cellspacing="0"'
357+
f' style="background-color:{status_bg};border-left:4px solid {status_color};">'
358+
f'<tr><td style="padding:12px 16px;">'
359+
f'<span style="font-size:16px;font-weight:600;color:{status_color};">'
360+
f'{status_icon} Overall Status: {overall_status}</span>'
361+
f'</td></tr>'
362+
f'<tr><td style="padding:4px 16px 12px 16px;">'
363+
f'<table role="presentation" cellpadding="0" cellspacing="0"><tr>'
364+
)
365+
# Accelerator name pill
366+
if accelerator_name:
367+
parts.append(
368+
f'<td style="padding-right:20px;vertical-align:top;">'
369+
f'<span style="font-size:11px;color:#666;">Accelerator</span><br>'
370+
f'<strong style="font-size:13px;">{_html_escape(accelerator_name)}'
371+
f'</strong></td>'
372+
)
373+
# Scan directory pill
374+
if scan_dir:
375+
parts.append(
376+
f'<td style="padding-right:20px;vertical-align:top;">'
377+
f'<span style="font-size:11px;color:#666;">Scan Directory</span><br>'
378+
f'<strong style="font-size:13px;">{_html_escape(scan_dir)}/</strong>'
379+
f'</td>'
380+
)
381+
# Error count pill
382+
err_pill_color = "#D32F2F" if total_errors > 0 else "#2E7D32"
383+
parts.append(
384+
f'<td style="padding-right:20px;vertical-align:top;">'
385+
f'<span style="font-size:11px;color:#666;">Errors</span><br>'
386+
f'<strong style="font-size:13px;color:{err_pill_color};">'
387+
f'{total_errors}</strong></td>'
388+
)
389+
# Warning count pill
390+
warn_pill_color = "#F57C00" if total_warnings > 0 else "#2E7D32"
391+
parts.append(
392+
f'<td style="vertical-align:top;">'
393+
f'<span style="font-size:11px;color:#666;">Warnings</span><br>'
394+
f'<strong style="font-size:13px;color:{warn_pill_color};">'
395+
f'{total_warnings}</strong></td>'
396+
)
397+
parts.append("</tr></table></td></tr></table></td></tr>")
398+
399+
# --- Per-pair detail sections ---
400+
parts.append('<tr><td style="padding:8px 24px 0 24px;">')
401+
for r in results:
402+
errors = [i for i in r.issues if i.severity == "ERROR"]
403+
warnings = [i for i in r.issues if i.severity == "WARNING"]
404+
405+
if not r.issues:
406+
badge = (
407+
'<span style="display:inline-block;padding:2px 8px;'
408+
'font-size:11px;font-weight:700;'
409+
'color:#2E7D32;background-color:#E8F5E9;">PASS</span>'
410+
)
411+
elif errors:
412+
badge = (
413+
'<span style="display:inline-block;padding:2px 8px;'
414+
'font-size:11px;font-weight:700;'
415+
'color:#D32F2F;background-color:#FFEBEE;">FAIL</span>'
416+
)
417+
else:
418+
badge = (
419+
'<span style="display:inline-block;padding:2px 8px;'
420+
'font-size:11px;font-weight:700;'
421+
'color:#F57C00;background-color:#FFF3E0;">WARN</span>'
422+
)
423+
424+
parts.append(
425+
f'<table role="presentation" width="100%" cellpadding="0"'
426+
f' cellspacing="0" style="margin-bottom:12px;border:1px solid #e0e0e0;">'
427+
f'<tr><td style="background-color:#fafafa;padding:10px 12px;'
428+
f'border-bottom:1px solid #e0e0e0;">'
429+
f'{badge} '
430+
f'<strong style="font-size:13px;">'
431+
f'{_html_escape(r.pair)}</strong>'
432+
f'<span style="float:right;font-size:11px;color:#888;">'
433+
f'{len(errors)} error(s), {len(warnings)} warning(s)</span>'
434+
f'</td></tr>'
435+
)
436+
437+
if r.issues:
438+
# --- Errors section ---
439+
if errors:
440+
parts.append(
441+
'<tr><td style="padding:8px 12px 4px 12px;">'
442+
'<strong style="font-size:12px;color:#D32F2F;">'
443+
'&#9679; Errors</strong></td></tr>'
444+
'<tr><td style="padding:0 12px;">'
445+
'<table role="presentation" width="100%" cellpadding="0"'
446+
' cellspacing="0" style="font-size:12px;border:1px solid #f5c6cb;">'
447+
'<tr style="background-color:#FFEBEE;">'
448+
'<th style="text-align:left;padding:6px 10px;'
449+
'border-bottom:1px solid #f5c6cb;width:180px;">Parameter</th>'
450+
'<th style="text-align:left;padding:6px 10px;'
451+
'border-bottom:1px solid #f5c6cb;">Details</th></tr>'
452+
)
453+
for idx, issue in enumerate(errors):
454+
bg = "#ffffff" if idx % 2 == 0 else "#fff5f5"
455+
parts.append(
456+
f'<tr style="background-color:{bg};">'
457+
f'<td style="padding:5px 10px;border-bottom:1px solid #f5c6cb;'
458+
f'vertical-align:top;font-family:Consolas,monospace;'
459+
f'font-size:11px;word-break:break-all;">'
460+
f'{_html_escape(issue.param_name)}</td>'
461+
f'<td style="padding:5px 10px;border-bottom:1px solid #f5c6cb;'
462+
f'vertical-align:top;">{_html_escape(issue.message)}</td>'
463+
f'</tr>'
464+
)
465+
parts.append("</table></td></tr>")
466+
467+
# --- Warnings section ---
468+
if warnings:
469+
parts.append(
470+
'<tr><td style="padding:8px 12px 4px 12px;">'
471+
'<strong style="font-size:12px;color:#F57C00;">'
472+
'&#9679; Warnings</strong></td></tr>'
473+
'<tr><td style="padding:0 12px 8px 12px;">'
474+
'<table role="presentation" width="100%" cellpadding="0"'
475+
' cellspacing="0" style="font-size:12px;border:1px solid #ffe0b2;">'
476+
'<tr style="background-color:#FFF3E0;">'
477+
'<th style="text-align:left;padding:6px 10px;'
478+
'border-bottom:1px solid #ffe0b2;width:180px;">Parameter</th>'
479+
'<th style="text-align:left;padding:6px 10px;'
480+
'border-bottom:1px solid #ffe0b2;">Details</th></tr>'
481+
)
482+
for idx, issue in enumerate(warnings):
483+
bg = "#ffffff" if idx % 2 == 0 else "#fffaf0"
484+
parts.append(
485+
f'<tr style="background-color:{bg};">'
486+
f'<td style="padding:5px 10px;border-bottom:1px solid #ffe0b2;'
487+
f'vertical-align:top;font-family:Consolas,monospace;'
488+
f'font-size:11px;word-break:break-all;">'
489+
f'{_html_escape(issue.param_name)}</td>'
490+
f'<td style="padding:5px 10px;border-bottom:1px solid #ffe0b2;'
491+
f'vertical-align:top;">{_html_escape(issue.message)}</td>'
492+
f'</tr>'
493+
)
494+
parts.append("</table></td></tr>")
495+
else:
496+
parts.append(
497+
'<tr><td style="padding:10px 12px;color:#2E7D32;'
498+
'font-size:12px;">All parameters validated successfully.'
499+
'</td></tr>'
500+
)
501+
502+
parts.append("</table>")
503+
504+
parts.append("</td></tr>")
505+
506+
# --- Footer with run URL ---
507+
footer_parts: list[str] = []
508+
if run_url:
509+
footer_parts.append(
510+
f'<a href="{_html_escape(run_url)}" style="display:inline-block;'
511+
f'padding:8px 16px;background-color:#0078D4;color:#ffffff;'
512+
f'text-decoration:none;font-size:12px;'
513+
f'font-weight:600;">View Workflow Run</a>'
514+
)
515+
if has_errors:
516+
footer_parts.append(
517+
'<p style="margin:10px 0 0 0;font-size:12px;color:#555;">'
518+
'Please fix the parameter mapping issues at your earliest convenience.</p>'
519+
)
520+
footer_parts.append(
521+
'<p style="margin:10px 0 0 0;font-size:12px;color:#999;">'
522+
'Best regards,<br>Your Automation Team</p>'
523+
)
524+
parts.append(
525+
f'<tr><td style="padding:14px 24px 20px 24px;border-top:1px solid #e0e0e0;">'
526+
f'{"".join(footer_parts)}</td></tr>'
527+
)
528+
529+
# --- Close wrapper ---
530+
parts.append("</table></td></tr></table></body></html>")
531+
return "".join(parts)
532+
533+
298534
# ---------------------------------------------------------------------------
299535
# Reporting
300536
# ---------------------------------------------------------------------------
@@ -379,6 +615,23 @@ def main() -> int:
379615
type=Path,
380616
help="Write results as JSON to the given file path.",
381617
)
618+
parser.add_argument(
619+
"--html-output",
620+
type=Path,
621+
help="Write a structured HTML email report to the given file path.",
622+
)
623+
parser.add_argument(
624+
"--accelerator-name",
625+
type=str,
626+
default="",
627+
help="Accelerator display name for the HTML report header.",
628+
)
629+
parser.add_argument(
630+
"--run-url",
631+
type=str,
632+
default="",
633+
help="Workflow run URL to include in the HTML report footer.",
634+
)
382635
args = parser.parse_args()
383636

384637
results: list[ValidationResult] = []
@@ -415,6 +668,19 @@ def main() -> int:
415668
)
416669
print(f"\nJSON report written to {args.json_output}")
417670

671+
# Optional HTML email report
672+
if args.html_output:
673+
scan_dir = str(args.dir) if args.dir else ""
674+
html = generate_html_report(
675+
results,
676+
accelerator_name=args.accelerator_name,
677+
run_url=args.run_url,
678+
scan_dir=scan_dir,
679+
)
680+
args.html_output.parent.mkdir(parents=True, exist_ok=True)
681+
args.html_output.write_text(html, encoding="utf-8")
682+
print(f"HTML report written to {args.html_output}")
683+
418684
has_errors = any(r.has_errors for r in results)
419685
return 1 if args.strict and has_errors else 0
420686

0 commit comments

Comments
 (0)