Skip to content

Commit

Permalink
Add CI environment variables to user-agent
Browse files Browse the repository at this point in the history
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.

PR-URL: #249
Credit: @isaacs
Close: #249
Reviewed-by: @isaacs
  • Loading branch information
isaacs committed Sep 30, 2019
1 parent bbc92fb commit ed993a2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/',
Expand Down
22 changes: 21 additions & 1 deletion lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down

0 comments on commit ed993a2

Please sign in to comment.