Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
dependabot-action/src/updater.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
84 lines (73 sloc)
2.26 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Docker, {Container} from 'dockerode' | |
import path from 'path' | |
import fs from 'fs' | |
import {JobDetails, ApiClient, Credential} from './api-client' | |
import {ContainerService} from './container-service' | |
import {FileUpdaterInput, FileFetcherInput} from './config-types' | |
import {ProxyBuilder, Proxy} from './proxy' | |
import {UpdaterBuilder} from './updater-builder' | |
export class Updater { | |
docker: Docker | |
outputHostPath: string | |
repoHostPath: string | |
constructor( | |
private readonly updaterImage: string, | |
private readonly proxyImage: string, | |
private readonly apiClient: ApiClient, | |
private readonly details: JobDetails, | |
private readonly credentials: Credential[], | |
private readonly workingDirectory: string | |
) { | |
this.docker = new Docker() | |
this.outputHostPath = path.join(workingDirectory, 'output') | |
this.repoHostPath = path.join(workingDirectory, 'repo') | |
} | |
/** | |
* Execute an update job and report the result to Dependabot API. | |
*/ | |
async runUpdater(): Promise<boolean> { | |
// Create required folders in the workingDirectory | |
fs.mkdirSync(this.outputHostPath) | |
const proxy = await new ProxyBuilder(this.docker, this.proxyImage).run( | |
this.apiClient.params.jobId, | |
this.credentials | |
) | |
await proxy.container.start() | |
try { | |
await this.runUpdate(proxy) | |
return true | |
} finally { | |
await this.cleanup(proxy) | |
} | |
} | |
private async runUpdate(proxy: Proxy): Promise<void> { | |
const name = `dependabot-job-${this.apiClient.params.jobId}` | |
const container = await this.createContainer(proxy, name, { | |
job: this.details | |
}) | |
await ContainerService.run(container) | |
} | |
private async createContainer( | |
proxy: Proxy, | |
containerName: string, | |
input: FileFetcherInput | FileUpdaterInput | |
): Promise<Container> { | |
return new UpdaterBuilder( | |
this.docker, | |
this.apiClient.params, | |
input, | |
this.outputHostPath, | |
proxy, | |
this.updaterImage | |
).run(containerName) | |
} | |
private async cleanup(proxy: Proxy): Promise<void> { | |
await proxy.shutdown() | |
if (fs.existsSync(this.outputHostPath)) { | |
fs.rmdirSync(this.outputHostPath, {recursive: true}) | |
} | |
if (fs.existsSync(this.repoHostPath)) { | |
fs.rmdirSync(this.repoHostPath, {recursive: true}) | |
} | |
} | |
} |