@open-cr-agent/vcs-gitlab 0.9.0 → 0.10.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/dist/adapter.js CHANGED
@@ -1,6 +1,7 @@
1
- import { errorMessage, OcraError } from "@open-cr-agent/core";
1
+ import { errorMessage, OcraError, } from "@open-cr-agent/core";
2
2
  import { gitlabSuggestion, PlatformReview, } from "@open-cr-agent/vcs-platform";
3
3
  import { DEVELOPER, GitLabApiError, } from "./client.js";
4
+ import { gitlabDiscussion } from "./discussion.js";
4
5
  // A GitLab merge request as ocra's review conversation (vcs-platform).
5
6
  export class GitLabAdapter extends PlatformReview {
6
7
  constructor(options) {
@@ -132,6 +133,15 @@ class GitLabPlatform {
132
133
  resolveThread(id) {
133
134
  return this.options.api.resolveDiscussion(this.options.iid, id);
134
135
  }
136
+ discussion(ref) {
137
+ return gitlabDiscussion(this.options.api, this.options.iid, ref);
138
+ }
139
+ async reply(discussion, body) {
140
+ if (discussion.replyTo === undefined)
141
+ await this.options.api.createNote(this.options.iid, body);
142
+ else
143
+ await this.options.api.replyToDiscussion(this.options.iid, discussion.replyTo, body);
144
+ }
135
145
  remember(note) {
136
146
  this.ids.set(note.author.username, note.author.id);
137
147
  if (note.resolved_by)
package/dist/client.d.ts CHANGED
@@ -33,6 +33,10 @@ declare const noteSchema: z.ZodObject<{
33
33
  id: z.ZodNumber;
34
34
  username: z.ZodString;
35
35
  }, z.core.$strip>>>;
36
+ position: z.ZodOptional<z.ZodNullable<z.ZodObject<{
37
+ new_path: z.ZodString;
38
+ new_line: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
39
+ }, z.core.$strip>>>;
36
40
  }, z.core.$strip>;
37
41
  export type Note = z.infer<typeof noteSchema>;
38
42
  declare const discussionSchema: z.ZodObject<{
@@ -53,6 +57,10 @@ declare const discussionSchema: z.ZodObject<{
53
57
  id: z.ZodNumber;
54
58
  username: z.ZodString;
55
59
  }, z.core.$strip>>>;
60
+ position: z.ZodOptional<z.ZodNullable<z.ZodObject<{
61
+ new_path: z.ZodString;
62
+ new_line: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
63
+ }, z.core.$strip>>>;
56
64
  }, z.core.$strip>>;
57
65
  }, z.core.$strip>;
58
66
  export type Discussion = z.infer<typeof discussionSchema>;
@@ -95,6 +103,7 @@ export declare class GitLabApi {
95
103
  createNote(iid: number, body: string): Promise<void>;
96
104
  updateNote(iid: number, noteId: number, body: string): Promise<void>;
97
105
  createDiscussion(iid: number, body: string, position: Position): Promise<void>;
106
+ replyToDiscussion(iid: number, discussionId: string, body: string): Promise<void>;
98
107
  resolveDiscussion(iid: number, discussionId: string): Promise<void>;
99
108
  accessLevel(userId: number): Promise<number>;
100
109
  findUser(username: string): Promise<number | undefined>;
package/dist/client.js CHANGED
@@ -25,6 +25,12 @@ const noteSchema = z.object({
25
25
  resolvable: z.boolean().optional(),
26
26
  resolved: z.boolean().optional(),
27
27
  resolved_by: user.nullable().optional(),
28
+ // On a line of the diff: the file and the new side's line, unset on a
29
+ // removed line.
30
+ position: z
31
+ .object({ new_path: z.string(), new_line: z.number().nullable().optional() })
32
+ .nullable()
33
+ .optional(),
28
34
  });
29
35
  const discussionSchema = z.object({
30
36
  id: z.string(),
@@ -123,6 +129,9 @@ export class GitLabApi {
123
129
  async createDiscussion(iid, body, position) {
124
130
  await this.request("POST", `/merge_requests/${iid}/discussions`, { body, position });
125
131
  }
132
+ async replyToDiscussion(iid, discussionId, body) {
133
+ await this.request("POST", `/merge_requests/${iid}/discussions/${encodeURIComponent(discussionId)}/notes`, { body });
134
+ }
126
135
  async resolveDiscussion(iid, discussionId) {
127
136
  await this.request("PUT", `/merge_requests/${iid}/discussions/${encodeURIComponent(discussionId)}?resolved=true`);
128
137
  }
@@ -0,0 +1,4 @@
1
+ import { type MentionRef } from "@open-cr-agent/core";
2
+ import type { PlatformDiscussion } from "@open-cr-agent/vcs-platform";
3
+ import type { GitLabApi } from "./client.js";
4
+ export declare function gitlabDiscussion(api: GitLabApi, iid: number, ref: MentionRef): Promise<PlatformDiscussion | undefined>;
@@ -0,0 +1,51 @@
1
+ import { OcraError } from "@open-cr-agent/core";
2
+ // A note on the merge request and the discussion it is in. Every note is in
3
+ // one on GitLab, a comment on the merge request as a whole too, so a reply
4
+ // always goes to its discussion; such a comment is read, as on GitHub,
5
+ // among the merge request's other comments.
6
+ export async function gitlabDiscussion(api, iid, ref) {
7
+ const id = Number(ref.id);
8
+ if (!Number.isSafeInteger(id) || id <= 0)
9
+ return undefined;
10
+ const discussions = await api.listDiscussions(iid);
11
+ const discussion = discussions.find((d) => d.notes.some((n) => n.id === id));
12
+ const conversation = discussion?.individual_note
13
+ ? discussions.filter((d) => d.individual_note || d === discussion)
14
+ : discussion
15
+ ? [discussion]
16
+ : [];
17
+ const notes = conversation
18
+ .flatMap((d) => d.notes)
19
+ .filter((n) => !n.system)
20
+ .sort((a, b) => a.id - b.id);
21
+ const note = notes.find((n) => n.id === id);
22
+ if (!discussion || !note)
23
+ return undefined;
24
+ const editors = await api.noteEditors(iid);
25
+ if (!editors.has(id)) {
26
+ throw new OcraError("VCS_API_FAILED", `GitLab did not say who last edited note ${id}`);
27
+ }
28
+ const editor = editors.get(id);
29
+ const said = (n) => ({ author: n.author.username, body: n.body });
30
+ const at = notes.indexOf(note);
31
+ const position = discussion.individual_note ? undefined : notes[0]?.position;
32
+ return {
33
+ comment: {
34
+ id: String(id),
35
+ author: note.author.username,
36
+ body: note.body,
37
+ ...(editor ? { editor } : {}),
38
+ },
39
+ earlier: notes.slice(0, at).map(said),
40
+ later: notes.slice(at + 1).map(said),
41
+ ...(position
42
+ ? {
43
+ on: {
44
+ path: position.new_path,
45
+ ...(position.new_line ? { line: position.new_line } : {}),
46
+ },
47
+ }
48
+ : {}),
49
+ replyTo: discussion.id,
50
+ };
51
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-cr-agent/vcs-gitlab",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "GitLab merge request adapter for Open-CR-Agent",
5
5
  "keywords": [
6
6
  "code-review",
@@ -33,8 +33,8 @@
33
33
  "!dist/**/*.map"
34
34
  ],
35
35
  "dependencies": {
36
- "@open-cr-agent/core": "0.9.0",
37
- "@open-cr-agent/vcs-platform": "0.9.0",
36
+ "@open-cr-agent/core": "0.10.0",
37
+ "@open-cr-agent/vcs-platform": "0.10.0",
38
38
  "zod": "^4.6.6"
39
39
  },
40
40
  "publishConfig": {