@open-cr-agent/vcs-gitlab 0.21.0 → 0.23.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.d.ts +2 -1
- package/dist/adapter.js +21 -1
- package/dist/checks.d.ts +4 -0
- package/dist/checks.js +75 -0
- package/dist/client.d.ts +5 -1
- package/dist/client.js +10 -2
- package/dist/commit.d.ts +6 -0
- package/dist/commit.js +18 -0
- package/dist/plugin.js +3 -1
- package/dist/target.js +1 -0
- package/package.json +3 -3
package/dist/adapter.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { type FileDiff } from "@open-cr-agent/core";
|
|
2
|
-
import { type CodeSource, type History, type PlatformChangeRequest, PlatformReview } from "@open-cr-agent/vcs-platform";
|
|
2
|
+
import { type CodeSource, type History, type MergeSource, type PlatformChangeRequest, PlatformReview } from "@open-cr-agent/vcs-platform";
|
|
3
3
|
import { type GitLabApi, type MergeRequest, type Position } from "./client.js";
|
|
4
4
|
export interface GitLabAdapterOptions {
|
|
5
5
|
iid: number;
|
|
6
6
|
api: GitLabApi;
|
|
7
7
|
code: CodeSource;
|
|
8
8
|
history?: History;
|
|
9
|
+
merge?: MergeSource;
|
|
9
10
|
snapshot?: MergeRequestSnapshot;
|
|
10
11
|
}
|
|
11
12
|
export interface MergeRequestSnapshot extends PlatformChangeRequest {
|
package/dist/adapter.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { errorMessage, OcraError, } from "@open-cr-agent/core";
|
|
2
2
|
import { gitlabSuggestion, PlatformReview, } from "@open-cr-agent/vcs-platform";
|
|
3
|
+
import { gitlabChecks, gitlabFailingOnBase } from "./checks.js";
|
|
3
4
|
import { DEVELOPER, GitLabApiError, } from "./client.js";
|
|
4
|
-
import { gitlabCommit } from "./commit.js";
|
|
5
|
+
import { gitlabBaseHead, gitlabCommit, gitlabPushBranch } from "./commit.js";
|
|
5
6
|
import { gitlabDiscussion } from "./discussion.js";
|
|
6
7
|
import { gitlabLinkedTasks } from "./tasks.js";
|
|
7
8
|
// A GitLab merge request as ocra's review conversation (vcs-platform).
|
|
@@ -12,6 +13,7 @@ export class GitLabAdapter extends PlatformReview {
|
|
|
12
13
|
platform: new GitLabPlatform(options),
|
|
13
14
|
code: options.code,
|
|
14
15
|
...(options.history ? { history: options.history } : {}),
|
|
16
|
+
...(options.merge ? { merge: options.merge } : {}),
|
|
15
17
|
});
|
|
16
18
|
}
|
|
17
19
|
}
|
|
@@ -150,6 +152,24 @@ class GitLabPlatform {
|
|
|
150
152
|
commit(commit) {
|
|
151
153
|
return gitlabCommit(this.options.api, this.options.iid, commit);
|
|
152
154
|
}
|
|
155
|
+
baseHead() {
|
|
156
|
+
return gitlabBaseHead(this.options.api, this.options.iid);
|
|
157
|
+
}
|
|
158
|
+
// GitLab's private commit address of the token's account.
|
|
159
|
+
async pushTarget() {
|
|
160
|
+
const target = await gitlabPushBranch(this.options.api, this.options.iid);
|
|
161
|
+
if ("refused" in target)
|
|
162
|
+
return target;
|
|
163
|
+
const { id, username } = await this.options.api.currentUser();
|
|
164
|
+
const email = `${id}-${username}@users.noreply.${this.options.api.host}`;
|
|
165
|
+
return { ...target, author: { name: username, email } };
|
|
166
|
+
}
|
|
167
|
+
checks(sha) {
|
|
168
|
+
return gitlabChecks(this.options.api, this.options.iid, sha);
|
|
169
|
+
}
|
|
170
|
+
failingOnBase() {
|
|
171
|
+
return gitlabFailingOnBase(this.options.api, this.options.iid);
|
|
172
|
+
}
|
|
153
173
|
remember(note) {
|
|
154
174
|
this.ids.set(note.author.username, note.author.id);
|
|
155
175
|
if (note.resolved_by)
|
package/dist/checks.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { PlatformCheck } from "@open-cr-agent/vcs-platform";
|
|
2
|
+
import { type GitLabApi } from "./client.js";
|
|
3
|
+
export declare function gitlabChecks(api: GitLabApi, iid: number, sha: string): Promise<PlatformCheck[]>;
|
|
4
|
+
export declare function gitlabFailingOnBase(api: GitLabApi, iid: number): Promise<string[]>;
|
package/dist/checks.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { LOG_TAIL_BYTES } from "@open-cr-agent/vcs-platform/internal";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { GitLabApiError } from "./client.js";
|
|
4
|
+
// A pipeline with more jobs than these pages hold is read in part.
|
|
5
|
+
const MAX_PAGES = 3;
|
|
6
|
+
const PER_PAGE = 100;
|
|
7
|
+
const UNFINISHED = new Set([
|
|
8
|
+
"created",
|
|
9
|
+
"waiting_for_resource",
|
|
10
|
+
"preparing",
|
|
11
|
+
"pending",
|
|
12
|
+
"running",
|
|
13
|
+
"scheduled",
|
|
14
|
+
]);
|
|
15
|
+
const pipelineSchema = z.object({ id: z.number(), sha: z.string() });
|
|
16
|
+
const jobSchema = z.object({
|
|
17
|
+
id: z.number(),
|
|
18
|
+
name: z.string(),
|
|
19
|
+
status: z.string(),
|
|
20
|
+
allow_failure: z.boolean().optional(),
|
|
21
|
+
});
|
|
22
|
+
const targetSchema = z.object({ target_branch: z.string() });
|
|
23
|
+
// The jobs of the merge request's newest pipeline on a commit; none when it
|
|
24
|
+
// has no pipeline there.
|
|
25
|
+
export async function gitlabChecks(api, iid, sha) {
|
|
26
|
+
const pipelines = z
|
|
27
|
+
.array(pipelineSchema)
|
|
28
|
+
.parse(await api.request("GET", `/merge_requests/${iid}/pipelines?per_page=${PER_PAGE}`));
|
|
29
|
+
const pipeline = pipelines.find((p) => p.sha === sha);
|
|
30
|
+
return pipeline ? jobsOf(api, pipeline.id) : [];
|
|
31
|
+
}
|
|
32
|
+
export async function gitlabFailingOnBase(api, iid) {
|
|
33
|
+
const { target_branch } = targetSchema.parse(await api.request("GET", `/merge_requests/${iid}`));
|
|
34
|
+
const ref = encodeURIComponent(target_branch);
|
|
35
|
+
const [latest] = z
|
|
36
|
+
.array(pipelineSchema)
|
|
37
|
+
.parse(await api.request("GET", `/pipelines?ref=${ref}&per_page=1`));
|
|
38
|
+
if (!latest)
|
|
39
|
+
return [];
|
|
40
|
+
const jobs = await jobsOf(api, latest.id);
|
|
41
|
+
return jobs.filter((job) => job.state === "failed").map((job) => job.name);
|
|
42
|
+
}
|
|
43
|
+
// A retried job is listed under its latest run. A job allowed to fail does
|
|
44
|
+
// not fail its pipeline, and a manual one waits for a person, not for time.
|
|
45
|
+
async function jobsOf(api, pipeline) {
|
|
46
|
+
const checks = [];
|
|
47
|
+
for (let page = 1; page <= MAX_PAGES; page += 1) {
|
|
48
|
+
const path = `/pipelines/${pipeline}/jobs?per_page=${PER_PAGE}&page=${page}`;
|
|
49
|
+
const batch = z.array(jobSchema).parse(await api.request("GET", path));
|
|
50
|
+
for (const job of batch) {
|
|
51
|
+
checks.push({
|
|
52
|
+
name: job.name,
|
|
53
|
+
state: UNFINISHED.has(job.status)
|
|
54
|
+
? "pending"
|
|
55
|
+
: job.status === "failed" && job.allow_failure !== true
|
|
56
|
+
? "failed"
|
|
57
|
+
: "passed",
|
|
58
|
+
log: () => traceOf(api, job.id),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
if (batch.length < PER_PAGE)
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
return checks;
|
|
65
|
+
}
|
|
66
|
+
async function traceOf(api, job) {
|
|
67
|
+
try {
|
|
68
|
+
return await api.tail(`/jobs/${job}/trace`, LOG_TAIL_BYTES);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
if (error instanceof GitLabApiError && [403, 404].includes(error.status))
|
|
72
|
+
return "";
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
}
|
package/dist/client.d.ts
CHANGED
|
@@ -109,6 +109,7 @@ export declare class GitLabApi {
|
|
|
109
109
|
private readonly api;
|
|
110
110
|
private readonly project;
|
|
111
111
|
constructor(project: string | number, options: GitLabApiOptions);
|
|
112
|
+
get host(): string;
|
|
112
113
|
currentUser(): Promise<{
|
|
113
114
|
id: number;
|
|
114
115
|
username: string;
|
|
@@ -130,6 +131,9 @@ export declare class GitLabApi {
|
|
|
130
131
|
noteEditors(iid: number): Promise<Map<number, string | undefined>>;
|
|
131
132
|
private pages;
|
|
132
133
|
private graphql;
|
|
133
|
-
|
|
134
|
+
tail(path: string, tailBytes: number): Promise<string>;
|
|
135
|
+
request(method: string, path: string, body?: unknown, tail?: {
|
|
136
|
+
tailBytes?: number;
|
|
137
|
+
}): Promise<unknown>;
|
|
134
138
|
}
|
|
135
139
|
export {};
|
package/dist/client.js
CHANGED
|
@@ -110,6 +110,10 @@ export class GitLabApi {
|
|
|
110
110
|
toError: (status, message) => new GitLabApiError(status, message),
|
|
111
111
|
};
|
|
112
112
|
}
|
|
113
|
+
// The instance's host name, as its noreply addresses end.
|
|
114
|
+
get host() {
|
|
115
|
+
return new URL(this.baseUrl).hostname;
|
|
116
|
+
}
|
|
113
117
|
async currentUser() {
|
|
114
118
|
return user.parse(await sendWithRetry(this.api, { method: "GET", url: `${this.baseUrl}/user`, path: "/user" }));
|
|
115
119
|
}
|
|
@@ -245,9 +249,13 @@ export class GitLabApi {
|
|
|
245
249
|
}
|
|
246
250
|
return result.data;
|
|
247
251
|
}
|
|
248
|
-
|
|
252
|
+
// The end of a text answer, a job's log.
|
|
253
|
+
async tail(path, tailBytes) {
|
|
254
|
+
return String(await this.request("GET", path, undefined, { tailBytes }));
|
|
255
|
+
}
|
|
256
|
+
request(method, path, body, tail = {}) {
|
|
249
257
|
const project = encodeURIComponent(String(this.project));
|
|
250
258
|
const url = `${this.baseUrl}/projects/${project}${path}`;
|
|
251
|
-
return sendWithRetry(this.api, { method, url, path, body });
|
|
259
|
+
return sendWithRetry(this.api, { method, url, path, body, ...tail });
|
|
252
260
|
}
|
|
253
261
|
}
|
package/dist/commit.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
1
|
import type { ChangeCommit, CommitResult } from "@open-cr-agent/core";
|
|
2
2
|
import { type GitLabApi } from "./client.js";
|
|
3
3
|
export declare function gitlabCommit(api: GitLabApi, iid: number, commit: ChangeCommit): Promise<CommitResult>;
|
|
4
|
+
export declare function gitlabBaseHead(api: GitLabApi, iid: number): Promise<string>;
|
|
5
|
+
export declare function gitlabPushBranch(api: GitLabApi, iid: number): Promise<{
|
|
6
|
+
branch: string;
|
|
7
|
+
} | {
|
|
8
|
+
refused: string;
|
|
9
|
+
}>;
|
package/dist/commit.js
CHANGED
|
@@ -58,6 +58,24 @@ export async function gitlabCommit(api, iid, commit) {
|
|
|
58
58
|
throw error;
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
|
+
const targetSchema = z.object({ target_branch: z.string() });
|
|
62
|
+
export async function gitlabBaseHead(api, iid) {
|
|
63
|
+
const { target_branch } = targetSchema.parse(await api.request("GET", `/merge_requests/${iid}`));
|
|
64
|
+
const branch = branchSchema.parse(await api.request("GET", `/repository/branches/${encodeURIComponent(target_branch)}`));
|
|
65
|
+
return branch.commit.id;
|
|
66
|
+
}
|
|
67
|
+
// The branch a merge of ocra's is pushed to: one of the project itself that
|
|
68
|
+
// the token's account may push to.
|
|
69
|
+
export async function gitlabPushBranch(api, iid) {
|
|
70
|
+
const source = sourceSchema.parse(await api.request("GET", `/merge_requests/${iid}`));
|
|
71
|
+
if (source.source_project_id !== source.target_project_id) {
|
|
72
|
+
return { refused: "the branch is on a fork, which ocra's token cannot write to" };
|
|
73
|
+
}
|
|
74
|
+
const branch = branchSchema.parse(await api.request("GET", `/repository/branches/${encodeURIComponent(source.source_branch)}`));
|
|
75
|
+
return branch.can_push === false
|
|
76
|
+
? { refused: "ocra's token may not push to the branch" }
|
|
77
|
+
: { branch: source.source_branch };
|
|
78
|
+
}
|
|
61
79
|
function refused(outcome, reason) {
|
|
62
80
|
return { committed: false, outcome, reason };
|
|
63
81
|
}
|
package/dist/plugin.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { changeRequestSchema, codeSourceSchema, commitIdSchema, fetchSchema, historySchema, signalSchema, } from "@open-cr-agent/vcs-platform";
|
|
1
|
+
import { changeRequestSchema, codeSourceSchema, commitIdSchema, fetchSchema, historySchema, mergeSourceSchema, signalSchema, } from "@open-cr-agent/vcs-platform";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { GitLabAdapter } from "./adapter.js";
|
|
4
4
|
import { GitLabApi } from "./client.js";
|
|
@@ -13,6 +13,7 @@ const optionsSchema = z.object({
|
|
|
13
13
|
code: codeSourceSchema,
|
|
14
14
|
snapshot: changeRequestSchema.extend({ mergeBaseSha: commitIdSchema }).optional(),
|
|
15
15
|
history: historySchema.optional(),
|
|
16
|
+
merge: mergeSourceSchema.optional(),
|
|
16
17
|
});
|
|
17
18
|
export const gitlabPlugin = {
|
|
18
19
|
name: "vcs-gitlab",
|
|
@@ -30,6 +31,7 @@ export const gitlabPlugin = {
|
|
|
30
31
|
api,
|
|
31
32
|
code: options.code,
|
|
32
33
|
...(options.history ? { history: options.history } : {}),
|
|
34
|
+
...(options.merge ? { merge: options.merge } : {}),
|
|
33
35
|
...(options.snapshot ? { snapshot: options.snapshot } : {}),
|
|
34
36
|
});
|
|
35
37
|
});
|
package/dist/target.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-cr-agent/vcs-gitlab",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.23.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.23.0",
|
|
37
|
+
"@open-cr-agent/vcs-platform": "0.23.0",
|
|
38
38
|
"zod": "^4.6.6"
|
|
39
39
|
},
|
|
40
40
|
"publishConfig": {
|