move GitHub specific to its own class
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
@@ -2,26 +2,18 @@ import fs from 'fs';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import * as tmp from 'tmp';
|
||||
import jwt_decode, {JwtPayload} from 'jwt-decode';
|
||||
import {GitHub} from '@actions/github/lib/utils';
|
||||
import * as github from '@actions/github';
|
||||
import {components as OctoOpenApiTypes} from '@octokit/openapi-types';
|
||||
|
||||
export type ReposGetResponseData = OctoOpenApiTypes['schemas']['repository'];
|
||||
export interface Jwt extends JwtPayload {
|
||||
ac?: string;
|
||||
}
|
||||
import {GitHub} from './github';
|
||||
|
||||
export class Context {
|
||||
public serverURL: string;
|
||||
public gitRef: string;
|
||||
public buildGitContext: string;
|
||||
public provenanceBuilderID: string;
|
||||
public octokit: InstanceType<typeof GitHub>;
|
||||
|
||||
private readonly _tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-actions-toolkit-')).split(path.sep).join(path.posix.sep);
|
||||
|
||||
constructor(githubToken?: string) {
|
||||
constructor() {
|
||||
this.gitRef = github.context.ref;
|
||||
if (github.context.sha && this.gitRef && !this.gitRef.startsWith('refs/')) {
|
||||
this.gitRef = `refs/heads/${github.context.ref}`;
|
||||
@@ -29,10 +21,8 @@ export class Context {
|
||||
if (github.context.sha && !this.gitRef.startsWith(`refs/pull/`)) {
|
||||
this.gitRef = github.context.sha;
|
||||
}
|
||||
this.serverURL = process.env.GITHUB_SERVER_URL || 'https://github.com';
|
||||
this.buildGitContext = `${this.serverURL}/${github.context.repo.owner}/${github.context.repo.repo}.git#${this.gitRef}`;
|
||||
this.provenanceBuilderID = `${this.serverURL}/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${github.context.runId}`;
|
||||
this.octokit = github.getOctokit(`${githubToken}`);
|
||||
this.buildGitContext = `${GitHub.serverURL}/${github.context.repo.owner}/${github.context.repo.repo}.git#${this.gitRef}`;
|
||||
this.provenanceBuilderID = `${GitHub.serverURL}/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${github.context.runId}`;
|
||||
}
|
||||
|
||||
public tmpDir(): string {
|
||||
@@ -42,30 +32,4 @@ export class Context {
|
||||
public tmpName(options?: tmp.TmpNameOptions): string {
|
||||
return tmp.tmpNameSync(options);
|
||||
}
|
||||
|
||||
public repoData(): Promise<ReposGetResponseData> {
|
||||
return this.octokit.rest.repos.get({...github.context.repo}).then(response => response.data as ReposGetResponseData);
|
||||
}
|
||||
|
||||
public parseRuntimeToken(): Jwt {
|
||||
return jwt_decode<Jwt>(process.env['ACTIONS_RUNTIME_TOKEN'] || '');
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
public fromPayload(path: string): any {
|
||||
return this.select(github.context.payload, path);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
private select(obj: any, path: string): any {
|
||||
if (!obj) {
|
||||
return undefined;
|
||||
}
|
||||
const i = path.indexOf('.');
|
||||
if (i < 0) {
|
||||
return obj[path];
|
||||
}
|
||||
const key = path.slice(0, i);
|
||||
return this.select(obj[key], path.slice(i + 1));
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,15 @@
|
||||
import {Context} from './context';
|
||||
import {Buildx} from './buildx';
|
||||
import {BuildKit} from './buildkit';
|
||||
import {GitHub} from './github';
|
||||
|
||||
export {BuildKit, BuildKitOpts} from './buildkit';
|
||||
export {Builder, BuilderOpts, BuilderInfo, NodeInfo} from './builder';
|
||||
export {BuildKit, BuildKitOpts} from './buildkit';
|
||||
export {Buildx, BuildxOpts} from './buildx';
|
||||
export {Context, ReposGetResponseData, Jwt} from './context';
|
||||
export {Context} from './context';
|
||||
export {Docker} from './docker';
|
||||
export {Git} from './git';
|
||||
export {GitHub, GitHubRepo, GitHubRuntimeToken} from './github';
|
||||
export {Util} from './util';
|
||||
|
||||
export interface ToolkitOpts {
|
||||
@@ -20,11 +22,13 @@ export interface ToolkitOpts {
|
||||
|
||||
export class Toolkit {
|
||||
public context: Context;
|
||||
public github: GitHub;
|
||||
public buildx: Buildx;
|
||||
public buildkit: BuildKit;
|
||||
|
||||
constructor(opts: ToolkitOpts = {}) {
|
||||
this.context = new Context(opts.githubToken);
|
||||
this.context = new Context();
|
||||
this.github = new GitHub({token: opts.githubToken});
|
||||
this.buildx = new Buildx({context: this.context});
|
||||
this.buildkit = new BuildKit({context: this.context, buildx: this.buildx});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user