Skip to content

Commit

Permalink
Add a CLI to build a github context and trigger a job
Browse files Browse the repository at this point in the history
  • Loading branch information
Barry Gordon committed Sep 13, 2021
1 parent 29644f5 commit 3a2b93d
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Updater output
output/output.json
repo
tmp/

# Dependency directory
node_modules
Expand Down
3 changes: 2 additions & 1 deletion __tests__/dependabot-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ describe('APIClient', () => {
jobId: 1,
jobToken: 'xxx',
credentialsToken: 'yyy',
dependabotApiUrl: 'https://localhost'
dependabotApiUrl: 'https://localhost',
dependabotApiDockerUrl: 'https://localhost'
})
beforeEach(jest.clearAllMocks)

Expand Down
26 changes: 6 additions & 20 deletions __tests__/updater-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {}
}

Expand Down
35 changes: 35 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -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 <id>', 'Job ID is required.')
.requiredOption('-t, --job-token <token>', 'Job token required.')
.requiredOption(
'-c, --credentials-token <token>',
'Job credentials token is required.'
)
.requiredOption(
'-d, --dependabot-api-url <url>',
'A URL for Dependabot API is required.'
)
.option(
'-d, --dependabot-api-docker-url <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)
7 changes: 6 additions & 1 deletion src/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
5 changes: 4 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion src/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down

0 comments on commit 3a2b93d

Please sign in to comment.