@@ -29,14 +29,19 @@ jest.spyOn(GitHub.prototype, 'repoData').mockImplementation((): Promise<GitHubRe
|
||||
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', () => {
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -54,12 +59,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');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -76,21 +100,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');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -31,27 +31,30 @@ export interface GitHubOpts {
|
||||
}
|
||||
|
||||
export class GitHub {
|
||||
public static readonly serverURL: string = process.env.GITHUB_SERVER_URL || 'https://github.com';
|
||||
public readonly octokit: InstanceType<typeof Octokit>;
|
||||
|
||||
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<GitHubActionsRuntimeToken>(token) : {};
|
||||
}
|
||||
|
||||
public repoData(): Promise<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