-
Notifications
You must be signed in to change notification settings - Fork 87
/
dapr_bot.js
203 lines (184 loc) · 5.54 KB
/
dapr_bot.js
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
197
198
199
200
201
202
203
// list of owner who can control dapr-bot workflow.
// TODO: Read owners from OWNERS file.
const owners = [
"yaron2",
"youngbupark",
"Haishi2016",
"lukekim",
"amanbha",
"msfussell",
"shalabhms",
"LMWF",
"artursouza",
"vinayada1",
"mukundansundar",
"wcs1only",
"orizohar",
"pruthvidhodda",
"mchmarny",
"tcnghia",
"berndverst",
"halspang",
"tanvigour",
"dmitsh",
"pkedy",
"CodeMonkeyLeet",
"XavierGeerinck",
"amulyavarote",
"shubham1172",
];
/**
* Execute fn if label exists on an issue.
* @param {*} github GitHub object reference
* @param {*} issue GitHub issue reference
* @param {*} label label name
* @param {*} fn async function
*/
async function executeIfIssueHasLabel(github, issue, label, fn) {
const response = await github.issues.listLabelsOnIssue({
issue_number: issue.number,
owner: issue.owner,
repo: issue.repo,
});
const labelNames = response.data.map((i) => i.name);
if (labelNames.indexOf(label) > -1) {
await fn();
}
}
/**
* Assign an issue to assignee.
* @param {*} github GitHub object reference
* @param {*} context GitHub action context
* @param {boolean} isPullRequest is the workflow triggered by a pull request?
*/
async function executeAssign(github, context, isPullRequest) {
const issue = context.issue;
if (isPullRequest) {
console.log("[executeAssign] pull requests unsupported, skipping command execution.");
return;
} else if (issue.assignees && issue.assignees.length !== 0) {
console.log("[executeAssign] issue already has assignees, skipping command execution.");
return;
}
await github.issues.addAssignees({
owner: issue.owner,
repo: issue.repo,
issue_number: issue.number,
assignees: [context.actor],
});
}
/**
* Add a label to the issue indicating that it needs the author's feedback.
* @param {*} github GitHub object reference
* @param {*} context GitHub action context
* @param {boolean} isPullRequest is the workflow triggered by a pull request?
* @returns
*/
async function executePingAuthor(github, context, isPullRequest) {
const issue = context.issue;
if (isPullRequest) {
console.log("[executePingAuthor] pull requests unsupported, skipping command execution.");
return;
} else if (owners.indexOf(context.actor) < 0) {
console.log("[executePingAuthor] user does not have privilege, skipping command execution.");
return;
}
// if there is a 'needs-team-attention' label, remove it.
await executeIfIssueHasLabel(github, issue, "needs-team-attention", async () => {
await github.issues.removeLabel({
issue_number: issue.number,
owner: issue.owner,
repo: issue.repo,
name: "needs-team-attention",
});
});
// Add new label
await github.issues.addLabels({
issue_number: issue.number,
owner: issue.owner,
repo: issue.repo,
labels: ["needs-author-feedback"],
});
}
/**
* Trigger e2e tests for pull request
* @param {*} github GitHub object reference
* @param {*} context GitHub action context
* @param {boolean} isPullRequest is the workflow triggered by a pull request?
*/
async function executeEndToEndTests(github, context, isPullRequest) {
const issue = context.issue;
if (!isPullRequest) {
console.log("[executeEndToEndTests] issues unsupported, skipping command execution.");
return;
} else if (owners.indexOf(context.actor) < 0) {
console.log("[executeEndToEndTests] user does not have privilege, skipping command execution.");
return;
}
// Get pull request
const pull = await github.pulls.get({
owner: issue.owner,
repo: issue.repo,
pull_number: issue.number,
});
if (pull && pull.data) {
// Get commit id and repo from pull head
const clientPayload = {
pull_head_ref: pull.data.head.sha,
pull_head_repo: pull.data.head.repo.full_name,
command: "ok-to-e2e-test",
issue: issue,
};
// Fire repository_dispatch event to trigger e2e test
await github.repos.createDispatchEvent({
owner: issue.owner,
repo: issue.repo,
event_type: "e2e-test",
client_payload: clientPayload,
});
console.log(`[executeEndToEndTests] triggered E2E tests for ${JSON.stringify(clientPayload)}.`);
}
}
module.exports = async ({ github, context }) => {
const payload = context.payload;
const issue = context.issue;
const isFromPulls = !!payload.issue.pull_request;
const commentBody = payload.comment.body;
if (!commentBody) {
console.log("[main] comment body not found, exiting.");
return;
}
// the author of this issue is interacting with it
if (!isFromPulls && context.actor == issue.owner) {
// if there is a 'needs-author-feedback' label,
// replace it with 'needs-team-attention' label.
await executeIfIssueHasLabel(github, issue, "needs-author-feedback", async () => {
await github.issues.removeLabel({
issue_number: issue.number,
owner: issue.owner,
repo: issue.repo,
name: "needs-author-feedback",
});
await github.issues.addLabels({
issue_number: issue.number,
owner: issue.owner,
repo: issue.repo,
labels: ["needs-team-attention"],
});
});
}
switch (commentBody) {
case "/assign":
executeAssign(github, context, isFromPulls);
break;
case "/ping-author":
executePingAuthor(github, context, isFromPulls);
break;
case "/ok-to-e2e-test":
executeEndToEndTests(github, context, isFromPulls);
break;
default:
console.log(`[main] command ${commentBody} not found, exiting.`);
break;
}
};