From 17f9c80d9cf29c80d763f1529e80368b68e05c8f Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Wed, 1 Feb 2023 01:42:02 +0100 Subject: [PATCH] github: apiURL Also took the opportunity to make some methods static Signed-off-by: CrazyMax --- __tests__/github.test.ts | 53 ++++++++++++++++++++++++++-------------- src/github.ts | 31 ++++++++++++----------- 2 files changed, 51 insertions(+), 33 deletions(-) diff --git a/__tests__/github.test.ts b/__tests__/github.test.ts index e24ad32..4f6c438 100644 --- a/__tests__/github.test.ts +++ b/__tests__/github.test.ts @@ -13,14 +13,19 @@ jest.spyOn(GitHub.prototype, 'repoData').mockImplementation((): Promise>(repoFixture as unknown); }); +describe('repoData', () => { + it('returns GitHub repository', async () => { + const github = new GitHub(); + expect((await github.repoData()).name).toEqual('Hello-World'); + }); +}); + describe('context', () => { it('returns repository name from payload', async () => { - const github = new GitHub(); - expect(github.context.payload.repository?.name).toEqual('test-docker-action'); + expect(GitHub.context.payload.repository?.name).toEqual('test-docker-action'); }); it('is repository private', async () => { - const github = new GitHub(); - expect(github.context.payload.repository?.private).toEqual(true); + expect(GitHub.context.payload.repository?.private).toEqual(true); }); }); @@ -38,12 +43,31 @@ describe('serverURL', () => { }); it('returns default', async () => { process.env.GITHUB_SERVER_URL = ''; - const github = new GitHub(); - expect(github.serverURL).toEqual('https://github.com'); + expect(GitHub.serverURL).toEqual('https://github.com'); }); it('returns from env', async () => { - const github = new GitHub(); - expect(github.serverURL).toEqual('https://foo.github.com'); + expect(GitHub.serverURL).toEqual('https://foo.github.com'); + }); +}); + +describe('apiURL', () => { + const originalEnv = process.env; + beforeEach(() => { + jest.resetModules(); + process.env = { + ...originalEnv, + GITHUB_API_URL: 'https://bar.github.com' + }; + }); + afterEach(() => { + process.env = originalEnv; + }); + it('returns default', async () => { + process.env.GITHUB_API_URL = ''; + expect(GitHub.apiURL).toEqual('https://api.github.com'); + }); + it('returns from env', async () => { + expect(GitHub.apiURL).toEqual('https://bar.github.com'); }); }); @@ -60,21 +84,12 @@ describe('actionsRuntimeToken', () => { }); it('empty', async () => { process.env.ACTIONS_RUNTIME_TOKEN = ''; - const github = new GitHub(); - expect(github.actionsRuntimeToken).toEqual({}); + expect(GitHub.actionsRuntimeToken).toEqual({}); }); it('fixture', async () => { process.env.ACTIONS_RUNTIME_TOKEN = fs.readFileSync(path.join(__dirname, 'fixtures', 'runtimeToken.txt')).toString().trim(); - const github = new GitHub(); - const runtimeToken = github.actionsRuntimeToken; + const runtimeToken = GitHub.actionsRuntimeToken; expect(runtimeToken.ac).toEqual('[{"Scope":"refs/heads/master","Permission":3}]'); expect(runtimeToken.iss).toEqual('vstoken.actions.githubusercontent.com'); }); }); - -describe('repoData', () => { - it('returns GitHub repository', async () => { - const github = new GitHub(); - expect((await github.repoData()).name).toEqual('Hello-World'); - }); -}); diff --git a/src/github.ts b/src/github.ts index c84857b..4b8e264 100644 --- a/src/github.ts +++ b/src/github.ts @@ -15,27 +15,30 @@ export interface GitHubOpts { } export class GitHub { - public static readonly serverURL: string = process.env.GITHUB_SERVER_URL || 'https://github.com'; public readonly octokit: InstanceType; constructor(opts?: GitHubOpts) { this.octokit = github.getOctokit(`${opts?.token}`); } - get context(): Context { - return github.context; - } - - get serverURL(): string { - return process.env.GITHUB_SERVER_URL || 'https://github.com'; - } - - get actionsRuntimeToken(): GitHubActionsRuntimeToken { - const token = process.env['ACTIONS_RUNTIME_TOKEN'] || ''; - return token ? jwt_decode(token) : {}; - } - public repoData(): Promise { return this.octokit.rest.repos.get({...github.context.repo}).then(response => response.data as GitHubRepo); } + + static get context(): Context { + return github.context; + } + + static get serverURL(): string { + return process.env.GITHUB_SERVER_URL || 'https://github.com'; + } + + static get apiURL(): string { + return process.env.GITHUB_API_URL || 'https://api.github.com'; + } + + static get actionsRuntimeToken(): GitHubActionsRuntimeToken { + const token = process.env['ACTIONS_RUNTIME_TOKEN'] || ''; + return token ? jwt_decode(token) : {}; + } }