summaryrefslogtreecommitdiff
path: root/model/pipeline/builder.ts
blob: b15a0442930935cc6af1cb21d1a9dd7d6f74c419 (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
import { FetchCodeJob } from '@emprespresso/ci_model';
import { PipelineImpl, Pipeline, PipelineStage } from './index.js';

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

export 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 = process.env.remote!,
        rev = process.env.rev!,
        private readonly refname = process.env.refname!,
    ) {
        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.refname.split(branchRefPrefix).at(1);
    }
}