From ef5d3f1c80684c186d6c1204b7560261a96737f6 Mon Sep 17 00:00:00 2001 From: rootkiller6788 Date: Thu, 24 Sep 2026 15:00:52 +0800 Subject: [PATCH 1/3] fix type of the tags field in update-traffic output gcloud returns tags as objects ({tag, url, inSpec, inStatus}), not strings. Our own test fixtures already use the object form, so the declared type was just wrong. Making the other fields optional too, since gcloud omits some of them depending on the version. --- src/output-parser.ts | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/output-parser.ts b/src/output-parser.ts index 4a5d3695..7b08c538 100644 --- a/src/output-parser.ts +++ b/src/output-parser.ts @@ -25,23 +25,33 @@ export interface ParseInputs { [key: string]: string | boolean; } +/** + * UpdateTrafficTag is a tag attached to a traffic target + */ +interface UpdateTrafficTag { + inSpec?: boolean; + inStatus?: boolean; + tag?: string; + url?: string; +} + /** * UpdateTrafficItem is the response type for the gcloud run services update-traffic command */ interface UpdateTrafficItem { - displayPercent: string; - displayRevisionId: string; - displayTags: string; - key: string; - latestRevision: boolean; - revisionName: string; - serviceUrl: string; - specPercent: string; - specTags: string; - statusPercent: string; - statusTags: string; - tags: string[]; - urls: string[]; + displayPercent?: string; + displayRevisionId?: string; + displayTags?: string; + key?: string; + latestRevision?: boolean; + revisionName?: string; + serviceUrl?: string; + specPercent?: string; + specTags?: string; + statusPercent?: string; + statusTags?: string; + tags?: UpdateTrafficTag[]; + urls?: string[]; } /** From 934d16c74312452a79654616bdada991e984263c Mon Sep 17 00:00:00 2001 From: rootkiller6788 Date: Thu, 24 Sep 2026 15:08:10 +0800 Subject: [PATCH 2/3] pick the url of the traffic target we asked for The parser walked the output and grabbed the first url it found, but gcloud prints every traffic target of the service, not just the one we changed. So with `--to-revisions LATEST=100` on a service that also has an old tagged revision at 0% traffic, the output url could be the tag url of that unrelated revision. Now we pass revision_traffic/tag_traffic into the parser and match on the target that was requested. For a revision we return the service url, for a tag we return that tag's url. If the target isn't in the output we keep the old behavior. --- src/main.ts | 7 ++- src/output-parser.ts | 103 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 100 insertions(+), 10 deletions(-) diff --git a/src/main.ts b/src/main.ts index e53f4382..ea43dfbe 100644 --- a/src/main.ts +++ b/src/main.ts @@ -323,7 +323,12 @@ export async function run(): Promise { `command exited ${updateTrafficExec.exitCode}, but stderr had no output`; throw new Error(`failed to update traffic: ${errMsg}, full command:\n\t${commandString}`); } - setActionOutputs(parseUpdateTrafficResponse(updateTrafficExec.stdout)); + setActionOutputs( + parseUpdateTrafficResponse(updateTrafficExec.stdout, { + revisionTraffic: revTraffic, + tagTraffic: tagTraffic, + }), + ); } } catch (err) { const msg = errorMessage(err); diff --git a/src/output-parser.ts b/src/output-parser.ts index 7b08c538..70ac500e 100644 --- a/src/output-parser.ts +++ b/src/output-parser.ts @@ -54,14 +54,27 @@ interface UpdateTrafficItem { urls?: string[]; } +/** + * UpdateTrafficInputs are the traffic inputs the update-traffic command was + * called with + */ +export interface UpdateTrafficInputs { + revisionTraffic?: string; + tagTraffic?: string; +} + /** * parseUpdateTrafficResponse parses the gcloud command response for update-traffic * into a common DeployCloudRunOutputs object * * @param stdout + * @param inputs Action inputs used in parsing logic * @returns DeployCloudRunOutputs */ -export function parseUpdateTrafficResponse(stdout: string | undefined): DeployCloudRunOutputs { +export function parseUpdateTrafficResponse( + stdout: string | undefined, + inputs?: UpdateTrafficInputs, +): DeployCloudRunOutputs { try { stdout = presence(stdout); if (!stdout || stdout === '{}' || stdout === '[]') { @@ -70,15 +83,23 @@ export function parseUpdateTrafficResponse(stdout: string | undefined): DeployCl const outputJSON: UpdateTrafficItem[] = JSON.parse(stdout); - // Default to service url - const responseItem = outputJSON[0]; - let url = responseItem?.serviceUrl; + // Prefer the url of the traffic target that was requested. gcloud lists + // every target of the service, not just the one this command changed, so + // picking an arbitrary entry can return a url that belongs to some + // unrelated revision. + let url = targetUrl(outputJSON, inputs); - // Maintain current logic to use first tag URL if present - for (const item of outputJSON) { - if (item?.urls?.length) { - url = item.urls[0]; - break; + if (!url) { + // Default to service url + const responseItem = outputJSON[0]; + url = responseItem?.serviceUrl; + + // Maintain current logic to use first tag URL if present + for (const item of outputJSON) { + if (item?.urls?.length) { + url = item.urls[0]; + break; + } } } @@ -91,6 +112,70 @@ export function parseUpdateTrafficResponse(stdout: string | undefined): DeployCl } } +/** + * targetUrl returns the url for the traffic target requested by the given + * inputs, or undefined if there is nothing to match against. + */ +function targetUrl(items: UpdateTrafficItem[], inputs?: UpdateTrafficInputs): string | undefined { + for (const tag of assignmentKeys(inputs?.tagTraffic)) { + const item = items.find((i) => tagNames(i).includes(tag)); + if (item) { + // The tag was requested explicitly, so its url is the interesting one. + const tagInfo = (item.tags || []).find((t) => t?.tag === tag); + return tagInfo?.url || item.urls?.[0] || item.serviceUrl; + } + } + + for (const revision of assignmentKeys(inputs?.revisionTraffic)) { + const item = items.find((i) => matchesRevision(i, revision)); + if (item) { + return item.serviceUrl || item.urls?.[0]; + } + } + + return undefined; +} + +/** + * assignmentKeys returns the keys of a gcloud assignment list such as + * "LATEST=100,my-revision=10". + */ +function assignmentKeys(value: string | undefined): string[] { + return (value || '') + .split(',') + .map((v) => v.split('=')[0].trim()) + .filter((v) => v !== ''); +} + +/** + * tagNames returns the tag names attached to a traffic target. gcloud reports + * them both as a list of objects and as a comma separated string. + */ +function tagNames(item: UpdateTrafficItem): string[] { + const names = (item?.tags || []).map((t) => t?.tag).filter((t): t is string => !!t); + + for (const raw of [item?.specTags, item?.statusTags, item?.displayTags]) { + for (const part of (raw || '').split(',')) { + const name = part.trim(); + if (name && name !== '-' && !names.includes(name)) { + names.push(name); + } + } + } + + return names; +} + +/** + * matchesRevision returns true if the traffic target is the given revision. + */ +function matchesRevision(item: UpdateTrafficItem, revision: string): boolean { + if (revision.toUpperCase() === 'LATEST') { + return item?.latestRevision === true; + } + return item?.key === revision || item?.revisionName === revision; +} + /** * parseDeployResponse parses the gcloud command response for gcloud run deploy/replace * into a common DeployCloudRunOutputs object From 83ece86d6ae2fe8154b54de83a700a7b185b9250 Mon Sep 17 00:00:00 2001 From: rootkiller6788 Date: Thu, 24 Sep 2026 15:08:14 +0800 Subject: [PATCH 3/3] add tests for the update-traffic output url Covers the LATEST=100 case from #589 (a tagged revision at 0% traffic listed first), picking a specific tag, and the fallbacks. --- tests/unit/output-parser.test.ts | 174 ++++++++++++++++++++++++++++++- 1 file changed, 172 insertions(+), 2 deletions(-) diff --git a/tests/unit/output-parser.test.ts b/tests/unit/output-parser.test.ts index c12ddb67..2e99cce8 100644 --- a/tests/unit/output-parser.test.ts +++ b/tests/unit/output-parser.test.ts @@ -192,6 +192,176 @@ test('#parseUpdateTrafficResponse', { concurrency: true }, async (suite) => { `, expected: { url: 'https://my-tag-1---test-basic-yaml-4goqgbaxqq-uc.a.run.app' }, }, + { + name: 'prefers the requested revision over the first tagged one', + stdout: ` + [ + { + "displayPercent": "0%", + "displayRevisionId": "test-basic-yaml-00005-yus", + "displayTags": "pr-1", + "key": "test-basic-yaml-00005-yus", + "latestRevision": false, + "revisionName": "test-basic-yaml-00005-yus", + "serviceUrl": "https://test-basic-yaml-4goqgbaxqq-uc.a.run.app", + "specPercent": "0", + "specTags": "pr-1", + "statusPercent": "0", + "statusTags": "pr-1", + "tags": [ + { + "inSpec": true, + "inStatus": true, + "tag": "pr-1", + "url": "https://pr-1---test-basic-yaml-4goqgbaxqq-uc.a.run.app" + } + ], + "urls": [ + "https://pr-1---test-basic-yaml-4goqgbaxqq-uc.a.run.app" + ] + }, + { + "displayPercent": "100%", + "displayRevisionId": "LATEST (currently test-basic-yaml-00007-leg)", + "displayTags": "dev", + "key": "LATEST", + "latestRevision": true, + "revisionName": "test-basic-yaml-00007-leg", + "serviceUrl": "https://test-basic-yaml-4goqgbaxqq-uc.a.run.app", + "specPercent": "100", + "specTags": "dev", + "statusPercent": "100", + "statusTags": "dev", + "tags": [ + { + "inSpec": true, + "inStatus": true, + "tag": "dev", + "url": "https://dev---test-basic-yaml-4goqgbaxqq-uc.a.run.app" + } + ], + "urls": [ + "https://dev---test-basic-yaml-4goqgbaxqq-uc.a.run.app" + ] + } + ] + `, + inputs: { revisionTraffic: 'LATEST=100' }, + expected: { url: 'https://test-basic-yaml-4goqgbaxqq-uc.a.run.app' }, + }, + { + name: 'prefers the requested tag over the first tagged one', + stdout: ` + [ + { + "key": "test-basic-yaml-00005-yus", + "latestRevision": false, + "revisionName": "test-basic-yaml-00005-yus", + "serviceUrl": "https://test-basic-yaml-4goqgbaxqq-uc.a.run.app", + "specTags": "my-tag-1", + "statusTags": "my-tag-1", + "tags": [ + { + "inSpec": true, + "inStatus": true, + "tag": "my-tag-1", + "url": "https://my-tag-1---test-basic-yaml-4goqgbaxqq-uc.a.run.app" + } + ], + "urls": [ + "https://my-tag-1---test-basic-yaml-4goqgbaxqq-uc.a.run.app" + ] + }, + { + "key": "test-basic-yaml-00006-juz", + "latestRevision": true, + "revisionName": "test-basic-yaml-00006-juz", + "serviceUrl": "https://test-basic-yaml-4goqgbaxqq-uc.a.run.app", + "specTags": "my-tag-2", + "statusTags": "my-tag-2", + "tags": [ + { + "inSpec": true, + "inStatus": true, + "tag": "my-tag-2", + "url": "https://my-tag-2---test-basic-yaml-4goqgbaxqq-uc.a.run.app" + } + ], + "urls": [ + "https://my-tag-2---test-basic-yaml-4goqgbaxqq-uc.a.run.app" + ] + } + ] + `, + inputs: { tagTraffic: 'my-tag-2=100' }, + expected: { url: 'https://my-tag-2---test-basic-yaml-4goqgbaxqq-uc.a.run.app' }, + }, + { + name: 'falls back to the service url when the requested revision has no tag', + stdout: ` + [ + { + "key": "LATEST", + "latestRevision": true, + "revisionName": "test-basic-yaml-00006-juz", + "serviceUrl": "https://test-basic-yaml-4goqgbaxqq-uc.a.run.app", + "specTags": "-", + "statusTags": "-", + "tags": [], + "urls": [] + }, + { + "key": "test-basic-yaml-00005-yus", + "latestRevision": false, + "revisionName": "test-basic-yaml-00005-yus", + "serviceUrl": "https://test-basic-yaml-4goqgbaxqq-uc.a.run.app", + "specTags": "my-tag-1", + "statusTags": "my-tag-1", + "tags": [ + { + "inSpec": true, + "inStatus": true, + "tag": "my-tag-1", + "url": "https://my-tag-1---test-basic-yaml-4goqgbaxqq-uc.a.run.app" + } + ], + "urls": [ + "https://my-tag-1---test-basic-yaml-4goqgbaxqq-uc.a.run.app" + ] + } + ] + `, + inputs: { revisionTraffic: 'test-basic-yaml-00006-juz=100' }, + expected: { url: 'https://test-basic-yaml-4goqgbaxqq-uc.a.run.app' }, + }, + { + name: 'falls back to the first tag url when the requested target is missing', + stdout: ` + [ + { + "key": "test-basic-yaml-00005-yus", + "latestRevision": false, + "revisionName": "test-basic-yaml-00005-yus", + "serviceUrl": "https://test-basic-yaml-4goqgbaxqq-uc.a.run.app", + "specTags": "my-tag-1", + "statusTags": "my-tag-1", + "tags": [ + { + "inSpec": true, + "inStatus": true, + "tag": "my-tag-1", + "url": "https://my-tag-1---test-basic-yaml-4goqgbaxqq-uc.a.run.app" + } + ], + "urls": [ + "https://my-tag-1---test-basic-yaml-4goqgbaxqq-uc.a.run.app" + ] + } + ] + `, + inputs: { revisionTraffic: 'some-other-revision=100' }, + expected: { url: 'https://my-tag-1---test-basic-yaml-4goqgbaxqq-uc.a.run.app' }, + }, { name: 'handles empty stdout', stdout: '', @@ -219,12 +389,12 @@ test('#parseUpdateTrafficResponse', { concurrency: true }, async (suite) => { if (tc.error) { assert.throws( () => { - parseUpdateTrafficResponse(tc.stdout); + parseUpdateTrafficResponse(tc.stdout, tc.inputs); }, { message: tc.error }, ); } else { - const actual = parseUpdateTrafficResponse(tc.stdout); + const actual = parseUpdateTrafficResponse(tc.stdout, tc.inputs); assert.deepStrictEqual(actual, tc.expected); } });