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 4a5d3695..70ac500e 100644 --- a/src/output-parser.ts +++ b/src/output-parser.ts @@ -25,23 +25,42 @@ 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[]; +} + +/** + * UpdateTrafficInputs are the traffic inputs the update-traffic command was + * called with + */ +export interface UpdateTrafficInputs { + revisionTraffic?: string; + tagTraffic?: string; } /** @@ -49,9 +68,13 @@ interface UpdateTrafficItem { * 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 === '[]') { @@ -60,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); + + 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; + // Maintain current logic to use first tag URL if present + for (const item of outputJSON) { + if (item?.urls?.length) { + url = item.urls[0]; + break; + } } } @@ -81,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 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); } });