From 3a2b93d91567460dbb37d579f56e4505509746d9 Mon Sep 17 00:00:00 2001 From: Barry Gordon Date: Mon, 13 Sep 2021 18:21:12 +0100 Subject: [PATCH] Add a CLI to build a github context and trigger a job --- .gitignore | 1 + __tests__/dependabot-api.test.ts | 3 ++- __tests__/updater-integration.test.ts | 26 +++++--------------- package.json | 3 ++- src/api-client.ts | 3 ++- src/cli.ts | 35 +++++++++++++++++++++++++++ src/inputs.ts | 7 +++++- src/main.ts | 5 +++- src/updater.ts | 2 +- 9 files changed, 59 insertions(+), 26 deletions(-) create mode 100644 src/cli.ts diff --git a/.gitignore b/.gitignore index 6467b3c..60c7624 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Updater output output/output.json repo +tmp/ # Dependency directory node_modules diff --git a/__tests__/dependabot-api.test.ts b/__tests__/dependabot-api.test.ts index a9044b7..99b816f 100644 --- a/__tests__/dependabot-api.test.ts +++ b/__tests__/dependabot-api.test.ts @@ -8,7 +8,8 @@ describe('APIClient', () => { jobId: 1, jobToken: 'xxx', credentialsToken: 'yyy', - dependabotApiUrl: 'https://localhost' + dependabotApiUrl: 'https://localhost', + dependabotApiDockerUrl: 'https://localhost' }) beforeEach(jest.clearAllMocks) diff --git a/__tests__/updater-integration.test.ts b/__tests__/updater-integration.test.ts index 04046a9..d604181 100644 --- a/__tests__/updater-integration.test.ts +++ b/__tests__/updater-integration.test.ts @@ -12,33 +12,21 @@ const FAKE_SERVER_PORT = 9000 describe('Updater', () => { let server: any - // To run the js-code itself against API: - // const params = { - // jobId: 1, - // jobToken: 'xxx', - // credentialsToken: 'xxx', - // dependabotAPI: 'http://host.docker.internal:3001' - // } - - // This runs the tests against a fake dependabot-api server using json-server - const fakeDependabotApiUrl = `http://localhost:${FAKE_SERVER_PORT}` // Used from this action to get job details and credentials - const externalDependabotApiUrl = - process.env.DEPENDABOT_API_URL || fakeDependabotApiUrl + const dependabotApiUrl = `http://localhost:${FAKE_SERVER_PORT}` // Used from within the updater container to update the job state and create prs const internalDockerHost = process.platform === 'darwin' ? 'host.docker.internal' : '172.17.0.1' - const internalDependabotApiUrl = - process.env.DEPENDABOT_API_URL || - `http://${internalDockerHost}:${FAKE_SERVER_PORT}` + const dependabotApiDockerUrl = `http://${internalDockerHost}:${FAKE_SERVER_PORT}` const params = new JobParameters( 1, process.env.JOB_TOKEN || 'job-token', process.env.CREDENTIALS_TOKEN || 'cred-token', - internalDependabotApiUrl + dependabotApiUrl, + dependabotApiDockerUrl ) - const client = axios.create({baseURL: externalDependabotApiUrl}) + const client = axios.create({baseURL: dependabotApiUrl}) const apiClient = new APIClient(client, params) beforeAll(async () => { @@ -50,9 +38,7 @@ describe('Updater', () => { await ImageService.pull(UPDATER_IMAGE_NAME) await ImageService.pull(PROXY_IMAGE_NAME) - if (externalDependabotApiUrl === fakeDependabotApiUrl) { - server = await runFakeDependabotApi(FAKE_SERVER_PORT) - } + server = await runFakeDependabotApi(FAKE_SERVER_PORT) }) afterEach(async () => { diff --git a/package.json b/package.json index 59cba59..c12ef1c 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "test": "SKIP_INTEGRATION_TESTS=true jest --detectOpenHandles", "test-integration": "jest --detectOpenHandles 'integration'", "prepare": "husky install", - "all": "npm run format && npm run lint && npm run package && npm test" + "all": "npm run format && npm run lint && npm run package && npm test", + "dependabot": "npx ts-node src/cli.ts" }, "repository": { "type": "git", diff --git a/src/api-client.ts b/src/api-client.ts index 7910e73..7b969b0 100644 --- a/src/api-client.ts +++ b/src/api-client.ts @@ -6,7 +6,8 @@ export class JobParameters { readonly jobId: number, readonly jobToken: string, readonly credentialsToken: string, - readonly dependabotApiUrl: string + readonly dependabotApiUrl: string, + readonly dependabotApiDockerUrl: string ) {} } diff --git a/src/cli.ts b/src/cli.ts new file mode 100644 index 0000000..f6036be --- /dev/null +++ b/src/cli.ts @@ -0,0 +1,35 @@ +#!/usr/bin/env node + +import {Command} from 'commander' +import {Context} from '@actions/github/lib/context' +import {run} from './main' + +const cli = new Command() + +cli + .version('0.0.1') + .description('Run an update against the specified Dependabot API service') + .requiredOption('-j, --job-id ', 'Job ID is required.') + .requiredOption('-t, --job-token ', 'Job token required.') + .requiredOption( + '-c, --credentials-token ', + 'Job credentials token is required.' + ) + .requiredOption( + '-d, --dependabot-api-url ', + 'A URL for Dependabot API is required.' + ) + .option( + '-d, --dependabot-api-docker-url ', + 'A URL to be used to access the API from Dependabot containers.' + ) + .parse(process.argv) + +const options = cli.opts() +const ctx = new Context() +ctx.eventName = 'workflow_dispatch' +ctx.payload = { + inputs: options +} + +run(ctx) diff --git a/src/inputs.ts b/src/inputs.ts index 8e9d0da..c8a7b7f 100644 --- a/src/inputs.ts +++ b/src/inputs.ts @@ -17,10 +17,15 @@ export function getJobParameters(ctx: Context): JobParameters | null { function fromWorkflowInputs(ctx: Context): JobParameters { const evt = ctx.payload as WorkflowDispatchEvent + + const dependabotApiDockerUrl = + evt.inputs.dependabotApiDockerUrl || evt.inputs.dependabotApiUrl + return new JobParameters( parseInt(evt.inputs.jobId as string, 10), evt.inputs.jobToken as string, evt.inputs.credentialsToken as string, - evt.inputs.dependabotApiUrl as string + evt.inputs.dependabotApiUrl as string, + dependabotApiDockerUrl as string ) } diff --git a/src/main.ts b/src/main.ts index 9062098..2a83d24 100644 --- a/src/main.ts +++ b/src/main.ts @@ -98,4 +98,7 @@ async function failJob( core.setFailed(error.message) } -run(github.context) +// Run the update in the current Actions context if called directly +if (require.main === module) { + run(github.context) +} diff --git a/src/updater.ts b/src/updater.ts index fb23c19..ebfcc04 100644 --- a/src/updater.ts +++ b/src/updater.ts @@ -145,7 +145,7 @@ export class Updater { `DEPENDABOT_JOB_PATH=${JOB_INPUT_PATH}/${JOB_INPUT_FILENAME}`, `DEPENDABOT_OUTPUT_PATH=${JOB_OUTPUT_PATH}/${JOB_OUTPUT_FILENAME}`, `DEPENDABOT_REPO_CONTENTS_PATH=${REPO_CONTENTS_PATH}`, - `DEPENDABOT_API_URL=${this.apiClient.params.dependabotApiUrl}`, + `DEPENDABOT_API_URL=${this.apiClient.params.dependabotApiDockerUrl}`, `SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt`, `http_proxy=${proxy.url}`, `HTTP_PROXY=${proxy.url}`,