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

Commit

Permalink
refactor(settings): extracted the settings scaffolding to another module
Browse files Browse the repository at this point in the history
for #1
  • Loading branch information
travi committed Jan 30, 2019
1 parent bc7a2bd commit 5064e46
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 140 deletions.
39 changes: 2 additions & 37 deletions src/scaffolder.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,8 @@
import chalk from 'chalk';
import writeYaml from '../third-party-wrappers/write-yaml';
import scaffoldSettings from './settings-scaffolder';

export function scaffold({vcs, projectRoot, projectType, description, homepage, visibility}) {
console.log(chalk.blue('Generating GitHub')); // eslint-disable-line no-console

return writeYaml(`${projectRoot}/.github/settings.yml`, {
repository: {
name: vcs.name,
description,
homepage,
private: 'Public' !== visibility,
has_wiki: false,
has_projects: false,
has_downloads: false,
allow_squash_merge: false,
allow_merge_commit: true,
allow_rebase_merge: true
},
labels: [
{name: 'bug', color: 'ee0701'},
{name: 'duplicate', color: 'cccccc'},
{name: 'enhancement', color: '84b6eb'},
{name: 'help wanted', color: '128A0C'},
{name: 'invalid', color: 'e6e6e6'},
{name: 'question', color: 'cc317c'},
{name: 'wontfix', color: 'ffffff'},
{name: 'breaking change', color: 'e0fc28'},
('JavaScript' === projectType) ? {name: 'greenkeeper', color: '00c775'} : undefined
].filter(Boolean),
branches: [
{
name: 'master',
protection: {
required_pull_request_reviews: null,
required_status_checks: null,
restrictions: null,
enforce_admins: true
}
}
]
});
return scaffoldSettings(projectRoot, vcs, description, homepage, visibility, projectType);
}
40 changes: 40 additions & 0 deletions src/settings-scaffolder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import writeYaml from '../third-party-wrappers/write-yaml';

export default function scaffoldSettings(projectRoot, vcs, description, homepage, visibility, projectType) {
return writeYaml(`${projectRoot}/.github/settings.yml`, {
repository: {
name: vcs.name,
description,
homepage,
private: 'Public' !== visibility,
has_wiki: false,
has_projects: false,
has_downloads: false,
allow_squash_merge: false,
allow_merge_commit: true,
allow_rebase_merge: true
},
labels: [
{name: 'bug', color: 'ee0701'},
{name: 'duplicate', color: 'cccccc'},
{name: 'enhancement', color: '84b6eb'},
{name: 'help wanted', color: '128A0C'},
{name: 'invalid', color: 'e6e6e6'},
{name: 'question', color: 'cc317c'},
{name: 'wontfix', color: 'ffffff'},
{name: 'breaking change', color: 'e0fc28'},
('JavaScript' === projectType) ? {name: 'greenkeeper', color: '00c775'} : undefined
].filter(Boolean),
branches: [
{
name: 'master',
protection: {
required_pull_request_reviews: null,
required_status_checks: null,
restrictions: null,
enforce_admins: true
}
}
]
});
}
111 changes: 8 additions & 103 deletions test/unit/scaffolder-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {assert} from 'chai';
import sinon from 'sinon';
import any from '@travi/any';
import * as yamlWriter from '../../third-party-wrappers/write-yaml';
import * as settingsSecaffolder from '../../src/settings-scaffolder';
import {scaffold} from '../../src/scaffolder';

suite('github', () => {
Expand All @@ -12,116 +12,21 @@ suite('github', () => {
setup(() => {
sandbox = sinon.createSandbox();

sandbox.stub(yamlWriter, 'default');
sandbox.stub(settingsSecaffolder, 'default');
});

teardown(() => sandbox.restore());

test('that the settings file is produced', async () => {
const description = any.sentence();
const homepage = any.url();
yamlWriter.default.resolves();
const vcs = {name: projectName};
const projectType = any.word();
const visibility = any.word();
settingsSecaffolder.default.resolves();

await scaffold({projectRoot, vcs: {name: projectName}, description, homepage});
await scaffold({projectRoot, vcs, description, homepage, projectType, visibility});

assert.calledWith(
yamlWriter.default,
`${projectRoot}/.github/settings.yml`,
{
repository: {
name: projectName,
description,
homepage,
private: true,
has_wiki: false,
has_projects: false,
has_downloads: false,
allow_squash_merge: false,
allow_merge_commit: true,
allow_rebase_merge: true
},
labels: [
{name: 'bug', color: 'ee0701'},
{name: 'duplicate', color: 'cccccc'},
{name: 'enhancement', color: '84b6eb'},
{name: 'help wanted', color: '128A0C'},
{name: 'invalid', color: 'e6e6e6'},
{name: 'question', color: 'cc317c'},
{name: 'wontfix', color: 'ffffff'},
{name: 'breaking change', color: 'e0fc28'}
],
branches: [
{
name: 'master',
protection: {
required_pull_request_reviews: null,
required_status_checks: null,
restrictions: null,
enforce_admins: true
}
}
]
}
);
});

test('that the greenkeeper label is defined for javascript projects', async () => {
yamlWriter.default.resolves();

await scaffold({vcs: {}, projectRoot, projectType: 'JavaScript'});

assert.calledWith(
yamlWriter.default,
`${projectRoot}/.github/settings.yml`,
sinon.match({
labels: [
{name: 'bug', color: 'ee0701'},
{name: 'duplicate', color: 'cccccc'},
{name: 'enhancement', color: '84b6eb'},
{name: 'help wanted', color: '128A0C'},
{name: 'invalid', color: 'e6e6e6'},
{name: 'question', color: 'cc317c'},
{name: 'wontfix', color: 'ffffff'},
{name: 'breaking change', color: 'e0fc28'},
{name: 'greenkeeper', color: '00c775'}
]
})
);
});

test('that the repository is marked as private when the visibility is `Private`', async () => {
yamlWriter.default.resolves();

await scaffold({vcs: {}, projectRoot, projectType: any.word(), visibility: 'Private'});

assert.calledWith(
yamlWriter.default,
`${projectRoot}/.github/settings.yml`,
sinon.match({repository: {private: true}})
);
});

test('that the repository is marked as not private when the visibility is `Public`', async () => {
yamlWriter.default.resolves();

await scaffold({vcs: {}, projectRoot, projectType: any.word(), visibility: 'Public'});

assert.calledWith(
yamlWriter.default,
`${projectRoot}/.github/settings.yml`,
sinon.match({repository: {private: false}})
);
});

test('that the repository is marked as private when the visibility is not specified', async () => {
yamlWriter.default.resolves();

await scaffold({vcs: {}, projectRoot, projectType: any.word()});

assert.calledWith(
yamlWriter.default,
`${projectRoot}/.github/settings.yml`,
sinon.match({repository: {private: true}})
);
assert.calledWith(settingsSecaffolder.default, projectRoot, vcs, description, homepage, visibility, projectType);
});
});
127 changes: 127 additions & 0 deletions test/unit/settings-scaffolder-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import {assert} from 'chai';
import sinon from 'sinon';
import any from '@travi/any';
import * as yamlWriter from '../../third-party-wrappers/write-yaml';
import scaffoldSettings from '../../src/settings-scaffolder';

suite('settings', () => {
let sandbox;
const projectRoot = any.string();
const projectName = any.string();

setup(() => {
sandbox = sinon.createSandbox();

sandbox.stub(yamlWriter, 'default');
});

teardown(() => sandbox.restore());

test('that the settings file is produced', async () => {
const description = any.sentence();
const homepage = any.url();
yamlWriter.default.resolves();

await scaffoldSettings(projectRoot, {name: projectName}, description, homepage);

assert.calledWith(
yamlWriter.default,
`${projectRoot}/.github/settings.yml`,
{
repository: {
name: projectName,
description,
homepage,
private: true,
has_wiki: false,
has_projects: false,
has_downloads: false,
allow_squash_merge: false,
allow_merge_commit: true,
allow_rebase_merge: true
},
labels: [
{name: 'bug', color: 'ee0701'},
{name: 'duplicate', color: 'cccccc'},
{name: 'enhancement', color: '84b6eb'},
{name: 'help wanted', color: '128A0C'},
{name: 'invalid', color: 'e6e6e6'},
{name: 'question', color: 'cc317c'},
{name: 'wontfix', color: 'ffffff'},
{name: 'breaking change', color: 'e0fc28'}
],
branches: [
{
name: 'master',
protection: {
required_pull_request_reviews: null,
required_status_checks: null,
restrictions: null,
enforce_admins: true
}
}
]
}
);
});

test('that the greenkeeper label is defined for javascript projects', async () => {
yamlWriter.default.resolves();

await scaffoldSettings(projectRoot, {}, null, null, null, 'JavaScript');

assert.calledWith(
yamlWriter.default,
`${projectRoot}/.github/settings.yml`,
sinon.match({
labels: [
{name: 'bug', color: 'ee0701'},
{name: 'duplicate', color: 'cccccc'},
{name: 'enhancement', color: '84b6eb'},
{name: 'help wanted', color: '128A0C'},
{name: 'invalid', color: 'e6e6e6'},
{name: 'question', color: 'cc317c'},
{name: 'wontfix', color: 'ffffff'},
{name: 'breaking change', color: 'e0fc28'},
{name: 'greenkeeper', color: '00c775'}
]
})
);
});

test('that the repository is marked as private when the visibility is `Private`', async () => {
yamlWriter.default.resolves();

await scaffoldSettings(projectRoot, {}, null, null, 'Private', any.word());

assert.calledWith(
yamlWriter.default,
`${projectRoot}/.github/settings.yml`,
sinon.match({repository: {private: true}})
);
});

test('that the repository is marked as not private when the visibility is `Public`', async () => {
yamlWriter.default.resolves();

await scaffoldSettings(projectRoot, {}, null, null, 'Public', any.word());

assert.calledWith(
yamlWriter.default,
`${projectRoot}/.github/settings.yml`,
sinon.match({repository: {private: false}})
);
});

test('that the repository is marked as private when the visibility is not specified', async () => {
yamlWriter.default.resolves();

await scaffoldSettings(projectRoot, {}, null, null, null, any.word());

assert.calledWith(
yamlWriter.default,
`${projectRoot}/.github/settings.yml`,
sinon.match({repository: {private: true}})
);
});
});

0 comments on commit 5064e46

Please sign in to comment.