@lbdudc/linea-git 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -2
- package/dist/access.d.ts +18 -0
- package/dist/access.js +18 -0
- package/dist/access.js.map +1 -0
- package/dist/auth.d.ts +11 -0
- package/dist/auth.js +16 -0
- package/dist/auth.js.map +1 -0
- package/dist/changelog/generate.d.ts +18 -0
- package/dist/changelog/generate.js +166 -0
- package/dist/changelog/generate.js.map +1 -0
- package/dist/changelog/index.d.ts +5 -0
- package/dist/changelog/index.js +3 -0
- package/dist/changelog/index.js.map +1 -0
- package/dist/changelog/parse.d.ts +43 -0
- package/dist/changelog/parse.js +91 -0
- package/dist/changelog/parse.js.map +1 -0
- package/dist/changelog/types.d.ts +85 -0
- package/dist/changelog/types.js +2 -0
- package/dist/changelog/types.js.map +1 -0
- package/dist/checkout/credential.d.ts +14 -0
- package/dist/checkout/credential.js +29 -0
- package/dist/checkout/credential.js.map +1 -0
- package/dist/checkout/git.d.ts +78 -0
- package/dist/checkout/git.js +191 -0
- package/dist/checkout/git.js.map +1 -0
- package/dist/checkout/index.d.ts +3 -0
- package/dist/checkout/index.js +4 -0
- package/dist/checkout/index.js.map +1 -0
- package/dist/checkout/sync.d.ts +156 -0
- package/dist/checkout/sync.js +211 -0
- package/dist/checkout/sync.js.map +1 -0
- package/dist/closing.d.ts +1 -0
- package/dist/closing.js +14 -0
- package/dist/closing.js.map +1 -0
- package/dist/config.d.ts +53 -0
- package/dist/config.js +40 -0
- package/dist/config.js.map +1 -0
- package/dist/deletion.d.ts +19 -0
- package/dist/deletion.js +27 -0
- package/dist/deletion.js.map +1 -0
- package/dist/errors.d.ts +26 -0
- package/dist/errors.js +47 -0
- package/dist/errors.js.map +1 -0
- package/dist/factory.d.ts +20 -0
- package/dist/factory.js +46 -0
- package/dist/factory.js.map +1 -0
- package/dist/github/client.d.ts +44 -0
- package/dist/github/client.js +145 -0
- package/dist/github/client.js.map +1 -0
- package/dist/github/config.d.ts +24 -0
- package/dist/github/config.js +11 -0
- package/dist/github/config.js.map +1 -0
- package/dist/github/provider.d.ts +510 -0
- package/dist/github/provider.js +1009 -0
- package/dist/github/provider.js.map +1 -0
- package/dist/gitlab/client.d.ts +50 -0
- package/dist/gitlab/client.js +228 -0
- package/dist/gitlab/client.js.map +1 -0
- package/dist/gitlab/provider.d.ts +487 -0
- package/dist/gitlab/provider.js +719 -0
- package/dist/gitlab/provider.js.map +1 -0
- package/dist/host.d.ts +27 -0
- package/dist/host.js +45 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/local/diff.d.ts +3 -0
- package/dist/local/diff.js +43 -0
- package/dist/local/diff.js.map +1 -0
- package/dist/local/git.d.ts +24 -0
- package/dist/local/git.js +51 -0
- package/dist/local/git.js.map +1 -0
- package/dist/local/health.d.ts +36 -0
- package/dist/local/health.js +57 -0
- package/dist/local/health.js.map +1 -0
- package/dist/local/hooks.d.ts +2 -0
- package/dist/local/hooks.js +44 -0
- package/dist/local/hooks.js.map +1 -0
- package/dist/local/http.d.ts +35 -0
- package/dist/local/http.js +160 -0
- package/dist/local/http.js.map +1 -0
- package/dist/local/provider.d.ts +571 -0
- package/dist/local/provider.js +1237 -0
- package/dist/local/provider.js.map +1 -0
- package/dist/local/store.d.ts +136 -0
- package/dist/local/store.js +171 -0
- package/dist/local/store.js.map +1 -0
- package/dist/packages/github-source.d.ts +69 -0
- package/dist/packages/github-source.js +133 -0
- package/dist/packages/github-source.js.map +1 -0
- package/dist/packages/gitlab-source.d.ts +59 -0
- package/dist/packages/gitlab-source.js +214 -0
- package/dist/packages/gitlab-source.js.map +1 -0
- package/dist/packages/index.d.ts +30 -0
- package/dist/packages/index.js +45 -0
- package/dist/packages/index.js.map +1 -0
- package/dist/packages/provider-source.d.ts +34 -0
- package/dist/packages/provider-source.js +57 -0
- package/dist/packages/provider-source.js.map +1 -0
- package/dist/packages/repository-files.d.ts +12 -0
- package/dist/packages/repository-files.js +16 -0
- package/dist/packages/repository-files.js.map +1 -0
- package/dist/provider.d.ts +888 -0
- package/dist/provider.js +29 -0
- package/dist/provider.js.map +1 -0
- package/dist/read-cache.d.ts +5 -0
- package/dist/read-cache.js +101 -0
- package/dist/read-cache.js.map +1 -0
- package/dist/registry/catalog.d.ts +55 -0
- package/dist/registry/catalog.js +143 -0
- package/dist/registry/catalog.js.map +1 -0
- package/dist/registry/kinds.d.ts +21 -0
- package/dist/registry/kinds.js +43 -0
- package/dist/registry/kinds.js.map +1 -0
- package/dist/registry/packages.d.ts +165 -0
- package/dist/registry/packages.js +290 -0
- package/dist/registry/packages.js.map +1 -0
- package/dist/registry/publish.d.ts +35 -0
- package/dist/registry/publish.js +155 -0
- package/dist/registry/publish.js.map +1 -0
- package/dist/registry/release.d.ts +63 -0
- package/dist/registry/release.js +79 -0
- package/dist/registry/release.js.map +1 -0
- package/dist/restoration.d.ts +14 -0
- package/dist/restoration.js +33 -0
- package/dist/restoration.js.map +1 -0
- package/dist/templates.d.ts +8 -0
- package/dist/templates.js +39 -0
- package/dist/templates.js.map +1 -0
- package/dist/testing/contract.d.ts +43 -0
- package/dist/testing/contract.js +713 -0
- package/dist/testing/contract.js.map +1 -0
- package/dist/testing/diff.d.ts +1 -0
- package/dist/testing/diff.js +65 -0
- package/dist/testing/diff.js.map +1 -0
- package/dist/testing/git-http.d.ts +12 -0
- package/dist/testing/git-http.js +308 -0
- package/dist/testing/git-http.js.map +1 -0
- package/dist/testing/github-double.d.ts +151 -0
- package/dist/testing/github-double.js +1129 -0
- package/dist/testing/github-double.js.map +1 -0
- package/dist/testing/gitlab-double.d.ts +161 -0
- package/dist/testing/gitlab-double.js +1554 -0
- package/dist/testing/gitlab-double.js.map +1 -0
- package/dist/testing/index.d.ts +7 -0
- package/dist/testing/index.js +8 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +12 -0
- package/dist/version.js.map +1 -0
- package/package.json +66 -4
|
@@ -0,0 +1,719 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import { Readable } from 'node:stream';
|
|
3
|
+
import { GitLabAuthError, GitLabSession } from 'gitlab-commando';
|
|
4
|
+
import { readRepositoryTemplates } from '../templates.js';
|
|
5
|
+
import { createGitLabClient, isBranchAlreadyExistsError } from './client.js';
|
|
6
|
+
/**
|
|
7
|
+
* GitLab, behind the `GitProvider` contract. Namespaces are groups, repositories are projects, change requests are merge requests.
|
|
8
|
+
* What it remembers for `rollback()` is what `gitlab-commando`'s session remembers.
|
|
9
|
+
*/
|
|
10
|
+
const MERGE_WAIT_MS = 5 * 60 * 1000;
|
|
11
|
+
const MERGE_POLL_MS = 500;
|
|
12
|
+
// a merge GitLab cannot do yet (it is still checking the request), or did not get to answer
|
|
13
|
+
const mergeNotYet = (status) => status === 0 || status >= 500 || [404, 405, 409, 422].includes(status);
|
|
14
|
+
const toNamespace = (group) => ({ id: group.id, fullPath: group.full_path, name: group.full_name ?? group.name });
|
|
15
|
+
const toFile = (file) => ({
|
|
16
|
+
path: file.file_path,
|
|
17
|
+
name: file.file_name,
|
|
18
|
+
content: file.content,
|
|
19
|
+
encoding: 'base64',
|
|
20
|
+
revision: file.last_commit_id ?? file.commit_id
|
|
21
|
+
});
|
|
22
|
+
const toCommit = (commit) => ({
|
|
23
|
+
shortId: commit.short_id,
|
|
24
|
+
title: commit.title,
|
|
25
|
+
authorName: commit.author_name,
|
|
26
|
+
createdAt: commit.created_at,
|
|
27
|
+
webUrl: commit.web_url,
|
|
28
|
+
message: commit.message
|
|
29
|
+
});
|
|
30
|
+
const toRelease = (release) => ({
|
|
31
|
+
tag: release.tag_name,
|
|
32
|
+
name: release.name || release.tag_name,
|
|
33
|
+
description: release.description ?? '',
|
|
34
|
+
createdAt: release.created_at ?? release.released_at,
|
|
35
|
+
url: release._links?.self ?? '',
|
|
36
|
+
// (GitLab has no prerelease: a release dated in the future is its "upcoming" one, which is not the same thing)
|
|
37
|
+
prerelease: false
|
|
38
|
+
});
|
|
39
|
+
const toChangeRequest = (mr) => ({
|
|
40
|
+
number: mr.iid,
|
|
41
|
+
title: mr.title,
|
|
42
|
+
sourceBranch: mr.source_branch,
|
|
43
|
+
targetBranch: mr.target_branch,
|
|
44
|
+
webUrl: mr.web_url,
|
|
45
|
+
hasConflicts: Boolean(mr.has_conflicts),
|
|
46
|
+
state: mr.state === 'merged' ? 'merged' : mr.state === 'closed' ? 'closed' : 'opened',
|
|
47
|
+
author: mr.author?.username,
|
|
48
|
+
createdAt: mr.created_at,
|
|
49
|
+
mergedAt: mr.merged_at ?? null,
|
|
50
|
+
headSha: mr.sha
|
|
51
|
+
});
|
|
52
|
+
const toIssue = (issue) => ({
|
|
53
|
+
number: issue.iid,
|
|
54
|
+
title: issue.title,
|
|
55
|
+
description: issue.description ?? '',
|
|
56
|
+
state: issue.state === 'closed' ? 'closed' : 'opened',
|
|
57
|
+
author: issue.author?.username,
|
|
58
|
+
assignees: (issue.assignees ?? []).map((user) => user.username),
|
|
59
|
+
labels: (issue.labels ?? []).map((label) => (typeof label === 'string' ? label : label.name)),
|
|
60
|
+
milestone: issue.milestone?.title ?? null,
|
|
61
|
+
createdAt: issue.created_at,
|
|
62
|
+
updatedAt: issue.updated_at,
|
|
63
|
+
closedAt: issue.closed_at ?? null,
|
|
64
|
+
webUrl: issue.web_url,
|
|
65
|
+
commentCount: issue.user_notes_count ?? 0
|
|
66
|
+
});
|
|
67
|
+
const toComment = (note) => ({ id: note.id, author: note.author?.username, body: note.body, createdAt: note.created_at, updatedAt: note.updated_at ?? note.created_at });
|
|
68
|
+
const toMergeRequest = (mr) => ({
|
|
69
|
+
number: mr.iid,
|
|
70
|
+
title: mr.title,
|
|
71
|
+
description: mr.description ?? '',
|
|
72
|
+
state: mr.state === 'merged' ? 'merged' : mr.state === 'closed' ? 'closed' : 'opened',
|
|
73
|
+
author: mr.author?.username,
|
|
74
|
+
sourceBranch: mr.source_branch,
|
|
75
|
+
targetBranch: mr.target_branch,
|
|
76
|
+
labels: (mr.labels ?? []).map((label) => (typeof label === 'string' ? label : label.name)),
|
|
77
|
+
reviewers: (mr.reviewers ?? []).map((user) => user.username),
|
|
78
|
+
assignees: (mr.assignees ?? []).map((user) => user.username),
|
|
79
|
+
createdAt: mr.created_at,
|
|
80
|
+
updatedAt: mr.updated_at,
|
|
81
|
+
mergedAt: mr.merged_at ?? null,
|
|
82
|
+
webUrl: mr.web_url,
|
|
83
|
+
hasConflicts: Boolean(mr.has_conflicts),
|
|
84
|
+
headSha: mr.sha,
|
|
85
|
+
commentCount: mr.user_notes_count ?? 0
|
|
86
|
+
});
|
|
87
|
+
const countLines = (patch, mark) => patch.split('\n').filter((line) => line.startsWith(mark) && !line.startsWith(`${mark}${mark}${mark} `)).length;
|
|
88
|
+
const toFileDiff = (change) => ({
|
|
89
|
+
oldPath: change.old_path,
|
|
90
|
+
newPath: change.new_path,
|
|
91
|
+
status: change.new_file ? 'added' : change.deleted_file ? 'deleted' : change.renamed_file ? 'renamed' : 'modified',
|
|
92
|
+
additions: countLines(change.diff ?? '', '+'),
|
|
93
|
+
deletions: countLines(change.diff ?? '', '-'),
|
|
94
|
+
patch: change.diff ?? ''
|
|
95
|
+
});
|
|
96
|
+
/** A GitLab discussion is a thread: the notes it has, minus the ones GitLab writes by itself. */
|
|
97
|
+
const toThread = (discussion) => {
|
|
98
|
+
const notes = (discussion.notes ?? []).filter((note) => !note.system);
|
|
99
|
+
const [first] = notes;
|
|
100
|
+
if (!first)
|
|
101
|
+
return null;
|
|
102
|
+
const at = first.position;
|
|
103
|
+
return {
|
|
104
|
+
id: discussion.id,
|
|
105
|
+
resolvable: Boolean(first.resolvable),
|
|
106
|
+
resolved: Boolean(first.resolvable) && notes.filter((note) => note.resolvable).every((note) => note.resolved),
|
|
107
|
+
position: at ? { path: at.new_path ?? at.old_path, line: at.new_line ?? at.old_line, side: at.new_line ? 'new' : 'old' } : undefined,
|
|
108
|
+
comments: notes.map(toComment)
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
const toPipeline = (p) => ({ id: p.id, sha: p.sha, status: p.status, ref: p.ref, createdAt: p.created_at, webUrl: p.web_url });
|
|
112
|
+
const toTokens = (data) => ({ accessToken: data.access_token, refreshToken: data.refresh_token ?? null });
|
|
113
|
+
/** `last_commit_id` is the GitLab way to say "only if the file is still as I read it". */
|
|
114
|
+
const toGitLabAction = (action) => ({
|
|
115
|
+
action: action.action,
|
|
116
|
+
file_path: action.path,
|
|
117
|
+
...(action.content === undefined ? {} : { content: action.content }),
|
|
118
|
+
...(action.encoding === undefined ? {} : { encoding: action.encoding }),
|
|
119
|
+
...(action.expectedRevision === undefined ? {} : { last_commit_id: action.expectedRevision })
|
|
120
|
+
});
|
|
121
|
+
/** A repository can be named by its id or by its path: the path travels URL-encoded. */
|
|
122
|
+
const idOf = (id) => (typeof id === 'string' && id.includes('/') ? encodeURIComponent(id) : id);
|
|
123
|
+
export class GitLabProvider {
|
|
124
|
+
kind = 'gitlab';
|
|
125
|
+
nestsNamespaces = true;
|
|
126
|
+
webhookPath = '/api/webhooks/gitlab';
|
|
127
|
+
config;
|
|
128
|
+
client;
|
|
129
|
+
session;
|
|
130
|
+
releaseUndo = [];
|
|
131
|
+
constructor(config) {
|
|
132
|
+
this.config = { ...config, url: config.url.replace(/\/+$/, '') };
|
|
133
|
+
this.client = createGitLabClient({ url: this.config.url });
|
|
134
|
+
this.session = new GitLabSession(this.config.url);
|
|
135
|
+
}
|
|
136
|
+
toRepository(project) {
|
|
137
|
+
return {
|
|
138
|
+
id: project.id,
|
|
139
|
+
name: project.name,
|
|
140
|
+
path: project.path_with_namespace,
|
|
141
|
+
description: project.description ?? '',
|
|
142
|
+
webUrl: project.web_url,
|
|
143
|
+
webIdeUrl: `${this.config.url}/-/ide/project/${project.path_with_namespace}/edit/main/-/`,
|
|
144
|
+
namespaceId: project.namespace?.id,
|
|
145
|
+
parentNamespaceId: project.namespace?.parent_id,
|
|
146
|
+
defaultBranch: project.default_branch,
|
|
147
|
+
topics: project.topics ?? project.tag_list ?? [],
|
|
148
|
+
createdAt: project.created_at
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
// ── identity ─────────────────────────────────────────────────────────────
|
|
152
|
+
authConfig() {
|
|
153
|
+
return {
|
|
154
|
+
kind: 'gitlab',
|
|
155
|
+
clientId: this.config.clientId,
|
|
156
|
+
redirectUri: this.config.redirectUri ?? '',
|
|
157
|
+
url: this.config.url,
|
|
158
|
+
authorizeUrl: `${this.config.url}/oauth/authorize`,
|
|
159
|
+
scope: 'api'
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
async exchangeCode({ code, redirectUri }) {
|
|
163
|
+
return toTokens(await this.session.requestAccessToken(code, this.config.clientId, this.config.clientSecret, redirectUri));
|
|
164
|
+
}
|
|
165
|
+
async refreshToken({ refreshToken }) {
|
|
166
|
+
return toTokens(await this.session.refreshAccessToken(refreshToken ?? '', this.config.clientId, this.config.clientSecret, this.config.redirectUri ?? ''));
|
|
167
|
+
}
|
|
168
|
+
async getUser({ token }) {
|
|
169
|
+
const user = await this.session.getUserData(token);
|
|
170
|
+
return { username: user.username, avatarUrl: user.avatar_url };
|
|
171
|
+
}
|
|
172
|
+
// ── namespaces ───────────────────────────────────────────────────────────
|
|
173
|
+
async listNamespaces({ token }) {
|
|
174
|
+
return (await this.client.getGroups(token)).map(toNamespace);
|
|
175
|
+
}
|
|
176
|
+
async findNamespace({ token, fullPath }) {
|
|
177
|
+
const group = await this.client.getGroup(token, fullPath);
|
|
178
|
+
return group === null ? null : toNamespace(group);
|
|
179
|
+
}
|
|
180
|
+
async createNamespace({ token, name, parentId }) {
|
|
181
|
+
return toNamespace(await this.session.createGroup(token, name, parentId));
|
|
182
|
+
}
|
|
183
|
+
deleteNamespace({ token, id }) {
|
|
184
|
+
return this.session.deleteGroup(token, id);
|
|
185
|
+
}
|
|
186
|
+
renameNamespace({ token, id, name }) {
|
|
187
|
+
return this.session.renameGroup(token, id, name);
|
|
188
|
+
}
|
|
189
|
+
// ── repositories ─────────────────────────────────────────────────────────
|
|
190
|
+
async createRepository({ token, name, namespaceId, topics, defaultBranch }) {
|
|
191
|
+
return this.toRepository(await this.session.createProject(token, name, namespaceId, topics, defaultBranch));
|
|
192
|
+
}
|
|
193
|
+
deleteRepository({ token, id }) {
|
|
194
|
+
return this.session.deleteProject(token, id);
|
|
195
|
+
}
|
|
196
|
+
async listPendingDeletion({ token, topic }) {
|
|
197
|
+
const projects = await this.client.getProjectsPendingDeletion(token, topic);
|
|
198
|
+
const named = (text) => text.replace(/-deletion_scheduled-\d+/g, '');
|
|
199
|
+
const scheduled = (segment) => /-deletion_scheduled-\d+$/.test(segment ?? '');
|
|
200
|
+
// the dates are something more to show: an instance whose GraphQL does not have them still lists what waits to be deleted
|
|
201
|
+
const dates = await this.client.getDeletionDates(token, projects.map((project) => project.id)).catch(() => new Map());
|
|
202
|
+
return projects.map((project) => {
|
|
203
|
+
const segments = (project.path_with_namespace ?? '').split('/');
|
|
204
|
+
const namespaceScheduled = scheduled(segments[segments.length - 2]);
|
|
205
|
+
const ancestorScheduled = segments.slice(0, -2).some(scheduled);
|
|
206
|
+
return {
|
|
207
|
+
id: project.id,
|
|
208
|
+
name: named(project.name),
|
|
209
|
+
path: named(project.path_with_namespace ?? ''),
|
|
210
|
+
webUrl: project.web_url ?? '',
|
|
211
|
+
namespaceId: project.namespace?.id,
|
|
212
|
+
parentNamespaceId: project.namespace?.parent_id,
|
|
213
|
+
deletesOn: dates.get(project.id) ?? null,
|
|
214
|
+
createdAt: project.created_at,
|
|
215
|
+
repositoryScheduled: scheduled(segments[segments.length - 1]) || (!namespaceScheduled && !ancestorScheduled),
|
|
216
|
+
namespaceScheduled,
|
|
217
|
+
ancestorScheduled,
|
|
218
|
+
};
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
restoreRepository({ token, id }) {
|
|
222
|
+
return this.client.send(token, 'POST', `projects/${id}/restore`);
|
|
223
|
+
}
|
|
224
|
+
restoreNamespace({ token, id }) {
|
|
225
|
+
return this.client.send(token, 'POST', `groups/${id}/restore`);
|
|
226
|
+
}
|
|
227
|
+
async renameRepository({ token, id, name }) {
|
|
228
|
+
return this.toRepository(await this.session.renameProject(token, id, name));
|
|
229
|
+
}
|
|
230
|
+
async getRepository({ token, id }) {
|
|
231
|
+
return this.toRepository(await this.client.getProject(token, id));
|
|
232
|
+
}
|
|
233
|
+
async findRepository({ token, path }) {
|
|
234
|
+
try {
|
|
235
|
+
return await this.getRepository({ token, id: path });
|
|
236
|
+
}
|
|
237
|
+
catch (error) {
|
|
238
|
+
if (error.status === 404)
|
|
239
|
+
return null;
|
|
240
|
+
throw error;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
async listRepositories({ token, topic }) {
|
|
244
|
+
return (await this.client.getEnrolledProjects(token, topic)).map((project) => this.toRepository(project));
|
|
245
|
+
}
|
|
246
|
+
async addTopic({ token, id, topic }) {
|
|
247
|
+
const project = await this.client.getProject(token, id);
|
|
248
|
+
const topics = project.topics ?? [];
|
|
249
|
+
if (!topics.includes(topic))
|
|
250
|
+
await this.client.updateProject(token, project.id, { topics: [...topics, topic] });
|
|
251
|
+
}
|
|
252
|
+
setDefaultBranch({ token, id, branch }) {
|
|
253
|
+
return this.session.setDefaultBranch(token, id, branch);
|
|
254
|
+
}
|
|
255
|
+
async getMembers({ token, id }) {
|
|
256
|
+
return (await this.session.getProjectMembers(token, id)).map((member) => ({ username: member.username, accessLevel: member.access_level }));
|
|
257
|
+
}
|
|
258
|
+
/** What the user may do on a repository: the level GitLab gives them there (inherited from the group included), 0 when none. */
|
|
259
|
+
async getAccessLevel({ token, id, username }) {
|
|
260
|
+
return (await this.getMembers({ token, id })).find((member) => member.username === username)?.accessLevel ?? 0;
|
|
261
|
+
}
|
|
262
|
+
// ── files, commits ───────────────────────────────────────────────────────
|
|
263
|
+
async getFile({ token, id, path, ref }) {
|
|
264
|
+
return toFile(await this.session.getFile(token, id, path, ref));
|
|
265
|
+
}
|
|
266
|
+
async getFileRevision({ token, id, path, ref }) {
|
|
267
|
+
return toFile(await this.client.getFileMeta(token, idOf(id), path, ref));
|
|
268
|
+
}
|
|
269
|
+
readFile({ token, id, path, ref }) {
|
|
270
|
+
return this.client.getRawFile(token, idOf(id), path, ref);
|
|
271
|
+
}
|
|
272
|
+
listFiles({ token, id, ref }) {
|
|
273
|
+
return this.client.getFilePaths(token, idOf(id), ref);
|
|
274
|
+
}
|
|
275
|
+
createFile({ token, id, path, branch, content }) {
|
|
276
|
+
return this.session.createFile(token, id, path, branch, content);
|
|
277
|
+
}
|
|
278
|
+
saveFile({ token, id, path, branch, content }) {
|
|
279
|
+
return this.session.saveFile(token, id, path, branch, content);
|
|
280
|
+
}
|
|
281
|
+
async commit({ token, id, branch, message, actions, track = true }) {
|
|
282
|
+
const gitlabActions = actions.map(toGitLabAction);
|
|
283
|
+
return track
|
|
284
|
+
? this.session.commit(token, id, gitlabActions, message, branch)
|
|
285
|
+
: this.client.commitActions(token, idOf(id), branch, message, gitlabActions);
|
|
286
|
+
}
|
|
287
|
+
async downloadArchive({ token, id, ref = 'main' }) {
|
|
288
|
+
return Readable.fromWeb((await this.client.downloadArchive(token, idOf(id), ref)));
|
|
289
|
+
}
|
|
290
|
+
async compare({ token, id, from, to }) {
|
|
291
|
+
const result = await this.client.get(token, `projects/${idOf(id)}/repository/compare?from=${encodeURIComponent(from)}&to=${encodeURIComponent(to)}&straight=false`);
|
|
292
|
+
return { commits: (result.commits ?? []).map(toCommit).reverse(), files: (result.diffs ?? []).map(toFileDiff) };
|
|
293
|
+
}
|
|
294
|
+
async getCommits({ token, id, ref, perPage = 10 }) {
|
|
295
|
+
return (await this.client.getCommits(token, idOf(id), ref, perPage)).map(toCommit);
|
|
296
|
+
}
|
|
297
|
+
// ── branches, tags ───────────────────────────────────────────────────────
|
|
298
|
+
createBranch({ token, id, branch, ref, track = true }) {
|
|
299
|
+
return track ? this.session.createBranch(token, id, branch, ref) : this.client.createBranch(token, idOf(id), branch, ref);
|
|
300
|
+
}
|
|
301
|
+
async listBranches({ token, id }) {
|
|
302
|
+
return (await this.client.listPaged(token, `projects/${idOf(id)}/repository/branches`)).map((branch) => branch.name);
|
|
303
|
+
}
|
|
304
|
+
async getBranch({ token, id, branch }) {
|
|
305
|
+
const found = await this.client.getBranch(token, idOf(id), branch);
|
|
306
|
+
return { name: found.name, lastCommit: found.commit ? toCommit(found.commit) : null };
|
|
307
|
+
}
|
|
308
|
+
async listTags({ token, id }) {
|
|
309
|
+
return (await this.client.getTags(token, idOf(id))).map((tag) => ({ name: tag.name }));
|
|
310
|
+
}
|
|
311
|
+
async getTag({ token, id, tag }) {
|
|
312
|
+
const found = await this.client.getTag(token, idOf(id), tag);
|
|
313
|
+
return found ? { name: found.name, commitId: found.commit?.id, message: typeof found.message === 'string' && found.message !== '' ? found.message : null } : null;
|
|
314
|
+
}
|
|
315
|
+
createTag({ token, id, tag, ref, message }) {
|
|
316
|
+
return message === undefined ? this.session.createTag(token, id, tag, ref) : this.client.createTag(token, idOf(id), tag, ref, message);
|
|
317
|
+
}
|
|
318
|
+
// ── releases ─────────────────────────────────────────────────────────────
|
|
319
|
+
async createRelease({ token, id, tag, name, description = '' }) {
|
|
320
|
+
// GitLab would make the tag itself from a `ref`: a release is of a tag that is already there
|
|
321
|
+
if (!await this.client.getTag(token, idOf(id), tag))
|
|
322
|
+
throw Object.assign(new Error(`There is no tag "${tag}"`), { status: 404 });
|
|
323
|
+
const made = toRelease(await this.client.send(token, 'POST', `projects/${idOf(id)}/releases`, { tag_name: tag, name: name || tag, description }));
|
|
324
|
+
this.releaseUndo.push(() => this.client.send(token, 'DELETE', `projects/${idOf(id)}/releases/${encodeURIComponent(tag)}`));
|
|
325
|
+
return made;
|
|
326
|
+
}
|
|
327
|
+
async getRelease({ token, id, tag }) {
|
|
328
|
+
try {
|
|
329
|
+
return toRelease(await this.client.get(token, `projects/${idOf(id)}/releases/${encodeURIComponent(tag)}`));
|
|
330
|
+
}
|
|
331
|
+
catch (error) {
|
|
332
|
+
if (error.status === 404)
|
|
333
|
+
return null;
|
|
334
|
+
throw error;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
async listReleases({ token, id }) {
|
|
338
|
+
return (await this.client.listPaged(token, `projects/${idOf(id)}/releases`)).map(toRelease);
|
|
339
|
+
}
|
|
340
|
+
async updateRelease({ token, id, tag, changes }) {
|
|
341
|
+
const body = {};
|
|
342
|
+
if (changes.name !== undefined)
|
|
343
|
+
body.name = changes.name || tag;
|
|
344
|
+
if (changes.description !== undefined)
|
|
345
|
+
body.description = changes.description;
|
|
346
|
+
return toRelease(await this.client.send(token, 'PUT', `projects/${idOf(id)}/releases/${encodeURIComponent(tag)}`, body));
|
|
347
|
+
}
|
|
348
|
+
// ── change requests ──────────────────────────────────────────────────────
|
|
349
|
+
async createChangeRequest({ token, id, title, sourceBranch, targetBranch }) {
|
|
350
|
+
return toChangeRequest(await this.session.createMergeRequest(token, id, title, sourceBranch, targetBranch));
|
|
351
|
+
}
|
|
352
|
+
async createOrRenameChangeRequest({ token, id, title, sourceBranch, targetBranch }) {
|
|
353
|
+
return toChangeRequest(await this.session.createOrRenameMergeRequest(token, id, title, sourceBranch, targetBranch));
|
|
354
|
+
}
|
|
355
|
+
async listChangeRequests({ token, id, state = 'opened', targetBranch }) {
|
|
356
|
+
return (await this.client.getMergeRequests(token, idOf(id), state, targetBranch)).map(toChangeRequest);
|
|
357
|
+
}
|
|
358
|
+
async listChangeRequestPipelines({ token, id, number }) {
|
|
359
|
+
return (await this.client.listMergeRequestPipelines(token, idOf(id), number)).map(toPipeline);
|
|
360
|
+
}
|
|
361
|
+
async mergeChangeRequest({ token, id, number, deleteSourceBranch = true }) {
|
|
362
|
+
const rethrowAuth = (error) => {
|
|
363
|
+
if (this.isAuthError(error))
|
|
364
|
+
throw error;
|
|
365
|
+
};
|
|
366
|
+
const read = () => this.client.getMergeRequest(token, idOf(id), number).catch((error) => {
|
|
367
|
+
rethrowAuth(error);
|
|
368
|
+
return null;
|
|
369
|
+
});
|
|
370
|
+
const deadline = Date.now() + (this.config.mergeWaitMs ?? MERGE_WAIT_MS);
|
|
371
|
+
const path = `projects/${idOf(id)}/merge_requests/${number}/merge`;
|
|
372
|
+
for (;;) {
|
|
373
|
+
const answered = await this.client.send(token, 'PUT', path, { should_remove_source_branch: deleteSourceBranch }).then(() => 200, (error) => {
|
|
374
|
+
rethrowAuth(error);
|
|
375
|
+
return error.status ?? 0;
|
|
376
|
+
});
|
|
377
|
+
const mr = await read();
|
|
378
|
+
if (mr?.state === 'merged')
|
|
379
|
+
return false;
|
|
380
|
+
const stuck = mr !== null && (mr.state !== 'opened' || ['conflict', 'broken_status'].includes(String(mr.detailed_merge_status ?? '')));
|
|
381
|
+
if (stuck || (mr !== null && !mergeNotYet(answered)))
|
|
382
|
+
break;
|
|
383
|
+
if (Date.now() >= deadline)
|
|
384
|
+
throw new Error(`GitLab did not take the merge of !${number} in time`);
|
|
385
|
+
await new Promise((resolve) => setTimeout(resolve, MERGE_POLL_MS));
|
|
386
|
+
}
|
|
387
|
+
// GitLab merges asynchronously: a merge that went through reads as `merging` for a moment. Look again before telling anyone it conflicted.
|
|
388
|
+
for (let attempt = 0; attempt < 30; attempt++) {
|
|
389
|
+
const mr = await read();
|
|
390
|
+
if (mr === null)
|
|
391
|
+
return true;
|
|
392
|
+
if (mr.state === 'merged')
|
|
393
|
+
return false;
|
|
394
|
+
const status = String(mr.detailed_merge_status ?? '');
|
|
395
|
+
const settling = mr.state === 'opened' && ['merging', 'checking', 'unchecked', 'preparing', 'ci_still_running'].includes(status);
|
|
396
|
+
if (!settling)
|
|
397
|
+
return true;
|
|
398
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
399
|
+
}
|
|
400
|
+
return true;
|
|
401
|
+
}
|
|
402
|
+
// ── CI, hooks ────────────────────────────────────────────────────────────
|
|
403
|
+
// ── issues ───────────────────────────────────────────────────────────────
|
|
404
|
+
capabilities() {
|
|
405
|
+
return {
|
|
406
|
+
issues: { labels: true, milestones: true, assignees: true, closingWords: false },
|
|
407
|
+
templates: { editable: false },
|
|
408
|
+
mergeRequests: { labels: true, reviewers: true, approvals: true, resolvableThreads: true, squash: true },
|
|
409
|
+
deletion: { namespaceIsRepository: false, delayed: true },
|
|
410
|
+
releases: { prerelease: false }
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
/** GitLab takes the ids of people and of milestones, LINEA speaks of usernames and titles. */
|
|
414
|
+
async userIds(token, usernames) {
|
|
415
|
+
return Promise.all(usernames.map(async (username) => {
|
|
416
|
+
const [found] = await this.client.get(token, `users?username=${encodeURIComponent(username)}`);
|
|
417
|
+
if (!found)
|
|
418
|
+
throw Object.assign(new Error(`There is no user "${username}"`), { status: 400 });
|
|
419
|
+
return found.id;
|
|
420
|
+
}));
|
|
421
|
+
}
|
|
422
|
+
async milestoneId(token, id, title) {
|
|
423
|
+
const [found] = await this.client.get(token, `projects/${idOf(id)}/milestones?title=${encodeURIComponent(title)}`);
|
|
424
|
+
if (!found)
|
|
425
|
+
throw Object.assign(new Error(`There is no milestone "${title}"`), { status: 400 });
|
|
426
|
+
return found.id;
|
|
427
|
+
}
|
|
428
|
+
async listIssues({ token, id, filter = {} }) {
|
|
429
|
+
const perPage = Math.min(Math.max(filter.perPage ?? 20, 1), 100);
|
|
430
|
+
const page = Math.max(filter.page ?? 1, 1);
|
|
431
|
+
const query = new URLSearchParams({ state: filter.state ?? 'opened', order_by: 'created_at', sort: 'desc', per_page: String(perPage), page: String(page) });
|
|
432
|
+
if (filter.author)
|
|
433
|
+
query.set('author_username', filter.author);
|
|
434
|
+
if (filter.assignee)
|
|
435
|
+
query.set('assignee_username', filter.assignee);
|
|
436
|
+
if (filter.labels?.length)
|
|
437
|
+
query.set('labels', filter.labels.join(','));
|
|
438
|
+
if (filter.milestone)
|
|
439
|
+
query.set('milestone', filter.milestone);
|
|
440
|
+
if (filter.search)
|
|
441
|
+
query.set('search', filter.search);
|
|
442
|
+
const found = await this.client.get(token, `projects/${idOf(id)}/issues?${query}`);
|
|
443
|
+
return { items: found.map(toIssue), page, perPage, hasMore: found.length === perPage };
|
|
444
|
+
}
|
|
445
|
+
async getIssue({ token, id, number }) {
|
|
446
|
+
return toIssue(await this.client.get(token, `projects/${idOf(id)}/issues/${number}`));
|
|
447
|
+
}
|
|
448
|
+
async createIssue({ token, id, title, description, labels, assignees, milestone }) {
|
|
449
|
+
const body = { title, description: description ?? '' };
|
|
450
|
+
if (labels?.length)
|
|
451
|
+
body.labels = labels.join(',');
|
|
452
|
+
if (assignees?.length)
|
|
453
|
+
body.assignee_ids = await this.userIds(token, assignees);
|
|
454
|
+
if (milestone)
|
|
455
|
+
body.milestone_id = await this.milestoneId(token, id, milestone);
|
|
456
|
+
return toIssue(await this.client.send(token, 'POST', `projects/${idOf(id)}/issues`, body));
|
|
457
|
+
}
|
|
458
|
+
async updateIssue({ token, id, number, changes }) {
|
|
459
|
+
const body = {};
|
|
460
|
+
if (changes.title !== undefined)
|
|
461
|
+
body.title = changes.title;
|
|
462
|
+
if (changes.description !== undefined)
|
|
463
|
+
body.description = changes.description;
|
|
464
|
+
if (changes.labels !== undefined)
|
|
465
|
+
body.labels = changes.labels.join(',');
|
|
466
|
+
if (changes.assignees !== undefined)
|
|
467
|
+
body.assignee_ids = await this.userIds(token, changes.assignees);
|
|
468
|
+
// (0 takes the issue out of its milestone)
|
|
469
|
+
if (changes.milestone !== undefined)
|
|
470
|
+
body.milestone_id = changes.milestone === null ? 0 : await this.milestoneId(token, id, changes.milestone);
|
|
471
|
+
if (changes.state !== undefined)
|
|
472
|
+
body.state_event = changes.state === 'closed' ? 'close' : 'reopen';
|
|
473
|
+
return toIssue(await this.client.send(token, 'PUT', `projects/${idOf(id)}/issues/${number}`, body));
|
|
474
|
+
}
|
|
475
|
+
async listIssueComments({ token, id, number }) {
|
|
476
|
+
const notes = await this.client.listPaged(token, `projects/${idOf(id)}/issues/${number}/notes?sort=asc&order_by=created_at`);
|
|
477
|
+
return notes.filter((note) => !note.system).map(toComment);
|
|
478
|
+
}
|
|
479
|
+
async addIssueComment({ token, id, number, body }) {
|
|
480
|
+
return toComment(await this.client.send(token, 'POST', `projects/${idOf(id)}/issues/${number}/notes`, { body }));
|
|
481
|
+
}
|
|
482
|
+
async listLabels({ token, id }) {
|
|
483
|
+
const labels = await this.client.listPaged(token, `projects/${idOf(id)}/labels`);
|
|
484
|
+
return labels.map((label) => ({ name: label.name, color: label.color }));
|
|
485
|
+
}
|
|
486
|
+
async listTemplates({ token, id, kind }) {
|
|
487
|
+
const { defaultBranch } = await this.getRepository({ token, id });
|
|
488
|
+
return readRepositoryTemplates(this, { token, id, kind, ref: defaultBranch ?? 'main', host: 'gitlab' });
|
|
489
|
+
}
|
|
490
|
+
async saveTemplate(_args) {
|
|
491
|
+
throw Object.assign(new Error('The templates of this host are files of the repository'), { status: 400 });
|
|
492
|
+
}
|
|
493
|
+
async deleteTemplate(_args) {
|
|
494
|
+
throw Object.assign(new Error('The templates of this host are files of the repository'), { status: 400 });
|
|
495
|
+
}
|
|
496
|
+
async listMilestones({ token, id }) {
|
|
497
|
+
const milestones = await this.client.listPaged(token, `projects/${idOf(id)}/milestones`);
|
|
498
|
+
return milestones.map((milestone) => ({ title: milestone.title, state: milestone.state === 'closed' ? 'closed' : 'opened', dueDate: milestone.due_date ?? null }));
|
|
499
|
+
}
|
|
500
|
+
// ── merge requests (the pages of them) ──────────────────────────────────
|
|
501
|
+
async listMergeRequests({ token, id, filter = {} }) {
|
|
502
|
+
const perPage = Math.min(Math.max(filter.perPage ?? 20, 1), 100);
|
|
503
|
+
const page = Math.max(filter.page ?? 1, 1);
|
|
504
|
+
const query = new URLSearchParams({ state: filter.state ?? 'opened', order_by: 'created_at', sort: 'desc', per_page: String(perPage), page: String(page) });
|
|
505
|
+
if (filter.author)
|
|
506
|
+
query.set('author_username', filter.author);
|
|
507
|
+
if (filter.reviewer)
|
|
508
|
+
query.set('reviewer_username', filter.reviewer);
|
|
509
|
+
if (filter.labels?.length)
|
|
510
|
+
query.set('labels', filter.labels.join(','));
|
|
511
|
+
if (filter.search)
|
|
512
|
+
query.set('search', filter.search);
|
|
513
|
+
if (filter.sourceBranch)
|
|
514
|
+
query.set('source_branch', filter.sourceBranch);
|
|
515
|
+
if (filter.targetBranch)
|
|
516
|
+
query.set('target_branch', filter.targetBranch);
|
|
517
|
+
const found = await this.client.get(token, `projects/${idOf(id)}/merge_requests?${query}`);
|
|
518
|
+
return { items: found.map(toMergeRequest), page, perPage, hasMore: found.length === perPage };
|
|
519
|
+
}
|
|
520
|
+
async getMergeRequest({ token, id, number }) {
|
|
521
|
+
const base = `projects/${idOf(id)}/merge_requests/${number}`;
|
|
522
|
+
const [mr, approvals] = await Promise.all([this.client.get(token, base), this.client.get(token, `${base}/approvals`).catch(() => null)]);
|
|
523
|
+
return {
|
|
524
|
+
...toMergeRequest(mr),
|
|
525
|
+
...(approvals ? { approvals: { required: approvals.approvals_required ?? null, given: (approvals.approved_by ?? []).map((entry) => entry.user?.username) } } : {})
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
async createMergeRequest({ token, id, title, description, sourceBranch, targetBranch, labels, reviewers }) {
|
|
529
|
+
const body = { title, description: description ?? '', source_branch: sourceBranch, target_branch: targetBranch };
|
|
530
|
+
if (labels?.length)
|
|
531
|
+
body.labels = labels.join(',');
|
|
532
|
+
if (reviewers?.length)
|
|
533
|
+
body.reviewer_ids = await this.userIds(token, reviewers);
|
|
534
|
+
return toMergeRequest(await this.client.send(token, 'POST', `projects/${idOf(id)}/merge_requests`, body));
|
|
535
|
+
}
|
|
536
|
+
async updateMergeRequest({ token, id, number, changes }) {
|
|
537
|
+
const body = {};
|
|
538
|
+
if (changes.title !== undefined)
|
|
539
|
+
body.title = changes.title;
|
|
540
|
+
if (changes.description !== undefined)
|
|
541
|
+
body.description = changes.description;
|
|
542
|
+
if (changes.labels !== undefined)
|
|
543
|
+
body.labels = changes.labels.join(',');
|
|
544
|
+
if (changes.reviewers !== undefined)
|
|
545
|
+
body.reviewer_ids = await this.userIds(token, changes.reviewers);
|
|
546
|
+
if (changes.state !== undefined)
|
|
547
|
+
body.state_event = changes.state === 'closed' ? 'close' : 'reopen';
|
|
548
|
+
return toMergeRequest(await this.client.send(token, 'PUT', `projects/${idOf(id)}/merge_requests/${number}`, body));
|
|
549
|
+
}
|
|
550
|
+
async getMergeRequestDiff({ token, id, number }) {
|
|
551
|
+
const mr = await this.client.get(token, `projects/${idOf(id)}/merge_requests/${number}/changes`);
|
|
552
|
+
return (mr.changes ?? []).map(toFileDiff);
|
|
553
|
+
}
|
|
554
|
+
async listMergeRequestThreads({ token, id, number }) {
|
|
555
|
+
const discussions = await this.client.listPaged(token, `projects/${idOf(id)}/merge_requests/${number}/discussions`);
|
|
556
|
+
return discussions.map(toThread).filter((thread) => thread !== null);
|
|
557
|
+
}
|
|
558
|
+
async thread(token, id, number, discussionId) {
|
|
559
|
+
const found = toThread(await this.client.get(token, `projects/${idOf(id)}/merge_requests/${number}/discussions/${discussionId}`));
|
|
560
|
+
if (!found)
|
|
561
|
+
throw Object.assign(new Error('There is no such thread'), { status: 404 });
|
|
562
|
+
return found;
|
|
563
|
+
}
|
|
564
|
+
async addMergeRequestComment({ token, id, number, body, position, threadId }) {
|
|
565
|
+
const base = `projects/${idOf(id)}/merge_requests/${number}`;
|
|
566
|
+
if (threadId !== undefined) {
|
|
567
|
+
await this.client.send(token, 'POST', `${base}/discussions/${threadId}/notes`, { body });
|
|
568
|
+
return this.thread(token, id, number, threadId);
|
|
569
|
+
}
|
|
570
|
+
const payload = { body };
|
|
571
|
+
if (position) {
|
|
572
|
+
// a note on a line is told which versions of the code the line is in: the three commits of the merge request
|
|
573
|
+
const refs = (await this.client.get(token, base)).diff_refs ?? {};
|
|
574
|
+
payload.position = {
|
|
575
|
+
position_type: 'text', base_sha: refs.base_sha, start_sha: refs.start_sha, head_sha: refs.head_sha, new_path: position.path, old_path: position.path,
|
|
576
|
+
...(position.side === 'new' ? { new_line: position.line } : { old_line: position.line })
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
const made = toThread(await this.client.send(token, 'POST', `${base}/discussions`, payload));
|
|
580
|
+
if (!made)
|
|
581
|
+
throw Object.assign(new Error('The comment was not kept'), { status: 400 });
|
|
582
|
+
return made;
|
|
583
|
+
}
|
|
584
|
+
async setThreadResolved({ token, id, number, threadId, resolved }) {
|
|
585
|
+
const made = toThread(await this.client.send(token, 'PUT', `projects/${idOf(id)}/merge_requests/${number}/discussions/${threadId}`, { resolved }));
|
|
586
|
+
if (!made)
|
|
587
|
+
throw Object.assign(new Error('There is no such thread'), { status: 404 });
|
|
588
|
+
return made;
|
|
589
|
+
}
|
|
590
|
+
async mergeMergeRequest({ token, id, number, options = {} }) {
|
|
591
|
+
const body = { should_remove_source_branch: Boolean(options.deleteSourceBranch), squash: Boolean(options.squash) };
|
|
592
|
+
if (options.message)
|
|
593
|
+
body[options.squash ? 'squash_commit_message' : 'merge_commit_message'] = options.message;
|
|
594
|
+
try {
|
|
595
|
+
await this.client.send(token, 'PUT', `projects/${idOf(id)}/merge_requests/${number}/merge`, body);
|
|
596
|
+
return false;
|
|
597
|
+
}
|
|
598
|
+
catch (error) {
|
|
599
|
+
// 405: it cannot be merged (not open, checks, conflicts); 406: it has conflicts
|
|
600
|
+
if ([405, 406].includes(error.status ?? 0))
|
|
601
|
+
return true;
|
|
602
|
+
throw error;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
async approveMergeRequest({ token, id, number }) {
|
|
606
|
+
await this.client.send(token, 'POST', `projects/${idOf(id)}/merge_requests/${number}/approve`);
|
|
607
|
+
}
|
|
608
|
+
async getLatestPipeline({ token, id, ref }) {
|
|
609
|
+
const [latest] = await this.client.getPipelines(token, idOf(id), ref, 1);
|
|
610
|
+
return latest ? { id: latest.id, sha: latest.sha, status: latest.status, ref: latest.ref, createdAt: latest.created_at, webUrl: latest.web_url } : null;
|
|
611
|
+
}
|
|
612
|
+
async listPipelines({ token, id, ref, perPage = 50 }) {
|
|
613
|
+
const found = await this.client.listPipelines(token, idOf(id), ref, perPage);
|
|
614
|
+
return found.map((p) => ({ id: p.id, sha: p.sha, status: p.status, ref: p.ref, createdAt: p.created_at, webUrl: p.web_url }));
|
|
615
|
+
}
|
|
616
|
+
async getPipelineJobs({ token, id, pipelineId }) {
|
|
617
|
+
const jobs = await this.client.getPipelineJobs(token, idOf(id), pipelineId);
|
|
618
|
+
// GitLab lists the newest first; they are shown in the order they run
|
|
619
|
+
return [...jobs].reverse().map((job) => ({
|
|
620
|
+
id: job.id, name: job.name, stage: job.stage ?? '', status: job.status,
|
|
621
|
+
duration: typeof job.duration === 'number' ? Math.round(job.duration) : null, webUrl: job.web_url ?? ''
|
|
622
|
+
}));
|
|
623
|
+
}
|
|
624
|
+
async retryPipeline({ token, id, pipelineId }) {
|
|
625
|
+
await this.client.retryPipeline(token, idOf(id), pipelineId);
|
|
626
|
+
}
|
|
627
|
+
async cancelPipeline({ token, id, pipelineId }) {
|
|
628
|
+
await this.client.cancelPipeline(token, idOf(id), pipelineId);
|
|
629
|
+
}
|
|
630
|
+
async enablePipelines({ token, id }) {
|
|
631
|
+
const project = await this.client.getProject(token, idOf(id));
|
|
632
|
+
// 'private' and 'enabled' both run pipelines; only a project that has them off needs the change
|
|
633
|
+
if (project.builds_access_level !== 'disabled')
|
|
634
|
+
return false;
|
|
635
|
+
await this.client.updateProject(token, idOf(id), { builds_access_level: 'enabled' });
|
|
636
|
+
return true;
|
|
637
|
+
}
|
|
638
|
+
async disablePipelines({ token, id }) {
|
|
639
|
+
const project = await this.client.getProject(token, idOf(id));
|
|
640
|
+
if (project.builds_access_level === 'disabled')
|
|
641
|
+
return false;
|
|
642
|
+
await this.client.updateProject(token, idOf(id), { builds_access_level: 'disabled' });
|
|
643
|
+
return true;
|
|
644
|
+
}
|
|
645
|
+
async pipelinesEnabled({ token, id }) {
|
|
646
|
+
const { builds_access_level: level } = await this.client.getProject(token, idOf(id));
|
|
647
|
+
return level === undefined ? null : level !== 'disabled';
|
|
648
|
+
}
|
|
649
|
+
async cancelJob({ token, id, jobId }) {
|
|
650
|
+
await this.client.cancelJob(token, idOf(id), jobId);
|
|
651
|
+
}
|
|
652
|
+
async getCiRunners({ token, id }) {
|
|
653
|
+
let allowed = true;
|
|
654
|
+
const [project, listed] = await Promise.all([
|
|
655
|
+
this.client.getProject(token, idOf(id)),
|
|
656
|
+
this.client.getProjectRunners(token, idOf(id)).catch(() => {
|
|
657
|
+
allowed = false;
|
|
658
|
+
return [];
|
|
659
|
+
})
|
|
660
|
+
]);
|
|
661
|
+
// the list says nothing of the tags: each runner is asked for
|
|
662
|
+
const runners = await Promise.all(listed.map(async (runner) => {
|
|
663
|
+
const detail = await this.client.getRunner(token, runner.id).catch(() => ({}));
|
|
664
|
+
return {
|
|
665
|
+
id: runner.id,
|
|
666
|
+
description: runner.description ?? '',
|
|
667
|
+
online: Boolean(runner.online),
|
|
668
|
+
paused: Boolean(runner.paused),
|
|
669
|
+
shared: Boolean(runner.is_shared),
|
|
670
|
+
tags: detail.tag_list ?? [],
|
|
671
|
+
runsUntagged: detail.run_untagged ?? true
|
|
672
|
+
};
|
|
673
|
+
}));
|
|
674
|
+
return { supported: true, instanceRunners: Boolean(project.shared_runners_enabled), listed: allowed, runners };
|
|
675
|
+
}
|
|
676
|
+
async setInstanceRunners({ token, id, enabled }) {
|
|
677
|
+
await this.client.updateProject(token, idOf(id), { shared_runners_enabled: enabled });
|
|
678
|
+
}
|
|
679
|
+
async getJobTrace({ token, id, jobId, lines = 40 }) {
|
|
680
|
+
const trace = await this.client.getJobTrace(token, idOf(id), jobId);
|
|
681
|
+
// the colors the runner prints are not for this, nor the time and stream GitLab puts before each line (when it keeps them) and the
|
|
682
|
+
// markers that open and close the sections of a job
|
|
683
|
+
return trace
|
|
684
|
+
.replace(/\u001b\[[0-9;]*[A-Za-z]/g, '')
|
|
685
|
+
.replace(/^\d{4}-\d{2}-\d{2}T[\d:.]+Z \d{2}[OE0-9]\+? ?/gm, '')
|
|
686
|
+
.split('\n')
|
|
687
|
+
.filter((line) => !/^section_(start|end):\d+:\S+\s*$/.test(line))
|
|
688
|
+
.slice(-lines).join('\n').trim();
|
|
689
|
+
}
|
|
690
|
+
async ensureWebhook({ token, id, url, secret, branch }) {
|
|
691
|
+
const existing = await this.client.listHooks(token, idOf(id));
|
|
692
|
+
if (Array.isArray(existing) && existing.some((hook) => hook.url === url))
|
|
693
|
+
return { status: 'already_exists' };
|
|
694
|
+
const hook = await this.client.createHook(token, idOf(id), { url, secret, ...(branch === undefined ? {} : { branch }) });
|
|
695
|
+
return { status: 'created', hookId: hook?.id ?? null };
|
|
696
|
+
}
|
|
697
|
+
/** Whether a webhook delivery is from GitLab: it repeats the secret in a header. */
|
|
698
|
+
verifyWebhook({ headers, secret }) {
|
|
699
|
+
const given = headers['x-gitlab-token'];
|
|
700
|
+
return typeof given === 'string' && given.length === secret.length && crypto.timingSafeEqual(Buffer.from(given), Buffer.from(secret));
|
|
701
|
+
}
|
|
702
|
+
parsePushEvent({ body }) {
|
|
703
|
+
return body?.object_kind === 'push' && body.project && String(body.ref).startsWith('refs/heads/') ? { repositoryId: body.project.id, branch: String(body.ref).replace(/^refs\/heads\//, '') } : null;
|
|
704
|
+
}
|
|
705
|
+
// ── unit of work, errors ─────────────────────────────────────────────────
|
|
706
|
+
async rollback() {
|
|
707
|
+
// the releases first: a release is of a tag, and the tag is what the session removes
|
|
708
|
+
while (this.releaseUndo.length)
|
|
709
|
+
await this.releaseUndo.pop()().catch(() => undefined);
|
|
710
|
+
return this.session.rollback();
|
|
711
|
+
}
|
|
712
|
+
isAuthError(error) {
|
|
713
|
+
return error instanceof GitLabAuthError || error?.name === 'GitAuthError';
|
|
714
|
+
}
|
|
715
|
+
isAlreadyExistsError(error) {
|
|
716
|
+
return isBranchAlreadyExistsError(error);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
//# sourceMappingURL=provider.js.map
|