Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion workspaces/config/lib/definitions/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2476,7 +2476,25 @@ const definitions = {
`,
flatten (key, obj, flatOptions) {
const value = obj[key]
const ciName = ciInfo.name?.toLowerCase().split(' ').join('-') || null
let ciName = ciInfo.name?.toLowerCase().split(' ').join('-') || null
Comment thread
reggi marked this conversation as resolved.
Outdated
if (ciInfo.GITHUB_ACTIONS) {
// Env vars can be absent, empty, or whitespace; normalize before use.
const serverUrl = (process.env.GITHUB_SERVER_URL || '').trim()
const runnerEnv = (process.env.RUNNER_ENVIRONMENT || '').trim()
if (serverUrl === 'https://github.com') {
Comment thread
reggi marked this conversation as resolved.
Outdated
if (runnerEnv === 'github-hosted') {
ciName = 'github-actions-dotcom-hosted'
} else if (runnerEnv === 'self-hosted') {
ciName = 'github-actions-dotcom-selfhosted'
} else {
ciName = 'github-actions-dotcom'
}
} else if (serverUrl.endsWith('.ghe.com')) {
Comment thread
reggi marked this conversation as resolved.
Outdated
ciName = 'github-actions-ghecom'
} else if (serverUrl) {
ciName = 'github-actions-ghes'
}
}
let inWorkspaces = false
if (obj.workspaces || obj.workspace && obj.workspace.length) {
inWorkspaces = true
Expand Down
88 changes: 88 additions & 0 deletions workspaces/config/test/definitions/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,94 @@ t.test('user-agent', t => {
t.end()
})

t.test('user-agent github actions ci variants', t => {
const npmVersion = '1.2.3'
const base = `npm/${npmVersion} node/${process.version} ` +
`${process.platform} ${process.arch} workspaces/false`

const cases = [
{
name: 'dotcom + github-hosted runner',
env: { GITHUB_SERVER_URL: 'https://github.com', RUNNER_ENVIRONMENT: 'github-hosted' },
expect: `${base} ci/github-actions-dotcom-hosted`,
},
{
name: 'dotcom + self-hosted runner',
env: { GITHUB_SERVER_URL: 'https://github.com', RUNNER_ENVIRONMENT: 'self-hosted' },
expect: `${base} ci/github-actions-dotcom-selfhosted`,
},
{
name: 'dotcom + missing runner environment',
env: { GITHUB_SERVER_URL: 'https://github.com', RUNNER_ENVIRONMENT: undefined },
expect: `${base} ci/github-actions-dotcom`,
},
{
name: 'dotcom + whitespace runner environment',
env: { GITHUB_SERVER_URL: 'https://github.com', RUNNER_ENVIRONMENT: ' ' },
expect: `${base} ci/github-actions-dotcom`,
},
{
name: 'ghe.com tenant',
env: { GITHUB_SERVER_URL: 'https://octocorp.ghe.com', RUNNER_ENVIRONMENT: 'github-hosted' },
expect: `${base} ci/github-actions-ghecom`,
},
{
name: 'ghes (non-empty, non github.com, non ghe.com)',
env: { GITHUB_SERVER_URL: 'https://github.example.com', RUNNER_ENVIRONMENT: 'self-hosted' },
expect: `${base} ci/github-actions-ghes`,
},
{
name: 'missing server url stays generic',
env: { GITHUB_SERVER_URL: undefined, RUNNER_ENVIRONMENT: undefined },
expect: `${base} ci/github-actions`,
},
{
name: 'whitespace server url stays generic',
env: { GITHUB_SERVER_URL: ' ', RUNNER_ENVIRONMENT: 'github-hosted' },
expect: `${base} ci/github-actions`,
},
]

for (const { name, env, expect } of cases) {
t.test(name, t => {
mockGlobals(t, { 'process.env': env })
const obj = {
'npm-version': npmVersion,
'user-agent': mockDefs({
'ci-info': { isCi: true, name: 'GitHub Actions', GITHUB_ACTIONS: true },
})['user-agent'].default,
}
const flat = {}
mockDefs({
Comment thread
reggi marked this conversation as resolved.
Outdated
'ci-info': { isCi: true, name: 'GitHub Actions', GITHUB_ACTIONS: true },
})['user-agent'].flatten('user-agent', obj, flat)
t.equal(flat.userAgent, expect)
t.equal(process.env.npm_config_user_agent, flat.userAgent, 'npm_user_config environment is set')
t.end()
})
}

t.test('non github-actions ci is unchanged', t => {
mockGlobals(t, {
'process.env': { GITHUB_SERVER_URL: 'https://github.com', RUNNER_ENVIRONMENT: 'github-hosted' },
})
const obj = {
'npm-version': npmVersion,
'user-agent': mockDefs({
'ci-info': { isCi: true, name: 'Travis CI', GITHUB_ACTIONS: false },
})['user-agent'].default,
}
const flat = {}
mockDefs({
'ci-info': { isCi: true, name: 'Travis CI', GITHUB_ACTIONS: false },
})['user-agent'].flatten('user-agent', obj, flat)
t.equal(flat.userAgent, `${base} ci/travis-ci`)
t.end()
})

t.end()
})

t.test('save-prefix', t => {
const obj = {
'save-exact': true,
Expand Down
Loading