Skip to content

Commit

Permalink
feat: add GHE support
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshallOfSound committed Jul 11, 2023
1 parent 9ef0697 commit 509af25
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createAppAuth, InstallationAuthOptions } from '@octokit/auth-app';
import { RequestInterface } from '@octokit/auth-app/dist-types/types';
import { Octokit } from '@octokit/rest';

export interface RepoInfo {
Expand Down Expand Up @@ -57,6 +58,7 @@ export async function getAuthOptionsForRepo(
repo: RepoInfo,
appCreds: AppCredentials,
authNarrowing: AuthNarrowing = {},
request?: RequestInterface,
) {
return await getAuthOptionsForInstallationId(appCreds, authNarrowing, async (octokit) => {
const installation = await octokit.apps.getRepoInstallation({
Expand All @@ -65,38 +67,42 @@ export async function getAuthOptionsForRepo(
});

return installation.data.id;
});
}, request);
}

export async function getAuthOptionsForOrg(
org: string,
appCreds: AppCredentials,
authNarrowing: AuthNarrowing = {},
request?: RequestInterface,
) {
return await getAuthOptionsForInstallationId(appCreds, authNarrowing, async (octokit) => {
const installation = await octokit.apps.getOrgInstallation({
org,
});

return installation.data.id;
});
}, request);
}

async function getAuthOptionsForInstallationId(
appCreds: AppCredentials,
authNarrowing: AuthNarrowing = {},
installationIdFetcher: (octokit: Octokit) => Promise<number>,
request?: RequestInterface,
): Promise<OctokitAuthOptions | null> {
const auth = createAppAuth({
appId: appCreds.appId,
privateKey: appCreds.privateKey,
request,
});
const appAuth = await auth({
type: 'app',
});

const octokit = new Octokit({
auth: appAuth.token,
request,
});

try {
Expand Down

0 comments on commit 509af25

Please sign in to comment.