@dtapp/ci-templates 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,519 @@
1
+ #!/bin/sh
2
+ # shellcheck disable=SC3043 # local 不是 POSIX 标准,但 dash / ash / bash 都支持
3
+ #
4
+ # 生成 CNB 流水线 IM 通知正文(企业微信 / 飞书 / 钉钉共用)。
5
+ #
6
+ # 三个 workflow 各自生成通知正文的逻辑原先内嵌在 YAML heredoc 里难以维护,
7
+ # 这里统一收口为 POSIX sh 实现(替代早先的 render-ci-notice.py):
8
+ # - sechook : 读取 cnbcool/cnb-sechook 写出的 sec_overview.md(完整扫描报告)
9
+ # - review : 读取 cnbcool/code-review 写出的 code_review.json(status + issues)
10
+ # - sca : 通过 CNB OpenAPI 读取 Commit Annotation(sca.<capability>.<mode>.<field>)
11
+ #
12
+ # 每个子命令生成两份正文:
13
+ # - <name>_notice.md : markdown 版,「查看详情」为 [查看详情](URL) 超链接
14
+ # → 企业微信 fromFile、钉钉 content
15
+ # - <name>_notice.txt: 纯文本版,URL 裸写(飞书 text 消息会自动识别为链接)
16
+ # → 飞书 content
17
+ # 两份都通过 CNB set-output(base64)导出,避免多行被逐行解析。
18
+ #
19
+ # 依赖镜像:docker.cnb.cool/dtapp/alpine:latest(已预装 jq / curl / coreutils / tzdata)。
20
+ # 时间:CNB_BUILD_START_TIME(UTC)经 epoch + 8h 转北京时间(UTC+8)。
21
+ #
22
+ # 用法:
23
+ # sh scripts/ci/render-ci-notice.sh sechook [输出.md]
24
+ # sh scripts/ci/render-ci-notice.sh review [输出.md]
25
+ # sh scripts/ci/render-ci-notice.sh sca full|diff [输出.md]
26
+ # sh scripts/ci/render-ci-notice.sh --self-test
27
+ #
28
+ # ⚠️ 保持 POSIX sh 语法:CNB 的 stage 默认用 sh(dash/ash)执行。
29
+ # 不要引入 [[ ]]、数组、BASH_SOURCE、$'...' 这类 bash 专有写法。
30
+
31
+ set -eu
32
+
33
+ die() {
34
+ printf 'error: %s\n' "$*" >&2
35
+ exit 1
36
+ }
37
+
38
+ # 链接样式,由 render_* 在生成两份正文时切换:md(markdown 链接)/ text(裸 URL)
39
+ LINK_STYLE='md'
40
+
41
+ # 时间行:CNB_BUILD_START_TIME 为 UTC(如 2026-11-18T05:45:30.123Z),转北京时间。
42
+ # 先把 UTC 字符串归一化成 "YYYY-MM-DD HH:MM:SS"(去掉 T / 毫秒 / Z),
43
+ # 再统一转成 epoch 秒 + 8h,最后格式化——避免 GNU date -d 与 BSD date -j 的时区语义差异。
44
+ build_time_text() {
45
+ local raw="${CNB_BUILD_START_TIME:-}" norm epoch result
46
+ if [ -z "${raw}" ]; then
47
+ printf '未知'
48
+ return 0
49
+ fi
50
+
51
+ norm="$(printf '%s' "${raw}" \
52
+ | sed -E 's/T/ /; s/\.[0-9]+Z?$//; s/Z$//')"
53
+
54
+ epoch="$(date -u -d "${norm} UTC" '+%s' 2>/dev/null || true)"
55
+ if [ -z "${epoch}" ]; then
56
+ epoch="$(date -u -j -f '%Y-%m-%d %H:%M:%S' "${norm}" '+%s' 2>/dev/null || true)"
57
+ fi
58
+ if [ -z "${epoch}" ]; then
59
+ printf '%s' "${raw}"
60
+ return 0
61
+ fi
62
+
63
+ epoch=$((epoch + 8 * 3600))
64
+ result="$(date -u -d "@${epoch}" '+%Y-%m-%d %H:%M:%S' 2>/dev/null || true)"
65
+ if [ -z "${result}" ]; then
66
+ result="$(date -u -r "${epoch}" '+%Y-%m-%d %H:%M:%S' 2>/dev/null || true)"
67
+ fi
68
+ printf '%s' "${result:-${raw}}"
69
+ }
70
+
71
+ # 所有通知共用的头信息:标题 + 时间 + 仓库 + PR 上下文 + 可点击的查看详情链接。
72
+ # header <标题> > 输出文件
73
+ header() {
74
+ local title="$1" url="${CNB_EVENT_URL:-}"
75
+ printf '## %s\n\n' "${title}"
76
+ printf -- '- 时间: %s (UTC+8)\n' "$(build_time_text)"
77
+ printf -- '- 仓库: %s\n' "${CNB_REPO_SLUG:-}"
78
+ if [ -n "${CNB_PULL_REQUEST_TITLE:-}" ]; then
79
+ printf -- '- PR: %s\n' "${CNB_PULL_REQUEST_TITLE}"
80
+ fi
81
+ if [ -n "${CNB_PULL_REQUEST_PROPOSER:-}" ]; then
82
+ printf -- '- 发起人: %s\n' "${CNB_PULL_REQUEST_PROPOSER}"
83
+ fi
84
+ if [ "${LINK_STYLE}" = 'md' ]; then
85
+ # 企业微信 / 钉钉 markdown:点击「查看详情」四个字即可跳转
86
+ printf '[查看详情](%s)\n\n' "${url}"
87
+ else
88
+ # 飞书 text 消息不渲染 markdown,裸 URL 由客户端自动识别为可点击链接
89
+ printf '查看详情: %s\n\n' "${url}"
90
+ fi
91
+ }
92
+
93
+ # 写正文文件 + 打印 CNB set-output(base64 避免多行被逐行解析)。
94
+ # emit <输出文件> <result key>
95
+ emit() {
96
+ local out_file="$1" key="$2" encoded
97
+ # coreutils 的 base64 -w0 不折行;busybox 不支持 -w,兜底 tr 去换行
98
+ encoded="$(base64 -w0 <"${out_file}" 2>/dev/null || base64 <"${out_file}" | tr -d '\n')"
99
+ printf '##[set-output %s=base64,%s]\n' "${key}" "${encoded}"
100
+ }
101
+
102
+ # 由 .md 路径推导 .txt 路径
103
+ text_path() {
104
+ printf '%s' "${1%.md}.txt"
105
+ }
106
+
107
+ # 由 .md 路径推导飞书 post JSON 路径
108
+ feishu_post_path() {
109
+ printf '%s' "${1%.md}_feishu.json"
110
+ }
111
+
112
+ # 把正文里的裸 URL 转成 markdown 链接(企业微信 / 钉钉)。
113
+ # 链接文字用短词,不显示完整 URL:
114
+ # "查看详情: URL" -> "[查看详情](URL)"
115
+ # "- 报告: URL" -> "- [查看报告](URL)"(去掉「报告:」前缀避免重复)
116
+ # 其他裸 URL -> "[查看链接](URL)"
117
+ # 已经在 `](...)` 里的 URL 跳过(header 的 [查看详情](URL) 不会被二次包装)。
118
+ md_linkify() {
119
+ awk '
120
+ {
121
+ line = $0; out = ""
122
+ while (match(line, /https?:\/\/[^ )<>]+/)) {
123
+ pre = substr(line, 1, RSTART - 1)
124
+ url = substr(line, RSTART, RLENGTH)
125
+ line = substr(line, RSTART + RLENGTH)
126
+ if (pre ~ /\($/) {
127
+ out = out pre url
128
+ } else {
129
+ if (pre ~ /查看详情: *$/) { sub(/查看详情: *$/, "", pre); label = "查看详情" }
130
+ else if (pre ~ /报告: *$/) { sub(/报告: *$/, "", pre); label = "查看报告" }
131
+ else { label = "查看链接" }
132
+ out = out pre "[" label "](" url ")"
133
+ }
134
+ }
135
+ print out line
136
+ }
137
+ '
138
+ }
139
+
140
+ # 把纯文本正文转成飞书 post 富文本 JSON(filepath 格式,外层包 content)。
141
+ # 每行文字作为一个 text 元素行;行内任何 URL 都拆成可点击的 a 标签。
142
+ # 「查看详情: <URL>」那行只保留一个「查看详情」a 标签。
143
+ # build_feishu_post <标题> <纯文本文件> <输出 json>
144
+ build_feishu_post() {
145
+ local title="$1" txt_file="$2" out_file="$3"
146
+ jq -n \
147
+ --arg title "${title}" \
148
+ --rawfile body "${txt_file}" \
149
+ '
150
+ def clean: sub("^## "; "") | sub("^### "; "") | sub("^- "; "");
151
+ # 把一行拆成 text/a 元素数组;链接文字用短词,不显示完整 URL。
152
+ # "查看详情: URL" -> 单个「查看详情」a;
153
+ # "报告: URL" -> 去掉「报告:」前缀后接「查看报告」a;
154
+ # 其他 URL -> 前缀文字 + 「查看链接」a。
155
+ def row:
156
+ clean as $line
157
+ | if $line == "" then []
158
+ elif ($line | startswith("查看详情: ")) then
159
+ [{ "tag": "a", "text": "查看详情", "href": ($line | sub("^查看详情: "; "")) }]
160
+ else
161
+ ([ $line | match("https?://[^ )<>]+"; "g") ]) as $ms
162
+ | if ($ms | length) == 0 then
163
+ [{ "tag": "text", "text": $line }]
164
+ else
165
+ reduce $ms[] as $m
166
+ ({ out: [], pos: 0 };
167
+ ($m.offset) as $start
168
+ | ($m.offset + ($m.string | length)) as $end
169
+ | ($line[.pos:$start]) as $seg
170
+ | (if ($seg | test("查看详情: *$")) then { pre: ($seg | sub("查看详情: *$"; "")), label: "查看详情" }
171
+ elif ($seg | test("报告: *$")) then { pre: ($seg | sub("报告: *$"; "")), label: "查看报告" }
172
+ else { pre: $seg, label: "查看链接" } end) as $h
173
+ | .out += (if ($h.pre | length) > 0 then [{ "tag": "text", "text": $h.pre }] else [] end)
174
+ | .out += [{ "tag": "a", "text": $h.label, "href": $line[$start:$end] }]
175
+ | .pos = $end)
176
+ | .out + (if .pos < ($line | length) then [{ "tag": "text", "text": $line[.pos:] }] else [] end)
177
+ end
178
+ end;
179
+ ($body | split("\n") | map(select(length > 0))) as $lines
180
+ | [ $lines[] | row ] as $rows
181
+ | { content: { post: { zh_cn: { title: $title, content: $rows } } } }
182
+ ' >"${out_file}"
183
+ }
184
+
185
+ # ── sechook:安全扫描报告全文 ──────────────────────────────────────────
186
+ sechook_body() {
187
+ local report="${SECHOOK_REPORT:-sec_overview.md}"
188
+ if [ -f "${report}" ]; then
189
+ cat "${report}"
190
+ else
191
+ printf '读取扫描报告失败: %s\n' "${report}"
192
+ fi
193
+ }
194
+
195
+ render_sechook() {
196
+ local md_file="$1" key="$2" txt_file feishu_file
197
+ txt_file="$(text_path "${md_file}")"
198
+ feishu_file="$(feishu_post_path "${md_file}")"
199
+ LINK_STYLE='md'
200
+ {
201
+ header '安全扫描结果'
202
+ sechook_body
203
+ } | md_linkify >"${md_file}"
204
+ LINK_STYLE='text'
205
+ {
206
+ header '安全扫描结果'
207
+ sechook_body
208
+ } >"${txt_file}"
209
+ build_feishu_post '安全扫描结果' "${txt_file}" "${feishu_file}"
210
+ emit "${md_file}" "${key}"
211
+ emit "${txt_file}" "${key}_text"
212
+ }
213
+
214
+ # ── review:AI 代码评审结果 ────────────────────────────────────────────
215
+ review_body() {
216
+ if [ "$(jq '.issues | length' code_review.json)" -gt 0 ]; then
217
+ printf '### 发现 %s 个问题\n\n' "$(jq '.issues | length' code_review.json)"
218
+ # 索引从 1 开始;problem/suggestion 去掉插件自带的 ** 加粗,避免裸 ** 破坏排版
219
+ jq -r '
220
+ .issues | to_entries[] |
221
+ "**\(.key + 1). [\(.value.severity // "info")] \(.value.file // ""):\(.value.start_line // "")**",
222
+ (.value.problem // "" | gsub("\\*\\*"; "") | select(length > 0)),
223
+ (.value.suggestion // "" | gsub("\\*\\*"; "") | select(length > 0)),
224
+ ""
225
+ ' code_review.json
226
+ else
227
+ printf '未发现问题,代码质量良好。\n'
228
+ fi
229
+ }
230
+
231
+ render_review() {
232
+ local md_file="$1" key="$2" txt_file status label title
233
+ [ -f code_review.json ] || die '找不到 code_review.json(code-review 插件需设置 output: ./code_review.json)'
234
+
235
+ status="$(jq -r '.status // "unknown"' code_review.json)"
236
+ case "${status}" in
237
+ passed) label='通过' ;;
238
+ needs_modification) label='需要修改' ;;
239
+ critical) label='存在严重问题' ;;
240
+ *) label="${status}" ;;
241
+ esac
242
+ title="AI 代码评审结果: ${label}"
243
+ txt_file="$(text_path "${md_file}")"
244
+
245
+ LINK_STYLE='md'
246
+ {
247
+ header "${title}"
248
+ review_body
249
+ } | md_linkify >"${md_file}"
250
+ LINK_STYLE='text'
251
+ {
252
+ header "${title}"
253
+ review_body
254
+ } >"${txt_file}"
255
+ build_feishu_post "${title}" "${txt_file}" "$(feishu_post_path "${md_file}")"
256
+ emit "${md_file}" "${key}"
257
+ emit "${txt_file}" "${key}_text"
258
+ }
259
+
260
+ # ── sca:从 Commit Annotation 读取漏洞/License/SBOM 摘要 ───────────────
261
+ # ANNOTATION_LINES 为临时文件,每行格式 |key|value,供 grep -F 精确取值
262
+ ANNOTATION_LINES=''
263
+
264
+ # ann_value <key>:从 annotation 行文件里取指定 key 的 value
265
+ ann_value() {
266
+ local key="$1" line
267
+ [ -n "${ANNOTATION_LINES}" ] || return 0
268
+ line="$(grep -F "|${key}|" "${ANNOTATION_LINES}" | head -n 1 || true)"
269
+ printf '%s' "${line#*|}" | cut -d'|' -f2-
270
+ }
271
+
272
+ # has_any <key>...:任一 key 有值即返回 0
273
+ has_any() {
274
+ local key
275
+ for key in "$@"; do
276
+ if [ -n "$(ann_value "${key}")" ]; then
277
+ return 0
278
+ fi
279
+ done
280
+ return 1
281
+ }
282
+
283
+ # sca_section <小节标题> <key 前缀> <"字段:显示名 字段:显示名 ...">
284
+ sca_section() {
285
+ local title="$1" prefix="$2" fields="$3"
286
+ local pair field label value gate reporter keys=''
287
+
288
+ for pair in ${fields}; do
289
+ keys="${keys} ${prefix}${pair%%:*}"
290
+ done
291
+ # shellcheck disable=SC2086 # keys 需按空格拆成多个参数
292
+ if ! has_any ${keys}; then
293
+ return 0
294
+ fi
295
+
296
+ printf '### %s\n' "${title}"
297
+ for pair in ${fields}; do
298
+ field="${pair%%:*}"
299
+ label="${pair#*:}"
300
+ value="$(ann_value "${prefix}${field}")"
301
+ if [ -n "${value}" ]; then
302
+ printf -- '- %s: %s\n' "${label}" "${value}"
303
+ fi
304
+ done
305
+
306
+ gate="$(ann_value "${prefix}gate")"
307
+ if [ -n "${gate}" ]; then
308
+ if [ "${gate}" = 'gate' ]; then
309
+ gate='命中门禁'
310
+ fi
311
+ printf -- '- 门禁: %s\n' "${gate}"
312
+ fi
313
+ reporter="$(ann_value "${prefix}reporter")"
314
+ if [ -n "${reporter}" ]; then
315
+ printf -- '- 报告: %s\n' "${reporter}"
316
+ fi
317
+ printf '\n'
318
+ }
319
+
320
+ sca_body() {
321
+ local mode="$1" label
322
+ if [ "${mode}" = 'full' ]; then
323
+ label='组件总数'
324
+ else
325
+ label='变化组件数'
326
+ fi
327
+ sca_section '开源组件漏洞' "sca.vulnerability.${mode}." \
328
+ 'critical.count:Critical high.count:High medium.count:Medium low.count:Low'
329
+ sca_section 'License 风险' "sca.license.${mode}." \
330
+ 'high.count:High medium.count:Medium low.count:Low'
331
+ sca_section 'SBOM 组件' "sca.sbom.${mode}." "total.count:${label}"
332
+
333
+ if ! grep -q '^|sca\.' "${ANNOTATION_LINES}" 2>/dev/null; then
334
+ printf '未读取到扫描结果(扫描可能未执行完成)。\n'
335
+ fi
336
+ if [ -n "${SCA_ERROR:-}" ]; then
337
+ printf '读取结果失败: %s\n' "${SCA_ERROR}"
338
+ fi
339
+ }
340
+
341
+ render_sca() {
342
+ local mode="$1" md_file="$2" key="$3"
343
+ local api repo sha token url raw_json txt_file title
344
+
345
+ case "${mode}" in
346
+ full) sha="${CNB_COMMIT:-}" ;;
347
+ diff) sha="${CNB_PULL_REQUEST_SHA:-${CNB_COMMIT:-}}" ;;
348
+ *) die "sca 子命令需要 full 或 diff,收到: ${mode}" ;;
349
+ esac
350
+
351
+ ANNOTATION_LINES="$(mktemp "${TMPDIR:-/tmp}/certflow-sca.XXXXXX")"
352
+ raw_json="$(mktemp "${TMPDIR:-/tmp}/certflow-sca-json.XXXXXX")"
353
+ SCA_ERROR=''
354
+
355
+ api="${CNB_API_ENDPOINT:-}"
356
+ api="${api%/}"
357
+ repo="${CNB_REPO_SLUG:-}"
358
+ token="${CNB_TOKEN:-}"
359
+
360
+ if [ -z "${api}" ] || [ -z "${repo}" ] || [ -z "${sha}" ] || [ -z "${token}" ]; then
361
+ SCA_ERROR='缺少 CNB_API_ENDPOINT / CNB_REPO_SLUG / CNB_TOKEN / SHA'
362
+ else
363
+ url="${api}/${repo}/-/git/commit-annotations/${sha}"
364
+ if ! curl -fsS --max-time 30 \
365
+ -H "Authorization: Bearer ${token}" \
366
+ -H 'Accept: application/json' \
367
+ "${url}" -o "${raw_json}" 2>/dev/null; then
368
+ SCA_ERROR='读取 Commit Annotation 失败(OpenAPI 请求错误)'
369
+ : >"${raw_json}"
370
+ fi
371
+ fi
372
+
373
+ if [ -s "${raw_json}" ]; then
374
+ jq -r '.[] | select(.key) | "|" + .key + "|" + (.value // "")' "${raw_json}" \
375
+ >"${ANNOTATION_LINES}" 2>/dev/null || : >"${ANNOTATION_LINES}"
376
+ fi
377
+ rm -f "${raw_json}"
378
+
379
+ if [ "${mode}" = 'full' ]; then
380
+ title='SCA 全量扫描结果'
381
+ else
382
+ title='SCA 增量扫描结果'
383
+ fi
384
+ txt_file="$(text_path "${md_file}")"
385
+
386
+ LINK_STYLE='md'
387
+ {
388
+ header "${title}"
389
+ sca_body "${mode}"
390
+ } | md_linkify >"${md_file}"
391
+ LINK_STYLE='text'
392
+ {
393
+ header "${title}"
394
+ sca_body "${mode}"
395
+ } >"${txt_file}"
396
+ build_feishu_post "${title}" "${txt_file}" "$(feishu_post_path "${md_file}")"
397
+
398
+ rm -f "${ANNOTATION_LINES}"
399
+ ANNOTATION_LINES=''
400
+ emit "${md_file}" "${key}"
401
+ emit "${txt_file}" "${key}_text"
402
+ }
403
+
404
+ # ── 自检 ──────────────────────────────────────────────────────────────
405
+ self_test() {
406
+ local tmp_dir cwd failures=0 out
407
+ tmp_dir="$(mktemp -d "${TMPDIR:-/tmp}/certflow-notice.XXXXXX")"
408
+ cwd="$(pwd)"
409
+
410
+ check() {
411
+ local name="$1" ok="$2" detail="${3:-}"
412
+ if [ "${ok}" = '1' ]; then
413
+ printf ' ✓ %s\n' "${name}"
414
+ else
415
+ printf ' ✗ %s%s\n' "${name}" "$([ -n "${detail}" ] && printf '(%s)' "${detail}")"
416
+ failures=$((failures + 1))
417
+ fi
418
+ }
419
+
420
+ printf '时间转换\n'
421
+ CNB_BUILD_START_TIME='2026-11-18T05:45:30.123Z'
422
+ out="$(build_time_text)"
423
+ check 'UTC 05:45:30 -> 北京 13:45:30' "$([ "${out}" = '2026-11-18 13:45:30' ] && printf 1 || printf 0)" "${out}"
424
+ CNB_BUILD_START_TIME='not-a-time'
425
+ out="$(build_time_text)"
426
+ check '非法时间原样返回' "$([ "${out}" = 'not-a-time' ] && printf 1 || printf 0)" "${out}"
427
+
428
+ CNB_BUILD_START_TIME='2026-11-18T05:45:30.123Z'
429
+ CNB_REPO_SLUG='dtapp/certflow'
430
+ CNB_PULL_REQUEST_TITLE='feat: test'
431
+ CNB_PULL_REQUEST_PROPOSER='tester'
432
+ CNB_EVENT_URL='https://cnb.cool/dtapp/certflow/-/pulls/1'
433
+
434
+ printf 'sechook\n'
435
+ printf '## 敏感信息统计\n高风险: 0\n## 漏洞统计\nCritical: 1\n' >"${tmp_dir}/sec_overview.md"
436
+ SECHOOK_REPORT="${tmp_dir}/sec_overview.md" render_sechook "${tmp_dir}/scan_notice.md" scan >/dev/null
437
+ out="$(cat "${tmp_dir}/scan_notice.md")"
438
+ check '含时间行' "$(printf '%s' "${out}" | grep -q -- '- 时间: 2026-11-18 13:45:30 (UTC+8)' && printf 1 || printf 0)"
439
+ check '含报告原文' "$(printf '%s' "${out}" | grep -q '高风险: 0' && printf 1 || printf 0)"
440
+ check 'md 版超链接' "$(printf '%s' "${out}" | grep -qF '[查看详情](https://cnb.cool/dtapp/certflow/-/pulls/1)' && printf 1 || printf 0)"
441
+ check 'md 版无裸 URL 行' "$(printf '%s' "${out}" | grep -qF '查看详情: https://' && printf 0 || printf 1)"
442
+ out="$(cat "${tmp_dir}/scan_notice.txt")"
443
+ check 'text 版裸 URL' "$(printf '%s' "${out}" | grep -qF '查看详情: https://cnb.cool/dtapp/certflow/-/pulls/1' && printf 1 || printf 0)"
444
+ check 'text 版无 markdown 链接' "$(printf '%s' "${out}" | grep -qF '[查看详情](' && printf 0 || printf 1)"
445
+ check '飞书 post 是合法 JSON' "$(jq -e '.content.post.zh_cn.title == "安全扫描结果"' "${tmp_dir}/scan_notice_feishu.json" >/dev/null 2>&1 && printf 1 || printf 0)"
446
+ check '飞书 post 含 a 标签超链接' "$(jq -e '[.content.post.zh_cn.content[][] | select(.tag == "a" and .text == "查看详情" and .href == "https://cnb.cool/dtapp/certflow/-/pulls/1")] | length > 0' "${tmp_dir}/scan_notice_feishu.json" >/dev/null 2>&1 && printf 1 || printf 0)"
447
+
448
+ printf 'review\n'
449
+ cd "${tmp_dir}"
450
+ cat >code_review.json <<'JSON'
451
+ {"status":"needs_modification","issues":[{"severity":"warning","file":"main.go","start_line":42,"problem":"**问题**: 缺少校验","suggestion":"**建议**: 增加判空"}]}
452
+ JSON
453
+ render_review review_notice.md review >/dev/null
454
+ out="$(cat review_notice.md)"
455
+ cd "${cwd}"
456
+ check '含状态中文' "$(printf '%s' "${out}" | grep -q '需要修改' && printf 1 || printf 0)"
457
+ check '含问题位置' "$(printf '%s' "${out}" | grep -q '\[warning\] main.go:42' && printf 1 || printf 0)"
458
+ check '去掉裸 **' "$(printf '%s' "${out}" | grep -q '问题: 缺少校验' && ! printf '%s' "${out}" | grep -q '\*\*问题\*\*' && printf 1 || printf 0)"
459
+ check 'md 版超链接' "$(printf '%s' "${out}" | grep -qF '[查看详情](https://cnb.cool/dtapp/certflow/-/pulls/1)' && printf 1 || printf 0)"
460
+
461
+ printf 'sca\n'
462
+ cat >"${tmp_dir}/annotations.json" <<'JSON'
463
+ [
464
+ {"key":"sca.vulnerability.full.high.count","value":"3"},
465
+ {"key":"sca.vulnerability.full.gate","value":"gate"},
466
+ {"key":"sca.vulnerability.full.reporter","value":"https://cnb.cool/report?tab=attachments"},
467
+ {"key":"sca.sbom.full.total.count","value":"128"}
468
+ ]
469
+ JSON
470
+ # 自检不联网:直接把 mock JSON 转成 |key|value 行,跳过 curl
471
+ ANNOTATION_LINES="${tmp_dir}/ann-lines"
472
+ jq -r '.[] | select(.key) | "|" + .key + "|" + (.value // "")' "${tmp_dir}/annotations.json" >"${ANNOTATION_LINES}"
473
+ LINK_STYLE='md'
474
+ {
475
+ header 'SCA 全量扫描结果'
476
+ sca_body full
477
+ } | md_linkify >"${tmp_dir}/sca_notice.md"
478
+ out="$(cat "${tmp_dir}/sca_notice.md")"
479
+ rm -f "${ANNOTATION_LINES}"
480
+ ANNOTATION_LINES=''
481
+ check '含漏洞计数' "$(printf '%s' "${out}" | grep -q 'High: 3' && printf 1 || printf 0)"
482
+ check '门禁映射中文' "$(printf '%s' "${out}" | grep -q '命中门禁' && printf 1 || printf 0)"
483
+ check '含 SBOM 组件数' "$(printf '%s' "${out}" | grep -q '组件总数: 128' && printf 1 || printf 0)"
484
+ check 'md 版超链接' "$(printf '%s' "${out}" | grep -qF '[查看详情](https://cnb.cool/dtapp/certflow/-/pulls/1)' && printf 1 || printf 0)"
485
+ check '报告 URL 已 linkify' "$(printf '%s' "${out}" | grep -qF '[查看报告](https://cnb.cool/report?tab=attachments)' && printf 1 || printf 0)"
486
+ check '不显示完整 URL 作链接文字' "$(printf '%s' "${out}" | grep -qF '[https://' && printf 0 || printf 1)"
487
+
488
+ rm -rf "${tmp_dir}"
489
+
490
+ if [ "${failures}" -ne 0 ]; then
491
+ printf '\n自检失败: %s 项\n' "${failures}" >&2
492
+ return 1
493
+ fi
494
+ printf '\n自检通过\n'
495
+ return 0
496
+ }
497
+
498
+ usage() {
499
+ cat <<'USAGE'
500
+ 用法:
501
+ sh scripts/ci/render-ci-notice.sh sechook [输出.md]
502
+ sh scripts/ci/render-ci-notice.sh review [输出.md]
503
+ sh scripts/ci/render-ci-notice.sh sca full|diff [输出.md]
504
+ sh scripts/ci/render-ci-notice.sh --self-test
505
+ USAGE
506
+ }
507
+
508
+ case "${1:-}" in
509
+ sechook) render_sechook "${2:-scan_notice.md}" scan ;;
510
+ review) render_review "${2:-review_notice.md}" review ;;
511
+ sca) render_sca "${2:-}" "${3:-sca_notice.md}" sca ;;
512
+ --self-test) self_test ;;
513
+ -h | --help) usage ;;
514
+ '')
515
+ usage >&2
516
+ exit 2
517
+ ;;
518
+ *) die "未知子命令: ${1}" ;;
519
+ esac