Skip to content

Commit

Permalink
Add updater integration spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Harrison committed Jul 20, 2021
1 parent 17b7260 commit c4de237
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 8 deletions.
47 changes: 47 additions & 0 deletions __tests__/fixtures/job-details/npm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"data": {
"attributes": {
"allowed-updates": [
{
"dependency-type": "direct",
"update-type": "all"
}
],
"credentials-metadata": [
{
"type": "git_source",
"host": "github.com"
}
],
"dependencies": null,
"existing-pull-requests": [],
"ignore-conditions": [],
"lockfile-only": false,
"max-updater-run-time": 2700,
"package-manager": "npm_and_yarn",
"source": {
"provider": "github",
"repo": "dsp-testing/dependabot-all-updates-test",
"directory": "/",
"branch": null,
"api-endpoint": "https://api.github.com/",
"hostname": "github.com"
},
"updating-a-pull-request": false,
"update-subdependencies": false,
"requirements-update-strategy": null,
"security-advisories": [],
"security-updates-only": false,
"vendor-dependencies": false,
"reject-external-code": false,
"experiments": {},
"commit-message-options": {
"include-scope": null,
"prefix": null,
"prefix-development": null
}
},
"id": "1001",
"type": "update-jobs"
}
}
51 changes: 51 additions & 0 deletions __tests__/updater-integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Docker from 'dockerode'
import fs from 'fs'
import path from 'path'
import {Updater} from '../src/updater'

describe('Updater', () => {
const docker = new Docker()
const mockDependabotAPI: any = {
getJobDetails: jest.fn(),
getCredentials: jest.fn(),
params: {
jobID: 1,
jobToken: 'xxx',
credentialsToken: 'yyy',
dependabotAPI: 'http://localhost'
}
}
const updater = new Updater(docker, mockDependabotAPI)

afterEach(() => {
docker.listContainers(function (err, containers) {
if (!containers) return

containers.forEach(function (containerInfo) {
if (containerInfo.Image.includes('docker.pkg.github.com/dependabot/dependabot-updater')) {
console.log('removing');

docker.getContainer(containerInfo.Id).remove();
}
});
});
});

jest.setTimeout(20000)
it('should fetch manifests', async () => {
mockDependabotAPI.getJobDetails.mockImplementation(() => {
return JSON.parse(fs.readFileSync(path.join(__dirname, "fixtures/job-details/npm.json")).toString()).data.attributes
})
mockDependabotAPI.getCredentials.mockImplementation(() => {
return [{
type: "git_source",
host: "github.com",
username: "x-access-token",
password: process.env.GITHUB_TOKEN,
}]
})

await updater.pullImage()
await updater.runUpdater()
})
})
9 changes: 8 additions & 1 deletion __tests__/updater.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import {Updater} from '../src/updater'
describe('Updater', () => {
const docker = new Docker()
const mockDependabotAPI: any = {
getJobDetails: jest.fn()
getJobDetails: jest.fn(),
getCredentials: jest.fn(),
params: {
jobID: 1,
jobToken: 'xxx',
credentialsToken: 'yyy',
dependabotAPI: 'http://localhost'
}
}
const updater = new Updater(docker, mockDependabotAPI)

Expand Down
4 changes: 1 addition & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion src/dependabot-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ export type JobDetails = {
'package-manager': PackageManager
}

export type Credential = {}
export type Credential = {
type: string
host: string
username?: string
password?: string
token?: string
}

export class DependabotAPI {
constructor(
Expand All @@ -42,4 +48,16 @@ export class DependabotAPI {

return res.data.data.attributes
}

async getCredentials(): Promise<Credential[]> {
const detailsURL = `/update_jobs/${this.params.jobID}/credentials`
const res = await this.client.get(detailsURL, {
headers: {Authorization: this.params.credentialsToken}
})
if (res.status !== 200) {
throw new Error(`Unexpected status code: ${res.status}`)
}

return res.data.data.attributes.credentials
}
}
11 changes: 8 additions & 3 deletions src/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export class Updater {
async runUpdater(): Promise<void> {
try {
const details = await this.dependabotAPI.getJobDetails()
const credentials: Credential[] = [] // TODO: fetch credentials from API
const credentials = await this.dependabotAPI.getCredentials()

const files = await this.runFileFetcher(details, credentials)
await this.runFileUpdater(details, files)
} catch (e) {
Expand Down Expand Up @@ -104,9 +105,9 @@ export class Updater {
`DEPENDABOT_JOB_TOKEN=${this.dependabotAPI.params.jobToken}`,
`DEPENDABOT_JOB_PATH=${JOB_INPUT_PATH}/${JOB_INPUT_FILENAME}`,
`DEPENDABOT_OUTPUT_PATH=${JOB_OUTPUT_PATH}`,
`DEPENDABOT_API_URL=${this.dependabotAPI}`
`DEPENDABOT_API_URL=${this.dependabotAPI.params.dependabotAPI}`
],
Cmd: ['bin/run', 'fetch_files']
Cmd: ['bin/run', updaterCommand]
})

core.info(`Created ${updaterCommand} container: ${container.id}`)
Expand Down Expand Up @@ -134,6 +135,10 @@ export class Updater {
container.modem.demuxStream(stream, process.stdout, process.stderr)

await container.wait()
const jobOutput = await container.getArchive({
path: JOB_OUTPUT_PATH
})
console.log('jobOutput', jobOutput)
} finally {
await container.remove()
core.info(`Cleaned up container ${container.id}`)
Expand Down

0 comments on commit c4de237

Please sign in to comment.