summaryrefslogtreecommitdiff
path: root/model/pipeline.ts
blob: 06361a403c90e05cdb47d9c1550ee773e2e618f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import type { FetchCodeJob, Job } from "./mod.ts";

export interface Pipeline {
  getStages(): ReadonlyArray<PipelineStage>;
  serialize(): string;
}

export interface PipelineStage {
  readonly parallelJobs: Array<Job>;
}

export interface PipelineBuilder {
  addStage(stage: PipelineStage): PipelineBuilder;
  build(): Pipeline;
}

export class PipelineImpl implements Pipeline {
  constructor(private readonly serialJobs: ReadonlyArray<PipelineStage>) {}

  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<PipelineStage> = [];

  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: [
        <FetchCodeJob> {
          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);
  }
}