11/**
2- * 维护者在 PR 提交 review 后, 同步 PR 的审核状态标签
2+ * 维护者在 PR 提交 review 后: 同步 PR 状态标签,并把结果回贴到关联 issue 通知投稿人
33 *
4- * approved → 已通过;changes_requested → 已拒绝。仅协作者的 review 会触发分支保护放行。
4+ * approved → 已通过;changes_requested → 已拒绝。PR 由机器人创建(作者不是投稿人),
5+ * 故审核结果必须经 PR 正文的 Closes #N 回到原 issue,否则投稿人在 issue 上看不到驳回意见。
56 */
67
78import { readFileSync } from "node:fs" ;
@@ -10,8 +11,10 @@ const token = process.env.GITHUB_TOKEN;
1011const repo = process . env . GITHUB_REPOSITORY ;
1112const api = process . env . GITHUB_API_URL ?? "https://api.github.com" ;
1213const event = JSON . parse ( readFileSync ( process . env . GITHUB_EVENT_PATH , "utf-8" ) ) ;
13- const number = event . pull_request . number ;
14- const state = event . review . state ;
14+ const pr = event . pull_request ;
15+ const review = event . review ;
16+ const number = pr . number ;
17+ const state = review . state ;
1518
1619const gh = ( method , path , payload ) =>
1720 fetch ( `${ api } ${ path } ` , {
@@ -22,12 +25,33 @@ const gh = (method, path, payload) =>
2225const add = ( n ) => gh ( "POST" , `/repos/${ repo } /issues/${ number } /labels` , { labels : [ n ] } ) ;
2326const remove = ( n ) => gh ( "DELETE" , `/repos/${ repo } /issues/${ number } /labels/${ encodeURIComponent ( n ) } ` ) ;
2427
28+ /** 从 PR 正文的 Closes #N 取关联 issue 号 */
29+ const linkedIssue = ( ) => {
30+ const matched = ( pr . body ?? "" ) . match ( / # ( \d + ) / ) ;
31+ return matched ? Number ( matched [ 1 ] ) : null ;
32+ } ;
33+
34+ /** 回贴到关联 issue,并 @ 投稿人 */
35+ async function notifyIssue ( body ) {
36+ const issueNo = linkedIssue ( ) ;
37+ if ( ! issueNo ) return ;
38+ const res = await gh ( "GET" , `/repos/${ repo } /issues/${ issueNo } ` ) ;
39+ const issue = res . ok ? await res . json ( ) : null ;
40+ const at = issue ?. user ?. login ? `@${ issue . user . login } ` : "" ;
41+ await gh ( "POST" , `/repos/${ repo } /issues/${ issueNo } /comments` , { body : at + body } ) ;
42+ }
43+
2544if ( state === "approved" ) {
2645 await add ( "已通过" ) ;
2746 await remove ( "待审核" ) ;
2847 await remove ( "已拒绝" ) ;
48+ await notifyIssue ( "✅ 审核通过,合并后即收录到插件市场。" ) ;
2949} else if ( state === "changes_requested" ) {
3050 await add ( "已拒绝" ) ;
3151 await remove ( "待审核" ) ;
3252 await remove ( "已通过" ) ;
53+ const note = review . body ?. trim ( ) ? "\n\n> " + review . body . trim ( ) . replace ( / \n / g, "\n> " ) : "" ;
54+ await notifyIssue (
55+ `❌ 维护者请求修改:${ note } \n\n请按意见修改脚本后,编辑本 issue 触发重新校验(或评论 \`/recheck\`)。` ,
56+ ) ;
3357}
0 commit comments