diff options
Diffstat (limited to 'worker/scripts/checkout_ci.ts')
-rwxr-xr-x | worker/scripts/checkout_ci.ts | 72 |
1 files changed, 36 insertions, 36 deletions
diff --git a/worker/scripts/checkout_ci.ts b/worker/scripts/checkout_ci.ts index 8e4dcca..fb71a16 100755 --- a/worker/scripts/checkout_ci.ts +++ b/worker/scripts/checkout_ci.ts @@ -11,6 +11,7 @@ import { Metric, prependWith, TraceUtil, + IEither, } from '@emprespresso/pengueno'; import { mkdir, readFile, rm } from 'fs/promises'; import { join } from 'path'; @@ -29,11 +30,14 @@ const eitherJob = getRequiredEnvVars(['remote', 'refname', 'rev']).mapRight( }, }, ); +const afterJob = eitherJob.flatMapAsync((job) => + Either.fromFailableAsync(() => rm(getWorkingDirectoryForCiJob(job), { recursive: true })), +); const ciRunMetric = Metric.fromName('checkout_ci.run'); -const _logJob = LogTraceable.of(eitherJob).bimap(TraceUtil.withTrace(`checkout_ci.${run}`)); +const _logJob = LogTraceable.of(eitherJob).flatMap(TraceUtil.withTrace(`checkout_ci.${run}`)); await LogMetricTraceable.ofLogTraceable(_logJob) - .bimap(TraceUtil.withMetricTrace(ciRunMetric)) + .flatMap(TraceUtil.withMetricTrace(ciRunMetric)) .map((tEitherJob) => tEitherJob.get().flatMapAsync((ciJob) => { const wd = getWorkingDirectoryForCiJob(ciJob); @@ -53,29 +57,28 @@ await LogMetricTraceable.ofLogTraceable(_logJob) ); }), ) - .map((tEitherCiJob) => - tEitherCiJob.get().then((eitherCiJob) => - eitherCiJob.flatMapAsync<{ cmd: Command; job: CheckoutCiJob }>((ciJob) => - Either.fromFailableAsync<Error, string>(() => - readFile(join(getSrcDirectoryForCiJob(ciJob), CI_WORKFLOW_FILE), 'utf-8'), - ).then((eitherWorkflowJson) => - eitherWorkflowJson - .flatMap((json) => Either.fromFailable<Error, unknown>(JSON.parse(json))) - .flatMap((eitherWorkflowParse) => { - if (isCiWorkflow(eitherWorkflowParse)) { - return Either.right({ - cmd: getPipelineGenerationCommand(ciJob, eitherWorkflowParse.workflow), - job: ciJob, - }); - } - return Either.left( - new Error("couldn't find any valid ci configuration (。•́︿•̀。), that's okay~"), - ); - }), - ), + .map(async (tEitherCiJob) => { + const eitherCiJob = await tEitherCiJob.get(); + const repoCiFileContents = await eitherCiJob.flatMapAsync((ciJob) => + Either.fromFailableAsync<Error, string>(() => + readFile(join(getSrcDirectoryForCiJob(ciJob), CI_WORKFLOW_FILE), 'utf-8'), ), - ), - ) + ); + return repoCiFileContents + .flatMap((fileText) => Either.fromFailable<Error, unknown>(() => JSON.parse(fileText))) + .flatMap((json) => { + return eitherCiJob.flatMap((ciJob): IEither<Error, { cmd: Command; job: CheckoutCiJob }> => { + if (!isCiWorkflow(json)) { + const e = new Error("couldn't find any valid ci configuration (。•́︿•̀。), that's okay~"); + return Either.left(e); + } + return Either.right({ + cmd: getPipelineGenerationCommand(ciJob, json.workflow), + job: ciJob, + }); + }); + }); + }) .map(async (tEitherPipelineGenerationCommand) => { const eitherJobCommand = await tEitherPipelineGenerationCommand.get(); const eitherPipeline = await eitherJobCommand.flatMapAsync((jobCommand) => @@ -107,17 +110,14 @@ await LogMetricTraceable.ofLogTraceable(_logJob) .get(), ); }) - .get() - .then((e) => - e - .flatMap(() => eitherJob) - .fold(({ isLeft, isRight, value }) => { - if (isLeft || !isRight) throw value; - return rm(getWorkingDirectoryForCiJob(value), { - recursive: true, - }); - }), - ); + .map(async (tCompletePipeline) => { + const completePipeline = await tCompletePipeline.get(); + return completePipeline.fold( + (e) => Promise.reject(e), + () => afterJob, + ); + }) + .get(); const getWorkingDirectoryForCiJob = (job: CheckoutCiJob) => `${job.arguments.returnPath}/${job.arguments.run}`; @@ -130,7 +130,7 @@ const getPipelineGenerationCommand = ( pipelineGeneratorPath: string, image = _image, runFlags = _runFlags, -) => [ +): Command => [ 'docker', 'run', ...runFlags, |