forked from lingodotdev/lingo.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlab.ts
More file actions
102 lines (86 loc) · 3.17 KB
/
gitlab.ts
File metadata and controls
102 lines (86 loc) · 3.17 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { Gitlab } from "@gitbeaker/rest";
import Z from "zod";
import { PlatformKit } from "./_base.js";
const gl = new Gitlab({ token: "" });
export class GitlabPlatformKit extends PlatformKit {
private _gitlab?: InstanceType<typeof Gitlab>;
constructor() {
super();
// change directory to current repository before executing replexica
process.chdir(this.platformConfig.projectDir);
}
private get gitlab(): InstanceType<typeof Gitlab> {
if (!this._gitlab) {
this._gitlab = new Gitlab({
token: this.platformConfig.glToken || "",
});
}
return this._gitlab;
}
get platformConfig() {
const env = Z.object({
GL_TOKEN: Z.string().optional(),
CI_COMMIT_BRANCH: Z.string(),
CI_MERGE_REQUEST_SOURCE_BRANCH_NAME: Z.string().optional(),
CI_PROJECT_NAMESPACE: Z.string(),
CI_PROJECT_NAME: Z.string(),
CI_PROJECT_ID: Z.string(),
CI_PROJECT_DIR: Z.string(),
CI_REPOSITORY_URL: Z.string(),
}).parse(process.env);
const config = {
glToken: env.GL_TOKEN,
baseBranchName: env.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME ?? env.CI_COMMIT_BRANCH,
repositoryOwner: env.CI_PROJECT_NAMESPACE,
repositoryName: env.CI_PROJECT_NAME,
gitlabProjectId: env.CI_PROJECT_ID,
projectDir: env.CI_PROJECT_DIR,
reporitoryUrl: env.CI_REPOSITORY_URL,
};
return config;
}
async branchExists({ branch }: { branch: string }): Promise<boolean> {
try {
await this.gitlab.Branches.show(this.platformConfig.gitlabProjectId, branch);
return true;
} catch {
return false;
}
}
async getOpenPullRequestNumber({ branch }: { branch: string }): Promise<number | undefined> {
const mergeRequests = await this.gitlab.MergeRequests.all({
projectId: this.platformConfig.gitlabProjectId,
sourceBranch: branch,
state: "opened",
});
return mergeRequests[0]?.iid;
}
async closePullRequest({ pullRequestNumber }: { pullRequestNumber: number }): Promise<void> {
await this.gitlab.MergeRequests.edit(this.platformConfig.gitlabProjectId, pullRequestNumber, {
stateEvent: "close",
});
}
async createPullRequest({ head, title, body }: { head: string; title: string; body?: string }): Promise<number> {
const mr = await this.gitlab.MergeRequests.create(
this.platformConfig.gitlabProjectId,
head,
this.platformConfig.baseBranchName,
title,
{
description: body,
}
);
return mr.iid;
}
async commentOnPullRequest({ pullRequestNumber, body }: { pullRequestNumber: number; body: string }): Promise<void> {
await this.gitlab.MergeRequestNotes.create(this.platformConfig.gitlabProjectId, pullRequestNumber, body);
}
gitConfig(): Promise<void> | void {
const glToken = this.platformConfig.glToken;
const url = `https://oauth2:${glToken}@gitlab.com/${this.platformConfig.repositoryOwner}/${this.platformConfig.repositoryName}.git`;
super.gitConfig(glToken, url);
}
buildPullRequestUrl(pullRequestNumber: number): string {
return `https://gitlab.com/${this.platformConfig.repositoryOwner}/${this.platformConfig.repositoryName}/-/merge_requests/${pullRequestNumber}`;
}
}