Skip to content

feat(templates): add support for custom template URLs #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions __tests__/templating/data.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
jest.mock('../../src/utils/logger');

import nock from 'nock';

afterEach(() => {
jest.resetModules();
delete process.env.TWILIO_SERVERLESS_TEMPLATE_BRANCH;
delete process.env.TWILIO_SERVERLESS_TEMPLATE_REPO;
});

describe('base API configuration', () => {
test('has the right default values', () => {
const {
TEMPLATES_URL,
CONTENT_BASE_URL,
} = require('../../src/templating/data');

expect(TEMPLATES_URL).toBe(
'https://raw.githubusercontent.com/twilio-labs/function-templates/master/templates.json'
);
expect(CONTENT_BASE_URL).toBe(
'https://api.github.com/repos/twilio-labs/function-templates/contents'
);
});

test('allows to override repo', () => {
process.env.TWILIO_SERVERLESS_TEMPLATE_REPO = 'dkundel/function-templates';

const {
TEMPLATES_URL,
CONTENT_BASE_URL,
} = require('../../src/templating/data');

expect(TEMPLATES_URL).toBe(
'https://raw.githubusercontent.com/dkundel/function-templates/master/templates.json'
);
expect(CONTENT_BASE_URL).toBe(
'https://api.github.com/repos/dkundel/function-templates/contents'
);
});

test('allows to override base branch', () => {
process.env.TWILIO_SERVERLESS_TEMPLATE_BRANCH = 'next';

const {
TEMPLATES_URL,
CONTENT_BASE_URL,
} = require('../../src/templating/data');

expect(TEMPLATES_URL).toBe(
'https://raw.githubusercontent.com/twilio-labs/function-templates/next/templates.json'
);
expect(CONTENT_BASE_URL).toBe(
'https://api.github.com/repos/twilio-labs/function-templates/contents'
);
});

test('allows to override branch and repo', () => {
process.env.TWILIO_SERVERLESS_TEMPLATE_REPO = 'philnash/function-templates';
process.env.TWILIO_SERVERLESS_TEMPLATE_BRANCH = 'development';

const {
TEMPLATES_URL,
CONTENT_BASE_URL,
} = require('../../src/templating/data');

expect(TEMPLATES_URL).toBe(
'https://raw.githubusercontent.com/philnash/function-templates/development/templates.json'
);
expect(CONTENT_BASE_URL).toBe(
'https://api.github.com/repos/philnash/function-templates/contents'
);
});
});

describe('getTemplateFiles', () => {
const gitHubApi = nock('https://api.github.com');

afterEach(() => {
gitHubApi.done();
});

test('contacts the GitHub API', async () => {
const { getTemplateFiles } = require('../../src/templating/data');

const basePath = '/repos/twilio-labs/function-templates/contents/blank';
const templateContentScope = gitHubApi
.get(`${basePath}?ref=master`)
.reply(200, []);
const result = await getTemplateFiles('blank');

expect(result).toEqual([]);
expect(templateContentScope.isDone()).toBeTruthy();
});
});
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"jest-express": "^1.10.1",
"lint-staged": "^8.2.1",
"listr-silent-renderer": "^1.1.1",
"nock": "^12.0.2",
"npm-run-all": "^4.1.5",
"prettier": "^1.18.2",
"rimraf": "^2.6.3",
Expand Down
40 changes: 25 additions & 15 deletions src/templating/data.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import got from 'got';
import { getDebugFunction } from '../utils/logger';
import { stripIndent } from 'common-tags';
import { readLocalEnvFile } from '../config/utils/env';
import got from 'got';
import { OutgoingHttpHeaders } from 'http';
import { getDebugFunction } from '../utils/logger';
const debug = getDebugFunction('twilio-run:new:template-data');

const TEMPLATES_URL =
'https://raw.githubusercontent.com/twilio-labs/function-templates/master/templates.json';
const CONTENT_BASE_URL =
'https://api.github.com/repos/twilio-labs/function-templates/contents';
const TEMPLATE_BASE_REPO =
process.env.TWILIO_SERVERLESS_TEMPLATE_REPO ||
'twilio-labs/function-templates';
const TEMPLATE_BASE_BRANCH =
process.env.TWILIO_SERVERLESS_TEMPLATE_BRANCH || 'master';

export const TEMPLATES_URL = `https://raw.githubusercontent.com/${TEMPLATE_BASE_REPO}/${TEMPLATE_BASE_BRANCH}/templates.json`;
export const CONTENT_BASE_URL = `https://api.github.com/repos/${TEMPLATE_BASE_REPO}/contents`;

export type Template = {
id: string;
Expand Down Expand Up @@ -59,10 +62,14 @@ async function getFiles(
directory: string
): Promise<TemplateFileInfo[]> {
const headers = buildHeader();
const response = await got(CONTENT_BASE_URL + `/${templateId}/${directory}`, {
json: true,
headers,
});
const response = await got(
CONTENT_BASE_URL +
`/${templateId}/${directory}?ref=${TEMPLATE_BASE_BRANCH}`,
{
json: true,
headers,
}
);
const repoContents = response.body as RawContentsPayload;
return repoContents.map(file => {
return {
Expand All @@ -78,10 +85,13 @@ export async function getTemplateFiles(
): Promise<TemplateFileInfo[]> {
try {
const headers = buildHeader();
const response = await got(CONTENT_BASE_URL + `/${templateId}`, {
json: true,
headers,
});
const response = await got(
CONTENT_BASE_URL + `/${templateId}?ref=${TEMPLATE_BASE_BRANCH}`,
{
json: true,
headers,
}
);
const repoContents = response.body as RawContentsPayload;

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