mirror of
https://github.com/docker/actions-toolkit.git
synced 2024-11-26 22:26:08 +08:00
move GitHub specific to its own class
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
parent
bb369fa859
commit
85ef93202e
@ -3,22 +3,18 @@ import path from 'path';
|
|||||||
import rimraf from 'rimraf';
|
import rimraf from 'rimraf';
|
||||||
import {describe, expect, jest, it, beforeEach, afterEach} from '@jest/globals';
|
import {describe, expect, jest, it, beforeEach, afterEach} from '@jest/globals';
|
||||||
|
|
||||||
import {Context, ReposGetResponseData} from '../src/context';
|
import {Context} from '../src/context';
|
||||||
|
|
||||||
const tmpDir = path.join('/tmp/.docker-actions-toolkit-jest').split(path.sep).join(path.posix.sep);
|
const tmpDir = path.join('/tmp/.docker-actions-toolkit-jest').split(path.sep).join(path.posix.sep);
|
||||||
const tmpName = path.join(tmpDir, '.tmpname-jest').split(path.sep).join(path.posix.sep);
|
const tmpName = path.join(tmpDir, '.tmpname-jest').split(path.sep).join(path.posix.sep);
|
||||||
|
|
||||||
beforeEach(() => {
|
jest.spyOn(Context.prototype, 'tmpDir').mockImplementation((): string => {
|
||||||
jest.clearAllMocks();
|
|
||||||
});
|
|
||||||
|
|
||||||
jest.spyOn(Context.prototype as any, 'tmpDir').mockImplementation((): string => {
|
|
||||||
if (!fs.existsSync(tmpDir)) {
|
if (!fs.existsSync(tmpDir)) {
|
||||||
fs.mkdirSync(tmpDir, {recursive: true});
|
fs.mkdirSync(tmpDir, {recursive: true});
|
||||||
}
|
}
|
||||||
return tmpDir;
|
return tmpDir;
|
||||||
});
|
});
|
||||||
jest.spyOn(Context.prototype as any, 'tmpName').mockImplementation((): string => {
|
jest.spyOn(Context.prototype, 'tmpName').mockImplementation((): string => {
|
||||||
return tmpName;
|
return tmpName;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -30,34 +26,6 @@ afterEach(() => {
|
|||||||
rimraf.sync(tmpDir);
|
rimraf.sync(tmpDir);
|
||||||
});
|
});
|
||||||
|
|
||||||
import * as repoFixture from './fixtures/repo.json';
|
|
||||||
jest.spyOn(Context.prototype as any, 'repoData').mockImplementation((): Promise<ReposGetResponseData> => {
|
|
||||||
return <Promise<ReposGetResponseData>>(repoFixture as unknown);
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('serverURL', () => {
|
|
||||||
const originalEnv = process.env;
|
|
||||||
beforeEach(() => {
|
|
||||||
jest.resetModules();
|
|
||||||
process.env = {
|
|
||||||
...originalEnv,
|
|
||||||
GITHUB_SERVER_URL: 'https://foo.github.com'
|
|
||||||
};
|
|
||||||
});
|
|
||||||
afterEach(() => {
|
|
||||||
process.env = originalEnv;
|
|
||||||
});
|
|
||||||
it('returns default', async () => {
|
|
||||||
process.env.GITHUB_SERVER_URL = '';
|
|
||||||
const context = new Context();
|
|
||||||
expect(context.serverURL).toEqual('https://github.com');
|
|
||||||
});
|
|
||||||
it('returns from env', async () => {
|
|
||||||
const context = new Context();
|
|
||||||
expect(context.serverURL).toEqual('https://foo.github.com');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('gitContext', () => {
|
describe('gitContext', () => {
|
||||||
it('returns refs/heads/master', async () => {
|
it('returns refs/heads/master', async () => {
|
||||||
const context = new Context();
|
const context = new Context();
|
||||||
@ -71,17 +39,3 @@ describe('provenanceBuilderID', () => {
|
|||||||
expect(context.provenanceBuilderID).toEqual('https://github.com/docker/actions-toolkit/actions/runs/123');
|
expect(context.provenanceBuilderID).toEqual('https://github.com/docker/actions-toolkit/actions/runs/123');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('repo', () => {
|
|
||||||
it('returns GitHub repository', async () => {
|
|
||||||
const context = new Context();
|
|
||||||
expect((await context.repoData()).name).toEqual('Hello-World');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('fromPayload', () => {
|
|
||||||
it('returns repository name from payload', async () => {
|
|
||||||
const context = new Context();
|
|
||||||
expect(await context.fromPayload('repository.name')).toEqual('test-docker-action');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
53
__tests__/github.test.ts
Normal file
53
__tests__/github.test.ts
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import {describe, expect, jest, it, beforeEach, afterEach} from '@jest/globals';
|
||||||
|
|
||||||
|
import {GitHub, GitHubRepo} from '../src/github';
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
import * as repoFixture from './fixtures/repo.json';
|
||||||
|
jest.spyOn(GitHub.prototype, 'repoData').mockImplementation((): Promise<GitHubRepo> => {
|
||||||
|
return <Promise<GitHubRepo>>(repoFixture as unknown);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('context', () => {
|
||||||
|
it('returns repository name from payload', async () => {
|
||||||
|
const github = new GitHub();
|
||||||
|
expect(github.context.payload.repository?.name).toEqual('test-docker-action');
|
||||||
|
});
|
||||||
|
it('is repository private', async () => {
|
||||||
|
const github = new GitHub();
|
||||||
|
expect(github.context.payload.repository?.private).toEqual(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('serverURL', () => {
|
||||||
|
const originalEnv = process.env;
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.resetModules();
|
||||||
|
process.env = {
|
||||||
|
...originalEnv,
|
||||||
|
GITHUB_SERVER_URL: 'https://foo.github.com'
|
||||||
|
};
|
||||||
|
});
|
||||||
|
afterEach(() => {
|
||||||
|
process.env = originalEnv;
|
||||||
|
});
|
||||||
|
it('returns default', async () => {
|
||||||
|
process.env.GITHUB_SERVER_URL = '';
|
||||||
|
const github = new GitHub();
|
||||||
|
expect(github.serverURL).toEqual('https://github.com');
|
||||||
|
});
|
||||||
|
it('returns from env', async () => {
|
||||||
|
const github = new GitHub();
|
||||||
|
expect(github.serverURL).toEqual('https://foo.github.com');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('repoData', () => {
|
||||||
|
it('returns GitHub repository', async () => {
|
||||||
|
const github = new GitHub();
|
||||||
|
expect((await github.repoData()).name).toEqual('Hello-World');
|
||||||
|
});
|
||||||
|
});
|
@ -2,26 +2,18 @@ import fs from 'fs';
|
|||||||
import os from 'os';
|
import os from 'os';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import * as tmp from 'tmp';
|
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 * as github from '@actions/github';
|
||||||
import {components as OctoOpenApiTypes} from '@octokit/openapi-types';
|
|
||||||
|
|
||||||
export type ReposGetResponseData = OctoOpenApiTypes['schemas']['repository'];
|
import {GitHub} from './github';
|
||||||
export interface Jwt extends JwtPayload {
|
|
||||||
ac?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class Context {
|
export class Context {
|
||||||
public serverURL: string;
|
|
||||||
public gitRef: string;
|
public gitRef: string;
|
||||||
public buildGitContext: string;
|
public buildGitContext: string;
|
||||||
public provenanceBuilderID: 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);
|
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;
|
this.gitRef = github.context.ref;
|
||||||
if (github.context.sha && this.gitRef && !this.gitRef.startsWith('refs/')) {
|
if (github.context.sha && this.gitRef && !this.gitRef.startsWith('refs/')) {
|
||||||
this.gitRef = `refs/heads/${github.context.ref}`;
|
this.gitRef = `refs/heads/${github.context.ref}`;
|
||||||
@ -29,10 +21,8 @@ export class Context {
|
|||||||
if (github.context.sha && !this.gitRef.startsWith(`refs/pull/`)) {
|
if (github.context.sha && !this.gitRef.startsWith(`refs/pull/`)) {
|
||||||
this.gitRef = github.context.sha;
|
this.gitRef = github.context.sha;
|
||||||
}
|
}
|
||||||
this.serverURL = process.env.GITHUB_SERVER_URL || 'https://github.com';
|
this.buildGitContext = `${GitHub.serverURL}/${github.context.repo.owner}/${github.context.repo.repo}.git#${this.gitRef}`;
|
||||||
this.buildGitContext = `${this.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}`;
|
||||||
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 {
|
public tmpDir(): string {
|
||||||
@ -42,30 +32,4 @@ export class Context {
|
|||||||
public tmpName(options?: tmp.TmpNameOptions): string {
|
public tmpName(options?: tmp.TmpNameOptions): string {
|
||||||
return tmp.tmpNameSync(options);
|
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 {Context} from './context';
|
||||||
import {Buildx} from './buildx';
|
import {Buildx} from './buildx';
|
||||||
import {BuildKit} from './buildkit';
|
import {BuildKit} from './buildkit';
|
||||||
|
import {GitHub} from './github';
|
||||||
|
|
||||||
export {BuildKit, BuildKitOpts} from './buildkit';
|
|
||||||
export {Builder, BuilderOpts, BuilderInfo, NodeInfo} from './builder';
|
export {Builder, BuilderOpts, BuilderInfo, NodeInfo} from './builder';
|
||||||
|
export {BuildKit, BuildKitOpts} from './buildkit';
|
||||||
export {Buildx, BuildxOpts} from './buildx';
|
export {Buildx, BuildxOpts} from './buildx';
|
||||||
export {Context, ReposGetResponseData, Jwt} from './context';
|
export {Context} from './context';
|
||||||
export {Docker} from './docker';
|
export {Docker} from './docker';
|
||||||
export {Git} from './git';
|
export {Git} from './git';
|
||||||
|
export {GitHub, GitHubRepo, GitHubRuntimeToken} from './github';
|
||||||
export {Util} from './util';
|
export {Util} from './util';
|
||||||
|
|
||||||
export interface ToolkitOpts {
|
export interface ToolkitOpts {
|
||||||
@ -20,11 +22,13 @@ export interface ToolkitOpts {
|
|||||||
|
|
||||||
export class Toolkit {
|
export class Toolkit {
|
||||||
public context: Context;
|
public context: Context;
|
||||||
|
public github: GitHub;
|
||||||
public buildx: Buildx;
|
public buildx: Buildx;
|
||||||
public buildkit: BuildKit;
|
public buildkit: BuildKit;
|
||||||
|
|
||||||
constructor(opts: ToolkitOpts = {}) {
|
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.buildx = new Buildx({context: this.context});
|
||||||
this.buildkit = new BuildKit({context: this.context, buildx: this.buildx});
|
this.buildkit = new BuildKit({context: this.context, buildx: this.buildx});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user