-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgitlab.ts
More file actions
160 lines (140 loc) · 5.21 KB
/
Copy pathgitlab.ts
File metadata and controls
160 lines (140 loc) · 5.21 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// SPDX-License-Identifier: AGPL-3.0-or-later
/**
* Utility client for integrating with a GitLab CE instance.
*/
import http from "node:http";
import https from "node:https";
import type {
GitCommitItem,
GitIssueItem,
GitJobItem,
GitMergeRequestItem,
GitPipelineItem,
GitTreeItem,
IGitProvider,
} from "../interfaces/git-provider.js";
const GITLAB_URL = process.env.GITLAB_URL || "https://gitlab.com";
const GITLAB_TOKEN = process.env.GITLAB_TOKEN || "";
export class GitlabError extends Error {
constructor(
public status: number,
message: string,
) {
super(message);
this.name = "GitlabError";
}
}
/**
* Make an authenticated request to the GitLab API.
*/
export function gitlabRequest<T = unknown>(path: string, options?: RequestInit): Promise<T> {
return new Promise((resolve, reject) => {
const url = new URL(`${GITLAB_URL}/api/v4${path}`);
const client = url.protocol === "http:" ? http : https;
const headers: Record<string, string> = {
"Content-Type": "application/json",
...(GITLAB_TOKEN ? { Authorization: `Bearer ${GITLAB_TOKEN}` } : {}),
};
if (options?.headers) {
Object.assign(headers, options.headers);
}
const req = client.request(
url,
{
method: options?.method || "GET",
headers,
family: 4, // Force IPv4
},
(res) => {
let data = "";
res.on("data", (chunk) => (data += chunk));
res.on("end", () => {
if (!res.statusCode || res.statusCode >= 400) {
reject(new GitlabError(res.statusCode || 500, `GitLab API error: ${res.statusCode} - ${data}`));
} else {
try {
resolve(JSON.parse(data) as T);
} catch (e) {
reject(e);
}
}
});
},
);
req.on("error", reject);
if (options?.body) req.write(options.body as string);
req.end();
});
}
export class GitLabProvider implements IGitProvider {
async getProject(projectIdOrPath: string): Promise<unknown> {
const encodedPath = encodeURIComponent(projectIdOrPath);
return gitlabRequest(`/projects/${encodedPath}`);
}
async getRepositoryTree(projectIdOrPath: string, ref = "main", path = ""): Promise<GitTreeItem[]> {
const encodedId = encodeURIComponent(projectIdOrPath);
const params = new URLSearchParams({ ref, path });
return gitlabRequest<GitTreeItem[]>(`/projects/${encodedId}/repository/tree?${params.toString()}`);
}
async getRepositoryFileRaw(projectIdOrPath: string, filePath: string, ref = "main"): Promise<string> {
return new Promise((resolve, reject) => {
const encodedId = encodeURIComponent(projectIdOrPath);
const encodedFile = encodeURIComponent(filePath);
const url = new URL(`${GITLAB_URL}/api/v4/projects/${encodedId}/repository/files/${encodedFile}/raw?ref=${ref}`);
const client = url.protocol === "http:" ? http : https;
const headers: Record<string, string> = {};
if (GITLAB_TOKEN) {
headers["Authorization"] = `Bearer ${GITLAB_TOKEN}`;
}
const req = client.request(
url,
{
method: "GET",
headers,
family: 4, // Force IPv4
},
(res) => {
let data = "";
res.on("data", (chunk) => (data += chunk));
res.on("end", () => {
if (!res.statusCode || res.statusCode >= 400) {
reject(new GitlabError(res.statusCode || 500, `Failed to fetch raw file: ${res.statusCode}`));
} else {
resolve(data);
}
});
},
);
req.on("error", reject);
req.end();
});
}
async getCommits(projectIdOrPath: string, refName = "main"): Promise<GitCommitItem[]> {
const encodedId = encodeURIComponent(projectIdOrPath);
const params = new URLSearchParams({ ref_name: refName });
return gitlabRequest<GitCommitItem[]>(`/projects/${encodedId}/repository/commits?${params.toString()}`);
}
async getPipelines(projectIdOrPath: string, refName = "main"): Promise<GitPipelineItem[]> {
const encodedId = encodeURIComponent(projectIdOrPath);
return gitlabRequest<GitPipelineItem[]>(`/projects/${encodedId}/pipelines?ref=${encodeURIComponent(refName)}`);
}
async getPipelineJobs(projectIdOrPath: string, pipelineId: string): Promise<GitJobItem[]> {
const encodedId = encodeURIComponent(projectIdOrPath);
return gitlabRequest<GitJobItem[]>(`/projects/${encodedId}/pipelines/${pipelineId}/jobs`);
}
async getIssues(projectIdOrPath: string): Promise<GitIssueItem[]> {
const encodedId = encodeURIComponent(projectIdOrPath);
return gitlabRequest<GitIssueItem[]>(`/projects/${encodedId}/issues?state=opened`);
}
async createIssue(projectIdOrPath: string, body: unknown): Promise<GitIssueItem> {
const encodedId = encodeURIComponent(projectIdOrPath);
return gitlabRequest<GitIssueItem>(`/projects/${encodedId}/issues`, {
method: "POST",
body: JSON.stringify(body),
});
}
async getMergeRequests(projectIdOrPath: string): Promise<GitMergeRequestItem[]> {
const encodedId = encodeURIComponent(projectIdOrPath);
return gitlabRequest<GitMergeRequestItem[]>(`/projects/${encodedId}/merge_requests?state=opened`);
}
}