Skip to content

Commit 7541ca2

Browse files
committed
Refactor based on reviews
Signed-off-by: Shubham Sharma <[email protected]>
1 parent f97ce77 commit 7541ca2

File tree

1 file changed

+162
-107
lines changed

1 file changed

+162
-107
lines changed

.github/scripts/dapr_bot.js

Lines changed: 162 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,187 @@
1+
// list of owner who can control dapr-bot workflow.
2+
// TODO: Read owners from OWNERS file.
3+
const owners = [
4+
"yaron2",
5+
"youngbupark",
6+
"Haishi2016",
7+
"lukekim",
8+
"amanbha",
9+
"msfussell",
10+
"shalabhms",
11+
"LMWF",
12+
"artursouza",
13+
"vinayada1",
14+
"mukundansundar",
15+
"wcs1only",
16+
"orizohar",
17+
"pruthvidhodda",
18+
"mchmarny",
19+
"tcnghia",
20+
"berndverst",
21+
"halspang",
22+
"tanvigour",
23+
"dmitsh",
24+
"pkedy",
25+
"CodeMonkeyLeet",
26+
"XavierGeerinck",
27+
"amulyavarote",
28+
"shubham1172"
29+
];
30+
131
/**
232
* Execute fn if label exists on an issue.
333
* @param {*} github GitHub object reference
4-
* @param {*} label label name
534
* @param {*} issue GitHub issue reference
35+
* @param {*} label label name
636
* @param {*} fn async function
737
*/
838
async function executeIfIssueHasLabel(github, issue, label, fn) {
9-
var response = await github.issues.listLabelsOnIssue({
39+
const response = await github.issues.listLabelsOnIssue({
1040
issue_number: issue.number,
1141
owner: issue.owner,
1242
repo: issue.repo,
1343
});
1444

15-
var labelNames = response.data.map((i) => i.name)
16-
for (const labelName of labelNames) {
17-
if (labelName == label) {
18-
await fn()
19-
}
45+
const labelNames = response.data.map((i) => i.name);
46+
if (labelNames.indexOf(label) > -1) {
47+
await fn();
48+
}
49+
}
50+
51+
/**
52+
* Assign an issue to assignee.
53+
* @param {*} github GitHub object reference
54+
* @param {*} context GitHub action context
55+
* @param {boolean} isPullRequest is the workflow triggered by a pull request?
56+
*/
57+
async function executeAssign(github, context, isPullRequest) {
58+
const issue = context.issue;
59+
60+
if (isPullRequest) {
61+
console.log("[executeAssign] pull requests unsupported, skipping command execution.");
62+
return;
63+
} else if (issue.assignees && issue.assignees.length !== 0) {
64+
console.log("[executeAssign] issue already has assignees, skipping command execution.");
65+
return;
2066
}
67+
68+
await github.issues.addAssignees({
69+
owner: issue.owner,
70+
repo: issue.repo,
71+
issue_number: issue.number,
72+
assignees: [context.actor],
73+
});
2174
}
2275

76+
/**
77+
* Add a label to the issue indicating that it needs the author's feedback.
78+
* @param {*} github GitHub object reference
79+
* @param {*} context GitHub action context
80+
* @param {boolean} isPullRequest is the workflow triggered by a pull request?
81+
* @returns
82+
*/
83+
async function executePingAuthor(github, context, isPullRequest) {
84+
const issue = context.issue;
85+
86+
if (isPullRequest) {
87+
console.log("[executePingAuthor] pull requests unsupported, skipping command execution.");
88+
return;
89+
} else if (owners.indexOf(context.actor) < 0) {
90+
console.log("[executePingAuthor] user does not have privilege, skipping command execution.");
91+
return;
92+
}
93+
94+
// if there is a 'needs-team-attention' label, remove it.
95+
await executeIfIssueHasLabel(github, issue, 'needs-team-attention', async () => {
96+
await github.issues.removeLabel({
97+
issue_number: issue.number,
98+
owner: issue.owner,
99+
repo: issue.repo,
100+
name: 'needs-team-attention'
101+
});
102+
});
23103

24-
module.exports = async ({ github, context }) => {
25-
// list of owner who can control dapr-bot workflow
26-
// TODO: Read owners from OWNERS file.
27-
const owners = [
28-
"yaron2",
29-
"youngbupark",
30-
"Haishi2016",
31-
"lukekim",
32-
"amanbha",
33-
"msfussell",
34-
"shalabhms",
35-
"LMWF",
36-
"artursouza",
37-
"vinayada1",
38-
"mukundansundar",
39-
"wcs1only",
40-
"orizohar",
41-
"pruthvidhodda",
42-
"mchmarny",
43-
"tcnghia",
44-
"berndverst",
45-
"halspang",
46-
"tanvigour",
47-
"dmitsh",
48-
"pkedy",
49-
"CodeMonkeyLeet",
50-
"XavierGeerinck",
51-
"amulyavarote",
52-
"shubham1172"
53-
];
104+
// Add new label
105+
await github.issues.addLabels({
106+
issue_number: issue.number,
107+
owner: issue.owner,
108+
repo: issue.repo,
109+
labels: ['needs-author-feedback']
110+
});
111+
}
54112

113+
/**
114+
* Trigger e2e tests for pull request
115+
* @param {*} github GitHub object reference
116+
* @param {*} context GitHub action context
117+
* @param {boolean} isPullRequest is the workflow triggered by a pull request?
118+
*/
119+
async function executeEndToEndTests(github, context, isPullRequest) {
120+
const issue = context.issue;
121+
122+
if (!isPullRequest) {
123+
console.log("[executeEndToEndTests] issues unsupported, skipping command execution.");
124+
return;
125+
} else if (owners.indexOf(context.actor) < 0) {
126+
console.log("[executeEndToEndTests] user does not have privilege, skipping command execution.");
127+
return;
128+
}
129+
130+
// Get pull request
131+
const pull = await github.pulls.get({
132+
owner: issue.owner,
133+
repo: issue.repo,
134+
pull_number: issue.number
135+
});
136+
137+
if (pull && pull.data) {
138+
// Get commit id and repo from pull head
139+
const clientPayload = {
140+
pull_head_ref: pull.data.head.sha,
141+
pull_head_repo: pull.data.head.repo.full_name,
142+
command: "ok-to-e2e-test",
143+
issue: issue,
144+
};
145+
146+
// Fire repository_dispatch event to trigger e2e test
147+
await github.repos.createDispatchEvent({
148+
owner: issue.owner,
149+
repo: issue.repo,
150+
event_type: "e2e-test",
151+
client_payload: clientPayload,
152+
});
153+
154+
console.log(`[executeEndToEndTests] triggered E2E tests for ${JSON.stringify(clientPayload)}.`);
155+
}
156+
}
157+
158+
159+
export default async ({ github, context }) => {
55160
const payload = context.payload;
56161
const issue = context.issue;
57162
const isFromPulls = !!payload.issue.pull_request;
58163
const commentBody = payload.comment.body;
59-
if (!isFromPulls && commentBody && commentBody.indexOf("/assign") == 0) {
60-
if (!issue.assignees || issue.assignees.length === 0) {
61-
await github.issues.addAssignees({
62-
owner: issue.owner,
63-
repo: issue.repo,
64-
issue_number: issue.number,
65-
assignees: [context.actor],
66-
})
67-
}
164+
165+
if (!commentBody) {
166+
console.log("[main] comment body not found, exiting.");
68167
return;
69168
}
70169

170+
switch (commentBody) {
171+
case "/assign":
172+
executeAssign(github, context, isFromPulls);
173+
break;
174+
case "/ping-author":
175+
executePingAuthor(github, context, isFromPulls);
176+
break;
177+
case "/ok-to-e2e-test":
178+
executeEndToEndTests(github, context, isFromPulls);
179+
break;
180+
default:
181+
console.log(`[main] command ${commentBody} not found, exiting.`);
182+
break;
183+
}
184+
71185
// the author of this issue is interacting with it
72186
if (!isFromPulls && context.actor == issue.owner) {
73187
// if there is a 'needs-author-feedback' label,
@@ -84,66 +198,7 @@ module.exports = async ({ github, context }) => {
84198
owner: issue.owner,
85199
repo: issue.repo,
86200
labels: ['needs-team-attention']
87-
})
88-
})
89-
}
90-
91-
// actions above this check are enabled for everyone.
92-
if (owners.indexOf(context.actor) < 0) {
93-
return;
94-
}
95-
96-
// commands for GH issues
97-
if (!isFromPulls && commentBody) {
98-
if (commentBody.indexOf("/ping-author") == 0) {
99-
// if there is a 'needs-team-attention' label, remove it.
100-
await executeIfIssueHasLabel(github, issue, 'needs-team-attention', async () => {
101-
await github.issues.removeLabel({
102-
issue_number: issue.number,
103-
owner: issue.owner,
104-
repo: issue.repo,
105-
name: 'needs-team-attention'
106-
});
107-
})
108-
// Add new label
109-
await github.issues.addLabels({
110-
issue_number: issue.number,
111-
owner: issue.owner,
112-
repo: issue.repo,
113-
labels: ['needs-author-feedback']
114-
})
115-
return;
116-
}
117-
}
118-
119-
// commands for GH pull requests
120-
if (isFromPulls && commentBody) {
121-
if (commentBody.indexOf("/ok-to-e2e-test") == 0) {
122-
// Get pull request
123-
const pull = await github.pulls.get({
124-
owner: issue.owner,
125-
repo: issue.repo,
126-
pull_number: issue.number
127201
});
128-
if (pull && pull.data) {
129-
// Get commit id and repo from pull head
130-
const clientPayload = {
131-
pull_head_ref: pull.data.head.sha,
132-
pull_head_repo: pull.data.head.repo.full_name,
133-
command: "ok-to-e2e-test",
134-
issue: issue,
135-
};
136-
137-
// Fire repository_dispatch event to trigger e2e test
138-
await github.repos.createDispatchEvent({
139-
owner: issue.owner,
140-
repo: issue.repo,
141-
event_type: "e2e-test",
142-
client_payload: clientPayload,
143-
});
144-
145-
console.log(`Trigger E2E test for ${JSON.stringify(clientPayload)}`);
146-
}
147-
}
202+
});
148203
}
149-
}
204+
};

0 commit comments

Comments
 (0)