Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
83 changes: 83 additions & 0 deletions __test__/orchestration-id.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

describe('getUserAgentWithOrchestrationId', () => {
let originalEnv: NodeJS.ProcessEnv

beforeEach(() => {
originalEnv = {...process.env}
})

afterEach(() => {
process.env = originalEnv
})

// Since getUserAgentWithOrchestrationId is not exported, we'll test it indirectly
// by mocking the getInput and testing the behavior through the main function integration
// For now, we'll create simple unit tests that verify the logic

test('appends orchestration ID when ACTIONS_ORCHESTRATION_ID is set', () => {
const baseUserAgent = 'actions/github-script'
const orchestrationId = 'test-orchestration-123'
process.env['ACTIONS_ORCHESTRATION_ID'] = orchestrationId

// Simulate the logic from getUserAgentWithOrchestrationId
const sanitized = orchestrationId.replace(/[^a-zA-Z0-9._-]/g, '')
const result = `${baseUserAgent} orchestration-id/${sanitized}`

expect(result).toBe(
'actions/github-script orchestration-id/test-orchestration-123'
)
})

test('sanitizes orchestration ID by removing special characters', () => {
const baseUserAgent = 'actions/github-script'
const orchestrationId = 'test@orchestration#123!abc$xyz'

// Simulate the logic from getUserAgentWithOrchestrationId
const sanitized = orchestrationId.replace(/[^a-zA-Z0-9._-]/g, '')
const result = `${baseUserAgent} orchestration-id/${sanitized}`

expect(result).toBe(
'actions/github-script orchestration-id/testorchestration123abcxyz'
)
})

test('preserves dots, hyphens, and underscores in orchestration ID', () => {
const baseUserAgent = 'actions/github-script'
const orchestrationId = 'test.orchestration-123_abc'

// Simulate the logic from getUserAgentWithOrchestrationId
const sanitized = orchestrationId.replace(/[^a-zA-Z0-9._-]/g, '')
const result = `${baseUserAgent} orchestration-id/${sanitized}`

expect(result).toBe(
'actions/github-script orchestration-id/test.orchestration-123_abc'
)
})

test('does not append orchestration ID when ACTIONS_ORCHESTRATION_ID is not set', () => {
const baseUserAgent = 'actions/github-script'
delete process.env['ACTIONS_ORCHESTRATION_ID']

// Simulate the logic from getUserAgentWithOrchestrationId
const orchestrationId = process.env['ACTIONS_ORCHESTRATION_ID']
const result = orchestrationId
? `${baseUserAgent} orchestration-id/${orchestrationId}`
: baseUserAgent

expect(result).toBe('actions/github-script')
})

test('does not append orchestration ID when it becomes empty after sanitization', () => {
const baseUserAgent = 'actions/github-script'
const orchestrationId = '@#$%^&*()'

// Simulate the logic from getUserAgentWithOrchestrationId
const sanitized = orchestrationId.replace(/[^a-zA-Z0-9._-]/g, '')
const result = sanitized
? `${baseUserAgent} orchestration-id/${sanitized}`
: baseUserAgent

expect(result).toBe('actions/github-script')
})
})
21 changes: 20 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36258,6 +36258,23 @@ const wrapRequire = new Proxy(require, {

process.on('unhandledRejection', handleError);
main().catch(handleError);
/**
* Gets the user agent string with orchestration ID appended if available
* @param userAgent The base user agent string
* @returns The user agent string with orchestration ID appended if ACTIONS_ORCHESTRATION_ID is set
*/
function getUserAgentWithOrchestrationId(userAgent) {
const orchestrationId = process.env['ACTIONS_ORCHESTRATION_ID'];
if (!orchestrationId) {
return userAgent;
}
// Sanitize orchestration ID - only keep alphanumeric, dots, hyphens, and underscores
const sanitized = orchestrationId.replace(/[^a-zA-Z0-9._-]/g, '');
if (!sanitized) {
return userAgent;
}
return `${userAgent} orchestration-id/${sanitized}`;
}
async function main() {
const token = core.getInput('github-token', { required: true });
const debug = core.getBooleanInput('debug');
Expand All @@ -36267,9 +36284,11 @@ async function main() {
const retries = parseInt(core.getInput('retries'));
const exemptStatusCodes = parseNumberArray(core.getInput('retry-exempt-status-codes'));
const [retryOpts, requestOpts] = getRetryOptions(retries, exemptStatusCodes, utils.defaults);
const baseUserAgent = userAgent || 'actions/github-script';
const finalUserAgent = getUserAgentWithOrchestrationId(baseUserAgent);
const opts = {
log: debug ? console : undefined,
userAgent: userAgent || undefined,
userAgent: finalUserAgent,
previews: previews ? previews.split(',') : undefined,
retry: retryOpts,
request: requestOpts
Expand Down
24 changes: 21 additions & 3 deletions package-lock.json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot undo changes in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted package-lock.json changes in commit b588811. All "peer": true flags have been removed.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ type Options = {
request?: RequestRequestOptions
}

/**
* Gets the user agent string with orchestration ID appended if available
* @param userAgent The base user agent string
* @returns The user agent string with orchestration ID appended if ACTIONS_ORCHESTRATION_ID is set
*/
function getUserAgentWithOrchestrationId(userAgent: string): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot move the helper method to the end of the file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the helper method to the end of the file in commit 8a9be95

const orchestrationId = process.env['ACTIONS_ORCHESTRATION_ID']
if (!orchestrationId) {
return userAgent
}

// Sanitize orchestration ID - only keep alphanumeric, dots, hyphens, and underscores
const sanitized = orchestrationId.replace(/[^a-zA-Z0-9._-]/g, '')
if (!sanitized) {
return userAgent
}

return `${userAgent} orchestration-id/${sanitized}`
}

async function main(): Promise<void> {
const token = core.getInput('github-token', {required: true})
const debug = core.getBooleanInput('debug')
Expand All @@ -39,9 +59,12 @@ async function main(): Promise<void> {
defaultGitHubOptions
)

const baseUserAgent = userAgent || 'actions/github-script'
const finalUserAgent = getUserAgentWithOrchestrationId(baseUserAgent)

const opts: Options = {
log: debug ? console : undefined,
userAgent: userAgent || undefined,
userAgent: finalUserAgent,
previews: previews ? previews.split(',') : undefined,
retry: retryOpts,
request: requestOpts
Expand Down