Skip to content

Commit

Permalink
fix: upgrade runner module to support upgrade octokit auth-app (#786)
Browse files Browse the repository at this point in the history
  • Loading branch information
npalm committed Jun 1, 2021
1 parent 3ac69a8 commit e110318
Show file tree
Hide file tree
Showing 3 changed files with 684 additions and 522 deletions.
17 changes: 14 additions & 3 deletions modules/runners/lambdas/runners/src/scale-runners/gh-auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Octokit } from '@octokit/rest';
import { request } from '@octokit/request';
import { createAppAuth } from '@octokit/auth-app';
import { Authentication, StrategyOptions } from '@octokit/auth-app/dist-types/types';
import {
Authentication,
StrategyOptions,
AppAuthentication,
InstallationAccessTokenAuthentication,
} from '@octokit/auth-app/dist-types/types';
import { OctokitOptions } from '@octokit/core/dist-types/types';
import { decrypt } from './kms';

Expand All @@ -20,7 +25,7 @@ export async function createGithubAuth(
installationId: number | undefined,
authType: 'app' | 'installation',
ghesApiUrl = '',
): Promise<Authentication> {
): Promise<AppAuthentication> {
const clientSecret = await decrypt(
process.env.GITHUB_APP_CLIENT_SECRET as string,
process.env.KMS_KEY_ID as string,
Expand Down Expand Up @@ -54,5 +59,11 @@ export async function createGithubAuth(
baseUrl: ghesApiUrl,
});
}
return await createAppAuth(authOptions)({ type: authType });
const result = (await createAppAuth(authOptions)({ type: authType })) as AppAuthentication;
return result;
// if (result.type == 'oauth-app') {
// return result;
// } else {
// throw Error(`Authentication type ${authType} is not`);
// }
}
17 changes: 9 additions & 8 deletions modules/runners/lambdas/runners/src/scale-runners/scale-up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export const scaleUp = async (eventSource: string, payload: ActionRequestMessage
).data.id;
}


const ghAuth = await createGithubAuth(installationId, 'installation', ghesApiUrl);

const githubInstallationClient = await createOctoClient(ghAuth.token, ghesApiUrl);
const checkRun = await githubInstallationClient.checks.get({
check_run_id: payload.id,
Expand All @@ -60,9 +60,10 @@ export const scaleUp = async (eventSource: string, payload: ActionRequestMessage
repoName: repoName,
});
console.info(
`${enableOrgLevel
? `Organization ${payload.repositoryOwner}`
: `Repo ${payload.repositoryOwner}/${payload.repositoryName}`
`${
enableOrgLevel
? `Organization ${payload.repositoryOwner}`
: `Repo ${payload.repositoryOwner}/${payload.repositoryName}`
} has ${currentRunners.length}/${maximumRunners} runners`,
);

Expand All @@ -71,9 +72,9 @@ export const scaleUp = async (eventSource: string, payload: ActionRequestMessage
const registrationToken = enableOrgLevel
? await githubInstallationClient.actions.createRegistrationTokenForOrg({ org: payload.repositoryOwner })
: await githubInstallationClient.actions.createRegistrationTokenForRepo({
owner: payload.repositoryOwner,
repo: payload.repositoryName,
});
owner: payload.repositoryOwner,
repo: payload.repositoryName,
});
const token = registrationToken.data.token;

const labelsArgument = runnerExtraLabels !== undefined ? `--labels ${runnerExtraLabels}` : '';
Expand All @@ -84,7 +85,7 @@ export const scaleUp = async (eventSource: string, payload: ActionRequestMessage
runnerConfig: enableOrgLevel
? `--url ${configBaseUrl}/${payload.repositoryOwner} --token ${token} ${labelsArgument}${runnerGroupArgument}`
: `--url ${configBaseUrl}/${payload.repositoryOwner}/${payload.repositoryName} ` +
`--token ${token} ${labelsArgument}`,
`--token ${token} ${labelsArgument}`,
orgName: orgName,
repoName: repoName,
});
Expand Down
Loading

0 comments on commit e110318

Please sign in to comment.