-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
177 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { debug } from "@actions/core"; | ||
import { getOctokit } from "@actions/github"; | ||
|
||
export type InstallationRetrievalDetails = Readonly< | ||
| { mode: "id"; id: number } | ||
| { mode: "organization"; org: string } | ||
| { mode: "repository"; owner: string; repo: string } | ||
| { mode: "user"; username: string } | ||
>; | ||
|
||
export const getInstallationRetrievalDetails = ({ | ||
mode, | ||
payload, | ||
}: Readonly<{ | ||
mode: string; | ||
payload: string; | ||
}>): InstallationRetrievalDetails => { | ||
switch (mode) { | ||
case "id": | ||
return { mode, id: parseInt(payload) }; | ||
case "organization": | ||
return { mode, org: payload }; | ||
case "repository": | ||
const [owner, repo] = payload.split("/"); | ||
return { mode, owner, repo }; | ||
case "user": | ||
return { mode, username: payload }; | ||
default: | ||
throw new Error(`Unsupported retrieval mode: "${mode}".`); | ||
} | ||
}; | ||
|
||
export const retrieveInstallationId = async ( | ||
details: InstallationRetrievalDetails, | ||
{ octokit }: Readonly<{ octokit: ReturnType<typeof getOctokit> }>, | ||
): Promise<number> => { | ||
let id: number; | ||
try { | ||
switch (details.mode) { | ||
case "id": | ||
({ id } = details); | ||
break; | ||
case "organization": | ||
({ | ||
data: { id }, | ||
} = await octokit.request("GET /orgs/{org}/installation", { | ||
org: details.org, | ||
})); | ||
break; | ||
case "repository": | ||
({ | ||
data: { id }, | ||
} = await octokit.request("GET /repos/{owner}/{repo}/installation", { | ||
owner: details.owner, | ||
repo: details.repo, | ||
})); | ||
break; | ||
case "user": | ||
({ | ||
data: { id }, | ||
} = await octokit.request("GET /users/{username}/installation", { | ||
username: details.username, | ||
})); | ||
break; | ||
} | ||
} catch (error: unknown) { | ||
throw new Error("Could not retrieve installation.", { cause: error }); | ||
} | ||
|
||
debug(`Retrieved installation ID: ${id}.`); | ||
return id; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
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"; | ||
|
||
await run(async () => { | ||
const options = parseOptions(); | ||
const token = await createInstallationAccessToken(options); | ||
setSecret(token); | ||
saveState(tokenKey, token); | ||
setOutput("token", token); | ||
info("Token created successfully"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { getState, info } from "@actions/core"; | ||
|
||
import { revokeInstallationAccessToken } from "./revoke-installation-access-token.js"; | ||
import { run } from "./run.js"; | ||
import { tokenKey } from "./state.js"; | ||
|
||
await run(async () => { | ||
const token = getState(tokenKey); | ||
await revokeInstallationAccessToken(token); | ||
info("Token revoked successfully"); | ||
await revokeInstallationAccessToken(token); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { getOctokit } from "@actions/github"; | ||
|
||
export const revokeInstallationAccessToken = async ( | ||
token: string, | ||
): Promise<void> => { | ||
try { | ||
const octokit = getOctokit(token); | ||
|
||
await octokit.request("DELETE /installation/token"); | ||
} catch (error: unknown) { | ||
throw new Error("Could not revoke installation access token.", { | ||
cause: error, | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { setFailed } from "@actions/core"; | ||
|
||
export const run = async (callback: () => Promise<unknown>) => { | ||
try { | ||
await callback(); | ||
} catch (error) { | ||
// Using `console.error()` instead of only passing `error` to `setFailed()` for better error reporting. | ||
// See https://github.com/actions/toolkit/issues/1527. | ||
console.error(error); | ||
setFailed(""); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const tokenKey = "token"; |