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: Don't try to revoke a token that has already expired #109

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions src/create-installation-access-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export const createInstallationAccessToken = async ({
permissions,
privateKey,
repositories,
}: InstallationAccessTokenCreationOptions): Promise<string> => {
}: InstallationAccessTokenCreationOptions): Promise<{
[s: string]: string;
}> => {
try {
const app = createAppAuth({
appId,
Expand All @@ -45,12 +47,12 @@ export const createInstallationAccessToken = async ({
);

const {
data: { token },
data: { token, expires_at },
} = await octokit.request(
"POST /app/installations/{installation_id}/access_tokens",
{ installation_id: installationId, permissions, repositories },
);
return token;
return { token: token, expiration: expires_at };
} catch (error: unknown) {
throw new Error("Could not create installation access token.", {
cause: error,
Expand Down
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { info, saveState, setOutput, setSecret } from "@actions/core";
import { createInstallationAccessToken } from "./create-installation-access-token.js";
import { parseOptions } from "./parse-options.js";
import { run } from "./run.js";
import { tokenKey } from "./state.js";
import { expirationKey, tokenKey } from "./state.js";

await run(async () => {
const options = parseOptions();
const token = await createInstallationAccessToken(options);
const { token, expiration } = await createInstallationAccessToken(options);
setSecret(token);
saveState(tokenKey, token);
saveState(expirationKey, expiration);
setOutput("token", token);
info("Token created successfully");
});
11 changes: 10 additions & 1 deletion src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getInput, getState, info } from "@actions/core";

import { revokeInstallationAccessToken } from "./revoke-installation-access-token.js";
import { run } from "./run.js";
import { tokenKey } from "./state.js";
import { expirationKey, tokenKey } from "./state.js";

await run(async () => {
if (!JSON.parse(getInput("revoke"))) {
Expand All @@ -15,6 +15,15 @@ await run(async () => {
info("No token to revoke");
return;
}

// if expiration is before now, then the token already expired and there's no need to revoke it
const expiration = getState(expirationKey);
const now = Date.now();
const expirationTime = Date.parse(expiration);
if (expirationTime < now) {
info("Token is already expired, no need to revoke it");
return;
}
await revokeInstallationAccessToken(token);
info("Token revoked successfully");
});
1 change: 1 addition & 0 deletions src/state.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const tokenKey = "token";
export const expirationKey = "expires_at";