Skip to content

Commit

Permalink
run container, call it "fetcher"
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Wagner committed Jun 29, 2021
1 parent 15ff84e commit ae4ba6c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 10 deletions.
1 change: 0 additions & 1 deletion __tests__/dependabot-api.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import axios, {AxiosInstance} from 'axios'
import {DependabotAPI, PackageManager} from '../src/dependabot-api'

describe('DependabotAPI', () => {
Expand Down
10 changes: 10 additions & 0 deletions __tests__/updater/fetcher.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Docker from 'dockerode'
import {runFileFetcher} from '../../src/updater/fetcher'

describe('runFileFetcher', () => {
const docker = new Docker()

it('should run the file fetcher', async () => {
await runFileFetcher(docker, 'debian:buster-slim')
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"format-check": "prettier --check **/*.ts",
"lint": "eslint src/**/*.ts",
"package": "ncc build --source-map --license licenses.txt",
"test": "jest",
"test": "jest --detectOpenHandles",
"all": "npm run build && npm run format && npm run lint && npm run package && npm test"
},
"repository": {
Expand Down
12 changes: 9 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,36 @@ import * as github from '@actions/github'
import {getJobParameters} from './inputs'
import Docker from 'dockerode'
import {runFileFetcher} from './updater/fetcher'
import {pullImage} from './updater/image'
import {DependabotAPI} from './dependabot-api'
import axios from 'axios'

const apiUrl = 'https://38d4f0538147.ngrok.io'

// FIXME: read from JobParameters? at the least this should be an updater (not core)
const updaterImage = 'dependabot/dependabot-core:0.156.3'

async function run(): Promise<void> {
try {
// Decode JobParameters:
const params = getJobParameters(github.context)
if (params === null) {
return
}
core.setSecret(params.jobToken)
core.setSecret(params.credentialsToken)

// TODO: api client: fetch job details
// Fetch JobParameters:
const client = axios.create({baseURL: apiUrl})
const api = new DependabotAPI(client, params)

const jobDetails = await api.getJobDetails()
core.info(`Details: ${JSON.stringify(jobDetails)}`)
// TODO: credentials

// TODO: the full docker jamboree
const docker = new Docker()
await runFileFetcher(docker)
await pullImage(docker, updaterImage)
await runFileFetcher(docker, updaterImage)
} catch (error) {
core.setFailed(error.message)
}
Expand Down
30 changes: 25 additions & 5 deletions src/updater/fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
import * as core from '@actions/core'
import * as Docker from 'dockerode'

export async function runFileFetcher(docker: Docker): Promise<void> {
// hello docker
const containers = await docker.listContainers()
for (const container of containers) {
core.info(`Container ${container.Id} - ${container.Names}`)
export async function runFileFetcher(
docker: Docker,
image: string
): Promise<void> {
const container = await docker.createContainer({
Image: image,
AttachStdout: true,
AttachStderr: true,
Cmd: ['/bin/bash', '-c', 'for i in `seq 3`; do echo .; sleep 1; done']
})
core.info(`Created container ${container.id}`)

try {
await container.start()
const stream = await container.attach({
stream: true,
stdout: true,
stderr: true
})
container.modem.demuxStream(stream, process.stdout, process.stderr)

await container.wait()
} finally {
await container.remove()
core.info(`Cleaned up container ${container.id}`)
}
}
10 changes: 10 additions & 0 deletions src/updater/image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as Docker from 'dockerode'

export async function pullImage(docker: Docker, image: string): Promise<void> {
const stream = await docker.pull(image)
await new Promise((resolve, reject) => {
docker.modem.followProgress(stream, (err: Error) =>
err ? reject(err) : resolve(null)
)
})
}

0 comments on commit ae4ba6c

Please sign in to comment.