1
1
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'
7
3
8
4
const port = process . env . OSST_ACTIONS_BOT_PORT || process . env . PORT || 8080
9
5
const appID = process . env . OSST_ACTIONS_BOT_APP_ID
10
6
const appPrivateKey = Buffer . from ( process . env . OSST_ACTIONS_BOT_APP_PRIVATE_KEY , 'base64' ) . toString ( 'utf-8' )
11
7
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
+
31
9
const octokit = new App ( {
32
10
appId : appID ,
33
11
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 } ,
39
28
webhooks : {
40
29
secret : appSecret
41
30
}
@@ -45,6 +34,17 @@ const middleware = createNodeMiddleware(octokit)
45
34
const app = express ( )
46
35
app . use ( middleware )
47
36
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
+
48
48
const fetchPull = async ( octokit , owner , repo , number ) => {
49
49
const { data} = await octokit . request ( 'GET /repos/{owner}/{repo}/pulls/{pull_number}' , {
50
50
owner : owner ,
@@ -66,7 +66,7 @@ const fetchCheck = async (octokit, owner, repo, ref, check) => {
66
66
return data . check_runs [ 0 ] . check_suite . id
67
67
}
68
68
69
- const fetchWorkflowRun = async ( octokit , owner , repo , suiteID , ref , sha , runID ) => {
69
+ const fetchWorkflowRun = async ( octokit , owner , repo , suiteID , ref , sha ) => {
70
70
const { data} = await octokit . request ( 'GET /repos/{owner}/{repo}/actions/runs' , {
71
71
owner : owner ,
72
72
repo : repo ,
@@ -87,16 +87,16 @@ const rerunWorkflow = async (octokit, owner, repo, runID) => {
87
87
} )
88
88
}
89
89
90
- const processRerunRequiredWorkflows = async ( octokit , body , owner , repo , number , actor , metadata ) => {
90
+ const processRerunRequiredWorkflows = async ( octokit , metadata , owner , repo , number , checks ) => {
91
91
console . log ( `[${ metadata } ] Retrieving PR information` )
92
92
const pr = await fetchPull ( octokit , owner , repo , number )
93
- for ( const name of requiredChecks ) {
93
+ for ( const name of checks ) {
94
94
try {
95
95
console . log ( `[${ metadata } ] Retrieving latest check suite for ${ name } ` )
96
96
const suiteID = await fetchCheck ( octokit , owner , repo , pr . head . ref , name )
97
97
98
98
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 )
100
100
101
101
console . log ( `[${ metadata } ] Rerunning workflow run ${ runID } ` )
102
102
await rerunWorkflow ( octokit , owner , repo , runID )
@@ -113,23 +113,28 @@ octokit.webhooks.on('issue_comment.created', async ({octokit, payload}) => {
113
113
const repo = payload . repository . name
114
114
const issueNumber = payload . issue . number
115
115
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` )
130
133
}
134
+ console . log ( `[${ metadata } ] Processing rerun-required-workflows` )
135
+ await processRerunRequiredWorkflows ( octokit , metadata , owner , repo , issueNumber , checks )
131
136
} catch ( e ) {
132
- console . log ( `Error: ${ e . message } ` )
137
+ console . error ( `Error: ${ e . message } ` )
133
138
}
134
139
} )
135
140
0 commit comments