diff options
Diffstat (limited to 'model')
-rw-r--r-- | model/deno.json | 2 | ||||
-rw-r--r-- | model/job.ts | 19 | ||||
-rw-r--r-- | model/pipeline.ts | 37 |
3 files changed, 44 insertions, 14 deletions
diff --git a/model/deno.json b/model/deno.json index d22242e..afd9f22 100644 --- a/model/deno.json +++ b/model/deno.json @@ -1,5 +1,5 @@ { - "name": "@liz-ci/model", + "name": "@emprespresso/ci-model", "version": "0.1.0", "exports": "./mod.ts" } diff --git a/model/job.ts b/model/job.ts index 53d548f..a3b52dd 100644 --- a/model/job.ts +++ b/model/job.ts @@ -1,8 +1,13 @@ +import { isObject } from "@emprespresso/pengueno"; + export type JobArgT = Record<string, string>; export interface Job { readonly type: string; readonly arguments: JobArgT; } +export const isJob = (j: unknown): j is Job => + !!(isObject(j) && "arguments" in j && isObject(j.arguments) && + "type" in j && typeof j.type === "string" && j); export interface FetchCodeJobProps extends JobArgT { readonly remoteUrl: string; @@ -40,3 +45,17 @@ export interface AnsiblePlaybookJob extends Job { readonly type: "ansible_playbook"; readonly arguments: AnsiblePlaybookJobProps; } + +export interface CheckoutCiJobProps extends JobArgT { + readonly remote: string; + readonly refname: string; + readonly rev: string; + + readonly run: string; + readonly returnPath: string; +} + +export interface CheckoutCiJob extends Job { + readonly type: "checkout_ci"; + readonly arguments: CheckoutCiJobProps; +} diff --git a/model/pipeline.ts b/model/pipeline.ts index 06361a4..b0db1b7 100644 --- a/model/pipeline.ts +++ b/model/pipeline.ts @@ -1,13 +1,20 @@ -import type { FetchCodeJob, Job } from "./mod.ts"; - -export interface Pipeline { - getStages(): ReadonlyArray<PipelineStage>; - serialize(): string; -} +import { Either, type IEither, isObject } from "@emprespresso/pengueno"; +import { type FetchCodeJob, isJob, type Job } from "@emprespresso/ci-model"; export interface PipelineStage { readonly parallelJobs: Array<Job>; } +export const isPipelineStage = (t: unknown): t is PipelineStage => + isObject(t) && "parallelJobs" in t && Array.isArray(t.parallelJobs) && + t.parallelJobs.every((j) => isJob(j)); + +export interface Pipeline { + readonly serialJobs: Array<PipelineStage>; + serialize(): string; +} +export const isPipeline = (t: unknown): t is Pipeline => + isObject(t) && "serialJobs" in t && Array.isArray(t.serialJobs) && + t.serialJobs.every((p) => isPipelineStage(p)); export interface PipelineBuilder { addStage(stage: PipelineStage): PipelineBuilder; @@ -15,18 +22,22 @@ export interface PipelineBuilder { } export class PipelineImpl implements Pipeline { - constructor(private readonly serialJobs: ReadonlyArray<PipelineStage>) {} - - public getStages() { - return this.serialJobs; - } + constructor(public readonly serialJobs: Array<PipelineStage>) {} public serialize() { return JSON.stringify(this.serialJobs); } - public static from(s: string): Pipeline { - return new PipelineImpl(JSON.parse(s)); + public static from(s: string): IEither<Error, Pipeline> { + return Either.fromFailable<Error, unknown>(() => JSON.parse(s)).flatMap< + Pipeline + >(( + eitherPipelineJson, + ) => + isPipeline(eitherPipelineJson) + ? Either.right(eitherPipelineJson) + : Either.left(new Error("oh noes D: its a bad pipewine :((")) + ).mapRight((pipeline) => new PipelineImpl(pipeline.serialJobs)); } } |