|
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 }) => { |
2 | 25 | // list of owner who can control dapr-bot workflow |
3 | 26 | // TODO: Read owners from OWNERS file. |
4 | 27 | const owners = [ |
@@ -49,52 +72,27 @@ export default async ({github, context}) => { |
49 | 72 | if (!isFromPulls && context.actor == issue.owner) { |
50 | 73 | // if there is a 'needs-author-feedback' label, |
51 | 74 | // 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 | + }) |
73 | 89 | } |
74 | 90 |
|
75 | 91 | // actions above this check are enabled for everyone. |
76 | 92 | if (owners.indexOf(context.actor) < 0) { |
77 | 93 | return; |
78 | 94 | } |
79 | 95 |
|
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 | | - |
98 | 96 | if (!isFromPulls && commentBody) { |
99 | 97 | if (commentBody.indexOf("/ok-to-e2e-test") == 0) { |
100 | 98 | // Get pull request |
@@ -123,13 +121,15 @@ export default async ({github, context}) => { |
123 | 121 | console.log(`Trigger E2E test for ${JSON.stringify(testPayload)}`); |
124 | 122 | } |
125 | 123 | } 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 | + }) |
133 | 133 | // Add new label |
134 | 134 | await github.issues.addLabels({ |
135 | 135 | issue_number: issue.number, |
|
0 commit comments