-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtransform-message.ts
More file actions
196 lines (175 loc) · 5.84 KB
/
transform-message.ts
File metadata and controls
196 lines (175 loc) · 5.84 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { components } from 'npm:@octokit/openapi-types';
import { stdin } from 'npm:zx';
type GitHubSchema = components['schemas'];
type GitHubUser = GitHubSchema['simple-user'];
interface GitHubAction
extends Record<'event_name' | 'actor' | 'server_url' | 'repository', string> {
action?: string;
ref?: string;
ref_name?: string;
event: {
head_commit?: GitHubSchema['git-commit'];
issue?: GitHubSchema['webhook-issues-opened']['issue'];
pull_request?: GitHubSchema['pull-request'];
discussion?: GitHubSchema['discussion'];
comment?: GitHubSchema['issue-comment'];
release?: GitHubSchema['release'];
};
}
// Helper functions
const getActionText = (action?: string) =>
action === 'closed' ? '关闭' : action?.includes('open') ? '打开' : '编辑';
const createLink = (href: string, text = href) => ({ tag: 'a', href, text });
const createText = (text: string) => ({ tag: 'text', text });
// create user link
const createUserLink = (user: GitHubUser) =>
user ? createLink(user.html_url, user.login) : createText('无');
const createContentItem = (
label: string,
value?: string | { tag: string; text: string },
) =>
[
createText(label),
typeof value === 'string'
? createText(value || '无')
: value || createText('无'),
] as [object, object];
type EventHandler = (
event: GitHubAction,
actionText: string,
) => {
title: string;
content: [object, object][];
};
// Event handlers
const eventHandlers: Record<string, EventHandler> = {
push: ({
event: { head_commit },
ref,
ref_name,
server_url,
repository,
actor,
}) => ({
title: 'GitHub 代码提交',
content: [
[createText('提交链接:'), createLink(head_commit!.url)],
[
createText('代码分支:'),
createLink(`${server_url}/${repository}/tree/${ref_name}`, ref),
],
[createText('提交作者:'), createLink(`${server_url}/${actor}`, actor)],
[createText('提交信息:'), createText(head_commit!.message)],
],
}),
issues: ({ event: { issue } }, actionText) => ({
title: `GitHub issue ${actionText}:${issue?.title}`,
content: [
[createText('链接:'), createLink(issue!.html_url)],
[
createText('作者:'),
createLink(issue!.user!.html_url!, issue!.user!.login),
],
[
createText('指派:'),
issue?.assignee
? createLink(issue.assignee.html_url!, issue.assignee.login)
: createText('无'),
],
[
createText('标签:'),
createText(issue?.labels?.map(({ name }) => name).join(', ') || '无'),
],
[createText('里程碑:'), createText(issue?.milestone?.title || '无')],
[createText('描述:'), createText(issue?.body || '无')],
],
}),
pull_request: ({ event: { pull_request } }, actionText) => ({
title: `GitHub PR ${actionText}:${pull_request?.title}`,
content: [
[createText('链接:'), createLink(pull_request!.html_url)],
[
createText('作者:'),
createLink(pull_request!.user.html_url, pull_request!.user.login),
],
[
createText('指派:'),
pull_request?.assignee
? createLink(
pull_request.assignee.html_url,
pull_request.assignee.login,
)
: createText('无'),
],
[
createText('标签:'),
createText(
pull_request?.labels?.map(({ name }) => name).join(', ') || '无',
),
],
[
createText('里程碑:'),
createText(pull_request?.milestone?.title || '无'),
],
[createText('描述:'), createText(pull_request?.body || '无')],
],
}),
discussion: ({ event: { discussion } }, actionText) => ({
title: `GitHub 讨论 ${actionText}:${discussion?.title || '无'}`,
content: [
createContentItem('链接:', discussion?.html_url),
createContentItem(
'作者:',
createUserLink(discussion!.user as GitHubUser),
),
createContentItem('描述:', discussion?.body || '无'),
],
}),
issue_comment: ({ event: { comment, issue } }) => ({
title: `GitHub issue 评论:${issue?.title || '未知 issue'}`,
content: [
createContentItem('链接:', comment?.html_url),
createContentItem('作者:', createUserLink(comment!.user!)),
createContentItem('描述:', comment?.body || '无'),
],
}),
discussion_comment: ({ event: { comment, discussion } }) => ({
title: `GitHub 讨论评论:${discussion?.title || '无'}`,
content: [
createContentItem('链接:', comment?.html_url),
createContentItem('作者:', createUserLink(comment!.user!)),
createContentItem('描述:', comment?.body || '无'),
],
}),
release: ({ event: { release } }) => ({
title: `GitHub Release 发布:${release!.name || release!.tag_name}`,
content: [
createContentItem('链接:', release!.html_url),
createContentItem('作者:', createUserLink(release!.author)),
createContentItem('描述:', release!.body!),
],
}),
};
// Main processor
const processEvent = (event: GitHubAction) => {
const { event_name, action } = event;
const actionText = getActionText(action);
const handler = eventHandlers[event_name];
if (!handler) throw new Error(`No handler found for event: ${event_name}`);
try {
return handler(event, actionText);
} catch (cause) {
throw new Error(
`Error processing ${event_name} event: ${(cause as Error).message}`,
{ cause },
);
}
};
// Main execution:Processing GitHub Events and Outputting Results
const event = JSON.parse((await stdin()) || '{}') as GitHubAction;
const zh_cn = processEvent(event);
if (zh_cn) console.log(JSON.stringify({ post: { zh_cn } }));
else
throw new Error(
`Unsupported ${event.event_name} event & ${event.action} action`,
);