Comparing version 1.0.3 to 1.1.0
{ | ||
"name": "get-ci-env", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "Get and normalize continuous integration service environment variables.", | ||
@@ -13,4 +13,3 @@ "main": "src/main.js", | ||
"format": "prettier --write --single-quote --trailing-comma all src/*.js", | ||
"preversion": "npm test", | ||
"postversion": "git push --follow-tags && npm publish" | ||
"preversion": "npm test" | ||
}, | ||
@@ -22,2 +21,5 @@ "dependencies": {}, | ||
}, | ||
"engines": { | ||
"node": ">= 8" | ||
}, | ||
"repository": { | ||
@@ -35,4 +37,5 @@ "type": "git", | ||
"circleci", | ||
"codefresh", | ||
"travis ci" | ||
] | ||
} |
# get-ci-env | ||
[![Travis Build Status][travis-icon]][travis] | ||
[![npm][npm-icon]][npm] | ||
[![David devDependencies Status][david-dev-icon]][david-dev] | ||
[![LGTM Grade][lgtm-icon]][lgtm] | ||
@@ -32,4 +34,5 @@ Get and normalize the environment variables from continuous integration services. This package is similar to [pvdlg's env-ci](https://github.com/pvdlg/env-ci) but goes for a simpler approach. This results in a more restricted and lightweight (zero dependencies) package: | ||
## Supported services | ||
- Travis CI | ||
- CircleCI | ||
- [CircleCI](https://circleci.com/) | ||
- [Codefresh](https://codefresh.io/) | ||
- [Travis CI](https://travis-ci.com/) | ||
@@ -56,3 +59,7 @@ ## Variables | ||
[travis-icon]: https://img.shields.io/travis/Siilwyn/get-ci-env/master.svg?style=flat-square | ||
[david-dev]: https://david-dm.org/Siilwyn/get-ci-env#info=devDependencies | ||
[david-dev]: https://david-dm.org/Siilwyn/get-ci-env?type=dev | ||
[david-dev-icon]: https://img.shields.io/david/dev/Siilwyn/get-ci-env.svg?style=flat-square | ||
[npm]: https://www.npmjs.com/package/get-ci-env | ||
[npm-icon]: https://img.shields.io/npm/v/get-ci-env.svg?style=flat-square | ||
[lgtm]: https://lgtm.com/projects/g/Siilwyn/get-ci-env/ | ||
[lgtm-icon]: https://img.shields.io/lgtm/grade/javascript/g/Siilwyn/get-ci-env.svg?style=flat-square |
'use strict'; | ||
module.exports = (env = process.env) => | ||
getCiService(env).then(service => normalizeCiEnv(env, service)); | ||
getCiService(env).then(service => normalizeCiEnv(envMapping(env), service)); | ||
const envMapping = env => ({ | ||
detection: { | ||
circleCi: 'CIRCLECI', | ||
codefresh: 'CF_BUILD_ID', | ||
travis: 'TRAVIS', | ||
}, | ||
branch: { | ||
circleCi: env.CIRCLE_BRANCH, | ||
codefresh: env.CF_BRANCH, | ||
travis: env.TRAVIS_PULL_REQUEST_BRANCH || env.TRAVIS_BRANCH, | ||
@@ -13,2 +19,3 @@ }, | ||
circleCi: env.CIRCLE_SHA1, | ||
codefresh: env.CF_REVISION, | ||
travis: env.TRAVIS_PULL_REQUEST_SHA || env.TRAVIS_COMMIT, | ||
@@ -18,2 +25,3 @@ }, | ||
circleCi: env.CIRCLE_PROJECT_USERNAME, | ||
codefresh: env.CF_REPO_OWNER, | ||
travis: env.TRAVIS_REPO_SLUG && env.TRAVIS_REPO_SLUG.split('/')[0], | ||
@@ -23,7 +31,14 @@ }, | ||
circleCi: env.CIRCLE_PROJECT_REPONAME, | ||
codefresh: env.CF_REPO_NAME, | ||
travis: env.TRAVIS_REPO_SLUG && env.TRAVIS_REPO_SLUG.split('/')[1], | ||
}, | ||
isPr: { | ||
circleCi: env.CIRCLE_PULL_REQUEST, | ||
codefresh: env.CF_PULL_REQUEST_NUMBER, | ||
travis: env.TRAVIS_PULL_REQUEST, | ||
}, | ||
prNumber: { | ||
circleCi: | ||
env.CIRCLE_PULL_REQUEST && env.CIRCLE_PULL_REQUEST.split('/').pop(), | ||
codefresh: env.CF_PULL_REQUEST_NUMBER, | ||
travis: env.TRAVIS_PULL_REQUEST, | ||
@@ -34,33 +49,34 @@ }, | ||
function getCiService(env) { | ||
const service = Object.entries({ | ||
travis: env.TRAVIS, | ||
circleCi: env.CIRCLECI, | ||
}).find(([service, match]) => match); | ||
const service = Object.entries(envMapping(env).detection).find( | ||
([service, match]) => env[match], | ||
); | ||
return service | ||
? Promise.resolve(service[0]) | ||
: Promise.reject('No matching CI service environment.'); | ||
: Promise.reject( | ||
`No supported CI service found, none of the following environment value is set: ${Object.values( | ||
envMapping(env).detection, | ||
).join(', ')}.`, | ||
); | ||
} | ||
function normalizeCiEnv(env, service) { | ||
const variableMapping = envMapping(env); | ||
function normalizeCiEnv(ciEnvMapping, service) { | ||
return { | ||
service, | ||
branch: variableMapping.branch[service], | ||
commit: variableMapping.commit[service], | ||
branch: ciEnvMapping.branch[service], | ||
commit: ciEnvMapping.commit[service], | ||
repo: { | ||
owner: variableMapping.repoOwner[service], | ||
name: variableMapping.repoName[service], | ||
owner: ciEnvMapping.repoOwner[service], | ||
name: ciEnvMapping.repoName[service], | ||
}, | ||
...getPrInfo(env, service, variableMapping), | ||
...getPrInfo(ciEnvMapping, service), | ||
}; | ||
} | ||
function getPrInfo(env, service, variableMapping) { | ||
if (!isPr(env)) return; | ||
function getPrInfo(ciEnvMapping, service) { | ||
if (!isTruthyCiVariable(ciEnvMapping.isPr[service])) return; | ||
return { | ||
pr: { | ||
number: variableMapping.prNumber[service], | ||
number: ciEnvMapping.prNumber[service], | ||
}, | ||
@@ -70,6 +86,4 @@ }; | ||
function isPr(env) { | ||
return [env.CIRCLE_PULL_REQUEST, env.TRAVIS_PULL_REQUEST].some( | ||
ciVariable => ciVariable && ciVariable !== 'false', | ||
); | ||
function isTruthyCiVariable(ciVariable) { | ||
return ciVariable && ciVariable !== 'false'; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38161
5
76
64