Skip to content

Commit f4b3038

Browse files
badsketchdkundel
authored andcommitted
feature: #90 allow users to specify a github token (#96)
* feat(templates): improve error message when github rate limited * 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 * feat(templates): removed possibility to specify github token in env file fix #90
1 parent f965275 commit f4b3038

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

src/templating/data.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import got from 'got';
22
import { getDebugFunction } from '../utils/logger';
3-
3+
import { stripIndent } from 'common-tags';
4+
import { readLocalEnvFile } from '../config/utils/env';
5+
import { OutgoingHttpHeaders } from 'http';
46
const debug = getDebugFunction('twilio-run:new:template-data');
57

68
const TEMPLATES_URL =
@@ -56,8 +58,10 @@ async function getFiles(
5658
templateId: string,
5759
directory: string
5860
): Promise<TemplateFileInfo[]> {
61+
const headers = buildHeader();
5962
const response = await got(CONTENT_BASE_URL + `/${templateId}/${directory}`, {
6063
json: true,
64+
headers,
6165
});
6266
const repoContents = response.body as RawContentsPayload;
6367
return repoContents.map(file => {
@@ -73,8 +77,10 @@ export async function getTemplateFiles(
7377
templateId: string
7478
): Promise<TemplateFileInfo[]> {
7579
try {
80+
const headers = buildHeader();
7681
const response = await got(CONTENT_BASE_URL + `/${templateId}`, {
7782
json: true,
83+
headers,
7884
});
7985
const repoContents = response.body as RawContentsPayload;
8086

@@ -104,12 +110,38 @@ export async function getTemplateFiles(
104110
debug(err.message);
105111

106112
if (err.response) {
107-
const bodyMessage = err.response.body as GitHubError;
108-
throw new Error(
109-
bodyMessage ? `${err.message}\n${bodyMessage.message}` : err.message
110-
);
113+
if (err.response.statusCode === 403) {
114+
throw new Error(
115+
stripIndent`
116+
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:
117+
118+
- Change your WiFi or make sure you are not connected to a VPN that might cause the rate limiting
119+
120+
121+
If the issue persists you can try one of the two options:
122+
123+
- 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
124+
125+
- Wait for a few minutes up to an hour and try again.
126+
`
127+
);
128+
} else {
129+
const bodyMessage = err.response.body as GitHubError;
130+
throw new Error(
131+
bodyMessage ? `${err.message}\n${bodyMessage.message}` : err.message
132+
);
133+
}
111134
}
112135

113136
throw new Error('Invalid template');
114137
}
115138
}
139+
140+
function buildHeader(): OutgoingHttpHeaders {
141+
let githubToken = '';
142+
if (process.env.TWILIO_SERVERLESS_GITHUB_TOKEN) {
143+
githubToken = process.env.TWILIO_SERVERLESS_GITHUB_TOKEN;
144+
}
145+
146+
return githubToken ? { Authorization: `token ${githubToken}` } : {};
147+
}

0 commit comments

Comments
 (0)