github: add serverURL annd provenanceBuilderID

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2023-01-25 03:20:18 +01:00
parent 380149da27
commit b03f6a405c
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
3 changed files with 48 additions and 3 deletions

View File

@ -1,10 +1,11 @@
import {describe, expect, jest, it, beforeEach} from '@jest/globals'; import {describe, expect, jest, it, beforeEach, afterEach} from '@jest/globals';
import {Context} from '@actions/github/lib/context';
import {Context} from '@actions/github/lib/context';
import {GitHub, Payload, ReposGetResponseData} from '../src/github'; import {GitHub, Payload, ReposGetResponseData} from '../src/github';
beforeEach(() => { beforeEach(() => {
jest.clearAllMocks(); jest.clearAllMocks();
GitHub.getInstance().reset();
}); });
jest.spyOn(GitHub.prototype, 'context').mockImplementation((): Context => { jest.spyOn(GitHub.prototype, 'context').mockImplementation((): Context => {
@ -24,12 +25,39 @@ jest.spyOn(GitHub.prototype as any, 'ref').mockImplementation((): string => {
return 'refs/heads/master'; return 'refs/heads/master';
}); });
describe('serverURL', () => {
const originalEnv = process.env;
beforeEach(() => {
jest.resetModules();
process.env = {
...originalEnv,
GITHUB_SERVER_URL: 'https://foo.github.com'
};
});
afterEach(() => {
process.env = originalEnv;
});
it('returns default', async () => {
process.env.GITHUB_SERVER_URL = '';
expect(GitHub.getInstance().serverURL()).toEqual('https://github.com');
});
it('returns from env', async () => {
expect(GitHub.getInstance().serverURL()).toEqual('https://foo.github.com');
});
});
describe('gitContext', () => { describe('gitContext', () => {
it('returns refs/heads/master', async () => { it('returns refs/heads/master', async () => {
expect(GitHub.getInstance().gitContext()).toEqual('https://github.com/docker/test-docker-action.git#refs/heads/master'); expect(GitHub.getInstance().gitContext()).toEqual('https://github.com/docker/test-docker-action.git#refs/heads/master');
}); });
}); });
describe('provenanceBuilderID', () => {
it('returns 123', async () => {
expect(GitHub.getInstance().provenanceBuilderID()).toEqual('https://github.com/docker/test-docker-action/actions/runs/123');
});
});
describe('repo', () => { describe('repo', () => {
it('returns GitHub repository', async () => { it('returns GitHub repository', async () => {
const repo = await GitHub.getInstance().repo(process.env.GITHUB_TOKEN || ''); const repo = await GitHub.getInstance().repo(process.env.GITHUB_TOKEN || '');

View File

@ -1,5 +1,6 @@
process.env = Object.assign({}, process.env, { process.env = Object.assign({}, process.env, {
GITHUB_REPOSITORY: 'docker/test-docker-action', GITHUB_REPOSITORY: 'docker/test-docker-action',
GITHUB_RUN_ID: '123',
RUNNER_TEMP: '/tmp/github_runner', RUNNER_TEMP: '/tmp/github_runner',
RUNNER_TOOL_CACHE: '/tmp/github_tool_cache' RUNNER_TOOL_CACHE: '/tmp/github_tool_cache'
}) as { }) as {

View File

@ -14,7 +14,9 @@ interface Jwt extends JwtPayload {
export class GitHub { export class GitHub {
private static instance?: GitHub; private static instance?: GitHub;
static getInstance = (): GitHub => (GitHub.instance = GitHub.instance ?? new GitHub()); static getInstance = (): GitHub => (GitHub.instance = GitHub.instance ?? new GitHub());
private static _serverURL: string;
private static _gitContext: string; private static _gitContext: string;
private static _provenanceBuilderID: string;
private constructor() { private constructor() {
let ref = this.ref(); let ref = this.ref();
@ -24,7 +26,13 @@ export class GitHub {
if (github.context.sha && !ref.startsWith(`refs/pull/`)) { if (github.context.sha && !ref.startsWith(`refs/pull/`)) {
ref = github.context.sha; ref = github.context.sha;
} }
GitHub._gitContext = `${process.env.GITHUB_SERVER_URL || 'https://github.com'}/${github.context.repo.owner}/${github.context.repo.repo}.git#${ref}`; GitHub._serverURL = process.env.GITHUB_SERVER_URL || 'https://github.com';
GitHub._gitContext = `${GitHub._serverURL}/${github.context.repo.owner}/${github.context.repo.repo}.git#${ref}`;
GitHub._provenanceBuilderID = `${GitHub._serverURL}/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${github.context.runId}`;
}
public reset() {
GitHub.instance = undefined;
} }
public context(): Context { public context(): Context {
@ -35,10 +43,18 @@ export class GitHub {
return github.context.ref; return github.context.ref;
} }
public serverURL() {
return GitHub._serverURL;
}
public gitContext() { public gitContext() {
return GitHub._gitContext; return GitHub._gitContext;
} }
public provenanceBuilderID() {
return GitHub._provenanceBuilderID;
}
private payload(): Payload { private payload(): Payload {
return github.context.payload; return github.context.payload;
} }