@ossjs/release
Version:
Minimalistic, opinionated, and predictable release automation tool.
26 lines (24 loc) • 1.55 kB
JavaScript
import { invariant } from "outvariant";
//#region src/utils/github/validate-access-token.ts
const requiredGitHubTokenScopes = [
"repo",
"admin:repo_hook",
"admin:org_hook"
];
const GITHUB_NEW_TOKEN_URL = `https://github.com/settings/tokens/new?scopes=${requiredGitHubTokenScopes.join(",")}`;
/**
* Check whether the given GitHub access token has sufficient permissions
* for this library to create and publish a new release.
*/
async function validateAccessToken(accessToken) {
const response = await fetch("https://api.github.com", { headers: { Authorization: `Bearer ${accessToken}` } });
const permissions = response.headers.get("x-oauth-scopes")?.split(",").map((scope) => scope.trim()) || [];
invariant(response.ok, "Failed to verify GitHub token permissions: GitHub API responded with %d %s. Please double-check your \"GITHUB_TOKEN\" environmental variable and try again.", response.status, response.statusText);
invariant(permissions.length > 0, "Failed to verify GitHub token permissions: GitHub API responded with an empty \"X-OAuth-Scopes\" header.");
const missingScopes = requiredGitHubTokenScopes.filter((scope) => {
return !permissions.includes(scope);
});
if (missingScopes.length > 0) invariant(false, "Provided \"GITHUB_TOKEN\" environment variable has insufficient permissions: missing scopes \"%s\". Please generate a new GitHub personal access token from this URL: %s", missingScopes.join(`", "`), GITHUB_NEW_TOKEN_URL);
}
//#endregion
export { GITHUB_NEW_TOKEN_URL, requiredGitHubTokenScopes, validateAccessToken };