import type { FetchCodeJob, Job } from "./mod.ts"; export interface Pipeline { getStages(): ReadonlyArray; serialize(): string; } export interface PipelineStage { readonly parallelJobs: Array; } export interface PipelineBuilder { addStage(stage: PipelineStage): PipelineBuilder; build(): Pipeline; } export class PipelineImpl implements Pipeline { constructor(private readonly serialJobs: ReadonlyArray) {} public getStages() { return this.serialJobs; } public serialize() { return JSON.stringify(this.serialJobs); } public static from(s: string): Pipeline { return new PipelineImpl(JSON.parse(s)); } } abstract class BasePipelineBuilder implements PipelineBuilder { protected readonly stages: Array = []; public addStage(stage: PipelineStage): PipelineBuilder { this.stages.push(stage); return this; } public build() { return new PipelineImpl(this.stages); } } export class DefaultGitHookPipelineBuilder extends BasePipelineBuilder { constructor( private readonly remoteUrl = Deno.env.get("remote")!, rev = Deno.env.get("rev")!, private readonly ref = Deno.env.get("ref")!, ) { super(); this.addStage({ parallelJobs: [ { type: "fetch_code", arguments: { remoteUrl, checkout: rev, path: this.getSourceDestination(), }, }, ], }); } public getSourceDestination() { return this.remoteUrl.split("/").at(-1) ?? "src"; } public getBranch(): string | undefined { const branchRefPrefix = "refs/heads/"; return this.ref.split(branchRefPrefix).at(1); } }