Repository navigation
136 lines (127 loc) · 5.62 KB
/
Copy pathwecom-digest-forward.yml
File metadata and controls
136 lines (127 loc) · 5.62 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
# Example: drop into a *public* m5stack repo as
# .github/workflows/wecom-digest-forward.yml
# Forwards issue/PR/comment/assign events to the private ops repo via
# repository_dispatch. Requires a secret on the *public* repo:
# WECOM_DIGEST_DISPATCH_PAT — PAT (or fine-grained token) with
# permission to create repository_dispatch on m5stack/wecom-github-digest.
#
# Never put webhook URLs or phones in this file.
name: Forward events to WeCom digest
on:
issues:
types: [opened, assigned]
pull_request_target:
# pull_request_target so fork PRs still forward metadata; we only read github.event
types: [opened, assigned, review_requested]
issue_comment:
types: [created]
pull_request_review_comment:
types: [created]
permissions:
contents: read
jobs:
forward:
runs-on: ubuntu-latest
steps:
- name: Require dispatch PAT
id: check
env:
DISPATCH_PAT: ${{ secrets.WECOM_DIGEST_DISPATCH_PAT }}
run: |
if [ -z "$DISPATCH_PAT" ]; then
echo "skip=true" >> "$GITHUB_OUTPUT"
echo "WECOM_DIGEST_DISPATCH_PAT not set; skipping forward"
else
echo "skip=false" >> "$GITHUB_OUTPUT"
fi
- name: Build client_payload + dispatch
if: steps.check.outputs.skip != 'true'
uses: actions/github-script@v7
env:
OPS_OWNER: m5stack
OPS_REPO: wecom-github-digest
with:
github-token: ${{ secrets.WECOM_DIGEST_DISPATCH_PAT }}
script: |
const ev = context.payload;
const name = context.eventName;
const action = context.payload.action;
let event_type = null;
let number, title, body, html_url, kind, assignees, assignee, comment_body, comment_url, comment_id;
const asLogins = (arr) => (arr || []).map(a => a && a.login).filter(Boolean);
const sender = (ev.sender && ev.sender.login) || '';
const sender_type = (ev.sender && ev.sender.type) || 'User';
const repo = context.repo.owner + '/' + context.repo.repo;
if (name === 'issues' || name === 'pull_request_target' || name === 'pull_request') {
const item = ev.issue || ev.pull_request;
number = item.number;
title = item.title || '';
body = (item.body || '').slice(0, 2000);
html_url = item.html_url || '';
kind = (ev.pull_request || String(name).startsWith('pull_request')) ? 'pull_request' : 'issue';
assignees = asLogins(item.assignees);
if (action === 'opened') {
event_type = 'opened';
} else if (action === 'assigned') {
event_type = 'assigned';
assignee = (ev.assignee && ev.assignee.login) || '';
} else if (action === 'review_requested') {
event_type = 'review_requested';
// Prefer user login; fall back to team slug. Reuse meta.assignee for ops reuse.
if (ev.requested_reviewer && ev.requested_reviewer.login) {
assignee = ev.requested_reviewer.login;
} else if (ev.requested_team && (ev.requested_team.slug || ev.requested_team.name)) {
assignee = ev.requested_team.slug || ev.requested_team.name;
} else {
assignee = '';
}
}
} else if (name === 'issue_comment' || name === 'pull_request_review_comment') {
event_type = 'comment';
const item = ev.issue || ev.pull_request;
const comment = ev.comment || {};
number = item ? item.number : 0;
title = item ? (item.title || '') : '';
html_url = comment.html_url || (item && item.html_url) || '';
kind = (item && item.pull_request) || name === 'pull_request_review_comment' ? 'pull_request' : 'issue';
assignees = asLogins(item && item.assignees);
comment_body = (comment.body || '').slice(0, 2000);
comment_url = comment.html_url || '';
comment_id = comment.id || '';
body = comment_body;
}
if (!event_type) {
core.info(`No forward mapping for ${name}.${action}`);
return;
}
// GitHub repository_dispatch client_payload allows ≤10 top-level keys.
// Pack optional fields into `meta` so we stay within the limit.
const client_payload = {
event_type,
repo,
number,
title: String(title || '').slice(0, 500),
body: String(body || '').slice(0, 2000),
html_url: html_url || '',
kind,
sender,
sender_type,
meta: JSON.stringify({
assignees: assignees || [],
assignee: assignee || '',
reviewer: event_type === 'review_requested' ? (assignee || '') : '',
comment_url: comment_url || '',
comment_id: comment_id || '',
}),
};
if (Object.keys(client_payload).length > 10) {
core.setFailed(`client_payload has ${Object.keys(client_payload).length} keys (max 10)`);
return;
}
await github.rest.repos.createDispatchEvent({
owner: process.env.OPS_OWNER,
repo: process.env.OPS_REPO,
event_type: 'wecom-digest',
client_payload,
});
core.info(`Dispatched wecom-digest ${event_type} for ${repo}#${number}`);