move GitHub specific to its own class
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
40
src/github.ts
Normal file
40
src/github.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import {GitHub as Octokit} from '@actions/github/lib/utils';
|
||||
import * as github from '@actions/github';
|
||||
import {components as OctoOpenApiTypes} from '@octokit/openapi-types';
|
||||
import jwt_decode, {JwtPayload} from 'jwt-decode';
|
||||
import {Context} from '@actions/github/lib/context';
|
||||
|
||||
export type GitHubRepo = OctoOpenApiTypes['schemas']['repository'];
|
||||
|
||||
export interface GitHubRuntimeToken extends JwtPayload {
|
||||
ac?: string;
|
||||
}
|
||||
|
||||
export interface GitHubOpts {
|
||||
token?: string;
|
||||
}
|
||||
|
||||
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 runtimeToken(): GitHubRuntimeToken {
|
||||
return jwt_decode<GitHubRuntimeToken>(`${process.env['ACTIONS_RUNTIME_TOKEN']}`);
|
||||
}
|
||||
|
||||
public repoData(): Promise<GitHubRepo> {
|
||||
return this.octokit.rest.repos.get({...github.context.repo}).then(response => response.data as GitHubRepo);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user