From 6a61612f931587c9ac577bdcd7aadc5962671214 Mon Sep 17 00:00:00 2001 From: Bethany Date: Wed, 2 Aug 2023 13:52:27 -0700 Subject: [PATCH] add util for pulling IDs from claims --- packages/artifact/src/internal/util.ts | 36 ++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 packages/artifact/src/internal/util.ts diff --git a/packages/artifact/src/internal/util.ts b/packages/artifact/src/internal/util.ts new file mode 100644 index 00000000..e5b3c8b1 --- /dev/null +++ b/packages/artifact/src/internal/util.ts @@ -0,0 +1,36 @@ +import { getRuntimeToken } from './config'; + +export interface BackendIds { + workflowRunBackendId: string; + workflowJobRunBackendId: string; +} + +export function getBackendIds(): BackendIds { + const token = getRuntimeToken(); + const parsedToken = JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString()) + if (!parsedToken["scp"]) { + throw new Error('Unable to get scp from token') + } + + const scp = parsedToken["scp"] + const scpParts = scp.split(' ') + if (scpParts.length == 0) { + throw new Error('No scp parts found') + } + + for (const part of scpParts) { + const partParts = part.split(':') + if(partParts.length == 0) { + continue + } + + if (partParts[0] == "Actions.Results") { + if (partParts.length == 3) { + return {workflowRunBackendId: partParts[1], workflowJobRunBackendId: partParts[2]} + } + throw new Error('Unable to parse Actions.Results scp part') + } + } + + throw new Error('Unable to find ids') +}