11import { createAppAuth } from '@octokit/auth-app' ;
22import { Octokit } from '@octokit/rest' ;
3+ import { AppAuth } from '@octokit/auth-app/dist-types/types' ;
4+ import yn from 'yn' ;
35
46export interface ActionRequestMessage {
57 id : number ;
@@ -9,40 +11,52 @@ export interface ActionRequestMessage {
911 installationId : number ;
1012}
1113
12- async function createGithubClient ( installationId : number ) : Promise < Octokit > {
13- const privateKey = process . env . GITHUB_APP_KEY as string ;
14+ function createGithubAppAuth ( installationId : number ) : AppAuth {
15+ const privateKey = Buffer . from ( process . env . GITHUB_APP_KEY_BASE64 as string , 'base64' ) . toString ( ) ;
1416 const appId : number = parseInt ( process . env . GITHUB_APP_ID as string ) ;
1517 const clientId = process . env . GITHUB_APP_CLIENT_ID as string ;
1618 const clientSecret = process . env . GITHUB_APP_CLIENT_SECRET as string ;
1719
18- try {
19- const auth = createAppAuth ( {
20- id : appId ,
21- privateKey : privateKey ,
22- installationId : installationId ,
23- clientId : clientId ,
24- clientSecret : clientSecret ,
25- } ) ;
26- const installationAuthentication = await auth ( { type : 'installation' } ) ;
20+ return createAppAuth ( {
21+ id : appId ,
22+ privateKey : privateKey ,
23+ installationId : installationId ,
24+ clientId : clientId ,
25+ clientSecret : clientSecret ,
26+ } ) ;
27+ }
2728
28- return new Octokit ( {
29- auth : installationAuthentication . token ,
30- } ) ;
31- } catch ( e ) {
32- Promise . reject ( e ) ;
33- }
29+ async function createInstallationClient ( githubAppAuth : AppAuth ) : Promise < Octokit > {
30+ const auth = await githubAppAuth ( { type : 'installation' } ) ;
31+ return new Octokit ( { auth : auth . token } ) ;
3432}
3533
3634export const handle = async ( eventSource : string , payload : ActionRequestMessage ) : Promise < void > => {
3735 if ( eventSource !== 'aws:sqs' ) throw Error ( 'Cannot handle non-SQS events!' ) ;
38- const githubClient = await createGithubClient ( payload . installationId ) ;
39- const queuedWorkflows = await githubClient . actions . listRepoWorkflowRuns ( {
36+ const enableOrgLevel = yn ( process . env . ENABLE_ORGANIZATION_RUNNERS ) ;
37+ const githubAppAuth = createGithubAppAuth ( payload . installationId ) ;
38+ const githubInstallationClient = await createInstallationClient ( githubAppAuth ) ;
39+ const queuedWorkflows = await githubInstallationClient . actions . listRepoWorkflowRuns ( {
4040 owner : payload . repositoryOwner ,
4141 repo : payload . repositoryName ,
4242 // @ts -ignore (typing is incorrect)
4343 status : 'queued' ,
4444 } ) ;
4545 console . info (
46- `Repo ${ payload . repositoryOwner } /${ payload . repositoryName } has ${ queuedWorkflows . total_count } queued workflow runs` ,
46+ `Repo ${ payload . repositoryOwner } /${ payload . repositoryName } has ${ queuedWorkflows . data . total_count } queued workflow runs` ,
4747 ) ;
48+
49+ if ( queuedWorkflows . data . total_count > 0 ) {
50+ // console.log(enableOrgLevel);
51+ // const currentRunners = enableOrgLevel
52+ // ? await githubInstallationClient.actions.listSelfHostedRunnersForOrg({
53+ // org: payload.repositoryOwner,
54+ // })
55+ // : await githubInstallationClient.actions.listSelfHostedRunnersForRepo({
56+ // owner: payload.repositoryOwner,
57+ // repo: payload.repositoryName,
58+ // });
59+ // // const currentOnlineRunners = currentRunners.data.runners.filter((r) => r.status === 'online');
60+ // // if (currentOnlineRunners.length > 0)
61+ }
4862} ;
0 commit comments