Skip to content

Commit e8f426f

Browse files
committed
Simplify logic
Signed-off-by: Brett Logan <[email protected]>
1 parent b8e9993 commit e8f426f

File tree

4 files changed

+213
-62
lines changed

4 files changed

+213
-62
lines changed

Dockerfile

-9
This file was deleted.

index.js

+53-48
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,30 @@
11
import express from 'express'
2-
import {Octokit} from '@octokit/core'
3-
import {App, createNodeMiddleware} from '@octokit/app'
4-
import {paginateRest} from '@octokit/plugin-paginate-rest'
5-
import {throttling} from '@octokit/plugin-throttling'
6-
import {retry} from '@octokit/plugin-retry'
2+
import {App, createNodeMiddleware, Octokit} from 'octokit'
73

84
const port = process.env.OSST_ACTIONS_BOT_PORT || process.env.PORT || 8080
95
const appID = process.env.OSST_ACTIONS_BOT_APP_ID
106
const appPrivateKey = Buffer.from(process.env.OSST_ACTIONS_BOT_APP_PRIVATE_KEY, 'base64').toString('utf-8')
117
const appSecret = process.env.OSST_ACTIONS_BOT_APP_WEBHOOK_SECRET
12-
const requiredChecks = [
13-
'policy-enforce-pr',
14-
'policy-enforce-pr-2'
15-
]
16-
const _Octokit = Octokit.plugin(paginateRest, retry, throttling).defaults({
17-
userAgent: 'oss-tooling-actions-bot/v1.0.0',
18-
throttle: {
19-
onRateLimit: (retryAfter, options) => {
20-
if (options.request.retryCount === 0) {
21-
console.log(`Request quota exhausted for request ${options.url}`)
22-
return true
23-
}
24-
},
25-
onSecondaryRateLimit: (retryAfter, options) => {
26-
console.log(`Abuse detected for request ${options.url}`)
27-
return true
28-
}
29-
}
30-
})
8+
319
const octokit = new App({
3210
appId: appID,
3311
privateKey: appPrivateKey,
34-
Octokit: _Octokit,
35-
oauth: {
36-
clientId: "",
37-
clientSecret: ""
38-
},
12+
Octokit: Octokit.defaults({
13+
userAgent: 'oss-tooling-actions-bot/v1.0.0',
14+
throttle: {
15+
onRateLimit: (retryAfter, options) => {
16+
if (options.request.retryCount === 0) {
17+
console.log(`Request quota exhausted for request ${options.url}`)
18+
return true
19+
}
20+
},
21+
onSecondaryRateLimit: (retryAfter, options) => {
22+
console.log(`Abuse detected for request ${options.url}`)
23+
return true
24+
}
25+
}
26+
}),
27+
oauth: {clientId: null, clientSecret: null},
3928
webhooks: {
4029
secret: appSecret
4130
}
@@ -45,6 +34,17 @@ const middleware = createNodeMiddleware(octokit)
4534
const app = express()
4635
app.use(middleware)
4736

37+
const retrieveRequiredChecks = async (properties) => {
38+
const requiredChecks = []
39+
for (const [_key, value] of Object.entries(properties)) {
40+
const key = _key.trim().toLowerCase()
41+
if (key.startsWith('osst_actions_bot')) {
42+
requiredChecks.push(value)
43+
}
44+
}
45+
return requiredChecks
46+
}
47+
4848
const fetchPull = async (octokit, owner, repo, number) => {
4949
const {data} = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', {
5050
owner: owner,
@@ -66,7 +66,7 @@ const fetchCheck = async (octokit, owner, repo, ref, check) => {
6666
return data.check_runs[0].check_suite.id
6767
}
6868

69-
const fetchWorkflowRun = async (octokit, owner, repo, suiteID, ref, sha, runID) => {
69+
const fetchWorkflowRun = async (octokit, owner, repo, suiteID, ref, sha) => {
7070
const {data} = await octokit.request('GET /repos/{owner}/{repo}/actions/runs', {
7171
owner: owner,
7272
repo: repo,
@@ -87,16 +87,16 @@ const rerunWorkflow = async (octokit, owner, repo, runID) => {
8787
})
8888
}
8989

90-
const processRerunRequiredWorkflows = async (octokit, body, owner, repo, number, actor, metadata) => {
90+
const processRerunRequiredWorkflows = async (octokit, metadata, owner, repo, number, checks) => {
9191
console.log(`[${metadata}] Retrieving PR information`)
9292
const pr = await fetchPull(octokit, owner, repo, number)
93-
for (const name of requiredChecks) {
93+
for (const name of checks) {
9494
try {
9595
console.log(`[${metadata}] Retrieving latest check suite for ${name}`)
9696
const suiteID = await fetchCheck(octokit, owner, repo, pr.head.ref, name)
9797

9898
console.log(`[${metadata}] Retrieving workflow runs for check suite ${suiteID}`)
99-
const runID = await fetchWorkflowRun(octokit, owner, repo, suiteID, pr.head.ref, pr.head.sha, pr.id)
99+
const runID = await fetchWorkflowRun(octokit, owner, repo, suiteID, pr.head.ref, pr.head.sha)
100100

101101
console.log(`[${metadata}] Rerunning workflow run ${runID}`)
102102
await rerunWorkflow(octokit, owner, repo, runID)
@@ -113,23 +113,28 @@ octokit.webhooks.on('issue_comment.created', async ({octokit, payload}) => {
113113
const repo = payload.repository.name
114114
const issueNumber = payload.issue.number
115115
const actor = payload.comment.user.login
116-
const metadata = `${actor}:${owner}:${repo}:${issueNumber}:${payload.comment.id}`
117-
if (payload.issue.pull_request) {
118-
console.log(`[${metadata}] Received command: '${body}' from ${actor}`)
119-
if (body.startsWith('/actions-bot')) {
120-
console.log(`[${metadata}] Processing command`)
121-
if (body.includes('rerun-required-workflows')) {
122-
console.log(`[${metadata}] Processing rerun-required-workflows`)
123-
await processRerunRequiredWorkflows(octokit, body, owner, repo, issueNumber, actor, metadata)
124-
} else {
125-
console.log(`[${metadata}] Unknown command`)
126-
}
127-
}
128-
} else {
129-
console.log(`[${metadata}] Issue is not a pull request`)
116+
const commentID = payload.comment.id
117+
const metadata = `${actor}:${owner}:${repo}:${issueNumber}:${commentID}`
118+
119+
if (!payload.issue.pull_request) {
120+
return console.log(`[${metadata}] Issue is not a pull request`)
121+
122+
}
123+
if (!body.startsWith('/actions-bot') || !body.includes('rerun-required-workflows')) {
124+
return console.log(`[${metadata}] Not a command: '${body}'`)
125+
}
126+
127+
console.log(`[${metadata}] Processing command '${body}'`)
128+
const properties = payload.repository.custom_properties
129+
console.log(`[${metadata}] Processing properties: ${JSON.stringify(properties)}`)
130+
const checks = await retrieveRequiredChecks(properties)
131+
if (checks.length === 0) {
132+
return console.log(`[${metadata}] No required checks found`)
130133
}
134+
console.log(`[${metadata}] Processing rerun-required-workflows`)
135+
await processRerunRequiredWorkflows(octokit, metadata, owner, repo, issueNumber, checks)
131136
} catch (e) {
132-
console.log(`Error: ${e.message}`)
137+
console.error(`Error: ${e.message}`)
133138
}
134139
})
135140

package-lock.json

+154-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "actions-bot",
3-
"version": "1.1.0",
3+
"version": "1.0.0",
44
"description": "",
55
"type": "module",
66
"main": "index.js",
@@ -23,7 +23,11 @@
2323
"@octokit/plugin-paginate-rest": "^10.0.0",
2424
"@octokit/plugin-retry": "^7.0.3",
2525
"@octokit/plugin-throttling": "^9.0.3",
26-
"express": "^4.18.3"
26+
"body-parser": "^1.20.2",
27+
"express": "^4.18.3",
28+
"express-rate-limit": "^7.2.0",
29+
"octokit": "^3.1.2",
30+
"raw-body": "^2.5.2"
2731
},
2832
"devDependencies": {
2933
"@vercel/ncc": "^0.38.1"

0 commit comments

Comments
 (0)