|
1
|
#!/usr/bin/env bash |
|
2
|
# |
|
3
|
# Posts a comment on a GitHub issue / PR using a server-side template. |
|
4
|
# |
|
5
|
# Usage: |
|
6
|
# GITHUB_EVENT_NUMBER=123 ./scripts/add_comment.sh \ |
|
7
|
# --workflow <triage|review> \ |
|
8
|
# --template <template-id> \ |
|
9
|
# [--params-file <path>] |
|
10
|
# |
|
11
|
# The issue / PR number is pinned to GITHUB_EVENT_NUMBER. The script refuses |
|
12
|
# to accept it on the command line so a prompt-injection cannot redirect the |
|
13
|
# comment to an unrelated issue. |
|
14
|
# |
|
15
|
# Templates live in .github/<workflow>-templates/<template-id>.md and must |
|
16
|
# exist in the repository checkout. Unknown templates are rejected. |
|
17
|
# |
|
18
|
# --params-file points to a JSON object. Each top-level key matching the |
|
19
|
# regex `[A-Z][A-Z0-9_]*` is interpolated into the template wherever the |
|
20
|
# literal placeholder `{{KEY}}` appears. Keys that don't match the regex |
|
21
|
# (e.g. lowercase keys) are ignored — the caller is responsible for |
|
22
|
# producing uppercase keys that match the placeholders. All values are |
|
23
|
# coerced to string and lightly sanitized: CR + null bytes are removed, |
|
24
|
# backticks are stripped, and `&` / `<` / `>` are HTML-escaped. This |
|
25
|
# blocks inline code spans and raw HTML, but is NOT a full Markdown |
|
26
|
# sanitizer — other constructs (`**bold**`, `_italic_`, `[link](url)`, |
|
27
|
# headings, lists, etc.) still render. Don't rely on this as the only |
|
28
|
# defense against attacker-supplied prose; the upstream schema validator |
|
29
|
# also caps length and constrains the field set. Any placeholder left in |
|
30
|
# the template after substitution is removed. |
|
31
|
# |
|
32
|
|
|
33
|
set -euo pipefail |
|
34
|
|
|
35
|
WORKFLOW="" |
|
36
|
TEMPLATE="" |
|
37
|
PARAMS_FILE="" |
|
38
|
|
|
39
|
require_value() { |
|
40
|
# Treat a missing arg, an empty string, or a next token that looks |
|
41
|
# like another flag (`--foo`) as a missing value so the failure points |
|
42
|
# at the right argument. |
|
43
|
if [[ $# -lt 2 || -z "${2:-}" || "$2" == --* ]]; then |
|
44
|
echo "Error: $1 requires a value" >&2 |
|
45
|
exit 1 |
|
46
|
fi |
|
47
|
} |
|
48
|
|
|
49
|
while [[ $# -gt 0 ]]; do |
|
50
|
case "$1" in |
|
51
|
--workflow) require_value "$@"; WORKFLOW="$2"; shift 2 ;; |
|
52
|
--template) require_value "$@"; TEMPLATE="$2"; shift 2 ;; |
|
53
|
--params-file) require_value "$@"; PARAMS_FILE="$2"; shift 2 ;; |
|
54
|
*) |
|
55
|
echo "Error: unknown argument '$1'" >&2 |
|
56
|
exit 1 |
|
57
|
;; |
|
58
|
esac |
|
59
|
done |
|
60
|
|
|
61
|
if [[ -z "$WORKFLOW" || -z "$TEMPLATE" ]]; then |
|
62
|
echo "Usage: $0 --workflow <triage|review> --template <id> [--params-file <path>]" >&2 |
|
63
|
exit 1 |
|
64
|
fi |
|
65
|
|
|
66
|
case "$WORKFLOW" in |
|
67
|
triage|review) ;; |
|
68
|
*) |
|
69
|
echo "Error: --workflow must be 'triage' or 'review'" >&2 |
|
70
|
exit 1 |
|
71
|
;; |
|
72
|
esac |
|
73
|
|
|
74
|
if ! [[ "$TEMPLATE" =~ ^[a-z][a-z0-9-]*$ ]]; then |
|
75
|
echo "Error: invalid template id '$TEMPLATE'" >&2 |
|
76
|
exit 1 |
|
77
|
fi |
|
78
|
|
|
79
|
ISSUE="${GITHUB_EVENT_NUMBER:-}" |
|
80
|
if ! [[ "$ISSUE" =~ ^[0-9]+$ ]]; then |
|
81
|
echo "Error: GITHUB_EVENT_NUMBER must be a numeric issue/PR number" >&2 |
|
82
|
exit 1 |
|
83
|
fi |
|
84
|
|
|
85
|
TEMPLATE_PATH=".github/${WORKFLOW}-templates/${TEMPLATE}.md" |
|
86
|
if [[ ! -f "$TEMPLATE_PATH" ]]; then |
|
87
|
echo "Error: template not found: $TEMPLATE_PATH" >&2 |
|
88
|
exit 1 |
|
89
|
fi |
|
90
|
|
|
91
|
if [[ -n "$PARAMS_FILE" && ! -f "$PARAMS_FILE" ]]; then |
|
92
|
echo "Error: params file not found: $PARAMS_FILE" >&2 |
|
93
|
exit 1 |
|
94
|
fi |
|
95
|
|
|
96
|
if ! command -v python3 >/dev/null 2>&1; then |
|
97
|
echo "Error: python3 is required to render comment templates but was not found in PATH" >&2 |
|
98
|
exit 1 |
|
99
|
fi |
|
100
|
|
|
101
|
# Render the template in Python for robust handling of multi-line values |
|
102
|
# and literal-string substitution (no shell-quoting / regex pitfalls). |
|
103
|
OUT_FILE=$(mktemp) |
|
104
|
trap 'rm -f "$OUT_FILE"' EXIT |
|
105
|
|
|
106
|
TEMPLATE_PATH="$TEMPLATE_PATH" PARAMS_FILE="${PARAMS_FILE:-}" OUT_FILE="$OUT_FILE" \ |
|
107
|
python3 - <<'PY' |
|
108
|
import json, os, re |
|
109
|
|
|
110
|
with open(os.environ["TEMPLATE_PATH"], "r", encoding="utf-8") as f: |
|
111
|
body = f.read() |
|
112
|
|
|
113
|
params = {} |
|
114
|
params_file = os.environ.get("PARAMS_FILE") or "" |
|
115
|
if params_file: |
|
116
|
with open(params_file, "r", encoding="utf-8") as f: |
|
117
|
params = json.load(f) |
|
118
|
if not isinstance(params, dict): |
|
119
|
raise SystemExit("params file must contain a JSON object") |
|
120
|
|
|
121
|
BACKTICK = chr(96) |
|
122
|
|
|
123
|
def sanitize(value): |
|
124
|
if isinstance(value, list): |
|
125
|
return "\n".join("- " + sanitize(v) for v in value) |
|
126
|
if value is None: |
|
127
|
return "" |
|
128
|
s = str(value) |
|
129
|
s = s.replace("\r", "").replace("\x00", "") |
|
130
|
s = s.replace(BACKTICK, "") |
|
131
|
# Escape `&` first so that pre-existing entities like `<` in the |
|
132
|
# input remain literal text (`&lt;`) instead of rendering as `<`. |
|
133
|
s = s.replace("&", "&") |
|
134
|
s = s.replace("<", "<").replace(">", ">") |
|
135
|
return s |
|
136
|
|
|
137
|
for key, value in params.items(): |
|
138
|
if not re.fullmatch(r"[A-Z][A-Z0-9_]*", str(key)): |
|
139
|
continue |
|
140
|
body = body.replace("{{" + key + "}}", sanitize(value)) |
|
141
|
|
|
142
|
body = re.sub(r"\{\{[A-Z0-9_]+\}\}", "", body) |
|
143
|
|
|
144
|
with open(os.environ["OUT_FILE"], "w", encoding="utf-8") as f: |
|
145
|
f.write(body) |
|
146
|
PY |
|
147
|
|
|
148
|
gh issue comment "$ISSUE" --body-file "$OUT_FILE" |
|
149
|
echo "Comment posted on #$ISSUE using template $WORKFLOW/$TEMPLATE" |