Skip to content

Commit

Permalink
refactor: separate services files and global utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Sep 19, 2018
1 parent f473d26 commit 88856f3
Show file tree
Hide file tree
Showing 50 changed files with 97 additions and 65 deletions.
44 changes: 22 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
'use strict';

const process = require('process');
const git = require('./lib/git');
const git = require('./services/git');

const services = {
appveyor: require('./lib/appveyor'),
bamboo: require('./lib/bamboo'),
bitbucket: require('./lib/bitbucket'),
bitrise: require('./lib/bitrise'),
buddy: require('./lib/buddy'),
buildkite: require('./lib/buildkite'),
circleci: require('./lib/circleci'),
cirrus: require('./lib/cirrus'),
codebuild: require('./lib/codebuild'),
codefresh: require('./lib/codefresh'),
codeship: require('./lib/codeship'),
drone: require('./lib/drone'),
gitlab: require('./lib/gitlab'),
jenkins: require('./lib/jenkins'),
sail: require('./lib/sail'),
semaphore: require('./lib/semaphore'),
shippable: require('./lib/shippable'),
teamcity: require('./lib/teamcity'),
travis: require('./lib/travis'),
vsts: require('./lib/vsts'),
wercker: require('./lib/wercker'),
appveyor: require('./services/appveyor'),
bamboo: require('./services/bamboo'),
bitbucket: require('./services/bitbucket'),
bitrise: require('./services/bitrise'),
buddy: require('./services/buddy'),
buildkite: require('./services/buildkite'),
circleci: require('./services/circleci'),
cirrus: require('./services/cirrus'),
codebuild: require('./services/codebuild'),
codefresh: require('./services/codefresh'),
codeship: require('./services/codeship'),
drone: require('./services/drone'),
gitlab: require('./services/gitlab'),
jenkins: require('./services/jenkins'),
sail: require('./services/sail'),
semaphore: require('./services/semaphore'),
shippable: require('./services/shippable'),
teamcity: require('./services/teamcity'),
travis: require('./services/travis'),
vsts: require('./services/vsts'),
wercker: require('./services/wercker'),
};

module.exports = ({env = process.env, cwd = process.cwd()} = {}) => {
Expand Down
9 changes: 1 addition & 8 deletions lib/git.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
const execa = require('execa');

function configuration(options) {
return {
commit: head(options),
branch: branch(options),
};
}

function head(options) {
try {
return execa.sync('git', ['rev-parse', 'HEAD'], options).stdout;
Expand All @@ -33,4 +26,4 @@ function branch(options) {
}
}

module.exports = {configuration, head, branch};
module.exports = {head, branch};
5 changes: 5 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function prNumber(pr) {
return (/\d+(?!.*\d+)/.exec(pr) || [])[0];
}

module.exports = {prNumber};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
"node": ">=6"
},
"files": [
"index.js",
"lib",
"index.js"
"services"
],
"homepage": "https://github.com/pvdlg/env-ci#readme",
"keywords": [
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion lib/buddy.js → services/buddy.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// https://buddy.works/knowledge/deployments/how-use-environment-variables#default-environment-variables

const {prNumber} = require('../lib/utils');

module.exports = {
detect({env}) {
return Boolean(env.BUDDY_WORKSPACE_ID);
},
configuration({env}) {
const pr = (/\d+(?!.*\d+)/.exec(env.BUDDY_EXECUTION_PULL_REQUEST_ID) || [])[0];
const pr = prNumber(env.BUDDY_EXECUTION_PULL_REQUEST_ID);
const isPr = Boolean(pr);

return {
Expand Down
File renamed without changes.
4 changes: 3 additions & 1 deletion lib/circleci.js → services/circleci.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables

const {prNumber} = require('../lib/utils');

module.exports = {
detect({env}) {
return Boolean(env.CIRCLECI);
},
configuration({env}) {
const pr = env.CIRCLE_PR_NUMBER || (/\d+(?!.*\d+)/.exec(env.CIRCLE_PULL_REQUEST || env.CI_PULL_REQUEST) || [])[0];
const pr = env.CIRCLE_PR_NUMBER || prNumber(env.CIRCLE_PULL_REQUEST || env.CI_PULL_REQUEST);
const isPr = Boolean(pr);

return {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/codebuild.js → services/codebuild.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {head, branch} = require('./git');
const {head, branch} = require('../lib/git');

// https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions services/git.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const {head, branch} = require('../lib/git');

module.exports = {
configuration(options) {
return {commit: head(options), branch: branch(options)};
},
};
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/jenkins.js → services/jenkins.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {head} = require('./git');
const {head} = require('../lib/git');

// https://wiki.jenkins.io/display/JENKINS/Building+a+software+project

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/semaphore.js → services/semaphore.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {head} = require('./git');
const {head} = require('../lib/git');

// https://semaphoreci.com/docs/available-environment-variables.html

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 10 additions & 6 deletions test/git.test.js → test/lib/git.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import test from 'ava';
import git from '../lib/git';
import {gitRepo, gitCommit, gitHead, gitCheckout} from './helpers/git-utils';
import {head, branch} from '../../lib/git';
import {gitRepo, gitCommit, gitHead, gitCheckout} from '../helpers/git-utils';

test('Git local repository', async t => {
const {cwd} = await gitRepo();
const commit = await gitCommit('Test commit message', {cwd});

t.deepEqual(git.configuration({cwd}), {commit, branch: 'master'});
t.is(head({cwd}), commit);
t.is(branch({cwd}), 'master');
});

test('Git cloned repository', async t => {
const {cwd} = await gitRepo(true);

t.deepEqual(git.configuration({cwd}), {commit: await gitHead({cwd}), branch: 'master'});
t.is(head({cwd}), await gitHead({cwd}));
t.is(branch({cwd}), 'master');
});

test('Git local repository with detached head', async t => {
const {cwd} = await gitRepo();
const commit = await gitCommit('Test commit message', {cwd});
await gitCheckout('HEAD~0', false, {cwd});

t.deepEqual(git.configuration({cwd}), {commit, branch: undefined});
t.is(head({cwd}), commit);
t.is(branch({cwd}), undefined);
});

test('Git cloned repository with detached head', async t => {
const {cwd} = await gitRepo(true);
await gitCheckout('HEAD~0', false, {cwd});

t.deepEqual(git.configuration({cwd}), {commit: await gitHead({cwd}), branch: 'master'});
t.is(head({cwd}), await gitHead({cwd}));
t.is(branch({cwd}), 'master');
});
8 changes: 8 additions & 0 deletions test/lib/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import test from 'ava';
import {prNumber} from '../../lib/utils';

test('prNumber', t => {
t.is(prNumber('https://github.com/owner/repo/pull/10'), '10');
t.is(prNumber('pull/10'), '10');
t.is(prNumber('https://gitlab.com/owner/repo/merge_requests/10'), '10');
});
2 changes: 1 addition & 1 deletion test/appveyor.test.js → test/services/appveyor.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import appveyor from '../lib/appveyor';
import appveyor from '../../services/appveyor';

const env = {
APPVEYOR: 'true',
Expand Down
2 changes: 1 addition & 1 deletion test/bamboo.test.js → test/services/bamboo.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import bamboo from '../lib/bamboo';
import bamboo from '../../services/bamboo';

/* eslint-disable camelcase */

Expand Down
2 changes: 1 addition & 1 deletion test/bitbucket.test.js → test/services/bitbucket.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import bitbucket from '../lib/bitbucket';
import bitbucket from '../../services/bitbucket';

const env = {
BITBUCKET_COMMIT: 'b5ce5ce',
Expand Down
2 changes: 1 addition & 1 deletion test/bitrise.test.js → test/services/bitrise.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import bitrise from '../lib/bitrise';
import bitrise from '../../services/bitrise';

const env = {
BITRISE_IO: 'true',
Expand Down
2 changes: 1 addition & 1 deletion test/buddy.test.js → test/services/buddy.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import buddy from '../lib/buddy';
import buddy from '../../services/buddy';

const env = {
BUDDY_WORKSPACE_ID: '111',
Expand Down
2 changes: 1 addition & 1 deletion test/buildkite.test.js → test/services/buildkite.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import buildkite from '../lib/buildkite';
import buildkite from '../../services/buildkite';

const env = {
BUILDKITE: 'true',
Expand Down
2 changes: 1 addition & 1 deletion test/circleci.test.js → test/services/circleci.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import circle from '../lib/circleci';
import circle from '../../services/circleci';

const env = {
CIRCLECI: 'true',
Expand Down
2 changes: 1 addition & 1 deletion test/cirrus.test.js → test/services/cirrus.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import cirrus from '../lib/cirrus';
import cirrus from '../../services/cirrus';

const env = {
CIRRUS_CI: 'true',
Expand Down
4 changes: 2 additions & 2 deletions test/codebuild.test.js → test/services/codebuild.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';
import codebuild from '../lib/codebuild';
import {gitRepo, gitHead} from './helpers/git-utils';
import codebuild from '../../services/codebuild';
import {gitRepo, gitHead} from '../helpers/git-utils';

const env = {
CODEBUILD_BUILD_ID: 'env-ci:40cc72d2-acd5-46f4-a86b-6a3dcd2a39a0',
Expand Down
2 changes: 1 addition & 1 deletion test/codefresh.test.js → test/services/codefresh.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import codefresh from '../lib/codefresh';
import codefresh from '../../services/codefresh';

const env = {
CF_BUILD_ID: '91011',
Expand Down
2 changes: 1 addition & 1 deletion test/codeship.test.js → test/services/codeship.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import codeship from '../lib/codeship';
import codeship from '../../services/codeship';

const env = {
CI_NAME: 'codeship',
Expand Down
2 changes: 1 addition & 1 deletion test/drone.test.js → test/services/drone.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import drone from '../lib/drone';
import drone from '../../services/drone';

const env = {
DRONE: 'true',
Expand Down
10 changes: 10 additions & 0 deletions test/services/git.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import test from 'ava';
import git from '../../services/git';
import {gitRepo, gitCommit} from '../helpers/git-utils';

test('Return "commit" and "branch" from local repository', async t => {
const {cwd} = await gitRepo();
const commit = await gitCommit('Test commit message', {cwd});

t.deepEqual(git.configuration({cwd}), {commit, branch: 'master'});
});
2 changes: 1 addition & 1 deletion test/gitlab.test.js → test/services/gitlab.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import gitlab from '../lib/gitlab';
import gitlab from '../../services/gitlab';

const env = {
GITLAB_CI: 'true',
Expand Down
2 changes: 1 addition & 1 deletion test/jenkins.test.js → test/services/jenkins.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import jenkins from '../lib/jenkins';
import jenkins from '../../services/jenkins';

const env = {
JENKINS_URL: 'http://jenkins.jenkins.example/',
Expand Down
2 changes: 1 addition & 1 deletion test/sail.test.js → test/services/sail.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import sail from '../lib/sail';
import sail from '../../services/sail';

const env = {
SAIL_COMMIT_SHA: 'full-commit-sha',
Expand Down
4 changes: 2 additions & 2 deletions test/semaphore.test.js → test/services/semaphore.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';
import semaphore from '../lib/semaphore';
import {gitRepo, gitCommit} from './helpers/git-utils';
import semaphore from '../../services/semaphore';
import {gitRepo, gitCommit} from '../helpers/git-utils';

const env = {
SEMAPHORE: 'true',
Expand Down
2 changes: 1 addition & 1 deletion test/shippable.test.js → test/services/shippable.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import shippable from '../lib/shippable';
import shippable from '../../services/shippable';

const env = {
SHIPPABLE: 'true',
Expand Down
2 changes: 1 addition & 1 deletion test/teamcity.test.js → test/services/teamcity.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import test from 'ava';
import tempy from 'tempy';
import teamcity from '../lib/teamcity';
import teamcity from '../../services/teamcity';

const env = {
TEAMCITY_VERSION: '2017.1.2 (build 46812)',
Expand Down
2 changes: 1 addition & 1 deletion test/travis.test.js → test/services/travis.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import travis from '../lib/travis';
import travis from '../../services/travis';

const env = {
TRAVIS: 'true',
Expand Down
2 changes: 1 addition & 1 deletion test/vsts.test.js → test/services/vsts.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import vsts from '../lib/vsts';
import vsts from '../../services/vsts';

const env = {
BUILD_BUILDURI: 'https://fabrikamfiber.visualstudio.com/_git/Scripts',
Expand Down
2 changes: 1 addition & 1 deletion test/wercker.test.js → test/services/wercker.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import wercker from '../lib/wercker';
import wercker from '../../services/wercker';

const env = {
WERCKER_MAIN_PIPELINE_STARTED: '123456',
Expand Down

0 comments on commit 88856f3

Please sign in to comment.