Skip to content

Commit

Permalink
fixup! Try without error wrapping to see if error message is clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
tibdex committed Sep 9, 2023
1 parent eff9aff commit 4369b4b
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 78 deletions.
77 changes: 43 additions & 34 deletions src/fetch-installation-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,49 @@ export const fetchInstallationToken = async ({

let installationId: number;

switch (installationRetrievalDetails.mode) {
case "id":
({ id: installationId } = installationRetrievalDetails);
break;
case "organization":
({
data: { id: installationId },
} = await octokit.request("GET /orgs/{org}/installation", {
org: installationRetrievalDetails.org,
}));
break;
case "repository":
({
data: { id: installationId },
} = await octokit.request("GET /repos/{owner}/{repo}/installation", {
owner: installationRetrievalDetails.owner,
repo: installationRetrievalDetails.repo,
}));
break;
case "user":
({
data: { id: installationId },
} = await octokit.request("GET /users/{username}/installation", {
username: installationRetrievalDetails.username,
}));
break;
try {
switch (installationRetrievalDetails.mode) {
case "id":
({ id: installationId } = installationRetrievalDetails);
break;
case "organization":
({
data: { id: installationId },
} = await octokit.request("GET /orgs/{org}/installation", {
org: installationRetrievalDetails.org,
}));
break;
case "repository":
({
data: { id: installationId },
} = await octokit.request("GET /repos/{owner}/{repo}/installation", {
owner: installationRetrievalDetails.owner,
repo: installationRetrievalDetails.repo,
}));
break;
case "user":
({
data: { id: installationId },
} = await octokit.request("GET /users/{username}/installation", {
username: installationRetrievalDetails.username,
}));
break;
}
} catch (error: unknown) {
throw new Error("Could not get retrieve installation.", { cause: error });
}

const {
data: { token },
} = await octokit.rest.apps.createInstallationAccessToken({
installation_id: installationId,
permissions,
repositories,
});
return token;
try {
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.", {
cause: error,
});
}
};
95 changes: 51 additions & 44 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,59 @@
import { Buffer } from "node:buffer";

import { getInput, info, setFailed, setOutput, setSecret } from "@actions/core";
import ensureError from "ensure-error";
import isBase64 from "is-base64";

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

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 permissionsInput = getInput("permissions");
const permissions = permissionsInput
? (JSON.parse(permissionsInput) as Record<string, string>)
: undefined;

const privateKeyInput = getInput("private_key", { required: true });
const privateKey = isBase64(privateKeyInput)
? Buffer.from(privateKeyInput, "base64").toString("utf8")
: privateKeyInput;

const repositoriesInput = getInput("repositories");
const repositories = repositoriesInput
? (JSON.parse(repositoriesInput) as string[])
: undefined;

const token = await fetchInstallationToken({
appId,
githubApiUrl,
installationRetrievalDetails,
permissions,
privateKey,
repositories,
});

setSecret(token);
setOutput("token", token);
info("Token generated successfully!");
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 permissionsInput = getInput("permissions");
const permissions = permissionsInput
? (JSON.parse(permissionsInput) as Record<string, string>)
: undefined;

const privateKeyInput = getInput("private_key", { required: true });
const privateKey = isBase64(privateKeyInput)
? Buffer.from(privateKeyInput, "base64").toString("utf8")
: privateKeyInput;

const repositoriesInput = getInput("repositories");
const repositories = repositoriesInput
? (JSON.parse(repositoriesInput) as string[])
: undefined;

const token = await fetchInstallationToken({
appId,
githubApiUrl,
installationRetrievalDetails,
permissions,
privateKey,
repositories,
});

setSecret(token);
setOutput("token", token);
info("Token generated successfully!");
} catch (_error: unknown) {
const error = ensureError(_error);
console.error(error);
setFailed("");
}

0 comments on commit 4369b4b

Please sign in to comment.