-
Notifications
You must be signed in to change notification settings - Fork 947
196 lines (180 loc) · 7.83 KB
/
gh_pr_review_notification.yml
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
name: '[gh] pull review notification'
on:
pull_request_target:
types: [ review_requested ]
workflow_run:
workflows:
- \[gh\] pull request review
types: [ completed ]
workflow_dispatch:
schedule:
# Run every day at 23:30 UTC(07:30 UTC+08:00).
- cron: '30 23 * * *'
jobs:
request_notification:
if: github.repository == 'Tencent/Hippy' && github.event.action == 'review_requested' && github.event.requested_reviewer.type == 'User'
runs-on: ubuntu-latest
steps:
- name: Notice
uses: actions/[email protected]
env:
MESSAGE: |
[${{ github.event.sender.login }}](https://github.com/${{ github.event.sender.login }}) requested your review on [#${{ github.event.pull_request.number }}](${{ github.event.pull_request.html_url }}) pull request.
> ${{ github.event.pull_request.title }}
> [${{ github.event.pull_request.html_url }}](${{ github.event.pull_request.html_url }})
> [%s changed files](${{ github.event.pull_request.html_url }}/files) with <font color="info">%s additions</font> and <font color="warning">%s deletions</font>
<@%s>
WECHAT_WORK_USERS: ${{ secrets.WECHAT_WORK_USERS }}
with:
script: |
const { format } = require("util");
const userid = JSON.parse(process.env.WECHAT_WORK_USERS)["${{ github.event.requested_reviewer.login }}"];
if (!userid) {
console.log("The reviewer ${{ github.event.requested_reviewer.login }} not found in secrets.WECHAT_WORK_USERS");
return;
}
await github.request("POST ${{ secrets.WECHAT_WORK_BOT_WEBHOOK }}", {
headers: {
"content-type": "application/json"
},
data: {
chatid: "${{ secrets.WECHAT_WORK_CHAT_ID }}",
visible_to_user: userid,
msgtype: "markdown",
markdown: {
content: format(process.env.MESSAGE, (${{ github.event.pull_request.changed_files }}).toLocaleString(), (${{ github.event.pull_request.additions }}).toLocaleString(), (${{ github.event.pull_request.deletions }}).toLocaleString(), userid),
attachments: [{
callback_id: "review",
actions: [{
name: "review_btn",
text: "Mark as Reviewed",
type: "button",
value: "Mark as Reviewed",
replace_text: "Already reviewed",
border_color: "2c974b",
text_color: "2c974b"
}, {
name: "ignore_btn",
text: "Mark as Ignored",
type: "button",
value: "Mark as Ignored",
replace_text: "Already ignored",
border_color: "6e7781",
text_color: "6e7781"
}]
}]
}
}
});
call_get_workflow_output:
if: github.repository == 'Tencent/Hippy' && github.event.action == 'completed' && github.event.workflow_run.conclusion == 'success'
uses: ./.github/workflows/reuse_get_workflow_output.yml
with:
workflow_run: ${{ github.event.workflow_run.id }}
changes_requested_comment:
needs: call_get_workflow_output
if: needs.call_get_workflow_output.outputs.action == 'submitted'
runs-on: ubuntu-latest
env:
MESSAGE: |
Hi, @{0}, I noticed that our reviewers requested changes to this pull request.
When you're done, **click the `Re-request review` button in the right sidebar(shown below)** to notify the reviewer.
![Re-request review button in the right sidebar](https://docs.github.com/assets/cb-4714/images/help/pull_requests/request-re-review.png)
steps:
- name: Parse
id: parse_workflow_output
env:
RAW: ${{ needs.call_get_workflow_output.outputs.raw }}
shell: python
run: |
import json
import os
raw = os.getenv("RAW")
data = json.loads(raw)
with open(os.getenv("GITHUB_OUTPUT"), 'w', encoding='utf-8') as file:
file.write("review_state=%s\n" % data["review"]["state"])
file.write("pull_request_number=%s\n" % data["pull_request"]["number"])
file.write("pull_request_user=%s\n" % data["pull_request"]["user"]["login"])
- name: Find
id: find_comment
if: steps.parse_workflow_output.outputs.review_state == 'changes_requested'
uses: peter-evans/[email protected]
with:
issue-number: ${{ steps.parse_workflow_output.outputs.pull_request_number }}
body-includes: ${{ format(env.MESSAGE, steps.parse_workflow_output.outputs.pull_request_user) }}
- name: Token
id: get-token
if: steps.find_comment.outputs.comment-id == '' && steps.parse_workflow_output.outputs.review_state == 'changes_requested'
uses: navikt/github-app-token-generator@v1
with:
private-key: ${{ secrets.BOT_APP_KEY }}
app-id: ${{ secrets.BOT_APP_ID }}
- name: Comment
uses: peter-evans/create-or-update-comment@v2
if: steps.get-token.outputs.token != ''
with:
token: ${{ steps.get-token.outputs.token }}
issue-number: ${{ steps.parse_workflow_output.outputs.pull_request_number }}
body: ${{ format(env.MESSAGE, steps.parse_workflow_output.outputs.pull_request_user) }}
reactions: eyes
requested_reviewers_notification:
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
steps:
- name: Notice
uses: actions/[email protected]
env:
WECHAT_WORK_USERS: ${{ secrets.WECHAT_WORK_USERS }}
with:
script: |
const { pulls } = github.rest;
const os = require('os');
const pull_requests = (await github.paginate(pulls.list, {
per_page: 100,
state: 'open',
...context.repo
})).filter(pull => pull.draft === false);
const requested_reviewers = await Promise.all(
pull_requests.map(
pull => pulls.listRequestedReviewers({
per_page: 100,
pull_number: pull.number,
...context.repo
}).then(({ data }) => data)
)
);
if (pull_requests.length !== requested_reviewers.length) {
throw new Error("The length of pull_requests and requested_reviewers is not equal");
}
const wechat_work_users = JSON.parse(process.env.WECHAT_WORK_USERS);
const notification_users = {};
pull_requests.forEach((pull, index) => {
requested_reviewers[index].users.forEach(reviewer => {
if (reviewer.type === 'User') {
const user_id = wechat_work_users[reviewer.login];
if (user_id) {
(notification_users[user_id] ||= []).push(pull);
}
}
});
});
await Promise.all(Object.entries(notification_users).map(([user_id, pulls]) => {
const content = [];
content.push(`<font color="warning">${pulls.length}</font> pull requests are waiting on your review.`);
content.push(...pulls.map(pull => `> [#${pull.number}](${pull.html_url}) ${pull.title}`));
content.push('');
content.push(`<@${user_id}>`);
return github.request("POST ${{ secrets.WECHAT_WORK_BOT_WEBHOOK }}", {
headers: {
"content-type": "application/json"
},
data: {
chatid: "${{ secrets.WECHAT_WORK_CHAT_ID }}",
visible_to_user: user_id,
msgtype: "markdown",
markdown: {
content: content.join(os.EOL)
}
}
});
}));