@open-cr-agent/vcs-gitlab 0.10.0 → 0.11.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 +9 -0
- package/dist/client.d.ts +20 -0
- package/dist/client.js +32 -0
- package/dist/commit.d.ts +3 -0
- package/dist/commit.js +63 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/stack.d.ts +2 -0
- package/dist/stack.js +52 -0
- package/dist/tasks.d.ts +3 -0
- package/dist/tasks.js +33 -0
- package/package.json +3 -3
package/dist/adapter.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
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 { gitlabCommit } from "./commit.js";
|
|
4
5
|
import { gitlabDiscussion } from "./discussion.js";
|
|
6
|
+
import { gitlabLinkedTasks } from "./tasks.js";
|
|
5
7
|
// A GitLab merge request as ocra's review conversation (vcs-platform).
|
|
6
8
|
export class GitLabAdapter extends PlatformReview {
|
|
7
9
|
constructor(options) {
|
|
@@ -58,6 +60,9 @@ class GitLabPlatform {
|
|
|
58
60
|
}
|
|
59
61
|
return editors.get(id);
|
|
60
62
|
}
|
|
63
|
+
linkedTasks() {
|
|
64
|
+
return gitlabLinkedTasks(this.options.api, this.options.iid);
|
|
65
|
+
}
|
|
61
66
|
async canWrite(login) {
|
|
62
67
|
const id = this.ids.get(login) ?? (await this.options.api.findUser(login));
|
|
63
68
|
if (id === undefined)
|
|
@@ -142,6 +147,9 @@ class GitLabPlatform {
|
|
|
142
147
|
else
|
|
143
148
|
await this.options.api.replyToDiscussion(this.options.iid, discussion.replyTo, body);
|
|
144
149
|
}
|
|
150
|
+
commit(commit) {
|
|
151
|
+
return gitlabCommit(this.options.api, this.options.iid, commit);
|
|
152
|
+
}
|
|
145
153
|
remember(note) {
|
|
146
154
|
this.ids.set(note.author.username, note.author.id);
|
|
147
155
|
if (note.resolved_by)
|
|
@@ -170,6 +178,7 @@ export function mergeRequestOf(mr) {
|
|
|
170
178
|
baseSha: refs.start_sha,
|
|
171
179
|
headSha: refs.head_sha,
|
|
172
180
|
mergeBaseSha: refs.base_sha,
|
|
181
|
+
branch: mr.source_branch,
|
|
173
182
|
...(mr.author ? { author: mr.author.username } : {}),
|
|
174
183
|
};
|
|
175
184
|
}
|
package/dist/client.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ declare const mergeRequestSchema: z.ZodObject<{
|
|
|
11
11
|
web_url: z.ZodString;
|
|
12
12
|
source_project_id: z.ZodNumber;
|
|
13
13
|
target_project_id: z.ZodNumber;
|
|
14
|
+
source_branch: z.ZodString;
|
|
14
15
|
diff_refs: z.ZodNullable<z.ZodObject<{
|
|
15
16
|
base_sha: z.ZodString;
|
|
16
17
|
start_sha: z.ZodString;
|
|
@@ -18,6 +19,23 @@ declare const mergeRequestSchema: z.ZodObject<{
|
|
|
18
19
|
}, z.core.$strip>>;
|
|
19
20
|
}, z.core.$strip>;
|
|
20
21
|
export type MergeRequest = z.infer<typeof mergeRequestSchema>;
|
|
22
|
+
declare const issueSchema: z.ZodObject<{
|
|
23
|
+
iid: z.ZodNumber;
|
|
24
|
+
project_id: z.ZodNumber;
|
|
25
|
+
title: z.ZodString;
|
|
26
|
+
description: z.ZodPipe<z.ZodNullable<z.ZodString>, z.ZodTransform<string, string | null>>;
|
|
27
|
+
state: z.ZodEnum<{
|
|
28
|
+
closed: "closed";
|
|
29
|
+
opened: "opened";
|
|
30
|
+
}>;
|
|
31
|
+
labels: z.ZodArray<z.ZodString>;
|
|
32
|
+
web_url: z.ZodString;
|
|
33
|
+
epic: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
34
|
+
iid: z.ZodNumber;
|
|
35
|
+
title: z.ZodString;
|
|
36
|
+
}, z.core.$strip>>>;
|
|
37
|
+
}, z.core.$strip>;
|
|
38
|
+
export type Issue = z.infer<typeof issueSchema>;
|
|
21
39
|
declare const noteSchema: z.ZodObject<{
|
|
22
40
|
id: z.ZodNumber;
|
|
23
41
|
body: z.ZodString;
|
|
@@ -98,6 +116,8 @@ export declare class GitLabApi {
|
|
|
98
116
|
fullPath(): Promise<string>;
|
|
99
117
|
resolvesOutdatedThreads(): Promise<boolean | undefined>;
|
|
100
118
|
getMergeRequest(iid: number): Promise<MergeRequest>;
|
|
119
|
+
closesIssues(iid: number): Promise<Issue[]>;
|
|
120
|
+
getIssue(iid: number): Promise<Issue | undefined>;
|
|
101
121
|
listNotes(iid: number): Promise<Note[]>;
|
|
102
122
|
listDiscussions(iid: number): Promise<Discussion[]>;
|
|
103
123
|
createNote(iid: number, body: string): Promise<void>;
|
package/dist/client.js
CHANGED
|
@@ -12,9 +12,24 @@ const mergeRequestSchema = z.object({
|
|
|
12
12
|
web_url: z.string(),
|
|
13
13
|
source_project_id: z.number(),
|
|
14
14
|
target_project_id: z.number(),
|
|
15
|
+
source_branch: z.string(),
|
|
15
16
|
// Null while GitLab is still preparing the diff of a new merge request.
|
|
16
17
|
diff_refs: z.object({ base_sha: sha, start_sha: sha, head_sha: sha }).nullable(),
|
|
17
18
|
});
|
|
19
|
+
const issueSchema = z.object({
|
|
20
|
+
iid: z.number(),
|
|
21
|
+
project_id: z.number(),
|
|
22
|
+
title: z.string(),
|
|
23
|
+
description: z
|
|
24
|
+
.string()
|
|
25
|
+
.nullable()
|
|
26
|
+
.transform((d) => d ?? ""),
|
|
27
|
+
state: z.enum(["opened", "closed"]),
|
|
28
|
+
labels: z.array(z.string()),
|
|
29
|
+
web_url: z.string(),
|
|
30
|
+
// Only on tiers with epics.
|
|
31
|
+
epic: z.object({ iid: z.number(), title: z.string() }).nullish(),
|
|
32
|
+
});
|
|
18
33
|
const noteSchema = z.object({
|
|
19
34
|
id: z.number(),
|
|
20
35
|
body: z.string(),
|
|
@@ -114,6 +129,23 @@ export class GitLabApi {
|
|
|
114
129
|
async getMergeRequest(iid) {
|
|
115
130
|
return mergeRequestSchema.parse(await this.request("GET", `/merge_requests/${iid}`));
|
|
116
131
|
}
|
|
132
|
+
// The issues the merge request closes when it merges, in GitLab's order.
|
|
133
|
+
async closesIssues(iid) {
|
|
134
|
+
return z
|
|
135
|
+
.array(issueSchema)
|
|
136
|
+
.parse(await this.request("GET", `/merge_requests/${iid}/closes_issues?per_page=10`));
|
|
137
|
+
}
|
|
138
|
+
// The project's issue, or undefined when it has none by that number.
|
|
139
|
+
async getIssue(iid) {
|
|
140
|
+
try {
|
|
141
|
+
return issueSchema.parse(await this.request("GET", `/issues/${iid}`));
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
if (error instanceof GitLabApiError && error.status === 404)
|
|
145
|
+
return undefined;
|
|
146
|
+
throw error;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
117
149
|
async listNotes(iid) {
|
|
118
150
|
return this.pages(`/merge_requests/${iid}/notes?sort=asc&order_by=created_at`, noteSchema, MAX_NOTE_PAGES);
|
|
119
151
|
}
|
package/dist/commit.d.ts
ADDED
package/dist/commit.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { GitLabApiError } from "./client.js";
|
|
3
|
+
const sourceSchema = z.object({
|
|
4
|
+
source_branch: z.string(),
|
|
5
|
+
source_project_id: z.number(),
|
|
6
|
+
target_project_id: z.number(),
|
|
7
|
+
});
|
|
8
|
+
const branchSchema = z.object({
|
|
9
|
+
commit: z.object({ id: z.string() }),
|
|
10
|
+
can_push: z.boolean().optional(),
|
|
11
|
+
});
|
|
12
|
+
const fileSchema = z.object({ last_commit_id: z.string() });
|
|
13
|
+
const createdSchema = z.object({ id: z.string() });
|
|
14
|
+
// One commit through the commits API on the merge request's source branch.
|
|
15
|
+
// GitLab takes no parent for it, so the branch is compared first, each
|
|
16
|
+
// updated file carries the commit that last changed it (GitLab refuses the
|
|
17
|
+
// update when another has since) and each new file is created (refused
|
|
18
|
+
// when one is there): a push in between never loses a line.
|
|
19
|
+
export async function gitlabCommit(api, iid, commit) {
|
|
20
|
+
const source = sourceSchema.parse(await api.request("GET", `/merge_requests/${iid}`));
|
|
21
|
+
if (source.source_project_id !== source.target_project_id) {
|
|
22
|
+
return refused("not_writable", "the branch is on a fork, which ocra's token cannot write to");
|
|
23
|
+
}
|
|
24
|
+
const branch = branchSchema.parse(await api.request("GET", `/repository/branches/${encodeURIComponent(source.source_branch)}`));
|
|
25
|
+
if (branch.commit.id !== commit.parent) {
|
|
26
|
+
return refused("head_moved", "the branch moved after ocra read it");
|
|
27
|
+
}
|
|
28
|
+
if (branch.can_push === false) {
|
|
29
|
+
return refused("not_writable", "ocra's token may not push to the branch");
|
|
30
|
+
}
|
|
31
|
+
const actions = [];
|
|
32
|
+
for (const f of commit.files) {
|
|
33
|
+
if (f.created) {
|
|
34
|
+
actions.push({ action: "create", file_path: f.path, content: f.content });
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
const path = `/repository/files/${encodeURIComponent(f.path)}?ref=${commit.parent}`;
|
|
38
|
+
const { last_commit_id } = fileSchema.parse(await api.request("GET", path));
|
|
39
|
+
actions.push({ action: "update", file_path: f.path, content: f.content, last_commit_id });
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
const created = createdSchema.parse(await api.request("POST", "/repository/commits", {
|
|
43
|
+
branch: source.source_branch,
|
|
44
|
+
commit_message: commit.message,
|
|
45
|
+
actions,
|
|
46
|
+
}));
|
|
47
|
+
return { committed: true, sha: created.id };
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
if (!(error instanceof GitLabApiError))
|
|
51
|
+
throw error;
|
|
52
|
+
if (error.status === 400 && /already exists|changed since/i.test(error.message)) {
|
|
53
|
+
return refused("head_moved", "the branch moved after ocra read it");
|
|
54
|
+
}
|
|
55
|
+
if (error.status === 403) {
|
|
56
|
+
return refused("not_writable", `GitLab refused the commit: ${error.message}`);
|
|
57
|
+
}
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function refused(outcome, reason) {
|
|
62
|
+
return { committed: false, outcome, reason };
|
|
63
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/stack.d.ts
ADDED
package/dist/stack.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { pickStacked } from "@open-cr-agent/vcs-platform";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { gitlabApi } from "./target.js";
|
|
4
|
+
const mergeRequestSchema = z.object({
|
|
5
|
+
iid: z.number(),
|
|
6
|
+
state: z.string(),
|
|
7
|
+
title: z.string(),
|
|
8
|
+
description: z.string().nullish(),
|
|
9
|
+
web_url: z.string(),
|
|
10
|
+
source_branch: z.string(),
|
|
11
|
+
target_branch: z.string(),
|
|
12
|
+
});
|
|
13
|
+
function stacked(request) {
|
|
14
|
+
return {
|
|
15
|
+
number: request.iid,
|
|
16
|
+
head: request.source_branch,
|
|
17
|
+
base: request.target_branch,
|
|
18
|
+
state: request.state === "opened" ? "open" : request.state === "merged" ? "merged" : "closed",
|
|
19
|
+
title: request.title,
|
|
20
|
+
body: request.description ?? "",
|
|
21
|
+
webUrl: request.web_url,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
// Merge requests between branches of the project itself. The token needs
|
|
25
|
+
// the api scope and the Developer role.
|
|
26
|
+
export async function gitlabStackHost(options, warn) {
|
|
27
|
+
const { api } = await gitlabApi({ ...options, warn }, options.repository, "ocra stack");
|
|
28
|
+
return {
|
|
29
|
+
async find(head) {
|
|
30
|
+
const query = new URLSearchParams({ source_branch: head, per_page: "20" });
|
|
31
|
+
const requests = z
|
|
32
|
+
.array(mergeRequestSchema)
|
|
33
|
+
.parse(await api.request("GET", `/merge_requests?${query}`));
|
|
34
|
+
return pickStacked(requests.map(stacked));
|
|
35
|
+
},
|
|
36
|
+
async open({ head, base, title, body, draft }) {
|
|
37
|
+
return stacked(mergeRequestSchema.parse(await api.request("POST", "/merge_requests", {
|
|
38
|
+
source_branch: head,
|
|
39
|
+
target_branch: base,
|
|
40
|
+
title: draft ? `Draft: ${title}` : title,
|
|
41
|
+
description: body,
|
|
42
|
+
remove_source_branch: true,
|
|
43
|
+
})));
|
|
44
|
+
},
|
|
45
|
+
async update(number, change) {
|
|
46
|
+
await api.request("PUT", `/merge_requests/${number}`, {
|
|
47
|
+
...(change.base !== undefined ? { target_branch: change.base } : {}),
|
|
48
|
+
...(change.body !== undefined ? { description: change.body } : {}),
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
package/dist/tasks.d.ts
ADDED
package/dist/tasks.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// GitLab names a branch made from an issue "<iid>-<title>".
|
|
2
|
+
const ISSUE_BRANCH = /^(\d+)-/;
|
|
3
|
+
// The issues of this project the merge request closes, or the one its source
|
|
4
|
+
// branch is named after (ADR-0052). Another project's issue is left out: a
|
|
5
|
+
// fork's author could otherwise pull text they cannot read into a review
|
|
6
|
+
// posted where they can.
|
|
7
|
+
export async function gitlabLinkedTasks(api, iid) {
|
|
8
|
+
const [mr, closing] = await Promise.all([api.getMergeRequest(iid), api.closesIssues(iid)]);
|
|
9
|
+
const own = closing.filter((issue) => issue.project_id === mr.target_project_id);
|
|
10
|
+
if (own.length > 0)
|
|
11
|
+
return own.map(linkedTask);
|
|
12
|
+
const branched = ISSUE_BRANCH.exec(mr.source_branch)?.[1];
|
|
13
|
+
if (!branched)
|
|
14
|
+
return [];
|
|
15
|
+
const issue = await api.getIssue(Number(branched));
|
|
16
|
+
return issue ? [linkedTask(issue)] : [];
|
|
17
|
+
}
|
|
18
|
+
function linkedTask(issue) {
|
|
19
|
+
return {
|
|
20
|
+
tracker: "gitlab",
|
|
21
|
+
key: `#${issue.iid}`,
|
|
22
|
+
url: issue.web_url,
|
|
23
|
+
title: issue.title,
|
|
24
|
+
state: issue.state === "opened" ? "open" : "closed",
|
|
25
|
+
labels: issue.labels,
|
|
26
|
+
body: issue.description,
|
|
27
|
+
// The issue names its epic, not the epic's description.
|
|
28
|
+
...(issue.epic
|
|
29
|
+
? { parent: { key: `&${issue.epic.iid}`, title: issue.epic.title, body: "" } }
|
|
30
|
+
: {}),
|
|
31
|
+
subtasks: [],
|
|
32
|
+
};
|
|
33
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-cr-agent/vcs-gitlab",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.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.
|
|
37
|
-
"@open-cr-agent/vcs-platform": "0.
|
|
36
|
+
"@open-cr-agent/core": "0.11.0",
|
|
37
|
+
"@open-cr-agent/vcs-platform": "0.11.0",
|
|
38
38
|
"zod": "^4.6.6"
|
|
39
39
|
},
|
|
40
40
|
"publishConfig": {
|