Skip to content

Commit

Permalink
fixup! Add support for organization and user installation retrieval a…
Browse files Browse the repository at this point in the history
…nd repository scoping
  • Loading branch information
tibdex committed Sep 9, 2023
1 parent 59f20c9 commit acf3218
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
24 changes: 11 additions & 13 deletions src/fetch-installation-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getOctokit } from "@actions/github";
import { createAppAuth } from "@octokit/auth-app";
import { request } from "@octokit/request";

import {InstallationRetrievalDetails} from "./installation-retrieval-details.js";
import { InstallationRetrievalDetails } from "./installation-retrieval-details.js";

export const fetchInstallationToken = async ({
appId,
Expand Down Expand Up @@ -38,21 +38,21 @@ export const fetchInstallationToken = async ({
try {
switch (installationRetrievalDetails.mode) {
case "id":
({id: installationId} = installationRetrievalDetails);
({ id: installationId } = installationRetrievalDetails);
break;
case "organization":
({
data: { id: installationId },
} = await octokit.request("GET /orgs/{org}/installation", {
org: installationRetrievalDetails.org
org: installationRetrievalDetails.org,
}));
break;
case "repository":
({
data: { id: installationId },
} = await octokit.request("GET /repos/{owner}/{repo}/installation", {
owner: installationRetrievalDetails.owner,
repo: installationRetrievalDetails.repo
repo: installationRetrievalDetails.repo,
}));
break;
case "user":
Expand All @@ -64,18 +64,16 @@ export const fetchInstallationToken = async ({
break;
}
} catch (error: unknown) {
throw new Error(
"Could not get retrieve installation.",
{ cause: error },
);
throw new Error("Could not get retrieve installation.", { cause: error });
}

try {
const { data: {token} } =
await octokit.rest.apps.createInstallationAccessToken({
installation_id: installationId,
permissions,
});
const {
data: { token },
} = await octokit.rest.apps.createInstallationAccessToken({
installation_id: installationId,
permissions,
});
return token;
} catch (error: unknown) {
throw new Error("Could not create installation access token.", {
Expand Down
16 changes: 12 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ import ensureError from "ensure-error";
import isBase64 from "is-base64";

import { fetchInstallationToken } from "./fetch-installation-token.js";
import {getInstallationRetrievalDetails} from "./installation-retrieval-details.js"
import { getInstallationRetrievalDetails } from "./installation-retrieval-details.js";

try {
const appId = getInput("app_id", { required: true });

const githubApiUrlInput = getInput("github_api_url", { required: true });
const githubApiUrl = new URL(githubApiUrlInput);

const installationRetrievalMode = getInput("installation_retrieval_mode", { required: true });
const installationRetrievalPayload = getInput("installation_retrieval_payload", { required: true });
const installationRetrievalDetails = getInstallationRetrievalDetails({mode: installationRetrievalMode, payload: installationRetrievalPayload});
const installationRetrievalMode = getInput("installation_retrieval_mode", {
required: true,
});
const installationRetrievalPayload = getInput(
"installation_retrieval_payload",
{ required: true },
);
const installationRetrievalDetails = getInstallationRetrievalDetails({
mode: installationRetrievalMode,
payload: installationRetrievalPayload,
});

const permissionsInput = getInput("permissions");
const permissions = permissionsInput
Expand Down
24 changes: 15 additions & 9 deletions src/installation-retrieval-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ export type InstallationRetrievalDetails = Readonly<
| { mode: "user"; username: string }
>;

export const getInstallationRetrievalDetails = ({mode, payload}: Readonly<{mode: string, payload: string}>): InstallationRetrievalDetails => {
switch (mode) {
export const getInstallationRetrievalDetails = ({
mode,
payload,
}: Readonly<{
mode: string;
payload: string;
}>): InstallationRetrievalDetails => {
switch (mode) {
case "id":
return {mode, id: Number(payload)};
return { mode, id: Number(payload) };
case "organization":
return {mode, org: payload};
return { mode, org: payload };
case "repository":
const [owner, repo] = payload.split("/");
return {mode, owner, repo};
const [owner, repo] = payload.split("/");
return { mode, owner, repo };
case "user":
return {mode, username: payload};
return { mode, username: payload };
default:
throw new Error(`Unsupported retrieval mode: "${mode}".`)
}
throw new Error(`Unsupported retrieval mode: "${mode}".`);
}
};

0 comments on commit acf3218

Please sign in to comment.