From ab093d5470ad713966c3951b40f37f20d1d0ef81 Mon Sep 17 00:00:00 2001 From: Barry Gordon Date: Mon, 20 Sep 2021 21:21:47 +0100 Subject: [PATCH 1/3] Remove the PackageManager enum --- __tests__/dependabot-api.test.ts | 4 ++-- __tests__/proxy-integration.test.ts | 4 ++-- __tests__/updater.test.ts | 4 ++-- src/api-client.ts | 8 +------- 4 files changed, 7 insertions(+), 13 deletions(-) diff --git a/__tests__/dependabot-api.test.ts b/__tests__/dependabot-api.test.ts index 99b816f..69f9854 100644 --- a/__tests__/dependabot-api.test.ts +++ b/__tests__/dependabot-api.test.ts @@ -1,4 +1,4 @@ -import {APIClient, PackageManager} from '../src/api-client' +import {APIClient} from '../src/api-client' describe('APIClient', () => { const mockAxios: any = { @@ -34,6 +34,6 @@ describe('APIClient', () => { const jobDetails = await api.getJobDetails() expect(jobDetails['allowed-updates'].length).toBe(1) - expect(jobDetails['package-manager']).toBe(PackageManager.NpmAndYarn) + expect(jobDetails['package-manager']).toBe('npm_and_yarn') }) }) diff --git a/__tests__/proxy-integration.test.ts b/__tests__/proxy-integration.test.ts index d6f9c62..e08b194 100644 --- a/__tests__/proxy-integration.test.ts +++ b/__tests__/proxy-integration.test.ts @@ -1,5 +1,5 @@ import Docker from 'dockerode' -import {Credential, JobDetails, PackageManager} from '../src/api-client' +import {Credential, JobDetails} from '../src/api-client' import {ImageService} from '../src/image-service' import {PROXY_IMAGE_NAME} from '../src/main' import {ProxyBuilder} from '../src/proxy' @@ -17,7 +17,7 @@ describe('ProxyBuilder', () => { 'dependency-type': 'all' } ], - 'package-manager': PackageManager.NpmAndYarn + 'package-manager': 'npm_and_yarn' } const credentials: Credential[] = [ { diff --git a/__tests__/updater.test.ts b/__tests__/updater.test.ts index caa02f0..976d039 100644 --- a/__tests__/updater.test.ts +++ b/__tests__/updater.test.ts @@ -1,6 +1,5 @@ import {UPDATER_IMAGE_NAME, PROXY_IMAGE_NAME} from '../src/main' import {Updater} from '../src/updater' -import {PackageManager} from '../src/api-client' describe('Updater', () => { const mockAPIClient: any = { @@ -13,6 +12,7 @@ describe('Updater', () => { dependabotApiUrl: 'http://host.docker.internal:3001' } } + const mockJobDetails: any = { id: '1', 'allowed-updates': [ @@ -20,7 +20,7 @@ describe('Updater', () => { 'dependency-type': 'all' } ], - 'package-manage': PackageManager.NpmAndYarn + 'package-manager': 'npm-and-yarn' } const updater = new Updater( diff --git a/src/api-client.ts b/src/api-client.ts index 7b969b0..cb68563 100644 --- a/src/api-client.ts +++ b/src/api-client.ts @@ -11,19 +11,13 @@ export class JobParameters { ) {} } -// TODO: Populate with enabled values -// TODO: Rescue unsupported values -export enum PackageManager { - NpmAndYarn = 'npm_and_yarn' -} - // JobDetails are information about the repository and dependencies to be updated export type JobDetails = { 'allowed-updates': { 'dependency-type': string }[] id: string - 'package-manager': PackageManager + 'package-manager': string } export type JobError = { From 906c59287cbb6bec462d6a90ef5d36134209a0ee Mon Sep 17 00:00:00 2001 From: Barry Gordon Date: Mon, 20 Sep 2021 21:22:55 +0100 Subject: [PATCH 2/3] Rename API client test to reflect class name --- __tests__/{dependabot-api.test.ts => api_client.test.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename __tests__/{dependabot-api.test.ts => api_client.test.ts} (100%) diff --git a/__tests__/dependabot-api.test.ts b/__tests__/api_client.test.ts similarity index 100% rename from __tests__/dependabot-api.test.ts rename to __tests__/api_client.test.ts From 750e51288c7f3114d62c83f4872599c2857a9e04 Mon Sep 17 00:00:00 2001 From: Barry Gordon Date: Mon, 20 Sep 2021 21:24:51 +0100 Subject: [PATCH 3/3] Prefer strict camel case class names --- __tests__/api_client.test.ts | 6 +++--- __tests__/main.test.ts | 10 +++++----- __tests__/updater-integration.test.ts | 4 ++-- __tests__/updater.test.ts | 6 +++--- src/api-client.ts | 2 +- src/main.ts | 6 +++--- src/updater.ts | 4 ++-- 7 files changed, 19 insertions(+), 19 deletions(-) diff --git a/__tests__/api_client.test.ts b/__tests__/api_client.test.ts index 69f9854..bf5a4e7 100644 --- a/__tests__/api_client.test.ts +++ b/__tests__/api_client.test.ts @@ -1,10 +1,10 @@ -import {APIClient} from '../src/api-client' +import {ApiClient} from '../src/api-client' -describe('APIClient', () => { +describe('ApiClient', () => { const mockAxios: any = { get: jest.fn() } - const api = new APIClient(mockAxios, { + const api = new ApiClient(mockAxios, { jobId: 1, jobToken: 'xxx', credentialsToken: 'yyy', diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 8e467da..b9ef95e 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -1,6 +1,6 @@ import * as core from '@actions/core' import {Context} from '@actions/github/lib/context' -import {APIClient} from '../src/api-client' +import {ApiClient} from '../src/api-client' import {Updater} from '../src/updater' import {ImageService} from '../src/image-service' import * as inputs from '../src/inputs' @@ -21,10 +21,10 @@ describe('run', () => { beforeEach(async () => { markJobAsProcessedSpy = jest.spyOn( - APIClient.prototype, + ApiClient.prototype, 'markJobAsProcessed' ) - reportJobErrorSpy = jest.spyOn(APIClient.prototype, 'reportJobError') + reportJobErrorSpy = jest.spyOn(ApiClient.prototype, 'reportJobError') jest.spyOn(core, 'info').mockImplementation(jest.fn()) jest.spyOn(core, 'setFailed').mockImplementation(jest.fn()) @@ -114,7 +114,7 @@ describe('run', () => { describe('when there is an error retrieving job details from DependabotAPI', () => { beforeEach(() => { jest - .spyOn(APIClient.prototype, 'getJobDetails') + .spyOn(ApiClient.prototype, 'getJobDetails') .mockImplementationOnce( jest.fn(async () => Promise.reject(new Error('error getting job details')) @@ -148,7 +148,7 @@ describe('run', () => { describe('when there is an error retrieving job credentials from DependabotAPI', () => { beforeEach(() => { jest - .spyOn(APIClient.prototype, 'getCredentials') + .spyOn(ApiClient.prototype, 'getCredentials') .mockImplementationOnce( jest.fn(async () => Promise.reject(new Error('error getting credentials')) diff --git a/__tests__/updater-integration.test.ts b/__tests__/updater-integration.test.ts index 7ad1b77..def0627 100644 --- a/__tests__/updater-integration.test.ts +++ b/__tests__/updater-integration.test.ts @@ -1,6 +1,6 @@ import axios from 'axios' -import {APIClient, JobParameters} from '../src/api-client' +import {ApiClient, JobParameters} from '../src/api-client' import {ImageService} from '../src/image-service' import {UPDATER_IMAGE_NAME, PROXY_IMAGE_NAME} from '../src/main' import {Updater} from '../src/updater' @@ -27,7 +27,7 @@ describe('Updater', () => { ) const client = axios.create({baseURL: dependabotApiUrl}) - const apiClient = new APIClient(client, params) + const apiClient = new ApiClient(client, params) beforeAll(async () => { // Skip the test when we haven't preloaded the updater image diff --git a/__tests__/updater.test.ts b/__tests__/updater.test.ts index 976d039..591e312 100644 --- a/__tests__/updater.test.ts +++ b/__tests__/updater.test.ts @@ -2,7 +2,7 @@ import {UPDATER_IMAGE_NAME, PROXY_IMAGE_NAME} from '../src/main' import {Updater} from '../src/updater' describe('Updater', () => { - const mockAPIClient: any = { + const mockApiClient: any = { getJobDetails: jest.fn(), getCredentials: jest.fn(), params: { @@ -26,13 +26,13 @@ describe('Updater', () => { const updater = new Updater( UPDATER_IMAGE_NAME, PROXY_IMAGE_NAME, - mockAPIClient, + mockApiClient, mockJobDetails, [] ) it('should fetch job details', async () => { - mockAPIClient.getJobDetails.mockImplementation(() => { + mockApiClient.getJobDetails.mockImplementation(() => { throw new Error('kaboom') }) updater.runUpdater() diff --git a/src/api-client.ts b/src/api-client.ts index cb68563..9f1186d 100644 --- a/src/api-client.ts +++ b/src/api-client.ts @@ -33,7 +33,7 @@ export type Credential = { token?: string } -export class APIClient { +export class ApiClient { constructor( private readonly client: AxiosInstance, readonly params: JobParameters diff --git a/src/main.ts b/src/main.ts index 2a83d24..62e6693 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,7 +4,7 @@ import {Context} from '@actions/github/lib/context' import {getJobParameters} from './inputs' import {ImageService} from './image-service' import {Updater} from './updater' -import {APIClient} from './api-client' +import {ApiClient} from './api-client' import axios from 'axios' export const UPDATER_IMAGE_NAME = @@ -34,7 +34,7 @@ export async function run(context: Context): Promise { core.setSecret(params.credentialsToken) const client = axios.create({baseURL: params.dependabotApiUrl}) - const apiClient = new APIClient(client, params) + const apiClient = new ApiClient(client, params) try { core.info('Fetching job details') @@ -86,7 +86,7 @@ export async function run(context: Context): Promise { } async function failJob( - apiClient: APIClient, + apiClient: ApiClient, error: Error, errorType = DependabotErrorType.Unknown ): Promise { diff --git a/src/updater.ts b/src/updater.ts index ebfcc04..ec40007 100644 --- a/src/updater.ts +++ b/src/updater.ts @@ -2,7 +2,7 @@ import * as core from '@actions/core' import Docker, {Container} from 'dockerode' import path from 'path' import fs from 'fs' -import {JobDetails, APIClient, Credential} from './api-client' +import {JobDetails, ApiClient, Credential} from './api-client' import {ContainerService} from './container-service' import {base64DecodeDependencyFile} from './utils' import {DependencyFile, FetchedFiles, FileUpdaterInput} from './config-types' @@ -22,7 +22,7 @@ export class Updater { constructor( private readonly updaterImage: string, private readonly proxyImage: string, - private readonly apiClient: APIClient, + private readonly apiClient: ApiClient, private readonly details: JobDetails, private readonly credentials: Credential[] ) {