move GitHub specific to its own class

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-01-30 02:31:09 +01:00
parent bb369fa859
commit 85ef93202e
5 changed files with 107 additions and 92 deletions

40
src/github.ts Normal file
View 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);
}
}