Skip to content

Commit 461a722

Browse files
committed
Fix the workflow and refactor
Signed-off-by: Shubham Sharma <[email protected]>
1 parent c917646 commit 461a722

File tree

2 files changed

+50
-49
lines changed

2 files changed

+50
-49
lines changed

.github/scripts/dapr_bot.js

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
1-
export default async ({github, context}) => {
1+
/**
2+
* Execute fn if label exists on an issue.
3+
* @param {*} github GitHub object reference
4+
* @param {*} label label name
5+
* @param {*} issue GitHub issue reference
6+
* @param {*} fn async function
7+
*/
8+
async function executeIfIssueHasLabel(github, issue, label, fn) {
9+
var response = await github.issues.listLabelsOnIssue({
10+
issue_number: issue.number,
11+
owner: issue.owner,
12+
repo: issue.repo,
13+
});
14+
15+
var labelNames = response.data.map((i) => i.name)
16+
for (const labelName of labelNames) {
17+
if (labelName == label) {
18+
await fn()
19+
}
20+
}
21+
}
22+
23+
24+
module.exports = async ({ github, context }) => {
225
// list of owner who can control dapr-bot workflow
326
// TODO: Read owners from OWNERS file.
427
const owners = [
@@ -49,52 +72,27 @@ export default async ({github, context}) => {
4972
if (!isFromPulls && context.actor == issue.owner) {
5073
// if there is a 'needs-author-feedback' label,
5174
// replace it with 'needs-team-attention' label.
52-
var labels = await github.issues.listLabelsOnIssue({
53-
issue_number: issue.number,
54-
owner: issue.owner,
55-
repo: issue.repo,
56-
});
57-
labels.forEach(label => {
58-
if (label.name == 'needs-author-feedback') {
59-
await github.issues.removeLabel({
60-
issue_number: issue.number,
61-
owner: issue.owner,
62-
repo: issue.repo,
63-
name: 'needs-author-feedback'
64-
});
65-
await github.issues.addLabels({
66-
issue_number: issue.number,
67-
owner: issue.owner,
68-
repo: issue.repo,
69-
labels: ['needs-team-attention']
70-
})
71-
}
72-
});
75+
await executeIfIssueHasLabel(github, issue, 'needs-author-feedback', async () => {
76+
await github.issues.removeLabel({
77+
issue_number: issue.number,
78+
owner: issue.owner,
79+
repo: issue.repo,
80+
name: 'needs-author-feedback'
81+
});
82+
await github.issues.addLabels({
83+
issue_number: issue.number,
84+
owner: issue.owner,
85+
repo: issue.repo,
86+
labels: ['needs-team-attention']
87+
})
88+
})
7389
}
7490

7591
// actions above this check are enabled for everyone.
7692
if (owners.indexOf(context.actor) < 0) {
7793
return;
7894
}
7995

80-
// Pollyfill: register createDispatchEvent because actions/[email protected]
81-
// does not have createDispatchEvent.
82-
github.registerEndpoints({
83-
repos: {
84-
createDispatchEvent: {
85-
"headers": { "accept": "application/vnd.github.everest-preview+json" },
86-
"method": "POST",
87-
"params": {
88-
"client_payload": { "type": "object" },
89-
"event_type": { "type": "string" },
90-
"owner": { "required": true, "type": "string" },
91-
"repo": { "required": true, "type": "string" }
92-
},
93-
"url": "/repos/:owner/:repo/dispatches"
94-
}
95-
}
96-
});
97-
9896
if (!isFromPulls && commentBody) {
9997
if (commentBody.indexOf("/ok-to-e2e-test") == 0) {
10098
// Get pull request
@@ -123,13 +121,15 @@ export default async ({github, context}) => {
123121
console.log(`Trigger E2E test for ${JSON.stringify(testPayload)}`);
124122
}
125123
} else if (commentBody.indexOf("/ping-author") == 0) {
126-
// Delete the label if exists
127-
await github.issues.removeLabel({
128-
issue_number: issue.number,
129-
owner: issue.owner,
130-
repo: issue.repo,
131-
name: 'needs-team-attention'
132-
});
124+
// if there is a 'needs-team-attention' label, remove it.
125+
await executeIfIssueHasLabel(github, issue, 'needs-team-attention', async () => {
126+
await github.issues.removeLabel({
127+
issue_number: issue.number,
128+
owner: issue.owner,
129+
repo: issue.repo,
130+
name: 'needs-team-attention'
131+
});
132+
})
133133
// Add new label
134134
await github.issues.addLabels({
135135
issue_number: issue.number,

.github/workflows/dapr-bot.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ jobs:
2222
name: bot-processor
2323
runs-on: ubuntu-latest
2424
steps:
25+
- uses: actions/checkout@v2 # to make the JS script available for the next step
2526
- name: Comment analyzer
26-
uses: actions/github-script@v1
27+
uses: actions/github-script@v4
2728
with:
2829
github-token: ${{secrets.DAPR_BOT_TOKEN}}
2930
script: |
30-
const script = require('${process.env.GITHUB_WORKSPACE}/.github/scripts/dapr_bot.js')
31+
const script = require('./.github/scripts/dapr_bot.js')
3132
await script({github, context})

0 commit comments

Comments
 (0)