From 4e205dbdd05d5f56c3f666dd2662a9b8dba52db3 Mon Sep 17 00:00:00 2001 From: mashagua <993498039@qq.com> Date: Fri, 27 Nov 2026 11:35:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=20Market=20=E5=8F=8C=E8=AF=AD?= =?UTF-8?q?=E5=81=A5=E5=BA=B7=E6=8A=A5=E5=91=8A=E9=A1=B6=E9=83=A8=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E5=8C=97=E4=BA=AC=E6=97=B6=E9=97=B4=E5=92=8C=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E9=93=BE=E6=8E=A5=20(#376)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 背景 #213 的当前双语报告没有可见生成时间,折叠区里保留的旧报告日期也容易被误读。 ## 修改 - 在当次双语报告的第一行显示实际生成时间,标明北京时间(UTC+08:00)。 - 使用 GitHub Actions 的运行上下文链接到本次运行;重跑时链接到准确的 attempt。本地生成没有 Actions 上下文时只显示时间。 - 不改定时设置、审核结果、旧报告存档或 issue 发布与开关规则。 补充:每次运行的报告工件都会有新时间。#213 在首次补上时间或报告内容变化时才更新正文;仅时间或链接变化时保留当前正文及其对应的运行链接,原有开关规则不变。 ## 验证 本地运行 6 项单元/入口回归测试通过,覆盖跨日换算、运行链接、首行、首次补充时间、相同报告不改正文和结果变化时更新。未触发生产工作流或改动 #213。 --- .../catalog/test_unhealthy_report_header.py | 74 +++++++++++++++++++ scripts/catalog/unhealthy_report.py | 46 ++++++++++-- 2 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 scripts/catalog/test_unhealthy_report_header.py diff --git a/scripts/catalog/test_unhealthy_report_header.py b/scripts/catalog/test_unhealthy_report_header.py new file mode 100644 index 0000000..d4a1cbc --- /dev/null +++ b/scripts/catalog/test_unhealthy_report_header.py @@ -0,0 +1,74 @@ +"""Regression checks for the report timestamp and Actions attempt link.""" +import os +import sys +import tempfile +import unittest +from datetime import datetime, timezone +from pathlib import Path +from unittest.mock import patch + +from unhealthy_report import START, main, managed_body, report_header, sync_plan + + +class ReportHeaderTests(unittest.TestCase): + def test_midnight_rollover_and_exact_attempt(self): + env = { + 'GITHUB_SERVER_URL': 'https://github.com', + 'GITHUB_REPOSITORY': 'desirecore/market', + 'GITHUB_RUN_ID': '45922584295', + 'GITHUB_RUN_ATTEMPT': '2', + } + header = report_header(datetime(2026, 11, 26, 23, 59, 1, tzinfo=timezone.utc), env) + self.assertIn('2026-11-27 07:59:01 (UTC+08:00)', header) + self.assertIn('https://github.com/desirecore/market/actions/runs/45922584295/attempts/2', header) + self.assertTrue(managed_body('', header + '\n\nReport').startswith(START + '\n' + header)) + + def test_no_actions_context_has_time_without_a_link(self): + header = report_header(datetime(2026, 11, 27, 2, 1, tzinfo=timezone.utc), {}) + self.assertIn('2026-11-27 10:01:00 (UTC+08:00)', header) + self.assertNotIn('Workflow run', header) + + def test_cli_places_header_first_and_links_to_run(self): + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + output = root / 'report.md' + args = ['unhealthy_report.py', '--output', str(output), '--complete'] + for name in ('results', 'coverage', 'tracked'): + path = root / (name + '.json') + path.write_text('[]') + args.extend(['--' + name, str(path)]) + env = {'GITHUB_REPOSITORY': 'desirecore/market', 'GITHUB_RUN_ID': '123'} + with patch.dict(os.environ, env, clear=True), patch.object(sys, 'argv', args): + main() + first = output.read_text().splitlines()[0] + self.assertIn('Report generated (Beijing time):', first) + self.assertIn('https://github.com/desirecore/market/actions/runs/123)', first) + + def test_unchanged_rerun_skips_body_edit_but_preserves_state_transitions(self): + old = report_header(datetime(2026, 11, 27, 2, 1, tzinfo=timezone.utc), { + 'GITHUB_REPOSITORY': 'desirecore/market', 'GITHUB_RUN_ID': '123', + }) + new = report_header(datetime(2026, 11, 28, 2, 1, tzinfo=timezone.utc), { + 'GITHUB_REPOSITORY': 'desirecore/market', 'GITHUB_RUN_ID': '456', + }) + body = managed_body('', old + '\n\nSame findings') + rerun = new + '\n\nSame findings' + self.assertEqual(sync_plan({'body': body, 'state': 'open'}, rerun, True, True), {}) + self.assertEqual(sync_plan({'body': body, 'state': 'closed'}, rerun, True, True), {'state': 'open'}) + self.assertEqual(sync_plan({'body': body, 'state': 'open'}, rerun, False, True), + {'state': 'closed', 'state_reason': 'completed'}) + + def test_existing_headerless_report_gets_a_header(self): + header = report_header(datetime(2026, 11, 27, 2, 1, tzinfo=timezone.utc), {}) + issue = {'body': managed_body('', 'Same findings'), 'state': 'open'} + edits = sync_plan(issue, header + '\n\nSame findings', True, True) + self.assertIn(START + '\n' + header, edits['body']) + + def test_changed_findings_publish_new_header_and_preserve_archived_text(self): + old = report_header(datetime(2026, 11, 27, 2, 1, tzinfo=timezone.utc), {}) + new = report_header(datetime(2026, 11, 28, 2, 1, tzinfo=timezone.utc), {}) + body = managed_body('Archived text', old + '\n\nOld findings') + edits = sync_plan({'body': body, 'state': 'open'}, new + '\n\nNew findings', True, True) + self.assertIn(START + '\n' + new, edits['body']) + self.assertNotIn(old, edits['body']) + self.assertIn('Archived text', edits['body']) diff --git a/scripts/catalog/unhealthy_report.py b/scripts/catalog/unhealthy_report.py index 2abdc2d..88baacb 100644 --- a/scripts/catalog/unhealthy_report.py +++ b/scripts/catalog/unhealthy_report.py @@ -9,6 +9,7 @@ import argparse import json import os import subprocess +from datetime import datetime, timedelta, timezone from pathlib import Path from check_upstream_health import render_markdown, summarize from update_diagnostics import render_findings @@ -20,12 +21,31 @@ GUIDE = ('通过普通 PR 修正来源/路径、审核精确目标 pin 及安装 'Use normal PRs to fix sources/paths and review exact target pins and installed notices. Failures stay drafts; never rebind old receipts. Unknown network status or unreviewed budget overflow is not a pass.') -def render(results, coverage, tracked, complete=True): +REPORT_HEADER_LABEL = '**报告生成时间(北京时间) / Report generated (Beijing time):** ' + + +def report_header(generated_at, env): + beijing = generated_at.astimezone(timezone(timedelta(hours=8))) + stamp = beijing.strftime('%Y-%m-%d %H:%M:%S') + header = f'{REPORT_HEADER_LABEL}{stamp} (UTC+08:00)' + repo, run_id = env.get('GITHUB_REPOSITORY'), env.get('GITHUB_RUN_ID', '') + if repo and run_id.isascii() and run_id.isdecimal(): + server = env.get('GITHUB_SERVER_URL', 'https://github.com').rstrip('/') + run_url = f'{server}/{repo}/actions/runs/{run_id}' + attempt = env.get('GITHUB_RUN_ATTEMPT', '') + if attempt.isascii() and attempt.isdecimal(): + run_url += f'/attempts/{attempt}' + header += f' · [本次运行 / Workflow run]({run_url})' + return header + + +def render(results, coverage, tracked, complete=True, *, header=None): rows = [] for item in [*coverage, *tracked]: if not item.get('parentId'): rows.extend(item.get('findings', [])) - out = [GUIDE, '', render_markdown(results), '', '## 更新审核结果 / Update audit results', '', + out = [header, ''] if header else [] + out += [GUIDE, '', render_markdown(results), '', '## 更新审核结果 / Update audit results', '', '完整逐项证据见同次 Actions 的 audit-results 工件;本报告不代表运行验收。 / Complete per-item evidence is in the audit-results artifact of the same Actions run; this report is not runtime acceptance.', '', '| 技能 / Skill | 状态 / State | 精确旧→新 pin / Exact pins | 原因 / Reason |', '|---|---|---|---|'] labels = {'audit-passed':'静态审核通过 / static audit passed', 'manual-review-required':'人工待审 / manual review', @@ -69,11 +89,24 @@ def managed_body(previous, report): return managed + '\n' +def without_generated_header(body): + prefix, marker, managed = body.partition(START + '\n') + header, newline, rest = managed.partition('\n') + if marker and newline and header.startswith(REPORT_HEADER_LABEL) and END in rest: + return prefix + marker + rest + return None + + def sync_plan(issue, report, actionable, complete): - body = managed_body(issue.get('body') or '', report) + previous = issue.get('body') or '' + body = managed_body(previous, report) edits = {} - if body != (issue.get('body') or ''): - edits['body'] = body + # The issue links to the run that generated the content it displays. + # A report without a header still gets one on its first publish. + if body != previous: + previous_content = without_generated_header(previous) + if previous_content is None or previous_content != without_generated_header(body): + edits['body'] = body if actionable and issue['state'] == 'closed': edits['state'] = 'open' elif not actionable and complete and issue['state'] == 'open': @@ -97,7 +130,8 @@ def main(): a = p.parse_args() complete = a.complete and all(path.is_file() for path in (a.results, a.coverage, a.tracked)) results, coverage, tracked = [json.loads(path.read_text()) if path.is_file() else [] for path in (a.results, a.coverage, a.tracked)] - report, actionable = render(results, coverage, tracked, complete) + header = report_header(datetime.now(timezone.utc), os.environ) + report, actionable = render(results, coverage, tracked, complete, header=header) a.output.write_text(report + '\n') if not a.publish: return