From f2c119864bb4fc277a5c2d6f40f3be5aa7ba93c8 Mon Sep 17 00:00:00 2001 From: Philip Harrison Date: Mon, 26 Jul 2021 13:00:07 +0100 Subject: [PATCH] Rename api client and url Disambuagiate the api client from the dependabot api url. --- __tests__/dependabot-api.test.ts | 8 ++++---- __tests__/updater-integration.test.ts | 10 +++++----- __tests__/updater.test.ts | 8 ++++---- script/hack-event.json | 4 ++-- src/{dependabot-api.ts => api-client.ts} | 4 ++-- src/inputs.ts | 6 +++--- src/main.ts | 6 +++--- src/updater.ts | 16 ++++++++-------- 8 files changed, 31 insertions(+), 31 deletions(-) rename src/{dependabot-api.ts => api-client.ts} (96%) diff --git a/__tests__/dependabot-api.test.ts b/__tests__/dependabot-api.test.ts index 4f3c285..f77acfc 100644 --- a/__tests__/dependabot-api.test.ts +++ b/__tests__/dependabot-api.test.ts @@ -1,14 +1,14 @@ -import {DependabotAPI, PackageManager} from '../src/dependabot-api' +import {APIClient, PackageManager} from '../src/api-client' -describe('DependabotAPI', () => { +describe('APIClient', () => { const mockAxios: any = { get: jest.fn() } - const api = new DependabotAPI(mockAxios, { + const api = new APIClient(mockAxios, { jobID: 1, jobToken: 'xxx', credentialsToken: 'yyy', - dependabotAPI: 'https://localhost' + dependabotAPIURL: 'https://localhost' }) beforeEach(jest.clearAllMocks) diff --git a/__tests__/updater-integration.test.ts b/__tests__/updater-integration.test.ts index 1e6eb57..3bb4df6 100644 --- a/__tests__/updater-integration.test.ts +++ b/__tests__/updater-integration.test.ts @@ -18,17 +18,17 @@ describe('Updater', () => { // This stubs out API calls from JS, but will run the updater against an API // running on the specified API endpoint. - const mockDependabotAPI: any = { + const mockAPIClient: any = { getJobDetails: jest.fn(), getCredentials: jest.fn(), params: { jobID: 1, jobToken: 'xxx', credentialsToken: 'yyy', - dependabotAPI: 'http://host.docker.internal:3001' + dependabotAPIURL: 'http://host.docker.internal:3001' } } - const updater = new Updater(docker, mockDependabotAPI) + const updater = new Updater(docker, mockAPIClient) beforeAll(() => { updater.pullImage() @@ -54,14 +54,14 @@ describe('Updater', () => { jest.setTimeout(20000) it('should fetch manifests', async () => { - mockDependabotAPI.getJobDetails.mockImplementation(() => { + mockAPIClient.getJobDetails.mockImplementation(() => { return JSON.parse( fs .readFileSync(path.join(__dirname, 'fixtures/job-details/npm.json')) .toString() ).data.attributes }) - mockDependabotAPI.getCredentials.mockImplementation(() => { + mockAPIClient.getCredentials.mockImplementation(() => { return [ { type: 'git_source', diff --git a/__tests__/updater.test.ts b/__tests__/updater.test.ts index 5ba8e08..519c709 100644 --- a/__tests__/updater.test.ts +++ b/__tests__/updater.test.ts @@ -3,20 +3,20 @@ import {Updater} from '../src/updater' describe('Updater', () => { const docker = new Docker() - const mockDependabotAPI: any = { + const mockAPIClient: any = { getJobDetails: jest.fn(), getCredentials: jest.fn(), params: { jobID: 1, jobToken: 'xxx', credentialsToken: 'yyy', - dependabotAPI: 'http://localhost' + dependabotAPIURL: 'http://localhost' } } - const updater = new Updater(docker, mockDependabotAPI) + const updater = new Updater(docker, mockAPIClient) it('should fetch job details', async () => { - mockDependabotAPI.getJobDetails.mockImplementation(() => { + mockAPIClient.getJobDetails.mockImplementation(() => { throw new Error('kaboom') }) updater.runUpdater() diff --git a/script/hack-event.json b/script/hack-event.json index dc51c92..b4df481 100644 --- a/script/hack-event.json +++ b/script/hack-event.json @@ -2,7 +2,7 @@ "action": "on-demand-test", "branch": "master", "client_payload": { - "dependabotAPI": "https://3c3fcfad18d1.ngrok.io", + "dependabotAPIURL": "https://3c3fcfad18d1.ngrok.io", "jobID": "8", "jobToken": "QkNM+m1lDFieJ0V6DAwUY+hG6PUKItjuS7EApTVsTQw=", "credentialsToken": "hMAqnnY+CJGUCQjKRUzSZnR+DnbW5YEA5nL2xh8zovk=", @@ -123,4 +123,4 @@ "type": "User", "url": "https://api.github.com/users/thepwagner" } -} \ No newline at end of file +} diff --git a/src/dependabot-api.ts b/src/api-client.ts similarity index 96% rename from src/dependabot-api.ts rename to src/api-client.ts index ffab5c9..a047b6c 100644 --- a/src/dependabot-api.ts +++ b/src/api-client.ts @@ -6,7 +6,7 @@ export class JobParameters { readonly jobID: number, readonly jobToken: string, readonly credentialsToken: string, - readonly dependabotAPI: string + readonly dependabotAPIURL: string ) {} } @@ -32,7 +32,7 @@ export type Credential = { token?: string } -export class DependabotAPI { +export class APIClient { constructor( private readonly client: AxiosInstance, readonly params: JobParameters diff --git a/src/inputs.ts b/src/inputs.ts index c05dec9..d9b7c74 100644 --- a/src/inputs.ts +++ b/src/inputs.ts @@ -4,7 +4,7 @@ import { RepositoryDispatchEvent, WorkflowDispatchEvent } from '@octokit/webhooks-definitions/schema' -import {JobParameters} from './dependabot-api' +import {JobParameters} from './api-client' // FIXME: '@octokit/webhooks-definitions' assumes this is the only repository_dispatch event type, workaround that // https://github.com/octokit/webhooks/blob/0b04a009507aa35811e91a10703bbb2a33bdeff4/payload-schemas/schemas/repository_dispatch/on-demand-test.schema.json#L14 @@ -28,7 +28,7 @@ function fromWorkflowInputs(ctx: Context): JobParameters { parseInt(evt.inputs.jobID as string, 10), evt.inputs.jobToken as string, evt.inputs.credentialsToken as string, - evt.inputs.dependabotAPI as string + evt.inputs.dependabotAPIURL as string ) } @@ -43,6 +43,6 @@ function fromRepoDispatch(ctx: Context): JobParameters | null { payload.jobID as number, payload.jobToken as string, payload.credentialsToken as string, - payload.dependabotAPI as string + payload.dependabotAPIURL as string ) } diff --git a/src/main.ts b/src/main.ts index 6337f63..f3431e1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,7 +3,7 @@ import * as github from '@actions/github' import {getJobParameters} from './inputs' import Docker from 'dockerode' import {Updater} from './updater' -import {DependabotAPI} from './dependabot-api' +import {APIClient} from './api-client' import axios from 'axios' async function run(): Promise { @@ -17,8 +17,8 @@ async function run(): Promise { core.setSecret(params.credentialsToken) const docker = new Docker() - const client = axios.create({baseURL: params.dependabotAPI}) - const api = new DependabotAPI(client, params) + const client = axios.create({baseURL: params.dependabotAPIURL}) + const api = new APIClient(client, params) const updater = new Updater(docker, api) await updater.pullImage() diff --git a/src/updater.ts b/src/updater.ts index a221993..57b9f75 100644 --- a/src/updater.ts +++ b/src/updater.ts @@ -2,7 +2,7 @@ import * as core from '@actions/core' import * as Docker from 'dockerode' import path from 'path' import fs from 'fs' -import {Credential, JobDetails, DependabotAPI} from './dependabot-api' +import {Credential, JobDetails, APIClient} from './api-client' import {Readable} from 'stream' import {pack} from 'tar-stream' @@ -19,7 +19,7 @@ const decode = (str: string): string => export class Updater { constructor( private readonly docker: Docker, - private readonly dependabotAPI: DependabotAPI, + private readonly apiClient: APIClient, private readonly updaterImage = DEFAULT_UPDATER_IMAGE ) {} @@ -60,8 +60,8 @@ export class Updater { */ async runUpdater(): Promise { try { - const details = await this.dependabotAPI.getJobDetails() - const credentials = await this.dependabotAPI.getCredentials() + const details = await this.apiClient.getJobDetails() + const credentials = await this.apiClient.getCredentials() // TODO: once the proxy is set up, remove credentials from the job details details['credentials'] = credentials @@ -140,11 +140,11 @@ export class Updater { AttachStdout: true, AttachStderr: true, Env: [ - `DEPENDABOT_JOB_ID=${this.dependabotAPI.params.jobID}`, - `DEPENDABOT_JOB_TOKEN=${this.dependabotAPI.params.jobToken}`, + `DEPENDABOT_JOB_ID=${this.apiClient.params.jobID}`, + `DEPENDABOT_JOB_TOKEN=${this.apiClient.params.jobToken}`, `DEPENDABOT_JOB_PATH=${JOB_INPUT_PATH}/${JOB_INPUT_FILENAME}`, `DEPENDABOT_OUTPUT_PATH=${JOB_OUTPUT_PATH}`, - `DEPENDABOT_API_URL=${this.dependabotAPI.params.dependabotAPI}` + `DEPENDABOT_API_URL=${this.apiClient.params.dependabotAPIURL}` ], Cmd: ['bin/run', updaterCommand], HostConfig: { @@ -182,7 +182,7 @@ export class Updater { container.modem.demuxStream(stream, process.stdout, process.stderr) const network = this.docker.getNetwork('host') - network.connect({Container: container}, (err, data) => core.info(err)) + network.connect({Container: container}, err => core.info(err)) await container.wait() } finally {