@zereight/mcp-gitlab 2.1.49 → 2.1.51
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.ko.md +5 -2
- package/README.md +7 -3
- package/README.zh-CN.md +6 -2
- package/build/auth-cli.js +95 -0
- package/build/cli-command.js +33 -0
- package/build/index.js +40 -20
- package/build/oauth-device-flow.js +158 -0
- package/build/oauth.js +20 -0
- package/build/schemas.js +58 -10
- package/build/scripts/generate-tool-docs.js +1 -1
- package/build/test/create-commit-actions.test.js +102 -0
- package/build/test/gitlab-artifact-entry-schema.test.js +44 -0
- package/build/test/merge-merge-request-schema.test.js +75 -0
- package/build/test/oauth-device-flow-tests.js +440 -0
- package/build/test/schema-tests.js +117 -3
- package/build/test/test-job-artifacts.js +30 -0
- package/build/test/test-permission-mode.js +28 -0
- package/build/tools/tool-descriptions.js +3 -3
- package/build/utils/gitlab-commit-actions.js +42 -0
- package/package.json +3 -3
|
@@ -281,7 +281,7 @@ function buildIndexPage(groupedToolsList) {
|
|
|
281
281
|
"Permission modes control which tools are exposed:",
|
|
282
282
|
"",
|
|
283
283
|
"- `GITLAB_PERMISSION_MODE=readonly` — hides every write tool regardless of toggles.",
|
|
284
|
-
"- `GITLAB_PERMISSION_MODE=modify` — allows create/update but blocks all `delete_*` tools.",
|
|
284
|
+
"- `GITLAB_PERMISSION_MODE=modify` — allows create/update but blocks all `delete_*` tools, plus `push_files` `delete`/`move` actions.",
|
|
285
285
|
"- `GITLAB_READ_ONLY_MODE=true` (deprecated) — same as `readonly`; prefer `GITLAB_PERMISSION_MODE=readonly`.",
|
|
286
286
|
"",
|
|
287
287
|
"See [Environment Variables](../configuration/environment-variables.md)",
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { FileOperationSchema } from "../schemas.js";
|
|
4
|
+
import { fileOperationsIncludeDeleteOrMove, toGitLabCommitActions, } from "../utils/gitlab-commit-actions.js";
|
|
5
|
+
function firstAction(actions) {
|
|
6
|
+
const entry = actions[0];
|
|
7
|
+
if (!entry) {
|
|
8
|
+
throw new Error("expected at least one commit action");
|
|
9
|
+
}
|
|
10
|
+
return entry;
|
|
11
|
+
}
|
|
12
|
+
describe("When mapping file operations to GitLab commit actions", () => {
|
|
13
|
+
describe("with a plain text file and no per-file encoding", () => {
|
|
14
|
+
it("should default the action to create", () => {
|
|
15
|
+
const entry = firstAction(toGitLabCommitActions([{ path: "a.txt", content: "hi" }], "text"));
|
|
16
|
+
assert.equal(entry.action, "create");
|
|
17
|
+
});
|
|
18
|
+
it("should keep the content unchanged when the global encoding is text", () => {
|
|
19
|
+
const entry = firstAction(toGitLabCommitActions([{ path: "a.txt", content: "hi" }], "text"));
|
|
20
|
+
assert.equal(entry.content, "hi");
|
|
21
|
+
});
|
|
22
|
+
it("should set encoding to the global text value", () => {
|
|
23
|
+
const entry = firstAction(toGitLabCommitActions([{ path: "a.txt", content: "hi" }], "text"));
|
|
24
|
+
assert.equal(entry.encoding, "text");
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
describe("with a plain text file and global base64 encoding", () => {
|
|
28
|
+
it("should base64-encode the content", () => {
|
|
29
|
+
const entry = firstAction(toGitLabCommitActions([{ path: "a.txt", content: "hi" }], "base64"));
|
|
30
|
+
assert.equal(entry.content, Buffer.from("hi").toString("base64"));
|
|
31
|
+
});
|
|
32
|
+
it("should set encoding to base64", () => {
|
|
33
|
+
const entry = firstAction(toGitLabCommitActions([{ path: "a.txt", content: "hi" }], "base64"));
|
|
34
|
+
assert.equal(entry.encoding, "base64");
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
describe("with an explicit per-file encoding", () => {
|
|
38
|
+
it("should pass text content through when encoding is text even if global is base64", () => {
|
|
39
|
+
const entry = firstAction(toGitLabCommitActions([{ path: "a.txt", content: "hi", encoding: "text" }], "base64"));
|
|
40
|
+
assert.equal(entry.content, "hi");
|
|
41
|
+
});
|
|
42
|
+
it("should keep the explicit text encoding on the payload", () => {
|
|
43
|
+
const entry = firstAction(toGitLabCommitActions([{ path: "a.txt", content: "hi", encoding: "text" }], "base64"));
|
|
44
|
+
assert.equal(entry.encoding, "text");
|
|
45
|
+
});
|
|
46
|
+
it("should pass base64 content through without encoding it again", () => {
|
|
47
|
+
const entry = firstAction(toGitLabCommitActions([{ path: "logo.png", content: "aGk=", encoding: "base64" }], "text"));
|
|
48
|
+
assert.equal(entry.content, "aGk=");
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
describe("with action delete", () => {
|
|
52
|
+
it("should omit content from the payload", () => {
|
|
53
|
+
const entry = firstAction(toGitLabCommitActions([{ path: "a.txt", action: "delete" }], "text"));
|
|
54
|
+
assert.equal("content" in entry, false);
|
|
55
|
+
});
|
|
56
|
+
it("should omit encoding from the payload", () => {
|
|
57
|
+
const entry = firstAction(toGitLabCommitActions([{ path: "a.txt", action: "delete" }], "text"));
|
|
58
|
+
assert.equal("encoding" in entry, false);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
describe("with action move", () => {
|
|
62
|
+
it("should omit content when the move has no new content", () => {
|
|
63
|
+
const entry = firstAction(toGitLabCommitActions([{ path: "b.txt", action: "move", previous_path: "a.txt" }], "text"));
|
|
64
|
+
assert.equal("content" in entry, false);
|
|
65
|
+
});
|
|
66
|
+
it("should include previous_path", () => {
|
|
67
|
+
const entry = firstAction(toGitLabCommitActions([{ path: "b.txt", action: "move", previous_path: "a.txt" }], "text"));
|
|
68
|
+
assert.equal(entry.previous_path, "a.txt");
|
|
69
|
+
});
|
|
70
|
+
it("should include content when a move supplies replacement content", () => {
|
|
71
|
+
const entry = firstAction(toGitLabCommitActions([{ path: "b.txt", action: "move", previous_path: "a.txt", content: "new" }], "text"));
|
|
72
|
+
assert.equal(entry.content, "new");
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
describe("with create or update missing content", () => {
|
|
76
|
+
it("should reject a create without content before sending an empty file", () => {
|
|
77
|
+
assert.throws(() => toGitLabCommitActions([{ path: "a.txt" }], "text"), /content is required when action is 'create'/);
|
|
78
|
+
});
|
|
79
|
+
it("should reject the FileOperation schema for create without content", () => {
|
|
80
|
+
const parsed = FileOperationSchema.safeParse({ path: "a.txt" });
|
|
81
|
+
assert.equal(parsed.success, false);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
describe("When checking push_files actions against modify mode", () => {
|
|
86
|
+
describe("with delete or move entries", () => {
|
|
87
|
+
it("should treat delete as blocked", () => {
|
|
88
|
+
assert.equal(fileOperationsIncludeDeleteOrMove([{ action: "delete" }]), true);
|
|
89
|
+
});
|
|
90
|
+
it("should treat move as blocked", () => {
|
|
91
|
+
assert.equal(fileOperationsIncludeDeleteOrMove([{ action: "move" }]), true);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
describe("with create or update entries", () => {
|
|
95
|
+
it("should allow omitted action", () => {
|
|
96
|
+
assert.equal(fileOperationsIncludeDeleteOrMove([{}]), false);
|
|
97
|
+
});
|
|
98
|
+
it("should allow update", () => {
|
|
99
|
+
assert.equal(fileOperationsIncludeDeleteOrMove([{ action: "update" }]), false);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, it } from "node:test";
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
import { GitLabArtifactEntrySchema } from "../schemas.js";
|
|
4
|
+
function validEntry(overrides = {}) {
|
|
5
|
+
return {
|
|
6
|
+
name: "report.xml",
|
|
7
|
+
path: "report.xml",
|
|
8
|
+
type: "file",
|
|
9
|
+
...overrides,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
describe("When parsing an artifact tree entry", () => {
|
|
13
|
+
describe("with a string mode", () => {
|
|
14
|
+
it("should keep the mode", () => {
|
|
15
|
+
const parsed = GitLabArtifactEntrySchema.parse(validEntry({ mode: "100644", size: 1024 }));
|
|
16
|
+
assert.strictEqual(parsed.mode, "100644");
|
|
17
|
+
assert.strictEqual(parsed.size, 1024);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
describe("with mode set to null", () => {
|
|
21
|
+
it("should accept null", () => {
|
|
22
|
+
const parsed = GitLabArtifactEntrySchema.parse(validEntry({ mode: null }));
|
|
23
|
+
assert.strictEqual(parsed.mode, null);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
describe("with size set to null", () => {
|
|
27
|
+
it("should accept null", () => {
|
|
28
|
+
const parsed = GitLabArtifactEntrySchema.parse(validEntry({ size: null }));
|
|
29
|
+
assert.strictEqual(parsed.size, null);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
describe("with mode and size omitted", () => {
|
|
33
|
+
it("should leave them undefined", () => {
|
|
34
|
+
const parsed = GitLabArtifactEntrySchema.parse(validEntry());
|
|
35
|
+
assert.strictEqual(parsed.mode, undefined);
|
|
36
|
+
assert.strictEqual(parsed.size, undefined);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
describe("with an unknown type", () => {
|
|
40
|
+
it("should reject the entry", () => {
|
|
41
|
+
assert.throws(() => GitLabArtifactEntrySchema.parse(validEntry({ type: "symlink" })));
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { describe, it } from "node:test";
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
import { GitLabMergeRequestSchema, MergeMergeRequestSchema } from "../schemas.js";
|
|
4
|
+
const HEAD_SHA = "e82eb4a098e32c796079ca3915e07487fc4db24c";
|
|
5
|
+
const PROJECT_ID = "group/project";
|
|
6
|
+
const MERGE_REQUEST_IID = "42";
|
|
7
|
+
function mergeArgs(overrides = {}) {
|
|
8
|
+
return {
|
|
9
|
+
project_id: PROJECT_ID,
|
|
10
|
+
merge_request_iid: MERGE_REQUEST_IID,
|
|
11
|
+
...overrides,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function mergeRequest(overrides = {}) {
|
|
15
|
+
return {
|
|
16
|
+
id: "1001",
|
|
17
|
+
iid: MERGE_REQUEST_IID,
|
|
18
|
+
project_id: "123",
|
|
19
|
+
title: "Add milestone exposure",
|
|
20
|
+
description: "Expose MR milestone",
|
|
21
|
+
state: "opened",
|
|
22
|
+
author: {
|
|
23
|
+
id: "1",
|
|
24
|
+
username: "octocat",
|
|
25
|
+
name: "Octo Cat",
|
|
26
|
+
avatar_url: null,
|
|
27
|
+
web_url: "https://gitlab.example.com/octocat",
|
|
28
|
+
},
|
|
29
|
+
assignees: [],
|
|
30
|
+
reviewers: [],
|
|
31
|
+
source_branch: "feature/milestone",
|
|
32
|
+
target_branch: "main",
|
|
33
|
+
web_url: "https://gitlab.example.com/group/project/-/merge_requests/42",
|
|
34
|
+
created_at: "2026-05-07T00:00:00.000Z",
|
|
35
|
+
updated_at: "2026-05-07T00:00:00.000Z",
|
|
36
|
+
merged_at: null,
|
|
37
|
+
closed_at: null,
|
|
38
|
+
merge_commit_sha: null,
|
|
39
|
+
...overrides,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
describe("When parsing merge_merge_request arguments", () => {
|
|
43
|
+
describe("with sha provided", () => {
|
|
44
|
+
it("should keep sha on the parsed object", () => {
|
|
45
|
+
const parsed = MergeMergeRequestSchema.parse(mergeArgs({ sha: HEAD_SHA }));
|
|
46
|
+
assert.strictEqual(parsed.sha, HEAD_SHA);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
describe("with sha omitted", () => {
|
|
50
|
+
it("should parse and leave sha undefined", () => {
|
|
51
|
+
const parsed = MergeMergeRequestSchema.parse(mergeArgs());
|
|
52
|
+
assert.strictEqual(parsed.sha, undefined);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
describe("with an unknown extra field", () => {
|
|
56
|
+
it("should strip the unknown field", () => {
|
|
57
|
+
const parsed = MergeMergeRequestSchema.parse(mergeArgs({ not_a_schema_field: "drop-me" }));
|
|
58
|
+
assert.equal("not_a_schema_field" in parsed, false);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
describe("When parsing a GitLab merge request response", () => {
|
|
63
|
+
describe("with sha present", () => {
|
|
64
|
+
it("should keep sha instead of stripping it", () => {
|
|
65
|
+
const parsed = GitLabMergeRequestSchema.parse(mergeRequest({ sha: HEAD_SHA }));
|
|
66
|
+
assert.strictEqual(parsed.sha, HEAD_SHA);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
describe("with sha omitted", () => {
|
|
70
|
+
it("should accept the payload", () => {
|
|
71
|
+
const parsed = GitLabMergeRequestSchema.parse(mergeRequest());
|
|
72
|
+
assert.strictEqual(parsed.sha, undefined);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import * as fs from "node:fs";
|
|
4
|
+
import * as os from "node:os";
|
|
5
|
+
import * as path from "node:path";
|
|
6
|
+
import { getPositionalCliCommand } from "../cli-command.js";
|
|
7
|
+
import { gitlabOriginFromApiUrl, runAuthCommandAsync } from "../auth-cli.js";
|
|
8
|
+
import { runDeviceAuthorizationGrantAsync } from "../oauth-device-flow.js";
|
|
9
|
+
import { GitLabOAuth } from "../oauth.js";
|
|
10
|
+
const GITLAB_URL = "https://gitlab.example";
|
|
11
|
+
const CLIENT_ID = "test-client-id";
|
|
12
|
+
const DEVICE_CODE = "device-code-not-for-stdout";
|
|
13
|
+
const USER_CODE = "0A44L90H";
|
|
14
|
+
const ACCESS_TOKEN = "oauth-device-flow-access-token";
|
|
15
|
+
const REFRESH_TOKEN = "refresh-token-device-flow";
|
|
16
|
+
function requestUrl(input) {
|
|
17
|
+
if (typeof input === "string") {
|
|
18
|
+
return input;
|
|
19
|
+
}
|
|
20
|
+
if (input instanceof URL) {
|
|
21
|
+
return input.href;
|
|
22
|
+
}
|
|
23
|
+
return input.url;
|
|
24
|
+
}
|
|
25
|
+
function jsonResponse(status, body) {
|
|
26
|
+
return new Response(JSON.stringify(body), {
|
|
27
|
+
status,
|
|
28
|
+
headers: { "Content-Type": "application/json" },
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function createFetchStub(plan, capture) {
|
|
32
|
+
const remainingTokens = [...plan.tokens];
|
|
33
|
+
return async (input, init) => {
|
|
34
|
+
const url = requestUrl(input);
|
|
35
|
+
if (url.includes("/oauth/authorize_device")) {
|
|
36
|
+
if (typeof init?.body === "string") {
|
|
37
|
+
capture?.authorizeBodies.push(init.body);
|
|
38
|
+
}
|
|
39
|
+
return jsonResponse(plan.authorize.status, plan.authorize.body);
|
|
40
|
+
}
|
|
41
|
+
if (url.includes("/oauth/token")) {
|
|
42
|
+
const next = remainingTokens.shift();
|
|
43
|
+
if (!next) {
|
|
44
|
+
throw new Error(`Unexpected token poll: ${url}`);
|
|
45
|
+
}
|
|
46
|
+
return jsonResponse(next.status, next.body);
|
|
47
|
+
}
|
|
48
|
+
throw new Error(`Unexpected fetch URL: ${url}`);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function deviceAuthorizationBody(interval = 5) {
|
|
52
|
+
return {
|
|
53
|
+
device_code: DEVICE_CODE,
|
|
54
|
+
user_code: USER_CODE,
|
|
55
|
+
verification_uri: `${GITLAB_URL}/oauth/device`,
|
|
56
|
+
verification_uri_complete: `${GITLAB_URL}/oauth/device?user_code=${USER_CODE}`,
|
|
57
|
+
expires_in: 300,
|
|
58
|
+
interval,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function successTokenBody() {
|
|
62
|
+
return {
|
|
63
|
+
access_token: ACCESS_TOKEN,
|
|
64
|
+
refresh_token: REFRESH_TOKEN,
|
|
65
|
+
expires_in: 7200,
|
|
66
|
+
token_type: "Bearer",
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function pendingBody() {
|
|
70
|
+
return { error: "authorization_pending" };
|
|
71
|
+
}
|
|
72
|
+
function createTempTokenPath() {
|
|
73
|
+
return path.join(os.tmpdir(), `gitlab-mcp-device-flow-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}.json`);
|
|
74
|
+
}
|
|
75
|
+
function createOauthClient(tokenStoragePath) {
|
|
76
|
+
return new GitLabOAuth({
|
|
77
|
+
clientId: CLIENT_ID,
|
|
78
|
+
redirectUri: "http://127.0.0.1:8888/callback",
|
|
79
|
+
gitlabUrl: GITLAB_URL,
|
|
80
|
+
scopes: ["api"],
|
|
81
|
+
tokenStoragePath,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
function createAuthorizeCapture() {
|
|
85
|
+
return { authorizeBodies: [] };
|
|
86
|
+
}
|
|
87
|
+
function capturedAuthorizeScope(bodies) {
|
|
88
|
+
if (bodies.length !== 1) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
const [body] = bodies;
|
|
92
|
+
if (body === undefined) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
return new URLSearchParams(body).get("scope");
|
|
96
|
+
}
|
|
97
|
+
async function runAuthCapturingScopeAsync(input) {
|
|
98
|
+
const tokenPath = createTempTokenPath();
|
|
99
|
+
const capture = createAuthorizeCapture();
|
|
100
|
+
const fetchImpl = createFetchStub({
|
|
101
|
+
authorize: { status: 200, body: deviceAuthorizationBody() },
|
|
102
|
+
tokens: [{ status: 200, body: successTokenBody() }],
|
|
103
|
+
}, capture);
|
|
104
|
+
try {
|
|
105
|
+
await runAuthCommandAsync({
|
|
106
|
+
argv: [
|
|
107
|
+
"node",
|
|
108
|
+
"index.js",
|
|
109
|
+
"auth",
|
|
110
|
+
`--client-id=${CLIENT_ID}`,
|
|
111
|
+
`--api-url=${GITLAB_URL}`,
|
|
112
|
+
`--token-path=${tokenPath}`,
|
|
113
|
+
...(input.extraArgv ?? []),
|
|
114
|
+
],
|
|
115
|
+
env: input.env ?? {},
|
|
116
|
+
stdout: createStdoutBuffer().stdout,
|
|
117
|
+
fetchImpl,
|
|
118
|
+
sleepAsync: async () => { },
|
|
119
|
+
});
|
|
120
|
+
return capturedAuthorizeScope(capture.authorizeBodies);
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
if (fs.existsSync(tokenPath)) {
|
|
124
|
+
fs.unlinkSync(tokenPath);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function createStdoutBuffer() {
|
|
129
|
+
const chunks = [];
|
|
130
|
+
return {
|
|
131
|
+
stdout: {
|
|
132
|
+
write(chunk) {
|
|
133
|
+
chunks.push(chunk);
|
|
134
|
+
return true;
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
text: () => chunks.join(""),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function createSleepRecorder() {
|
|
141
|
+
const delays = [];
|
|
142
|
+
return {
|
|
143
|
+
delays,
|
|
144
|
+
async sleepAsync(ms) {
|
|
145
|
+
delays.push(ms);
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
function readStoredToken(value) {
|
|
150
|
+
if (typeof value !== "object" || value === null) {
|
|
151
|
+
throw new Error("token file is not an object");
|
|
152
|
+
}
|
|
153
|
+
const accessToken = Reflect.get(value, "access_token");
|
|
154
|
+
const refreshToken = Reflect.get(value, "refresh_token");
|
|
155
|
+
const expiresIn = Reflect.get(value, "expires_in");
|
|
156
|
+
const createdAt = Reflect.get(value, "created_at");
|
|
157
|
+
const tokenType = Reflect.get(value, "token_type");
|
|
158
|
+
if (typeof accessToken !== "string") {
|
|
159
|
+
throw new Error("access_token missing");
|
|
160
|
+
}
|
|
161
|
+
if (typeof createdAt !== "number") {
|
|
162
|
+
throw new Error("created_at missing");
|
|
163
|
+
}
|
|
164
|
+
if (typeof tokenType !== "string") {
|
|
165
|
+
throw new Error("token_type missing");
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
access_token: accessToken,
|
|
169
|
+
refresh_token: typeof refreshToken === "string" ? refreshToken : undefined,
|
|
170
|
+
expires_in: typeof expiresIn === "number" ? expiresIn : undefined,
|
|
171
|
+
created_at: createdAt,
|
|
172
|
+
token_type: tokenType,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
describe("When reading the CLI command", () => {
|
|
176
|
+
describe("with auth as the first positional argument", () => {
|
|
177
|
+
it("should return auth", () => {
|
|
178
|
+
assert.equal(getPositionalCliCommand(["node", "index.js", "auth"]), "auth");
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
describe("with only --token", () => {
|
|
182
|
+
it("should return undefined so MCP startup stays unchanged", () => {
|
|
183
|
+
assert.equal(getPositionalCliCommand(["node", "index.js", "--token=glpat-xxxxxxxx"]), undefined);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
describe("with flags before auth", () => {
|
|
187
|
+
it("should still return auth", () => {
|
|
188
|
+
assert.equal(getPositionalCliCommand(["node", "index.js", "--api-url=https://gitlab.com", "auth"]), "auth");
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
describe("with tsx index.ts auth", () => {
|
|
192
|
+
it("should skip the script path and return auth", () => {
|
|
193
|
+
assert.equal(getPositionalCliCommand(["node", "/usr/local/bin/tsx", "index.ts", "auth", "--help"]), "auth");
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
describe("with space-separated --api-url before auth", () => {
|
|
197
|
+
it("should skip the URL value and return auth", () => {
|
|
198
|
+
assert.equal(getPositionalCliCommand([
|
|
199
|
+
"node",
|
|
200
|
+
"index.js",
|
|
201
|
+
"--api-url",
|
|
202
|
+
"https://gitlab.example/api/v4",
|
|
203
|
+
"auth",
|
|
204
|
+
]), "auth");
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
describe("with space-separated --client-id before auth", () => {
|
|
208
|
+
it("should skip the client id value and return auth", () => {
|
|
209
|
+
assert.equal(getPositionalCliCommand(["node", "index.js", "--client-id", "app-id", "auth"]), "auth");
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
describe("with -h before auth", () => {
|
|
213
|
+
it("should not consume auth as a help value", () => {
|
|
214
|
+
assert.equal(getPositionalCliCommand(["node", "index.js", "-h", "auth"]), "auth");
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
describe("When converting a GitLab API URL", () => {
|
|
219
|
+
describe("with an /api/v4 suffix", () => {
|
|
220
|
+
it("should strip it the same way initializeOAuthClient does", () => {
|
|
221
|
+
assert.equal(gitlabOriginFromApiUrl("https://gitlab.com/api/v4"), "https://gitlab.com");
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
describe("with a trailing slash after /api/v4", () => {
|
|
225
|
+
it("should strip the suffix and trailing slash", () => {
|
|
226
|
+
assert.equal(gitlabOriginFromApiUrl("https://gitlab.example/api/v4/"), "https://gitlab.example");
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
describe("When running device authorization grant", () => {
|
|
231
|
+
describe("with authorization_pending then a token", () => {
|
|
232
|
+
it("should return TokenData compatible with the existing OAuth store", async () => {
|
|
233
|
+
const fetchImpl = createFetchStub({
|
|
234
|
+
authorize: { status: 200, body: deviceAuthorizationBody() },
|
|
235
|
+
tokens: [
|
|
236
|
+
{ status: 400, body: pendingBody() },
|
|
237
|
+
{ status: 200, body: successTokenBody() },
|
|
238
|
+
],
|
|
239
|
+
});
|
|
240
|
+
const sleep = createSleepRecorder();
|
|
241
|
+
const token = await runDeviceAuthorizationGrantAsync({
|
|
242
|
+
gitlabUrl: GITLAB_URL,
|
|
243
|
+
clientId: CLIENT_ID,
|
|
244
|
+
scopes: ["api"],
|
|
245
|
+
fetchImpl,
|
|
246
|
+
sleepAsync: sleep.sleepAsync,
|
|
247
|
+
});
|
|
248
|
+
assert.equal(token.access_token, ACCESS_TOKEN);
|
|
249
|
+
assert.equal(token.refresh_token, REFRESH_TOKEN);
|
|
250
|
+
assert.equal(token.expires_in, 7200);
|
|
251
|
+
assert.equal(token.token_type, "Bearer");
|
|
252
|
+
assert.equal(typeof token.created_at, "number");
|
|
253
|
+
assert.deepEqual(sleep.delays, [5000]);
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
describe("with slow_down then a token", () => {
|
|
257
|
+
it("should wait the increased interval before the next poll", async () => {
|
|
258
|
+
const fetchImpl = createFetchStub({
|
|
259
|
+
authorize: { status: 200, body: deviceAuthorizationBody(5) },
|
|
260
|
+
tokens: [
|
|
261
|
+
{ status: 400, body: { error: "slow_down" } },
|
|
262
|
+
{ status: 200, body: successTokenBody() },
|
|
263
|
+
],
|
|
264
|
+
});
|
|
265
|
+
const sleep = createSleepRecorder();
|
|
266
|
+
await runDeviceAuthorizationGrantAsync({
|
|
267
|
+
gitlabUrl: GITLAB_URL,
|
|
268
|
+
clientId: CLIENT_ID,
|
|
269
|
+
scopes: ["api"],
|
|
270
|
+
fetchImpl,
|
|
271
|
+
sleepAsync: sleep.sleepAsync,
|
|
272
|
+
});
|
|
273
|
+
assert.deepEqual(sleep.delays, [10000]);
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
describe("When the token endpoint returns expired_token", () => {
|
|
278
|
+
describe("with no token file yet", () => {
|
|
279
|
+
it("should throw and not write a token file", async () => {
|
|
280
|
+
const tokenPath = createTempTokenPath();
|
|
281
|
+
const fetchImpl = createFetchStub({
|
|
282
|
+
authorize: { status: 200, body: deviceAuthorizationBody() },
|
|
283
|
+
tokens: [{ status: 400, body: { error: "expired_token" } }],
|
|
284
|
+
});
|
|
285
|
+
const oauth = createOauthClient(tokenPath);
|
|
286
|
+
await assert.rejects(() => oauth.runDeviceFlowAsync({ fetchImpl, sleepAsync: async () => { } }), /Device code expired/);
|
|
287
|
+
assert.equal(fs.existsSync(tokenPath), false);
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
describe("When the token endpoint returns access_denied", () => {
|
|
292
|
+
describe("with no token file yet", () => {
|
|
293
|
+
it("should throw and not write a token file", async () => {
|
|
294
|
+
const tokenPath = createTempTokenPath();
|
|
295
|
+
const fetchImpl = createFetchStub({
|
|
296
|
+
authorize: { status: 200, body: deviceAuthorizationBody() },
|
|
297
|
+
tokens: [{ status: 400, body: { error: "access_denied" } }],
|
|
298
|
+
});
|
|
299
|
+
const oauth = createOauthClient(tokenPath);
|
|
300
|
+
await assert.rejects(() => oauth.runDeviceFlowAsync({ fetchImpl, sleepAsync: async () => { } }), /Authorization was denied/);
|
|
301
|
+
assert.equal(fs.existsSync(tokenPath), false);
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
describe("When GitLab returns 404 for authorize_device", () => {
|
|
306
|
+
describe("with a self-managed instance", () => {
|
|
307
|
+
it("should tell the user to use a PAT", async () => {
|
|
308
|
+
const fetchImpl = createFetchStub({
|
|
309
|
+
authorize: { status: 404, body: { error: "not_found" } },
|
|
310
|
+
tokens: [],
|
|
311
|
+
});
|
|
312
|
+
await assert.rejects(() => runDeviceAuthorizationGrantAsync({
|
|
313
|
+
gitlabUrl: GITLAB_URL,
|
|
314
|
+
clientId: CLIENT_ID,
|
|
315
|
+
scopes: ["api"],
|
|
316
|
+
fetchImpl,
|
|
317
|
+
sleepAsync: async () => { },
|
|
318
|
+
}), /GITLAB_PERSONAL_ACCESS_TOKEN/);
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
describe("When device flow succeeds", () => {
|
|
323
|
+
describe("with GitLabOAuth.saveToken", () => {
|
|
324
|
+
it("should persist the existing token file shape", async () => {
|
|
325
|
+
const tokenPath = createTempTokenPath();
|
|
326
|
+
const fetchImpl = createFetchStub({
|
|
327
|
+
authorize: { status: 200, body: deviceAuthorizationBody() },
|
|
328
|
+
tokens: [{ status: 200, body: successTokenBody() }],
|
|
329
|
+
});
|
|
330
|
+
try {
|
|
331
|
+
await createOauthClient(tokenPath).runDeviceFlowAsync({
|
|
332
|
+
fetchImpl,
|
|
333
|
+
sleepAsync: async () => { },
|
|
334
|
+
});
|
|
335
|
+
const raw = JSON.parse(fs.readFileSync(tokenPath, "utf8"));
|
|
336
|
+
const stored = readStoredToken(raw);
|
|
337
|
+
assert.equal(stored.access_token, ACCESS_TOKEN);
|
|
338
|
+
assert.equal(stored.refresh_token, REFRESH_TOKEN);
|
|
339
|
+
assert.equal(stored.expires_in, 7200);
|
|
340
|
+
assert.equal(stored.token_type, "Bearer");
|
|
341
|
+
assert.equal(typeof stored.created_at, "number");
|
|
342
|
+
assert.equal(fs.statSync(tokenPath).mode & 0o777, 0o600);
|
|
343
|
+
}
|
|
344
|
+
finally {
|
|
345
|
+
if (fs.existsSync(tokenPath)) {
|
|
346
|
+
fs.unlinkSync(tokenPath);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
describe("When running the auth command", () => {
|
|
353
|
+
describe("with --help", () => {
|
|
354
|
+
it("should print usage and not fetch", async () => {
|
|
355
|
+
const out = createStdoutBuffer();
|
|
356
|
+
const fetchImpl = async () => {
|
|
357
|
+
throw new Error("fetch should not run for --help");
|
|
358
|
+
};
|
|
359
|
+
await runAuthCommandAsync({
|
|
360
|
+
argv: ["node", "index.js", "auth", "--help"],
|
|
361
|
+
env: {},
|
|
362
|
+
stdout: out.stdout,
|
|
363
|
+
fetchImpl,
|
|
364
|
+
});
|
|
365
|
+
assert.match(out.text(), /zereight-mcp-gitlab auth/);
|
|
366
|
+
assert.equal(out.text().includes(DEVICE_CODE), false);
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
describe("without a client id", () => {
|
|
370
|
+
it("should fail before calling GitLab", async () => {
|
|
371
|
+
await assert.rejects(() => runAuthCommandAsync({
|
|
372
|
+
argv: ["node", "index.js", "auth"],
|
|
373
|
+
env: {},
|
|
374
|
+
stdout: createStdoutBuffer().stdout,
|
|
375
|
+
fetchImpl: async () => {
|
|
376
|
+
throw new Error("fetch should not run without client id");
|
|
377
|
+
},
|
|
378
|
+
}), /GITLAB_OAUTH_CLIENT_ID/);
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
describe("with a successful device flow", () => {
|
|
382
|
+
it("should print the user code and not the device code or access token", async () => {
|
|
383
|
+
const tokenPath = createTempTokenPath();
|
|
384
|
+
const out = createStdoutBuffer();
|
|
385
|
+
const fetchImpl = createFetchStub({
|
|
386
|
+
authorize: { status: 200, body: deviceAuthorizationBody() },
|
|
387
|
+
tokens: [{ status: 200, body: successTokenBody() }],
|
|
388
|
+
});
|
|
389
|
+
try {
|
|
390
|
+
await runAuthCommandAsync({
|
|
391
|
+
argv: [
|
|
392
|
+
"node",
|
|
393
|
+
"index.js",
|
|
394
|
+
"auth",
|
|
395
|
+
`--client-id=${CLIENT_ID}`,
|
|
396
|
+
`--api-url=${GITLAB_URL}`,
|
|
397
|
+
`--token-path=${tokenPath}`,
|
|
398
|
+
],
|
|
399
|
+
env: {},
|
|
400
|
+
stdout: out.stdout,
|
|
401
|
+
fetchImpl,
|
|
402
|
+
sleepAsync: async () => { },
|
|
403
|
+
});
|
|
404
|
+
const printed = out.text();
|
|
405
|
+
assert.equal(printed.includes(USER_CODE), true);
|
|
406
|
+
assert.match(printed, /Token saved to/);
|
|
407
|
+
assert.match(printed, /GITLAB_USE_OAUTH=true/);
|
|
408
|
+
assert.match(printed, /GITLAB_OAUTH_TOKEN_PATH/);
|
|
409
|
+
assert.equal(printed.includes(DEVICE_CODE), false);
|
|
410
|
+
assert.equal(printed.includes(ACCESS_TOKEN), false);
|
|
411
|
+
assert.equal(fs.existsSync(tokenPath), true);
|
|
412
|
+
}
|
|
413
|
+
finally {
|
|
414
|
+
if (fs.existsSync(tokenPath)) {
|
|
415
|
+
fs.unlinkSync(tokenPath);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
});
|
|
419
|
+
});
|
|
420
|
+
describe("without a read-only flag", () => {
|
|
421
|
+
it("should request the api scope", async () => {
|
|
422
|
+
assert.equal(await runAuthCapturingScopeAsync({}), "api");
|
|
423
|
+
});
|
|
424
|
+
});
|
|
425
|
+
describe("with --permission-mode=readonly", () => {
|
|
426
|
+
it("should request the read_api scope", async () => {
|
|
427
|
+
assert.equal(await runAuthCapturingScopeAsync({ extraArgv: ["--permission-mode=readonly"] }), "read_api");
|
|
428
|
+
});
|
|
429
|
+
});
|
|
430
|
+
describe("with GITLAB_PERMISSION_MODE=readonly", () => {
|
|
431
|
+
it("should request the read_api scope", async () => {
|
|
432
|
+
assert.equal(await runAuthCapturingScopeAsync({ env: { GITLAB_PERMISSION_MODE: "readonly" } }), "read_api");
|
|
433
|
+
});
|
|
434
|
+
});
|
|
435
|
+
describe("with --read-only=true", () => {
|
|
436
|
+
it("should request the read_api scope", async () => {
|
|
437
|
+
assert.equal(await runAuthCapturingScopeAsync({ extraArgv: ["--read-only=true"] }), "read_api");
|
|
438
|
+
});
|
|
439
|
+
});
|
|
440
|
+
});
|