Skip to content

Commit

Permalink
Merge pull request #49 from github/feelepxyz/rename-api-client-url
Browse files Browse the repository at this point in the history
Rename api client and url
  • Loading branch information
Philip Harrison authored and GitHub committed Jul 26, 2021
2 parents eab8bd2 + f2c1198 commit f99526c
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 31 deletions.
8 changes: 4 additions & 4 deletions __tests__/dependabot-api.test.ts
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
10 changes: 5 additions & 5 deletions __tests__/updater-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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',
Expand Down
8 changes: 4 additions & 4 deletions __tests__/updater.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions script/hack-event.json
Original file line number Diff line number Diff line change
Expand Up @@ -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=",
Expand Down Expand Up @@ -123,4 +123,4 @@
"type": "User",
"url": "https://api.github.com/users/thepwagner"
}
}
}
4 changes: 2 additions & 2 deletions src/dependabot-api.ts → src/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class JobParameters {
readonly jobID: number,
readonly jobToken: string,
readonly credentialsToken: string,
readonly dependabotAPI: string
readonly dependabotAPIURL: string
) {}
}

Expand All @@ -32,7 +32,7 @@ export type Credential = {
token?: string
}

export class DependabotAPI {
export class APIClient {
constructor(
private readonly client: AxiosInstance,
readonly params: JobParameters
Expand Down
6 changes: 3 additions & 3 deletions src/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
}

Expand All @@ -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
)
}
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
Expand All @@ -17,8 +17,8 @@ async function run(): Promise<void> {
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()

Expand Down
16 changes: 8 additions & 8 deletions src/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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
) {}

Expand Down Expand Up @@ -60,8 +60,8 @@ export class Updater {
*/
async runUpdater(): Promise<void> {
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

Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f99526c

Please sign in to comment.