diff --git a/index.js b/index.js index 02e4c72..1d7a810 100644 --- a/index.js +++ b/index.js @@ -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()} = {}) => { diff --git a/lib/git.js b/lib/git.js index e81c8ed..90e176b 100644 --- a/lib/git.js +++ b/lib/git.js @@ -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; @@ -33,4 +26,4 @@ function branch(options) { } } -module.exports = {configuration, head, branch}; +module.exports = {head, branch}; diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 0000000..5532e1c --- /dev/null +++ b/lib/utils.js @@ -0,0 +1,5 @@ +function prNumber(pr) { + return (/\d+(?!.*\d+)/.exec(pr) || [])[0]; +} + +module.exports = {prNumber}; diff --git a/package.json b/package.json index 3f499cd..5bb2277 100644 --- a/package.json +++ b/package.json @@ -24,8 +24,9 @@ "node": ">=6" }, "files": [ + "index.js", "lib", - "index.js" + "services" ], "homepage": "https://github.com/pvdlg/env-ci#readme", "keywords": [ diff --git a/lib/appveyor.js b/services/appveyor.js similarity index 100% rename from lib/appveyor.js rename to services/appveyor.js diff --git a/lib/bamboo.js b/services/bamboo.js similarity index 100% rename from lib/bamboo.js rename to services/bamboo.js diff --git a/lib/bitbucket.js b/services/bitbucket.js similarity index 100% rename from lib/bitbucket.js rename to services/bitbucket.js diff --git a/lib/bitrise.js b/services/bitrise.js similarity index 100% rename from lib/bitrise.js rename to services/bitrise.js diff --git a/lib/buddy.js b/services/buddy.js similarity index 84% rename from lib/buddy.js rename to services/buddy.js index 38eef7e..764d5f2 100644 --- a/lib/buddy.js +++ b/services/buddy.js @@ -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 { diff --git a/lib/buildkite.js b/services/buildkite.js similarity index 100% rename from lib/buildkite.js rename to services/buildkite.js diff --git a/lib/circleci.js b/services/circleci.js similarity index 81% rename from lib/circleci.js rename to services/circleci.js index 0a076dc..cffdcc0 100644 --- a/lib/circleci.js +++ b/services/circleci.js @@ -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 { diff --git a/lib/cirrus.js b/services/cirrus.js similarity index 100% rename from lib/cirrus.js rename to services/cirrus.js diff --git a/lib/codebuild.js b/services/codebuild.js similarity index 91% rename from lib/codebuild.js rename to services/codebuild.js index cc25457..2f9a2e8 100644 --- a/lib/codebuild.js +++ b/services/codebuild.js @@ -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 diff --git a/lib/codefresh.js b/services/codefresh.js similarity index 100% rename from lib/codefresh.js rename to services/codefresh.js diff --git a/lib/codeship.js b/services/codeship.js similarity index 100% rename from lib/codeship.js rename to services/codeship.js diff --git a/lib/drone.js b/services/drone.js similarity index 100% rename from lib/drone.js rename to services/drone.js diff --git a/services/git.js b/services/git.js new file mode 100644 index 0000000..4eb06c8 --- /dev/null +++ b/services/git.js @@ -0,0 +1,7 @@ +const {head, branch} = require('../lib/git'); + +module.exports = { + configuration(options) { + return {commit: head(options), branch: branch(options)}; + }, +}; diff --git a/lib/gitlab.js b/services/gitlab.js similarity index 100% rename from lib/gitlab.js rename to services/gitlab.js diff --git a/lib/jenkins.js b/services/jenkins.js similarity index 95% rename from lib/jenkins.js rename to services/jenkins.js index 9638d1a..12a8d8d 100644 --- a/lib/jenkins.js +++ b/services/jenkins.js @@ -1,4 +1,4 @@ -const {head} = require('./git'); +const {head} = require('../lib/git'); // https://wiki.jenkins.io/display/JENKINS/Building+a+software+project diff --git a/lib/sail.js b/services/sail.js similarity index 100% rename from lib/sail.js rename to services/sail.js diff --git a/lib/semaphore.js b/services/semaphore.js similarity index 93% rename from lib/semaphore.js rename to services/semaphore.js index d56b770..f3d0062 100644 --- a/lib/semaphore.js +++ b/services/semaphore.js @@ -1,4 +1,4 @@ -const {head} = require('./git'); +const {head} = require('../lib/git'); // https://semaphoreci.com/docs/available-environment-variables.html diff --git a/lib/shippable.js b/services/shippable.js similarity index 100% rename from lib/shippable.js rename to services/shippable.js diff --git a/lib/teamcity.js b/services/teamcity.js similarity index 100% rename from lib/teamcity.js rename to services/teamcity.js diff --git a/lib/travis.js b/services/travis.js similarity index 100% rename from lib/travis.js rename to services/travis.js diff --git a/lib/vsts.js b/services/vsts.js similarity index 100% rename from lib/vsts.js rename to services/vsts.js diff --git a/lib/wercker.js b/services/wercker.js similarity index 100% rename from lib/wercker.js rename to services/wercker.js diff --git a/test/git.test.js b/test/lib/git.test.js similarity index 58% rename from test/git.test.js rename to test/lib/git.test.js index 618cec8..39590ee 100644 --- a/test/git.test.js +++ b/test/lib/git.test.js @@ -1,18 +1,20 @@ 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 => { @@ -20,12 +22,14 @@ test('Git local repository with detached head', async t => { 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'); }); diff --git a/test/lib/utils.test.js b/test/lib/utils.test.js new file mode 100644 index 0000000..57e6c9b --- /dev/null +++ b/test/lib/utils.test.js @@ -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'); +}); diff --git a/test/appveyor.test.js b/test/services/appveyor.test.js similarity index 96% rename from test/appveyor.test.js rename to test/services/appveyor.test.js index 1314b40..229c3d3 100644 --- a/test/appveyor.test.js +++ b/test/services/appveyor.test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import appveyor from '../lib/appveyor'; +import appveyor from '../../services/appveyor'; const env = { APPVEYOR: 'true', diff --git a/test/bamboo.test.js b/test/services/bamboo.test.js similarity index 93% rename from test/bamboo.test.js rename to test/services/bamboo.test.js index c71e6de..3f473ad 100644 --- a/test/bamboo.test.js +++ b/test/services/bamboo.test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import bamboo from '../lib/bamboo'; +import bamboo from '../../services/bamboo'; /* eslint-disable camelcase */ diff --git a/test/bitbucket.test.js b/test/services/bitbucket.test.js similarity index 91% rename from test/bitbucket.test.js rename to test/services/bitbucket.test.js index d0329bb..aec4d3f 100644 --- a/test/bitbucket.test.js +++ b/test/services/bitbucket.test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import bitbucket from '../lib/bitbucket'; +import bitbucket from '../../services/bitbucket'; const env = { BITBUCKET_COMMIT: 'b5ce5ce', diff --git a/test/bitrise.test.js b/test/services/bitrise.test.js similarity index 95% rename from test/bitrise.test.js rename to test/services/bitrise.test.js index 4fd4e6a..93d44ef 100644 --- a/test/bitrise.test.js +++ b/test/services/bitrise.test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import bitrise from '../lib/bitrise'; +import bitrise from '../../services/bitrise'; const env = { BITRISE_IO: 'true', diff --git a/test/buddy.test.js b/test/services/buddy.test.js similarity index 96% rename from test/buddy.test.js rename to test/services/buddy.test.js index 89f41dd..e13606e 100644 --- a/test/buddy.test.js +++ b/test/services/buddy.test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import buddy from '../lib/buddy'; +import buddy from '../../services/buddy'; const env = { BUDDY_WORKSPACE_ID: '111', diff --git a/test/buildkite.test.js b/test/services/buildkite.test.js similarity index 96% rename from test/buildkite.test.js rename to test/services/buildkite.test.js index 7bc9852..25fe584 100644 --- a/test/buildkite.test.js +++ b/test/services/buildkite.test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import buildkite from '../lib/buildkite'; +import buildkite from '../../services/buildkite'; const env = { BUILDKITE: 'true', diff --git a/test/circleci.test.js b/test/services/circleci.test.js similarity index 97% rename from test/circleci.test.js rename to test/services/circleci.test.js index a888365..f70fb0b 100644 --- a/test/circleci.test.js +++ b/test/services/circleci.test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import circle from '../lib/circleci'; +import circle from '../../services/circleci'; const env = { CIRCLECI: 'true', diff --git a/test/cirrus.test.js b/test/services/cirrus.test.js similarity index 96% rename from test/cirrus.test.js rename to test/services/cirrus.test.js index 4dc59d5..af40447 100644 --- a/test/cirrus.test.js +++ b/test/services/cirrus.test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import cirrus from '../lib/cirrus'; +import cirrus from '../../services/cirrus'; const env = { CIRRUS_CI: 'true', diff --git a/test/codebuild.test.js b/test/services/codebuild.test.js similarity index 86% rename from test/codebuild.test.js rename to test/services/codebuild.test.js index 024c7cd..14b495c 100644 --- a/test/codebuild.test.js +++ b/test/services/codebuild.test.js @@ -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', diff --git a/test/codefresh.test.js b/test/services/codefresh.test.js similarity index 95% rename from test/codefresh.test.js rename to test/services/codefresh.test.js index 1485849..d8ff9ec 100644 --- a/test/codefresh.test.js +++ b/test/services/codefresh.test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import codefresh from '../lib/codefresh'; +import codefresh from '../../services/codefresh'; const env = { CF_BUILD_ID: '91011', diff --git a/test/codeship.test.js b/test/services/codeship.test.js similarity index 90% rename from test/codeship.test.js rename to test/services/codeship.test.js index a63c24a..3c7fd60 100644 --- a/test/codeship.test.js +++ b/test/services/codeship.test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import codeship from '../lib/codeship'; +import codeship from '../../services/codeship'; const env = { CI_NAME: 'codeship', diff --git a/test/drone.test.js b/test/services/drone.test.js similarity index 95% rename from test/drone.test.js rename to test/services/drone.test.js index fefb647..27c7d52 100644 --- a/test/drone.test.js +++ b/test/services/drone.test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import drone from '../lib/drone'; +import drone from '../../services/drone'; const env = { DRONE: 'true', diff --git a/test/services/git.test.js b/test/services/git.test.js new file mode 100644 index 0000000..11dadf1 --- /dev/null +++ b/test/services/git.test.js @@ -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'}); +}); diff --git a/test/gitlab.test.js b/test/services/gitlab.test.js similarity index 93% rename from test/gitlab.test.js rename to test/services/gitlab.test.js index d4b71d8..c0294f9 100644 --- a/test/gitlab.test.js +++ b/test/services/gitlab.test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import gitlab from '../lib/gitlab'; +import gitlab from '../../services/gitlab'; const env = { GITLAB_CI: 'true', diff --git a/test/jenkins.test.js b/test/services/jenkins.test.js similarity index 97% rename from test/jenkins.test.js rename to test/services/jenkins.test.js index a26248c..df1436c 100644 --- a/test/jenkins.test.js +++ b/test/services/jenkins.test.js @@ -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/', diff --git a/test/sail.test.js b/test/services/sail.test.js similarity index 95% rename from test/sail.test.js rename to test/services/sail.test.js index 6c9c64d..e72431d 100644 --- a/test/sail.test.js +++ b/test/services/sail.test.js @@ -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', diff --git a/test/semaphore.test.js b/test/services/semaphore.test.js similarity index 90% rename from test/semaphore.test.js rename to test/services/semaphore.test.js index 2b21e0b..0ba2214 100644 --- a/test/semaphore.test.js +++ b/test/services/semaphore.test.js @@ -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', diff --git a/test/shippable.test.js b/test/services/shippable.test.js similarity index 95% rename from test/shippable.test.js rename to test/services/shippable.test.js index 11786e1..93e3e9f 100644 --- a/test/shippable.test.js +++ b/test/services/shippable.test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import shippable from '../lib/shippable'; +import shippable from '../../services/shippable'; const env = { SHIPPABLE: 'true', diff --git a/test/teamcity.test.js b/test/services/teamcity.test.js similarity index 95% rename from test/teamcity.test.js rename to test/services/teamcity.test.js index 405f790..55db9e4 100644 --- a/test/teamcity.test.js +++ b/test/services/teamcity.test.js @@ -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)', diff --git a/test/travis.test.js b/test/services/travis.test.js similarity index 95% rename from test/travis.test.js rename to test/services/travis.test.js index b7db7c9..ca77500 100644 --- a/test/travis.test.js +++ b/test/services/travis.test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import travis from '../lib/travis'; +import travis from '../../services/travis'; const env = { TRAVIS: 'true', diff --git a/test/vsts.test.js b/test/services/vsts.test.js similarity index 95% rename from test/vsts.test.js rename to test/services/vsts.test.js index 91731c5..cb2f5c6 100644 --- a/test/vsts.test.js +++ b/test/services/vsts.test.js @@ -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', diff --git a/test/wercker.test.js b/test/services/wercker.test.js similarity index 92% rename from test/wercker.test.js rename to test/services/wercker.test.js index edde835..cf5d657 100644 --- a/test/wercker.test.js +++ b/test/services/wercker.test.js @@ -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',