Skip to content

Commit 450193c

Browse files
authored
Merge pull request #695 from actions/copilot/add-orchestration-id-user-agent
Add ACTIONS_ORCHESTRATION_ID to user-agent string
2 parents ed59741 + b67a972 commit 450193c

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

.github/workflows/integration.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ jobs:
167167
exit 1
168168
fi
169169
echo "- Validating user-agent set to an empty string"
170-
expected="octokit-core.js/"
170+
expected="actions/github-script octokit-core.js/"
171171
if [[ "${{steps.user-agent-empty.outputs.result}}" != "$expected"* ]]; then
172172
echo $'::error::\u274C' "Expected user-agent to start with '$expected', got ${{steps.user-agent-empty.outputs.result}}"
173173
exit 1

dist/index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36267,9 +36267,11 @@ async function main() {
3626736267
const retries = parseInt(core.getInput('retries'));
3626836268
const exemptStatusCodes = parseNumberArray(core.getInput('retry-exempt-status-codes'));
3626936269
const [retryOpts, requestOpts] = getRetryOptions(retries, exemptStatusCodes, utils.defaults);
36270+
const baseUserAgent = userAgent || 'actions/github-script';
36271+
const finalUserAgent = getUserAgentWithOrchestrationId(baseUserAgent);
3627036272
const opts = {
3627136273
log: debug ? console : undefined,
36272-
userAgent: userAgent || undefined,
36274+
userAgent: finalUserAgent,
3627336275
previews: previews ? previews.split(',') : undefined,
3627436276
retry: retryOpts,
3627536277
request: requestOpts
@@ -36313,6 +36315,20 @@ function handleError(err) {
3631336315
console.error(err);
3631436316
core.setFailed(`Unhandled error: ${err}`);
3631536317
}
36318+
/**
36319+
* Gets the user agent string with orchestration ID appended if available
36320+
* @param userAgent The base user agent string
36321+
* @returns The user agent string with orchestration ID appended if ACTIONS_ORCHESTRATION_ID is set
36322+
*/
36323+
function getUserAgentWithOrchestrationId(userAgent) {
36324+
const orchestrationId = process.env['ACTIONS_ORCHESTRATION_ID'];
36325+
if (!orchestrationId) {
36326+
return userAgent;
36327+
}
36328+
// Sanitize orchestration ID - replace invalid characters with underscore
36329+
const sanitized = orchestrationId.replace(/[^a-zA-Z0-9._-]/g, '_');
36330+
return `${userAgent} actions_orchestration_id/${sanitized}`;
36331+
}
3631636332

3631736333
})();
3631836334

src/main.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ async function main(): Promise<void> {
3939
defaultGitHubOptions
4040
)
4141

42+
const baseUserAgent = userAgent || 'actions/github-script'
43+
const finalUserAgent = getUserAgentWithOrchestrationId(baseUserAgent)
44+
4245
const opts: Options = {
4346
log: debug ? console : undefined,
44-
userAgent: userAgent || undefined,
47+
userAgent: finalUserAgent,
4548
previews: previews ? previews.split(',') : undefined,
4649
retry: retryOpts,
4750
request: requestOpts
@@ -96,3 +99,20 @@ function handleError(err: any): void {
9699
console.error(err)
97100
core.setFailed(`Unhandled error: ${err}`)
98101
}
102+
103+
/**
104+
* Gets the user agent string with orchestration ID appended if available
105+
* @param userAgent The base user agent string
106+
* @returns The user agent string with orchestration ID appended if ACTIONS_ORCHESTRATION_ID is set
107+
*/
108+
function getUserAgentWithOrchestrationId(userAgent: string): string {
109+
const orchestrationId = process.env['ACTIONS_ORCHESTRATION_ID']
110+
if (!orchestrationId) {
111+
return userAgent
112+
}
113+
114+
// Sanitize orchestration ID - replace invalid characters with underscore
115+
const sanitized = orchestrationId.replace(/[^a-zA-Z0-9._-]/g, '_')
116+
117+
return `${userAgent} actions_orchestration_id/${sanitized}`
118+
}

0 commit comments

Comments
 (0)