Skip to content

Commit e105f36

Browse files
authored
feat(templates): add support for custom template URLs (#115)
1 parent 523e09a commit e105f36

File tree

4 files changed

+150
-15
lines changed

4 files changed

+150
-15
lines changed

__tests__/templating/data.test.ts

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
jest.mock('../../src/utils/logger');
2+
3+
import nock from 'nock';
4+
5+
afterEach(() => {
6+
jest.resetModules();
7+
delete process.env.TWILIO_SERVERLESS_TEMPLATE_BRANCH;
8+
delete process.env.TWILIO_SERVERLESS_TEMPLATE_REPO;
9+
});
10+
11+
describe('base API configuration', () => {
12+
test('has the right default values', () => {
13+
const {
14+
TEMPLATES_URL,
15+
CONTENT_BASE_URL,
16+
} = require('../../src/templating/data');
17+
18+
expect(TEMPLATES_URL).toBe(
19+
'https://raw.githubusercontent.com/twilio-labs/function-templates/master/templates.json'
20+
);
21+
expect(CONTENT_BASE_URL).toBe(
22+
'https://api.github.com/repos/twilio-labs/function-templates/contents'
23+
);
24+
});
25+
26+
test('allows to override repo', () => {
27+
process.env.TWILIO_SERVERLESS_TEMPLATE_REPO = 'dkundel/function-templates';
28+
29+
const {
30+
TEMPLATES_URL,
31+
CONTENT_BASE_URL,
32+
} = require('../../src/templating/data');
33+
34+
expect(TEMPLATES_URL).toBe(
35+
'https://raw.githubusercontent.com/dkundel/function-templates/master/templates.json'
36+
);
37+
expect(CONTENT_BASE_URL).toBe(
38+
'https://api.github.com/repos/dkundel/function-templates/contents'
39+
);
40+
});
41+
42+
test('allows to override base branch', () => {
43+
process.env.TWILIO_SERVERLESS_TEMPLATE_BRANCH = 'next';
44+
45+
const {
46+
TEMPLATES_URL,
47+
CONTENT_BASE_URL,
48+
} = require('../../src/templating/data');
49+
50+
expect(TEMPLATES_URL).toBe(
51+
'https://raw.githubusercontent.com/twilio-labs/function-templates/next/templates.json'
52+
);
53+
expect(CONTENT_BASE_URL).toBe(
54+
'https://api.github.com/repos/twilio-labs/function-templates/contents'
55+
);
56+
});
57+
58+
test('allows to override branch and repo', () => {
59+
process.env.TWILIO_SERVERLESS_TEMPLATE_REPO = 'philnash/function-templates';
60+
process.env.TWILIO_SERVERLESS_TEMPLATE_BRANCH = 'development';
61+
62+
const {
63+
TEMPLATES_URL,
64+
CONTENT_BASE_URL,
65+
} = require('../../src/templating/data');
66+
67+
expect(TEMPLATES_URL).toBe(
68+
'https://raw.githubusercontent.com/philnash/function-templates/development/templates.json'
69+
);
70+
expect(CONTENT_BASE_URL).toBe(
71+
'https://api.github.com/repos/philnash/function-templates/contents'
72+
);
73+
});
74+
});
75+
76+
describe('getTemplateFiles', () => {
77+
const gitHubApi = nock('https://api.github.com');
78+
79+
afterEach(() => {
80+
gitHubApi.done();
81+
});
82+
83+
test('contacts the GitHub API', async () => {
84+
const { getTemplateFiles } = require('../../src/templating/data');
85+
86+
const basePath = '/repos/twilio-labs/function-templates/contents/blank';
87+
const templateContentScope = gitHubApi
88+
.get(`${basePath}?ref=master`)
89+
.reply(200, []);
90+
const result = await getTemplateFiles('blank');
91+
92+
expect(result).toEqual([]);
93+
expect(templateContentScope.isDone()).toBeTruthy();
94+
});
95+
});

package-lock.json

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
"jest-express": "^1.10.1",
107107
"lint-staged": "^8.2.1",
108108
"listr-silent-renderer": "^1.1.1",
109+
"nock": "^12.0.2",
109110
"npm-run-all": "^4.1.5",
110111
"prettier": "^1.18.2",
111112
"rimraf": "^2.6.3",

src/templating/data.ts

+25-15
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import got from 'got';
2-
import { getDebugFunction } from '../utils/logger';
31
import { stripIndent } from 'common-tags';
4-
import { readLocalEnvFile } from '../config/utils/env';
2+
import got from 'got';
53
import { OutgoingHttpHeaders } from 'http';
4+
import { getDebugFunction } from '../utils/logger';
65
const debug = getDebugFunction('twilio-run:new:template-data');
76

8-
const TEMPLATES_URL =
9-
'https://raw.githubusercontent.com/twilio-labs/function-templates/master/templates.json';
10-
const CONTENT_BASE_URL =
11-
'https://api.github.com/repos/twilio-labs/function-templates/contents';
7+
const TEMPLATE_BASE_REPO =
8+
process.env.TWILIO_SERVERLESS_TEMPLATE_REPO ||
9+
'twilio-labs/function-templates';
10+
const TEMPLATE_BASE_BRANCH =
11+
process.env.TWILIO_SERVERLESS_TEMPLATE_BRANCH || 'master';
12+
13+
export const TEMPLATES_URL = `https://raw.githubusercontent.com/${TEMPLATE_BASE_REPO}/${TEMPLATE_BASE_BRANCH}/templates.json`;
14+
export const CONTENT_BASE_URL = `https://api.github.com/repos/${TEMPLATE_BASE_REPO}/contents`;
1215

1316
export type Template = {
1417
id: string;
@@ -59,10 +62,14 @@ async function getFiles(
5962
directory: string
6063
): Promise<TemplateFileInfo[]> {
6164
const headers = buildHeader();
62-
const response = await got(CONTENT_BASE_URL + `/${templateId}/${directory}`, {
63-
json: true,
64-
headers,
65-
});
65+
const response = await got(
66+
CONTENT_BASE_URL +
67+
`/${templateId}/${directory}?ref=${TEMPLATE_BASE_BRANCH}`,
68+
{
69+
json: true,
70+
headers,
71+
}
72+
);
6673
const repoContents = response.body as RawContentsPayload;
6774
return repoContents.map(file => {
6875
return {
@@ -78,10 +85,13 @@ export async function getTemplateFiles(
7885
): Promise<TemplateFileInfo[]> {
7986
try {
8087
const headers = buildHeader();
81-
const response = await got(CONTENT_BASE_URL + `/${templateId}`, {
82-
json: true,
83-
headers,
84-
});
88+
const response = await got(
89+
CONTENT_BASE_URL + `/${templateId}?ref=${TEMPLATE_BASE_BRANCH}`,
90+
{
91+
json: true,
92+
headers,
93+
}
94+
);
8595
const repoContents = response.body as RawContentsPayload;
8696

8797
const assets = repoContents.find(file => file.name === 'assets')

0 commit comments

Comments
 (0)