diff --git a/__tests__/github.test.ts b/__tests__/github.test.ts index 9053c13..01cc841 100644 --- a/__tests__/github.test.ts +++ b/__tests__/github.test.ts @@ -1,10 +1,11 @@ -import {describe, expect, jest, it, beforeEach} from '@jest/globals'; -import {Context} from '@actions/github/lib/context'; +import {describe, expect, jest, it, beforeEach, afterEach} from '@jest/globals'; +import {Context} from '@actions/github/lib/context'; import {GitHub, Payload, ReposGetResponseData} from '../src/github'; beforeEach(() => { jest.clearAllMocks(); + GitHub.getInstance().reset(); }); jest.spyOn(GitHub.prototype, 'context').mockImplementation((): Context => { @@ -24,12 +25,39 @@ jest.spyOn(GitHub.prototype as any, 'ref').mockImplementation((): string => { 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', () => { it('returns refs/heads/master', async () => { 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', () => { it('returns GitHub repository', async () => { const repo = await GitHub.getInstance().repo(process.env.GITHUB_TOKEN || ''); diff --git a/jest.config.ts b/jest.config.ts index e351829..f94c65b 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -1,5 +1,6 @@ process.env = Object.assign({}, process.env, { GITHUB_REPOSITORY: 'docker/test-docker-action', + GITHUB_RUN_ID: '123', RUNNER_TEMP: '/tmp/github_runner', RUNNER_TOOL_CACHE: '/tmp/github_tool_cache' }) as { diff --git a/src/github.ts b/src/github.ts index efbe808..791aab6 100644 --- a/src/github.ts +++ b/src/github.ts @@ -14,7 +14,9 @@ interface Jwt extends JwtPayload { export class GitHub { private static instance?: GitHub; static getInstance = (): GitHub => (GitHub.instance = GitHub.instance ?? new GitHub()); + private static _serverURL: string; private static _gitContext: string; + private static _provenanceBuilderID: string; private constructor() { let ref = this.ref(); @@ -24,7 +26,13 @@ export class GitHub { if (github.context.sha && !ref.startsWith(`refs/pull/`)) { 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 { @@ -35,10 +43,18 @@ export class GitHub { return github.context.ref; } + public serverURL() { + return GitHub._serverURL; + } + public gitContext() { return GitHub._gitContext; } + public provenanceBuilderID() { + return GitHub._provenanceBuilderID; + } + private payload(): Payload { return github.context.payload; }