From 0b25efc6ebbd311e10ec0fae6206eb1c6e205fcc Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 10 Sep 2019 19:28:01 -0700 Subject: [PATCH] Add CI environment variables to user-agent The `npm-in-ci` header has been sent for some time now by make-fetch-happen, but User-Agent is more reliably logged and respected by proxies and CDNs, so that's a better place to put this. --- lib/config/defaults.js | 3 ++- lib/npm.js | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/config/defaults.js b/lib/config/defaults.js index f563357d4cba3..1ee307580fc14 100644 --- a/lib/config/defaults.js +++ b/lib/config/defaults.js @@ -201,7 +201,8 @@ Object.defineProperty(exports, 'defaults', {get: function () { 'user-agent': 'npm/{npm-version} ' + 'node/{node-version} ' + '{platform} ' + - '{arch}', + '{arch} ' + + '{ci}', 'read-only': false, 'rebuild-bundle': true, registry: 'https://registry.npmjs.org/', diff --git a/lib/npm.js b/lib/npm.js index 2ee9a991264c7..7ef46e8b29357 100644 --- a/lib/npm.js +++ b/lib/npm.js @@ -281,7 +281,27 @@ ua = ua.replace(/\{npm-version\}/gi, npm.version) ua = ua.replace(/\{platform\}/gi, process.platform) ua = ua.replace(/\{arch\}/gi, process.arch) - config.set('user-agent', ua) + + // continuous integration platforms + const ci = process.env.GERRIT_PROJECT ? 'ci/gerrit' + : process.env.GITLAB_CI ? 'ci/gitlab' + : process.env.CIRCLECI ? 'ci/circle-ci' + : process.env.SEMAPHORE ? 'ci/semaphore' + : process.env.DRONE ? 'ci/drone' + : process.env.GITHUB_ACTION ? 'ci/github-actions' + : process.env.TDDIUM ? 'ci/tddium' + : process.env.JENKINS_URL ? 'ci/jenkins' + : process.env['bamboo.buildKey'] ? 'ci/bamboo' + : process.env.GO_PIPELINE_NAME ? 'ci/gocd' + // codeship and a few others + : process.env.CI_NAME ? `ci/${process.env.CI_NAME}` + // test travis last, since many of these mimic it + : process.env.TRAVIS ? 'ci/travis-ci' + : process.env.CI === 'true' || process.env.CI === '1' ? 'ci/custom' + : '' + ua = ua.replace(/\{ci\}/gi, ci) + + config.set('user-agent', ua.trim()) if (config.get('metrics-registry') == null) { config.set('metrics-registry', config.get('registry'))