diff --git a/__tests__/api_client.test.ts b/__tests__/dependabot-api.test.ts similarity index 80% rename from __tests__/api_client.test.ts rename to __tests__/dependabot-api.test.ts index bf5a4e7..99b816f 100644 --- a/__tests__/api_client.test.ts +++ b/__tests__/dependabot-api.test.ts @@ -1,10 +1,10 @@ -import {ApiClient} from '../src/api-client' +import {APIClient, PackageManager} 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', @@ -34,6 +34,6 @@ describe('ApiClient', () => { const jobDetails = await api.getJobDetails() expect(jobDetails['allowed-updates'].length).toBe(1) - expect(jobDetails['package-manager']).toBe('npm_and_yarn') + expect(jobDetails['package-manager']).toBe(PackageManager.NpmAndYarn) }) }) diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index b9ef95e..8e467da 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__/proxy-integration.test.ts b/__tests__/proxy-integration.test.ts index e08b194..d6f9c62 100644 --- a/__tests__/proxy-integration.test.ts +++ b/__tests__/proxy-integration.test.ts @@ -1,5 +1,5 @@ import Docker from 'dockerode' -import {Credential, JobDetails} from '../src/api-client' +import {Credential, JobDetails, PackageManager} 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': 'npm_and_yarn' + 'package-manager': PackageManager.NpmAndYarn } const credentials: Credential[] = [ { diff --git a/__tests__/updater-integration.test.ts b/__tests__/updater-integration.test.ts index def0627..7ad1b77 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 591e312..caa02f0 100644 --- a/__tests__/updater.test.ts +++ b/__tests__/updater.test.ts @@ -1,8 +1,9 @@ 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 = { + const mockAPIClient: any = { getJobDetails: jest.fn(), getCredentials: jest.fn(), params: { @@ -12,7 +13,6 @@ describe('Updater', () => { dependabotApiUrl: 'http://host.docker.internal:3001' } } - const mockJobDetails: any = { id: '1', 'allowed-updates': [ @@ -20,19 +20,19 @@ describe('Updater', () => { 'dependency-type': 'all' } ], - 'package-manager': 'npm-and-yarn' + 'package-manage': PackageManager.NpmAndYarn } 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 9f1186d..7b969b0 100644 --- a/src/api-client.ts +++ b/src/api-client.ts @@ -11,13 +11,19 @@ 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': string + 'package-manager': PackageManager } export type JobError = { @@ -33,7 +39,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 62e6693..2a83d24 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 ec40007..ebfcc04 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[] ) {