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:
@@ -1,6 +1,7 @@
|
||||
import * as exec from '@actions/exec';
|
||||
|
||||
import {Buildx} from './buildx';
|
||||
import {Context} from './context';
|
||||
|
||||
export interface BuilderInfo {
|
||||
name?: string;
|
||||
@@ -20,14 +21,21 @@ export interface NodeInfo {
|
||||
}
|
||||
|
||||
export interface BuilderOpts {
|
||||
context: Context;
|
||||
buildx?: Buildx;
|
||||
}
|
||||
|
||||
export class Builder {
|
||||
private buildx: Buildx;
|
||||
private readonly context: Context;
|
||||
private readonly buildx: Buildx;
|
||||
|
||||
constructor(opts?: BuilderOpts) {
|
||||
this.buildx = opts?.buildx || new Buildx();
|
||||
constructor(opts: BuilderOpts) {
|
||||
this.context = opts.context;
|
||||
this.buildx =
|
||||
opts?.buildx ||
|
||||
new Buildx({
|
||||
context: this.context
|
||||
});
|
||||
}
|
||||
|
||||
public async inspect(name: string): Promise<BuilderInfo> {
|
||||
|
||||
@@ -1,37 +1,36 @@
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
import * as semver from 'semver';
|
||||
import * as tmp from 'tmp';
|
||||
|
||||
import {Context} from './context';
|
||||
import {Buildx} from './buildx';
|
||||
import {Builder, BuilderInfo} from './builder';
|
||||
|
||||
export interface BuildKitOpts {
|
||||
context: Context;
|
||||
buildx?: Buildx;
|
||||
}
|
||||
|
||||
export class BuildKit {
|
||||
private buildx: Buildx;
|
||||
private readonly context: Context;
|
||||
private readonly buildx: Buildx;
|
||||
private containerNamePrefix = 'buildx_buildkit_';
|
||||
private tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-actions-toolkit-')).split(path.sep).join(path.posix.sep);
|
||||
|
||||
constructor(opts?: BuildKitOpts) {
|
||||
this.buildx = opts?.buildx || new Buildx();
|
||||
}
|
||||
|
||||
public tmpDir() {
|
||||
return this.tmpdir;
|
||||
}
|
||||
|
||||
public tmpName(options?: tmp.TmpNameOptions): string {
|
||||
return tmp.tmpNameSync(options);
|
||||
constructor(opts: BuildKitOpts) {
|
||||
this.context = opts.context;
|
||||
this.buildx =
|
||||
opts?.buildx ||
|
||||
new Buildx({
|
||||
context: this.context
|
||||
});
|
||||
}
|
||||
|
||||
private async getBuilderInfo(name: string): Promise<BuilderInfo> {
|
||||
const builder = new Builder({buildx: this.buildx});
|
||||
const builder = new Builder({
|
||||
context: this.context,
|
||||
buildx: this.buildx
|
||||
});
|
||||
return builder.inspect(name);
|
||||
}
|
||||
|
||||
@@ -118,7 +117,7 @@ export class BuildKit {
|
||||
}
|
||||
s = fs.readFileSync(s, {encoding: 'utf-8'});
|
||||
}
|
||||
const configFile = this.tmpName({tmpdir: this.tmpDir()});
|
||||
const configFile = this.context.tmpName({tmpdir: this.context.tmpDir()});
|
||||
fs.writeFileSync(configFile, s);
|
||||
return configFile;
|
||||
}
|
||||
|
||||
@@ -1,39 +1,32 @@
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import * as exec from '@actions/exec';
|
||||
import {parse} from 'csv-parse/sync';
|
||||
import * as semver from 'semver';
|
||||
import * as tmp from 'tmp';
|
||||
|
||||
import {Docker} from './docker';
|
||||
import {Context} from './context';
|
||||
|
||||
export interface BuildxOpts {
|
||||
context: Context;
|
||||
standalone?: boolean;
|
||||
}
|
||||
|
||||
export class Buildx {
|
||||
private _standalone: boolean;
|
||||
private _version: Promise<string>;
|
||||
private _tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-actions-toolkit-')).split(path.sep).join(path.posix.sep);
|
||||
private readonly context: Context;
|
||||
public standalone: boolean;
|
||||
public version: Promise<string>;
|
||||
|
||||
constructor(opts?: BuildxOpts) {
|
||||
this._standalone = opts?.standalone ?? !Docker.isAvailable();
|
||||
this._version = this.getVersion();
|
||||
}
|
||||
|
||||
public tmpDir() {
|
||||
return this._tmpdir;
|
||||
}
|
||||
|
||||
public tmpName(options?: tmp.TmpNameOptions): string {
|
||||
return tmp.tmpNameSync(options);
|
||||
constructor(opts: BuildxOpts) {
|
||||
this.context = opts.context;
|
||||
this.standalone = opts?.standalone ?? !Docker.isAvailable();
|
||||
this.version = this.getVersion();
|
||||
}
|
||||
|
||||
public getCommand(args: Array<string>) {
|
||||
return {
|
||||
command: this._standalone ? 'buildx' : 'docker',
|
||||
args: this._standalone ? args : ['buildx', ...args]
|
||||
command: this.standalone ? 'buildx' : 'docker',
|
||||
args: this.standalone ? args : ['buildx', ...args]
|
||||
};
|
||||
}
|
||||
|
||||
@@ -71,10 +64,6 @@ export class Buildx {
|
||||
});
|
||||
}
|
||||
|
||||
public async version(): Promise<string> {
|
||||
return this._version;
|
||||
}
|
||||
|
||||
public async printVersion() {
|
||||
const cmd = this.getCommand(['version']);
|
||||
await exec.exec(cmd.command, cmd.args, {
|
||||
@@ -91,16 +80,16 @@ export class Buildx {
|
||||
}
|
||||
|
||||
public async versionSatisfies(range: string, version?: string): Promise<boolean> {
|
||||
const ver = version ?? (await this.version());
|
||||
const ver = version ?? (await this.version);
|
||||
return semver.satisfies(ver, range) || /^[0-9a-f]{7}$/.exec(ver) !== null;
|
||||
}
|
||||
|
||||
public getBuildImageIDFilePath(): string {
|
||||
return path.join(this.tmpDir(), 'iidfile').split(path.sep).join(path.posix.sep);
|
||||
return path.join(this.context.tmpDir(), 'iidfile').split(path.sep).join(path.posix.sep);
|
||||
}
|
||||
|
||||
public getBuildMetadataFilePath(): string {
|
||||
return path.join(this.tmpDir(), 'metadata-file').split(path.sep).join(path.posix.sep);
|
||||
return path.join(this.context.tmpDir(), 'metadata-file').split(path.sep).join(path.posix.sep);
|
||||
}
|
||||
|
||||
public getBuildImageID(): string | undefined {
|
||||
@@ -143,7 +132,7 @@ export class Buildx {
|
||||
return this.generateBuildSecret(kvp, true);
|
||||
}
|
||||
|
||||
private generateBuildSecret(kvp: string, file: boolean): string {
|
||||
public generateBuildSecret(kvp: string, file: boolean): string {
|
||||
const delimiterIndex = kvp.indexOf('=');
|
||||
const key = kvp.substring(0, delimiterIndex);
|
||||
let value = kvp.substring(delimiterIndex + 1);
|
||||
@@ -156,7 +145,7 @@ export class Buildx {
|
||||
}
|
||||
value = fs.readFileSync(value, {encoding: 'utf-8'});
|
||||
}
|
||||
const secretFile = this.tmpName({tmpdir: this.tmpDir()});
|
||||
const secretFile = this.context.tmpName({tmpdir: this.context.tmpDir()});
|
||||
fs.writeFileSync(secretFile, value);
|
||||
return `id=${key},src=${secretFile}`;
|
||||
}
|
||||
|
||||
71
src/context.ts
Normal file
71
src/context.ts
Normal 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));
|
||||
}
|
||||
}
|
||||
34
src/git.ts
34
src/git.ts
@@ -1,19 +1,21 @@
|
||||
import * as exec from '@actions/exec';
|
||||
|
||||
export async function getRemoteSha(repo: string, ref: string): Promise<string> {
|
||||
return await exec
|
||||
.getExecOutput(`git`, ['ls-remote', repo, ref], {
|
||||
ignoreReturnCode: true,
|
||||
silent: true
|
||||
})
|
||||
.then(res => {
|
||||
if (res.stderr.length > 0 && res.exitCode != 0) {
|
||||
throw new Error(res.stderr);
|
||||
}
|
||||
const [rsha] = res.stdout.trim().split(/[\s\t]/);
|
||||
if (rsha.length == 0) {
|
||||
throw new Error(`Cannot find remote ref for ${repo}#${ref}`);
|
||||
}
|
||||
return rsha;
|
||||
});
|
||||
export class Git {
|
||||
public static async getRemoteSha(repo: string, ref: string): Promise<string> {
|
||||
return await exec
|
||||
.getExecOutput(`git`, ['ls-remote', repo, ref], {
|
||||
ignoreReturnCode: true,
|
||||
silent: true
|
||||
})
|
||||
.then(res => {
|
||||
if (res.stderr.length > 0 && res.exitCode != 0) {
|
||||
throw new Error(res.stderr);
|
||||
}
|
||||
const [rsha] = res.stdout.trim().split(/[\s\t]/);
|
||||
if (rsha.length == 0) {
|
||||
throw new Error(`Cannot find remote ref for ${repo}#${ref}`);
|
||||
}
|
||||
return rsha;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
import jwt_decode, {JwtPayload} from 'jwt-decode';
|
||||
import * as github from '@actions/github';
|
||||
import {Context} from '@actions/github/lib/context';
|
||||
import {components as OctoOpenApiTypes} from '@octokit/openapi-types';
|
||||
import {WebhookPayload} from '@actions/github/lib/interfaces';
|
||||
|
||||
export type Payload = WebhookPayload;
|
||||
export type ReposGetResponseData = OctoOpenApiTypes['schemas']['repository'];
|
||||
|
||||
interface Jwt extends JwtPayload {
|
||||
ac?: string;
|
||||
}
|
||||
|
||||
export class GitHub {
|
||||
private static instance?: GitHub;
|
||||
static getInstance = (): GitHub => (GitHub.instance = GitHub.instance ?? new GitHub());
|
||||
private static _serverURL: string;
|
||||
private static _gitContext: string;
|
||||
private static _provenanceBuilderID: string;
|
||||
|
||||
private constructor() {
|
||||
let ref = this.ref();
|
||||
if (github.context.sha && ref && !ref.startsWith('refs/')) {
|
||||
ref = `refs/heads/${this.ref()}`;
|
||||
}
|
||||
if (github.context.sha && !ref.startsWith(`refs/pull/`)) {
|
||||
ref = github.context.sha;
|
||||
}
|
||||
GitHub._serverURL = process.env.GITHUB_SERVER_URL || 'https://github.com';
|
||||
GitHub._gitContext = `${GitHub._serverURL}/${github.context.repo.owner}/${github.context.repo.repo}.git#${ref}`;
|
||||
GitHub._provenanceBuilderID = `${GitHub._serverURL}/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${github.context.runId}`;
|
||||
}
|
||||
|
||||
public reset() {
|
||||
GitHub.instance = undefined;
|
||||
}
|
||||
|
||||
public context(): Context {
|
||||
return github.context;
|
||||
}
|
||||
|
||||
private ref(): string {
|
||||
return github.context.ref;
|
||||
}
|
||||
|
||||
public serverURL() {
|
||||
return GitHub._serverURL;
|
||||
}
|
||||
|
||||
public gitContext() {
|
||||
return GitHub._gitContext;
|
||||
}
|
||||
|
||||
public provenanceBuilderID() {
|
||||
return GitHub._provenanceBuilderID;
|
||||
}
|
||||
|
||||
private payload(): Payload {
|
||||
return github.context.payload;
|
||||
}
|
||||
|
||||
public repo(token: string): Promise<ReposGetResponseData> {
|
||||
return github
|
||||
.getOctokit(token)
|
||||
.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(this.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));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
export * as buildkit from './buildkit';
|
||||
export * as buildx from './buildx';
|
||||
export * as docker from './docker';
|
||||
export * as git from './git';
|
||||
export * as github from './github';
|
||||
export * as util from './util';
|
||||
31
src/toolkit.ts
Normal file
31
src/toolkit.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import {Context} from './context';
|
||||
import {Buildx} from './buildx';
|
||||
import {BuildKit} from './buildkit';
|
||||
|
||||
export {BuildKit, BuildKitOpts} from './buildkit';
|
||||
export {Builder, BuilderOpts, BuilderInfo, NodeInfo} from './builder';
|
||||
export {Buildx, BuildxOpts} from './buildx';
|
||||
export {Context, ReposGetResponseData, Jwt} from './context';
|
||||
export {Docker} from './docker';
|
||||
export {Git} from './git';
|
||||
export {Util} from './util';
|
||||
|
||||
export interface ToolkitOpts {
|
||||
/**
|
||||
* GitHub token to use for authentication.
|
||||
* Uses `process.env.GITHUB_TOKEN` by default.
|
||||
*/
|
||||
githubToken?: string;
|
||||
}
|
||||
|
||||
export class Toolkit {
|
||||
public context: Context;
|
||||
public buildx: Buildx;
|
||||
public buildkit: BuildKit;
|
||||
|
||||
constructor(opts: ToolkitOpts = {}) {
|
||||
this.context = new Context(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