From 14c236326ddd01adc75ceb98b9a4b87312127d98 Mon Sep 17 00:00:00 2001 From: Danielwang405 Date: Sun, 3 Nov 2019 03:04:06 -0500 Subject: [PATCH 1/3] feat(templates): improve error message when github rate limited --- src/templating/data.ts | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/templating/data.ts b/src/templating/data.ts index c374a6e4..821c315b 100644 --- a/src/templating/data.ts +++ b/src/templating/data.ts @@ -1,5 +1,6 @@ import got from 'got'; import { getDebugFunction } from '../utils/logger'; +import { stripIndent } from 'common-tags'; const debug = getDebugFunction('twilio-run:new:template-data'); @@ -104,10 +105,27 @@ export async function getTemplateFiles( debug(err.message); if (err.response) { - const bodyMessage = err.response.body as GitHubError; - throw new Error( - bodyMessage ? `${err.message}\n${bodyMessage.message}` : err.message - ); + if (err.response.statusCode === 403) { + throw new Error( + stripIndent` + We are sorry but we failed fetching the requested template from GitHub because your IP address has been rate limited. Please try the following to resolve the issue: + + - Change your WiFi or make sure you are not connected to a VPN that might cause the rate limiting + + + If the issue persists you can try one of the two options: + + - Get a GitHub developer token following https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line (no permissions needed) and add it as TWILIO_SERVERLESS_GITHUB_TOKEN to your environment variables + + - Wait for a few minutes up to an hour and try again. + ` + ); + } else { + const bodyMessage = err.response.body as GitHubError; + throw new Error( + bodyMessage ? `${err.message}\n${bodyMessage.message}` : err.message + ); + } } throw new Error('Invalid template'); From 4e3ce463bace0986ec2e1af097f6438589ba1994 Mon Sep 17 00:00:00 2001 From: Danielwang405 Date: Sat, 16 Nov 2019 01:10:31 -0500 Subject: [PATCH 2/3] feat(templates): add option to specify github auth token users pulling templates from github can bypass rate limit by specifying a TWILIO_SERVERLESS_GITHUB_TOKEN in their environment fix #90 --- src/templating/data.ts | 22 +++++++++++++++++++++- src/types/generic.ts | 1 + 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/templating/data.ts b/src/templating/data.ts index 821c315b..f4d875ba 100644 --- a/src/templating/data.ts +++ b/src/templating/data.ts @@ -1,7 +1,8 @@ import got from 'got'; import { getDebugFunction } from '../utils/logger'; import { stripIndent } from 'common-tags'; - +import { readLocalEnvFile } from '../config/utils/env'; +import { OutgoingHttpHeaders } from 'http'; const debug = getDebugFunction('twilio-run:new:template-data'); const TEMPLATES_URL = @@ -57,8 +58,10 @@ async function getFiles( templateId: string, directory: string ): Promise { + const headers = await buildHeaders(); const response = await got(CONTENT_BASE_URL + `/${templateId}/${directory}`, { json: true, + headers, }); const repoContents = response.body as RawContentsPayload; return repoContents.map(file => { @@ -74,8 +77,10 @@ export async function getTemplateFiles( templateId: string ): Promise { try { + const headers = await buildHeaders(); const response = await got(CONTENT_BASE_URL + `/${templateId}`, { json: true, + headers, }); const repoContents = response.body as RawContentsPayload; @@ -131,3 +136,18 @@ export async function getTemplateFiles( throw new Error('Invalid template'); } } + +async function buildHeaders(): Promise { + let githubToken = ''; + const { localEnv } = await readLocalEnvFile({ cwd: process.cwd() }); + + if (localEnv.TWILIO_SERVERLESS_GITHUB_TOKEN) { + githubToken = localEnv.TWILIO_SERVERLESS_GITHUB_TOKEN; + } + + if (process.env.TWILIO_SERVERLESS_GITHUB_TOKEN) { + githubToken = process.env.TWILIO_SERVERLESS_GITHUB_TOKEN; + } + + return githubToken ? { Authorization: `token ${githubToken}` } : {}; +} diff --git a/src/types/generic.ts b/src/types/generic.ts index 23b62ff8..7ec404e3 100644 --- a/src/types/generic.ts +++ b/src/types/generic.ts @@ -3,4 +3,5 @@ import { EnvironmentVariables } from '@twilio-labs/serverless-api'; export type EnvironmentVariablesWithAuth = EnvironmentVariables & { ACCOUNT_SID?: string; AUTH_TOKEN?: string; + TWILIO_SERVERLESS_GITHUB_TOKEN?: string; }; From 08cca65e37923f2b9ec5aec3222b7b513b7a0865 Mon Sep 17 00:00:00 2001 From: Danielwang405 Date: Mon, 20 Jan 2020 03:32:56 -0500 Subject: [PATCH 3/3] feat(templates): removed possibility to specify github token in env file fix #90 --- src/templating/data.ts | 12 +++--------- src/types/generic.ts | 1 - 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/templating/data.ts b/src/templating/data.ts index f4d875ba..d4cd0a60 100644 --- a/src/templating/data.ts +++ b/src/templating/data.ts @@ -58,7 +58,7 @@ async function getFiles( templateId: string, directory: string ): Promise { - const headers = await buildHeaders(); + const headers = buildHeader(); const response = await got(CONTENT_BASE_URL + `/${templateId}/${directory}`, { json: true, headers, @@ -77,7 +77,7 @@ export async function getTemplateFiles( templateId: string ): Promise { try { - const headers = await buildHeaders(); + const headers = buildHeader(); const response = await got(CONTENT_BASE_URL + `/${templateId}`, { json: true, headers, @@ -137,14 +137,8 @@ export async function getTemplateFiles( } } -async function buildHeaders(): Promise { +function buildHeader(): OutgoingHttpHeaders { let githubToken = ''; - const { localEnv } = await readLocalEnvFile({ cwd: process.cwd() }); - - if (localEnv.TWILIO_SERVERLESS_GITHUB_TOKEN) { - githubToken = localEnv.TWILIO_SERVERLESS_GITHUB_TOKEN; - } - if (process.env.TWILIO_SERVERLESS_GITHUB_TOKEN) { githubToken = process.env.TWILIO_SERVERLESS_GITHUB_TOKEN; } diff --git a/src/types/generic.ts b/src/types/generic.ts index 7ec404e3..23b62ff8 100644 --- a/src/types/generic.ts +++ b/src/types/generic.ts @@ -3,5 +3,4 @@ import { EnvironmentVariables } from '@twilio-labs/serverless-api'; export type EnvironmentVariablesWithAuth = EnvironmentVariables & { ACCOUNT_SID?: string; AUTH_TOKEN?: string; - TWILIO_SERVERLESS_GITHUB_TOKEN?: string; };