Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: upgrade octokit app auth for runners module #786

Merged
merged 1 commit into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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