global enhancements

- create context object and remove github one
- more tests and improve mocks

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-01-30 00:08:45 +01:00
parent b03f6a405c
commit c857b8425c
19 changed files with 536 additions and 459 deletions

71
src/context.ts Normal file
View File

@@ -0,0 +1,71 @@
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;
}
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) {
this.gitRef = github.context.ref;
if (github.context.sha && this.gitRef && !this.gitRef.startsWith('refs/')) {
this.gitRef = `refs/heads/${github.context.ref}`;
}
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}`);
}
public tmpDir(): string {
return this._tmpDir;
}
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));
}
}