@leitwerk-dev/gitlab 0.2.1 → 0.3.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/src/testing.ts CHANGED
@@ -5,6 +5,7 @@ import type {
5
5
  GitLabClientLike,
6
6
  GitLabDiff,
7
7
  GitLabFeedback,
8
+ GitLabIssue,
8
9
  GitLabMergeRequest,
9
10
  GitLabNote,
10
11
  GitLabNoteReaction,
@@ -12,10 +13,17 @@ import type {
12
13
  GitLabProject,
13
14
  } from "./client.js";
14
15
 
16
+ export { GITLAB_MR_KIND } from "./external.js";
15
17
  export { setupGitLabIntegration } from "./setup.js";
16
18
 
17
19
  /** @public */
18
20
  interface LocalState {
21
+ /** @public */
22
+ issues?: GitLabIssue[];
23
+ /** @public */
24
+ issueNotes?: Record<string, GitLabNote[]>;
25
+ /** @public */
26
+ labels?: Record<string, string[]>;
19
27
  /** @internal */
20
28
  projects: GitLabProject[];
21
29
  /** @internal */
@@ -33,13 +41,16 @@ interface LocalState {
33
41
  /** @public */
34
42
  reactions?: Record<string, GitLabNoteReaction[]>;
35
43
  }
36
- /** Persistent GitLab test boundary. Repository URLs use local Git's file transport. @public */
44
+ /** Persistent GitLab test boundary. Repository URLs use local Git's file transport. */
45
+ /** @public */
37
46
  export class LocalGitLabAdapter {
38
47
  /** @public */
39
48
  state: LocalState;
40
49
  /** @public */
41
50
  loseNextCommentResponse = false;
42
51
  /** @public */
52
+ loseNextMergeRequestResponse = false;
53
+ /** @public */
43
54
  loseNextReplyResponse = false;
44
55
  /** @public */
45
56
  loseNextReactionResponse = false;
@@ -136,6 +147,67 @@ export class LocalGitLabAdapter {
136
147
  return value;
137
148
  }
138
149
  /** @public */
150
+ createIssue(projectId: number, title: string, labels: string[] = ["use-leitwerk"]): GitLabIssue {
151
+ this.state.issues ??= [];
152
+ const project = this.project(projectId);
153
+ const iid = this.state.issues.filter((i) => i.project_id === projectId).length + 1;
154
+ const issue: GitLabIssue = {
155
+ id: this.state.issues.length + 1,
156
+ iid,
157
+ project_id: projectId,
158
+ title,
159
+ description: "",
160
+ web_url: `${project.web_url}/-/issues/${iid}`,
161
+ state: "opened",
162
+ labels,
163
+ };
164
+ this.state.issues.push(issue);
165
+ this.save();
166
+ return issue;
167
+ }
168
+ /** @public */
169
+ merge(request: GitLabMergeRequest) {
170
+ const base = this.git(request.project_id, ["rev-parse", request.target_branch]);
171
+ const head = this.git(request.project_id, ["rev-parse", request.source_branch]);
172
+ const tree = this.git(request.project_id, ["merge-tree", "--write-tree", base, head]);
173
+ const commit = this.git(request.project_id, [
174
+ "-c",
175
+ "user.name=Leitwerk Bot",
176
+ "-c",
177
+ "user.email=bot@gitlab.test",
178
+ "commit-tree",
179
+ tree,
180
+ "-p",
181
+ base,
182
+ "-p",
183
+ head,
184
+ "-m",
185
+ request.title,
186
+ ]);
187
+ this.git(request.project_id, [
188
+ "update-ref",
189
+ `refs/heads/${request.target_branch}`,
190
+ commit,
191
+ base,
192
+ ]);
193
+ request.state = "merged";
194
+ request.merge_commit_sha = commit;
195
+ this.save();
196
+ }
197
+ private conflicts(request: GitLabMergeRequest): boolean {
198
+ try {
199
+ this.git(request.project_id, [
200
+ "merge-tree",
201
+ "--write-tree",
202
+ request.target_branch,
203
+ request.source_branch,
204
+ ]);
205
+ return false;
206
+ } catch {
207
+ return true;
208
+ }
209
+ }
210
+ /** @public */
139
211
  client(): GitLabClientLike {
140
212
  const mr = (id: number, iid: number) => {
141
213
  const m = this.state.mrs.find((m) => m.project_id === id && m.iid === iid);
@@ -149,6 +221,72 @@ export class LocalGitLabAdapter {
149
221
  });
150
222
  return {
151
223
  baseUrl: this.baseUrl,
224
+ listIssues: async (id) =>
225
+ structuredClone(
226
+ (this.state.issues ?? []).filter((i) => i.project_id === id && i.state === "opened"),
227
+ ),
228
+ getIssue: async (id, iid) => {
229
+ const issue = (this.state.issues ?? []).find((i) => i.project_id === id && i.iid === iid);
230
+ if (!issue) throw new Error("Unknown local GitLab issue");
231
+ return structuredClone(issue);
232
+ },
233
+ updateIssue: async (id, iid, patch) => {
234
+ const issue = (this.state.issues ?? []).find((i) => i.project_id === id && i.iid === iid);
235
+ if (!issue) throw new Error("Unknown local GitLab issue");
236
+ if (patch.labels !== undefined) issue.labels = patch.labels ? patch.labels.split(",") : [];
237
+ if (patch.state_event === "close") issue.state = "closed";
238
+ this.save();
239
+ return structuredClone(issue);
240
+ },
241
+ listIssueNotes: async (id, iid) =>
242
+ structuredClone(this.state.issueNotes?.[`${id}:${iid}`] ?? []),
243
+ addIssueNote: async (id, iid, body) => {
244
+ this.state.issueNotes ??= {};
245
+ this.state.issueNotes[`${id}:${iid}`] ??= [];
246
+ const notes = this.state.issueNotes[`${id}:${iid}`];
247
+ return this.append(notes, { id: notes.length + 1, body }, "Comment");
248
+ },
249
+ listLabels: async (id) => (this.state.labels?.[id] ?? []).map((name) => ({ name })),
250
+ createLabel: async (id, name) => {
251
+ this.state.labels ??= {};
252
+ this.state.labels[id] ??= [];
253
+ const labels = this.state.labels[id];
254
+ if (!labels.includes(name)) labels.push(name);
255
+ this.save();
256
+ return { name };
257
+ },
258
+ listBranchMergeRequests: async (id, source, target) =>
259
+ structuredClone(
260
+ this.state.mrs.filter(
261
+ (m) => m.project_id === id && m.source_branch === source && m.target_branch === target,
262
+ ),
263
+ ),
264
+ createMergeRequest: async (id, input) => {
265
+ const request = this.openMr(this.project(id), input.source_branch);
266
+ Object.assign(request, {
267
+ title: input.title,
268
+ description: input.description,
269
+ target_branch: input.target_branch,
270
+ labels: [],
271
+ });
272
+ this.save();
273
+ if (this.loseNextMergeRequestResponse) {
274
+ this.loseNextMergeRequestResponse = false;
275
+ throw new Error("Response lost after merge request creation");
276
+ }
277
+ return structuredClone(request);
278
+ },
279
+ preflightRepository: async (id, baseBranch, workBranch) => {
280
+ this.git(id, ["rev-parse", `refs/heads/${baseBranch}`]);
281
+ this.git(id, ["check-ref-format", `refs/heads/${workBranch}`]);
282
+ this.git(id, [
283
+ "push",
284
+ "--dry-run",
285
+ this.project(id).http_url_to_repo,
286
+ `${baseBranch}:refs/heads/${workBranch}`,
287
+ ]);
288
+ },
289
+
152
290
  getProject: async (id) => structuredClone(this.project(id)),
153
291
  listProjects: async () => structuredClone(this.state.projects),
154
292
  getGroup: async (id) => ({ id: 1, full_path: String(id) }),
@@ -158,6 +296,7 @@ export class LocalGitLabAdapter {
158
296
  ),
159
297
  getMergeRequest: async (id, iid) => ({
160
298
  ...structuredClone(mr(id, iid)),
299
+ has_conflicts: this.conflicts(mr(id, iid)),
161
300
  sha: commit(mr(id, iid).source_project_id, mr(id, iid).source_branch).id,
162
301
  }),
163
302
  listMergeRequests: async (id, label) =>
package/src/tools.ts CHANGED
@@ -20,29 +20,50 @@ async function existingRemote<T>(read: () => Promise<T>): Promise<T | null> {
20
20
  }
21
21
  }
22
22
  /** @public */
23
- export function resolveGitLabBinding(
23
+ export function resolveGitLabRepositoryBinding(
24
24
  ctx: Pick<IntegrationToolExecutionContext, "project" | "process">,
25
+ /** @public */
26
+ /** @public */
25
27
  ): {
26
28
  /** @public */
27
29
  profile: string;
28
30
  /** @public */
29
31
  projectId: number;
30
- /** @public */
31
- iid: number;
32
32
  } {
33
33
  if (!ctx.project || ctx.project.instanceId !== ctx.process.id)
34
34
  throw new Error("An authorized GitLab process project is required");
35
35
  const binding = ctx.project.metadata?.gitlab as
36
- | { profile?: unknown; projectId?: unknown; iid?: unknown }
36
+ | { profile?: unknown; projectId?: unknown }
37
37
  | undefined;
38
38
  if (
39
39
  !binding ||
40
40
  typeof binding.profile !== "string" ||
41
+ !binding.profile.trim() ||
41
42
  typeof binding.projectId !== "number" ||
42
- typeof binding.iid !== "number"
43
+ !Number.isSafeInteger(binding.projectId) ||
44
+ binding.projectId <= 0
43
45
  )
44
46
  throw new Error("Invalid GitLab project binding");
45
- return { profile: binding.profile, projectId: binding.projectId, iid: binding.iid };
47
+ return { profile: binding.profile, projectId: binding.projectId };
48
+ }
49
+ /** @public */
50
+ export function resolveGitLabBinding(
51
+ ctx: Pick<IntegrationToolExecutionContext, "project" | "process">,
52
+ /** @public */
53
+ /** @public */
54
+ ): {
55
+ /** @public */
56
+ profile: string;
57
+ /** @public */
58
+ projectId: number;
59
+ /** @public */
60
+ iid: number;
61
+ } {
62
+ const repository = resolveGitLabRepositoryBinding(ctx);
63
+ const iid = (ctx.project?.metadata?.gitlab as { iid?: unknown })?.iid;
64
+ if (typeof iid !== "number" || !Number.isSafeInteger(iid) || iid <= 0)
65
+ throw new Error("Invalid GitLab project binding: a merge request is required");
66
+ return { ...repository, iid };
46
67
  }
47
68
  /** @internal */
48
69
  export async function ensureGitLabComment(input: {
@@ -64,6 +85,8 @@ export async function ensureGitLabComment(input: {
64
85
  discussionId?: string;
65
86
  /** @internal */
66
87
  signal?: AbortSignal;
88
+ /** @public */
89
+ /** @public */
67
90
  }): Promise<{
68
91
  /** @internal */
69
92
  marker: string;
@@ -102,9 +125,9 @@ export async function ensureGitLabSeenReaction(input: {
102
125
  writes: ExternalWrites;
103
126
  /** @public */
104
127
  instanceId: string;
105
- /** @public */
128
+ /** @internal */
106
129
  projectId: number;
107
- /** @public */
130
+ /** @internal */
108
131
  iid: number;
109
132
  /** @public */
110
133
  noteId: number;
@@ -167,12 +190,15 @@ export function registerGitLabTools(api: ServerExtensionAPI, integration: GitLab
167
190
  [...required],
168
191
  ),
169
192
  async execute(ctx, args) {
193
+ if (name === "gitlab_get_identity")
194
+ return integration
195
+ .client(resolveGitLabRepositoryBinding(ctx).profile)
196
+ .resolveGitIdentity(ctx.signal);
170
197
  const b = resolveGitLabBinding(ctx);
171
198
  const client = integration.client(b.profile);
172
199
  if (name === "gitlab_observe_merge_request")
173
200
  return observeMergeRequest(client, b.projectId, b.iid, ctx.signal);
174
201
  if (name === "gitlab_get_changes") return client.getChanges(b.projectId, b.iid, ctx.signal);
175
- if (name === "gitlab_get_identity") return client.resolveGitIdentity(ctx.signal);
176
202
  if (name === "gitlab_comment" || name === "gitlab_reply")
177
203
  return ensureGitLabComment({
178
204
  client,