-
Notifications
You must be signed in to change notification settings - Fork 0
137 lines (121 loc) · 4.25 KB
/
normalize-update-time.yml
File metadata and controls
137 lines (121 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
name: 拒绝 updateTime 修改
on:
pull_request_target:
types: [opened, synchronize, reopened]
paths:
- 'plugins.v4.json'
permissions:
contents: read
pull-requests: write
issues: write
jobs:
reject:
name: 检查并关闭 updateTime 变更
runs-on: ubuntu-latest
steps:
- name: Detect updateTime change
uses: actions/github-script@v7
with:
script: |
const pr = context.payload.pull_request;
const { owner, repo } = context.repo;
async function getUpdateTime(targetOwner, targetRepo, ref) {
const { data } = await github.rest.repos.getContent({
owner: targetOwner,
repo: targetRepo,
path: 'plugins.v4.json',
ref,
});
const content = Buffer.from(data.content, data.encoding || 'base64').toString('utf8');
const json = JSON.parse(content);
return json.updateTime;
}
let baseTime;
let headTime;
try {
let mergeBaseSha = pr.merge_base_sha;
if (!mergeBaseSha) {
const { data: prData } = await github.rest.pulls.get({
owner,
repo,
pull_number: pr.number,
});
mergeBaseSha = prData.merge_base_sha;
}
if (!mergeBaseSha) {
try {
const { data: compare } = await github.rest.repos.compareCommits({
owner,
repo,
base: pr.base.sha || pr.base.ref,
head: `${pr.head.repo.owner.login}:${pr.head.ref}`,
});
mergeBaseSha = compare.merge_base_commit && compare.merge_base_commit.sha;
} catch (err) {
console.log(`compareCommits failed: ${err.message}`);
}
}
if (!mergeBaseSha) {
console.log('merge_base_sha not found; fallback to base sha.');
mergeBaseSha = pr.base.sha || pr.base.ref;
}
if (!mergeBaseSha) {
console.log('base sha not found; skip.');
return;
}
baseTime = await getUpdateTime(
pr.base.repo.owner.login,
pr.base.repo.name,
mergeBaseSha
);
headTime = await getUpdateTime(
pr.head.repo.owner.login,
pr.head.repo.name,
pr.head.sha
);
} catch (err) {
console.log(`Failed to read plugins.v4.json: ${err.message}`);
return;
}
if (baseTime === headTime) {
console.log('updateTime unchanged; skip.');
return;
}
const body = [
'## ⚠️ updateTime 修改被拒绝',
'',
`检测到 updateTime 被修改(base: ${baseTime || '缺失'}, current: ${headTime || '缺失'})。`,
'请删除 updateTime 的修改后重新提交。',
'updateTime 会在 PR 合并后由 CI 自动更新。',
].join('\n');
const { data: comments } = await github.rest.issues.listComments({
owner,
repo,
issue_number: pr.number,
per_page: 100,
});
const botComment = comments.find(
c => c.user.type === 'Bot' && c.body.includes('updateTime 修改被拒绝')
);
if (botComment) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: botComment.id,
body,
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number: pr.number,
body,
});
}
await github.rest.pulls.update({
owner,
repo,
pull_number: pr.number,
state: 'closed',
});
console.log('PR closed due to updateTime change.');