github: workflowRunURL

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2024-02-04 11:08:41 +01:00
parent 26949f5f39
commit 11cc7c697d
No known key found for this signature in database
GPG key ID: ADE44D8C9D44FBE4
4 changed files with 14 additions and 9 deletions

View file

@ -55,9 +55,3 @@ describe('gitContext', () => {
expect(Context.gitContext()).toEqual('https://github.com/docker/actions-toolkit.git#refs/heads/master'); expect(Context.gitContext()).toEqual('https://github.com/docker/actions-toolkit.git#refs/heads/master');
}); });
}); });
describe('provenanceBuilderID', () => {
it('returns 123', async () => {
expect(Context.provenanceBuilderID()).toEqual('https://github.com/docker/actions-toolkit/actions/runs/123');
});
});

View file

@ -89,6 +89,12 @@ describe('apiURL', () => {
}); });
}); });
describe('workflowRunURL', () => {
it('returns 123', async () => {
expect(GitHub.workflowRunURL).toEqual('https://github.com/docker/actions-toolkit/actions/runs/123');
});
});
describe('actionsRuntimeToken', () => { describe('actionsRuntimeToken', () => {
const originalEnv = process.env; const originalEnv = process.env;
beforeEach(() => { beforeEach(() => {

View file

@ -20,6 +20,7 @@ import * as core from '@actions/core';
import {parse} from 'csv-parse/sync'; import {parse} from 'csv-parse/sync';
import {Context} from '../context'; import {Context} from '../context';
import {GitHub} from '../github';
const parseKvp = (kvp: string): [string, string] => { const parseKvp = (kvp: string): [string, string] => {
const delimiterIndex = kvp.indexOf('='); const delimiterIndex = kvp.indexOf('=');
@ -111,7 +112,7 @@ export class Inputs {
return input; return input;
} }
try { try {
return core.getBooleanInput(name) ? `builder-id=${Context.provenanceBuilderID()}` : 'false'; return core.getBooleanInput(name) ? `builder-id=${GitHub.workflowRunURL}` : 'false';
} catch (err) { } catch (err) {
// not a valid boolean, so we assume it's a string // not a valid boolean, so we assume it's a string
return Inputs.resolveProvenanceAttrs(input); return Inputs.resolveProvenanceAttrs(input);
@ -120,7 +121,7 @@ export class Inputs {
public static resolveProvenanceAttrs(input: string): string { public static resolveProvenanceAttrs(input: string): string {
if (!input) { if (!input) {
return `builder-id=${Context.provenanceBuilderID()}`; return `builder-id=${GitHub.workflowRunURL}`;
} }
// parse attributes from input // parse attributes from input
const fields = parse(input, { const fields = parse(input, {
@ -138,7 +139,7 @@ export class Inputs {
} }
} }
// if not add builder-id attribute // if not add builder-id attribute
return `${input},builder-id=${Context.provenanceBuilderID()}`; return `${input},builder-id=${GitHub.workflowRunURL}`;
} }
public static hasLocalExporter(exporters: string[]): boolean { public static hasLocalExporter(exporters: string[]): boolean {

View file

@ -49,6 +49,10 @@ export class GitHub {
return process.env.GITHUB_API_URL || 'https://api.github.com'; return process.env.GITHUB_API_URL || 'https://api.github.com';
} }
static get workflowRunURL(): string {
return `${GitHub.serverURL}/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${github.context.runId}`;
}
static get actionsRuntimeToken(): GitHubActionsRuntimeToken | undefined { static get actionsRuntimeToken(): GitHubActionsRuntimeToken | undefined {
const token = process.env['ACTIONS_RUNTIME_TOKEN'] || ''; const token = process.env['ACTIONS_RUNTIME_TOKEN'] || '';
return token ? (jwtDecode<JwtPayload>(token) as GitHubActionsRuntimeToken) : undefined; return token ? (jwtDecode<JwtPayload>(token) as GitHubActionsRuntimeToken) : undefined;