fix: prevent duplicate orchestration ID in user-agent
Add idempotency check to getUserAgentWithOrchestrationId — if the tag is already present in baseUserAgent, return it unchanged. This prevents doubling when both the exported helper and getOctokitOptions run for the same client.
This commit is contained in:
@@ -59,6 +59,14 @@ describe('orchestration ID support', () => {
|
|||||||
process.env['ACTIONS_ORCHESTRATION_ID'] = ' '
|
process.env['ACTIONS_ORCHESTRATION_ID'] = ' '
|
||||||
expect(getUserAgentWithOrchestrationId('my-app')).toBe('my-app')
|
expect(getUserAgentWithOrchestrationId('my-app')).toBe('my-app')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('does not duplicate orchestration ID if already present in base', () => {
|
||||||
|
process.env['ACTIONS_ORCHESTRATION_ID'] = 'abc-123'
|
||||||
|
const alreadyTagged = 'my-app actions_orchestration_id/abc-123'
|
||||||
|
expect(getUserAgentWithOrchestrationId(alreadyTagged)).toBe(
|
||||||
|
alreadyTagged
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('public re-export', () => {
|
describe('public re-export', () => {
|
||||||
@@ -110,5 +118,15 @@ describe('orchestration ID support', () => {
|
|||||||
const opts = getOctokitOptions('fake-token')
|
const opts = getOctokitOptions('fake-token')
|
||||||
expect(opts.userAgent).toBe('actions_orchestration_id/bad_chars_here_')
|
expect(opts.userAgent).toBe('actions_orchestration_id/bad_chars_here_')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('does not duplicate orchestration ID when caller already applied it', () => {
|
||||||
|
process.env['ACTIONS_ORCHESTRATION_ID'] = 'test-orch-id'
|
||||||
|
const opts = getOctokitOptions('fake-token', {
|
||||||
|
userAgent: 'my-app actions_orchestration_id/test-orch-id'
|
||||||
|
})
|
||||||
|
expect(opts.userAgent).toBe(
|
||||||
|
'my-app actions_orchestration_id/test-orch-id'
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -49,8 +49,10 @@ export function getUserAgentWithOrchestrationId(
|
|||||||
const orchId = process.env['ACTIONS_ORCHESTRATION_ID']?.trim()
|
const orchId = process.env['ACTIONS_ORCHESTRATION_ID']?.trim()
|
||||||
if (orchId) {
|
if (orchId) {
|
||||||
const sanitizedId = orchId.replace(/[^a-z0-9_.-]/gi, '_')
|
const sanitizedId = orchId.replace(/[^a-z0-9_.-]/gi, '_')
|
||||||
|
const tag = `actions_orchestration_id/${sanitizedId}`
|
||||||
|
if (baseUserAgent?.includes(tag)) return baseUserAgent
|
||||||
const ua = baseUserAgent ? `${baseUserAgent} ` : ''
|
const ua = baseUserAgent ? `${baseUserAgent} ` : ''
|
||||||
return `${ua}actions_orchestration_id/${sanitizedId}`
|
return `${ua}${tag}`
|
||||||
}
|
}
|
||||||
return baseUserAgent
|
return baseUserAgent
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user