From c99595dc4755c99a6aa05a237c952bc1d9c08dfb Mon Sep 17 00:00:00 2001 From: Federico Date: Sat, 21 Nov 2020 13:27:14 -0600 Subject: Add `quick-fork-deletion` feature (#3734) --- source/github-helpers/api.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'source/github-helpers/api.ts') diff --git a/source/github-helpers/api.ts b/source/github-helpers/api.ts index 98b2ddf9..504e4755 100644 --- a/source/github-helpers/api.ts +++ b/source/github-helpers/api.ts @@ -51,6 +51,7 @@ interface RestResponse extends AnyObject { export const escapeKey = (value: string | number): string => '_' + String(value).replace(/[ ./-]/g, '_'); export class RefinedGitHubAPIError extends Error { + response: AnyObject = {}; constructor(...messages: string[]) { super(messages.join('\n')); } @@ -66,6 +67,14 @@ export async function expectToken(): Promise { return personalToken; } +export async function expectTokenScope(scope: string): Promise { + const {headers} = await v3('/'); + const tokenScopes = headers.get('X-OAuth-Scopes')!; + if (!tokenScopes.split(', ').includes(scope)) { + throw new Error(`The token you provided does not have the \`${scope}\` scope. It only includes \`${tokenScopes}\``); + } +} + const api3 = pageDetect.isEnterprise() ? `${location.origin}/api/v3/` : 'https://api.github.com/'; @@ -210,11 +219,13 @@ export async function getError(apiResponse: JsonObject): Promise