Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
feat(repo-creation): prevented creation without an auth-ed octokit cl…
Browse files Browse the repository at this point in the history
…ient

closes #1
  • Loading branch information
travi committed Apr 11, 2019
1 parent ab6b4b7 commit cf935c7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
19 changes: 17 additions & 2 deletions src/github-client-factory.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import chalk from 'chalk';
import Octokit from '../third-party-wrappers/octokit';
import netrc from '../third-party-wrappers/netrc';

function getPersonalAccessTokenFromNetRc() {
console.error(chalk.grey('Getting GitHub Personal Access Token from ~/.netrc')); // eslint-disable-line no-console

const githubNetrcElement = netrc()['github.com'];

if (githubNetrcElement) return githubNetrcElement.login;

console.error(chalk.grey('No GitHub Personal Access Token available in ~/.netrc')); // eslint-disable-line no-console

return undefined;
}

export function factory() {
const personalAccessToken = netrc()['github.com'].login;
const personalAccessToken = getPersonalAccessTokenFromNetRc();

if (personalAccessToken) return new Octokit({auth: `token ${personalAccessToken}`});

return new Octokit({auth: `token ${personalAccessToken}`});
return undefined;
}
4 changes: 2 additions & 2 deletions src/scaffolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export async function scaffold({name, owner, projectRoot, projectType, descripti

const [, creationResult] = await Promise.all([
scaffoldSettings(projectRoot, name, description, homepage, visibility, projectType),
create(name, owner, visibility, octokit)
...octokit ? [create(name, owner, visibility, octokit)] : []
]);

return creationResult;
return {...creationResult};
}
6 changes: 6 additions & 0 deletions test/unit/github-client-factory-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ suite('github client factory', () => {
assert.equal(factory(), instance);
assert.calledWithNew(octokit.default);
});

test('that no client is returned if no token is available in the netrc', () => {
netrc.default.returns({});

assert.isUndefined(factory());
});
});
29 changes: 23 additions & 6 deletions test/unit/scaffolder-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ suite('github', () => {
let sandbox;
const projectRoot = any.string();
const projectName = any.string();
const description = any.sentence();
const homepage = any.url();
const projectType = any.word();
const projectOwner = any.word();
const visibility = any.word();

setup(() => {
sandbox = sinon.createSandbox();
Expand All @@ -22,18 +27,13 @@ suite('github', () => {
teardown(() => sandbox.restore());

test('that the settings file is produced and the repository is created', async () => {
const description = any.sentence();
const homepage = any.url();
const projectType = any.word();
const projectOwner = any.word();
const visibility = any.word();
const creationResult = any.simpleObject();
const octokitClient = any.simpleObject();
settingsScaffolder.default.resolves();
creator.default.withArgs(projectName, projectOwner, visibility, octokitClient).resolves(creationResult);
clientFactory.factory.returns(octokitClient);

assert.equal(
assert.deepEqual(
await scaffold({
projectRoot,
name: projectName,
Expand All @@ -56,4 +56,21 @@ suite('github', () => {
projectType
);
});

test('that the repo is not created if an octokit client is not available', async () => {
clientFactory.factory.returns(undefined);

assert.deepEqual(
await scaffold({
projectRoot,
name: projectName,
owner: projectOwner,
description,
homepage,
projectType,
visibility
}),
{}
);
});
});

0 comments on commit cf935c7

Please sign in to comment.