github: apiURL
Also took the opportunity to make some methods static Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
@@ -13,14 +13,19 @@ jest.spyOn(GitHub.prototype, 'repoData').mockImplementation((): Promise<GitHubRe
|
|||||||
return <Promise<GitHubRepo>>(repoFixture as unknown);
|
return <Promise<GitHubRepo>>(repoFixture as unknown);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('repoData', () => {
|
||||||
|
it('returns GitHub repository', async () => {
|
||||||
|
const github = new GitHub();
|
||||||
|
expect((await github.repoData()).name).toEqual('Hello-World');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('context', () => {
|
describe('context', () => {
|
||||||
it('returns repository name from payload', async () => {
|
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 () => {
|
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 () => {
|
it('returns default', async () => {
|
||||||
process.env.GITHUB_SERVER_URL = '';
|
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 () => {
|
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 () => {
|
it('empty', async () => {
|
||||||
process.env.ACTIONS_RUNTIME_TOKEN = '';
|
process.env.ACTIONS_RUNTIME_TOKEN = '';
|
||||||
const github = new GitHub();
|
expect(GitHub.actionsRuntimeToken).toEqual({});
|
||||||
expect(github.actionsRuntimeToken).toEqual({});
|
|
||||||
});
|
});
|
||||||
it('fixture', async () => {
|
it('fixture', async () => {
|
||||||
process.env.ACTIONS_RUNTIME_TOKEN = fs.readFileSync(path.join(__dirname, 'fixtures', 'runtimeToken.txt')).toString().trim();
|
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.ac).toEqual('[{"Scope":"refs/heads/master","Permission":3}]');
|
||||||
expect(runtimeToken.iss).toEqual('vstoken.actions.githubusercontent.com');
|
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');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|||||||
@@ -15,27 +15,30 @@ export interface GitHubOpts {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class GitHub {
|
export class GitHub {
|
||||||
public static readonly serverURL: string = process.env.GITHUB_SERVER_URL || 'https://github.com';
|
|
||||||
public readonly octokit: InstanceType<typeof Octokit>;
|
public readonly octokit: InstanceType<typeof Octokit>;
|
||||||
|
|
||||||
constructor(opts?: GitHubOpts) {
|
constructor(opts?: GitHubOpts) {
|
||||||
this.octokit = github.getOctokit(`${opts?.token}`);
|
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<GitHubActionsRuntimeToken>(token) : {};
|
|
||||||
}
|
|
||||||
|
|
||||||
public repoData(): Promise<GitHubRepo> {
|
public repoData(): Promise<GitHubRepo> {
|
||||||
return this.octokit.rest.repos.get({...github.context.repo}).then(response => response.data as GitHubRepo);
|
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<GitHubActionsRuntimeToken>(token) : {};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user