Skip to content

Commit

Permalink
Check if files exist after file fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Harrison committed Jul 21, 2021
1 parent eb67794 commit 42364b8
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions src/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const JOB_OUTPUT_PATH = '/home/dependabot/dependabot-updater/output/output.json'
const DEFAULT_UPDATER_IMAGE =
'docker.pkg.github.com/dependabot/dependabot-updater:latest'

const decode = (str: string):string => Buffer.from(str, 'base64').toString('binary');
const decode = (str: string): string =>
Buffer.from(str, 'base64').toString('binary')

export class Updater {
constructor(
Expand Down Expand Up @@ -63,14 +64,20 @@ export class Updater {
const credentials = await this.dependabotAPI.getCredentials()

const files = await this.runFileFetcher(details, credentials)
if (!files) {
core.error(`failed during fetch, skipping updater`)
// TODO: report job runner_error?
return
}

await this.runFileUpdater(details, files)
} catch (e) {
// TODO: report job runner_error?
core.error(`Error ${e}`)
}
}

private decodeBase64Content(file: DependencyFile) {
private decodeBase64Content(file: DependencyFile): string {
const fileCopy = JSON.parse(JSON.stringify(file))
fileCopy.content = decode(fileCopy.content)
return fileCopy
Expand All @@ -79,21 +86,28 @@ export class Updater {
private async runFileFetcher(
details: JobDetails,
credentials: Credential[]
): Promise<FetchedFiles> {
): Promise<void | FetchedFiles> {
const container = await this.createContainer(details, 'fetch_files')
await this.storeContainerInput(container, {
job: details,
credentials
})
await this.runContainer(container)

const fileFetcherSync = fs.readFileSync(path.join(__dirname, "../output/output.json")).toString();
const outputPath = path.join(__dirname, '../output/output.json')
if (!fs.existsSync(outputPath)) {
return
}

const fileFetcherSync = fs.readFileSync(outputPath).toString()
const fileFetcherOutput = JSON.parse(fileFetcherSync)

const fetchedFiles: FetchedFiles = {
base_commit_sha: fileFetcherOutput.base_commit_sha,
base64_dependency_files: fileFetcherOutput.base64_dependency_files,
dependency_files: fileFetcherOutput.base64_dependency_files.map(this.decodeBase64Content)
base_commit_sha: fileFetcherOutput.base_commit_sha,
base64_dependency_files: fileFetcherOutput.base64_dependency_files,
dependency_files: fileFetcherOutput.base64_dependency_files.map(
(file: DependencyFile) => this.decodeBase64Content(file)
)
}

return fetchedFiles
Expand Down Expand Up @@ -123,7 +137,12 @@ export class Updater {
],
Cmd: ['bin/run', updaterCommand],
HostConfig: {
'Binds': [path.join(__dirname, '../output') + ':/home/dependabot/dependabot-updater/output:rw']
Binds: [
`${path.join(
__dirname,
'../output'
)}:/home/dependabot/dependabot-updater/output:rw`
]
}
})

Expand Down Expand Up @@ -152,10 +171,6 @@ 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 42364b8

Please sign in to comment.