1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
import pytest
from mcp_server_gitlab_python.server import create_server, GitLabPythonServer
from unittest.mock import MagicMock, patch
def test_create_server():
server = create_server()
assert server is not None
def make_mock_diff(old_path, new_path, diff):
m = MagicMock()
m.old_path = old_path
m.new_path = new_path
m.diff = diff
return m
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_get_mr_diff_small_diff(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
proj = MagicMock()
mr = MagicMock()
mock_gitlab.return_value.projects.get.return_value = proj
proj.mergerequests.get.return_value = mr
mr.diffs.list.return_value = [
make_mock_diff("file1.txt", "file1.txt", "diff content 1"),
make_mock_diff("file2.py", "file2.py", "diff content 2"),
]
result = server.get_mr_diff("project/path", 1, max_size_kb=100)
assert "diff" in result
assert "file1.txt" in result["diff"]
assert result["temp_file_path"] is None
assert result["size_kb"] < 100
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
@patch("tempfile.NamedTemporaryFile")
def test_get_mr_diff_large_diff_temp_file(mock_tempfile, mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
proj = MagicMock()
mr = MagicMock()
mock_gitlab.return_value.projects.get.return_value = proj
proj.mergerequests.get.return_value = mr
# Create a large diff
large_diff = "x" * (101 * 1024)
mock_diff = make_mock_diff("bigfile.txt", "bigfile.txt", large_diff)
mr.diffs.list.return_value = [mock_diff]
mock_file = MagicMock()
mock_file.name = "/tmp/mr_diff_12345.diff"
mock_tempfile.return_value.__enter__.return_value = mock_file
result = server.get_mr_diff("project/path", 1, max_size_kb=100)
assert result["diff_too_large"] is True
assert result["temp_file_path"] == "/tmp/mr_diff_12345.diff"
assert result["size_kb"] > 100
assert "message" in result
mock_file.write.assert_called_once()
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_get_mr_diff_filter_extensions(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
proj = MagicMock()
mr = MagicMock()
mock_gitlab.return_value.projects.get.return_value = proj
proj.mergerequests.get.return_value = mr
# One file should be filtered out
mr.diffs.list.return_value = [
make_mock_diff("file.lock", "file.lock", "should be filtered"),
make_mock_diff("file.txt", "file.txt", "should be present"),
]
result = server.get_mr_diff("project/path", 1, max_size_kb=100, filter_extensions=[".lock"])
assert "file.lock" not in result["diff"]
assert "file.txt" in result["diff"]
assert "should be present" in result["diff"]
assert "should be filtered" not in result["diff"]
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_get_mr_diff_error_handling(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
mock_gitlab.return_value.projects.get.side_effect = Exception("Not found")
result = server.get_mr_diff("project/path", 1)
assert "error" in result
assert "Not found" in result["error"]
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_run_ci_pipeline_success_with_branch(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
proj = MagicMock()
pipeline = MagicMock()
pipeline.id = 123
pipeline.web_url = "https://gitlab.com/project/-/pipelines/123"
proj.pipelines.create.return_value = pipeline
mock_gitlab.return_value.projects.get.return_value = proj
result = server.run_ci_pipeline(
project="project/path",
branch="main",
variables={"FOO": "bar"},
web_mode=False,
working_directory="/tmp"
)
assert result["success"] is True
assert result["pipeline_id"] == 123
assert result["pipeline_url"] == "https://gitlab.com/project/-/pipelines/123"
assert result["branch"] == "main"
assert result["web_mode"] is False
proj.pipelines.create.assert_called_once()
args = proj.pipelines.create.call_args[0][0]
assert args["ref"] == "main"
assert {"key": "FOO", "value": "bar"} in args["variables"]
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
@patch("git.Repo")
def test_run_ci_pipeline_success_current_branch(mock_repo, mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
proj = MagicMock()
pipeline = MagicMock()
pipeline.id = 456
pipeline.web_url = "https://gitlab.com/project/-/pipelines/456"
proj.pipelines.create.return_value = pipeline
proj.default_branch = "main"
mock_gitlab.return_value.projects.get.return_value = proj
# Simulate git branch detection using GitPython
mock_branch = MagicMock()
mock_branch.name = "feature-branch"
mock_repo.return_value.active_branch = mock_branch
result = server.run_ci_pipeline(
project="project/path",
branch=None,
variables=None,
web_mode=False,
working_directory="/tmp"
)
assert result["success"] is True
assert result["pipeline_id"] == 456
assert result["pipeline_url"] == "https://gitlab.com/project/-/pipelines/456"
assert result["branch"] == "feature-branch"
proj.pipelines.create.assert_called_once()
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
@patch("git.Repo")
def test_run_ci_pipeline_fallback_to_default_branch(mock_repo, mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
proj = MagicMock()
pipeline = MagicMock()
pipeline.id = 789
pipeline.web_url = "https://gitlab.com/project/-/pipelines/789"
proj.pipelines.create.return_value = pipeline
proj.default_branch = "main"
mock_gitlab.return_value.projects.get.return_value = proj
# Simulate git branch detection failure by raising an exception
mock_repo.return_value.active_branch = property(lambda self: (_ for _ in ()).throw(Exception("No branch")))
result = server.run_ci_pipeline(
project="project/path",
branch=None,
variables=None,
web_mode=False,
working_directory="/tmp"
)
assert result["success"] is True
assert result["pipeline_id"] == 789
assert result["pipeline_url"] == "https://gitlab.com/project/-/pipelines/789"
assert result["branch"] == "main"
proj.pipelines.create.assert_called_once()
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_run_ci_pipeline_web_mode(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
proj = MagicMock()
pipeline = MagicMock()
pipeline.id = 321
pipeline.web_url = "https://gitlab.com/project/-/pipelines/321"
proj.pipelines.create.return_value = pipeline
mock_gitlab.return_value.projects.get.return_value = proj
result = server.run_ci_pipeline(
project="project/path",
branch="main",
variables={"FOO": "bar"},
web_mode=True,
working_directory="/tmp"
)
assert result["success"] is True
assert result["web_mode"] is True
args = proj.pipelines.create.call_args[0][0]
# Should include CI_PIPELINE_SOURCE=web
assert {"key": "CI_PIPELINE_SOURCE", "value": "web"} in args["variables"]
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_run_ci_pipeline_error_handling(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
mock_gitlab.return_value.projects.get.side_effect = Exception("Not found")
result = server.run_ci_pipeline(
project="project/path",
branch="main",
variables=None,
web_mode=False,
working_directory="/tmp"
)
assert "error" in result
assert "Not found" in result["error"]
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_find_group(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
group1 = MagicMock()
group1.id = 1
group1.name = "Test Group"
group1.full_path = "test-group"
group1.web_url = "https://gitlab.com/groups/test-group"
group1.description = "A test group"
mock_gitlab.return_value.groups.list.return_value = [group1]
result = server.find_group("test-group")
assert isinstance(result, list)
assert result[0]["name"] == "Test Group"
assert result[0]["full_path"] == "test-group"
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_search_epics_success(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
group = MagicMock()
epic = MagicMock()
epic.id = 1
epic.iid = 101
epic.title = "Epic Title"
epic.web_url = "https://gitlab.com/groups/test-group/-/epics/101"
epic.state = "opened"
epic.created_at = "2024-01-01T00:00:00Z"
epic.updated_at = "2024-01-02T00:00:00Z"
epic.author = {"id": 2, "name": "Author"}
epic.labels = ["label1"]
epic.description = "Epic description"
group.epics.list.return_value = [epic]
mock_gitlab.return_value.groups.get.return_value = group
result = server.search_epics("test-group", state="opened")
assert "epics" in result
assert result["epics"][0]["title"] == "Epic Title"
assert result["epics"][0]["state"] == "opened"
assert result["epics"][0]["author"]["name"] == "Author"
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_search_epics_error(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
mock_gitlab.return_value.groups.get.side_effect = Exception("Group not found")
result = server.search_epics("bad-group")
assert "error" in result
assert "Group not found" in result["error"]
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_create_epic_success(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
group = MagicMock()
epic = MagicMock()
epic.web_url = "https://gitlab.com/groups/test-group/-/epics/101"
group.epics.create.return_value = epic
mock_gitlab.return_value.groups.get.return_value = group
result = server.create_epic("test-group", "Epic Title", "Epic description", labels=["label1"])
assert "url" in result
assert result["url"] == "https://gitlab.com/groups/test-group/-/epics/101"
group.epics.create.assert_called_once()
args = group.epics.create.call_args[0][0]
assert args["title"] == "Epic Title"
assert args["description"] == "Epic description"
assert args["labels"] == ["label1"]
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_create_epic_error(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
mock_gitlab.return_value.groups.get.side_effect = Exception("Group not found")
result = server.create_epic("bad-group", "Epic Title", "Epic description")
assert "error" in result
assert "Group not found" in result["error"]
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_update_issue_success(mock_gitlab, mock_settings):
"""Test successful update of a GitLab issue."""
server = GitLabPythonServer(working_directory="/tmp")
proj = MagicMock()
issue = MagicMock()
issue.web_url = "https://gitlab.com/project/-/issues/42"
proj.issues.get.return_value = issue
mock_gitlab.return_value.projects.get.return_value = proj
result = server.update_issue("project/path", 42, title="Updated Title", description="Updated desc", labels=["bug"])
assert "url" in result
assert result["url"] == "https://gitlab.com/project/-/issues/42"
proj.issues.get.assert_called_once_with(42)
assert issue.title == "Updated Title"
assert issue.description == "Updated desc"
assert issue.labels == ["bug"]
issue.save.assert_called_once()
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_update_issue_error(mock_gitlab, mock_settings):
"""Test error handling for update_issue when project or issue is not found."""
server = GitLabPythonServer(working_directory="/tmp")
mock_gitlab.return_value.projects.get.side_effect = Exception("Project not found")
result = server.update_issue("bad-project", 42, title="Updated Title")
assert "error" in result
assert "Project not found" in result["error"]
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_update_epic_success(mock_gitlab, mock_settings):
"""Test successful update of a GitLab epic."""
server = GitLabPythonServer(working_directory="/tmp")
group = MagicMock()
epic = MagicMock()
epic.web_url = "https://gitlab.com/groups/test-group/-/epics/101"
group.epics.get.return_value = epic
mock_gitlab.return_value.groups.get.return_value = group
result = server.update_epic("test-group", 101, title="Updated Epic", description="Updated desc", labels=["feature"])
assert "url" in result
assert result["url"] == "https://gitlab.com/groups/test-group/-/epics/101"
group.epics.get.assert_called_once_with(101)
assert epic.title == "Updated Epic"
assert epic.description == "Updated desc"
assert epic.labels == ["feature"]
epic.save.assert_called_once()
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_update_epic_error(mock_gitlab, mock_settings):
"""Test error handling for update_epic when group or epic is not found."""
server = GitLabPythonServer(working_directory="/tmp")
mock_gitlab.return_value.groups.get.side_effect = Exception("Group not found")
result = server.update_epic("bad-group", 101, title="Updated Epic")
assert "error" in result
assert "Group not found" in result["error"]
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_list_issue_comments_success(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
proj = MagicMock()
issue = MagicMock()
note1 = MagicMock()
note1.id = 1
note1.body = "First comment"
note1.author = {"id": 2, "name": "Alice"}
note1.created_at = "2024-01-01T00:00:00Z"
note1.updated_at = "2024-01-01T01:00:00Z"
note1.system = False
note2 = MagicMock()
note2.id = 2
note2.body = "Second comment"
note2.author = {"id": 3, "name": "Bob"}
note2.created_at = "2024-01-02T00:00:00Z"
note2.updated_at = "2024-01-02T01:00:00Z"
note2.system = True
issue.notes.list.return_value = [note1, note2]
proj.issues.get.return_value = issue
mock_gitlab.return_value.projects.get.return_value = proj
result = server.list_issue_comments("project/path", 42)
assert "comments" in result
assert len(result["comments"]) == 2
assert result["comments"][0]["body"] == "First comment"
assert result["comments"][1]["author"]["name"] == "Bob"
proj.issues.get.assert_called_once_with(42)
issue.notes.list.assert_called_once_with(all=True)
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_list_issue_comments_error(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
mock_gitlab.return_value.projects.get.side_effect = Exception("Project not found")
result = server.list_issue_comments("bad-project", 42)
assert "error" in result
assert "Project not found" in result["error"]
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_create_issue_comment_success(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
proj = MagicMock()
issue = MagicMock()
note = MagicMock()
note.id = 123
note.body = "A new comment"
note.author = {"id": 2, "name": "Alice"}
note.created_at = "2024-01-01T00:00:00Z"
note.updated_at = "2024-01-01T01:00:00Z"
note.system = False
issue.notes.create.return_value = note
proj.issues.get.return_value = issue
mock_gitlab.return_value.projects.get.return_value = proj
result = server.create_issue_comment("project/path", 42, "A new comment")
assert result["id"] == 123
assert result["body"] == "A new comment"
assert result["author"]["name"] == "Alice"
issue.notes.create.assert_called_once_with({"body": "A new comment"})
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_create_issue_comment_error(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
mock_gitlab.return_value.projects.get.side_effect = Exception("Project not found")
result = server.create_issue_comment("bad-project", 42, "A new comment")
assert "error" in result
assert "Project not found" in result["error"]
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_list_epic_comments_success(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
group = MagicMock()
epic = MagicMock()
note1 = MagicMock()
note1.id = 1
note1.body = "Epic comment 1"
note1.author = {"id": 2, "name": "Alice"}
note1.created_at = "2024-01-01T00:00:00Z"
note1.updated_at = "2024-01-01T01:00:00Z"
note1.system = False
note2 = MagicMock()
note2.id = 2
note2.body = "Epic comment 2"
note2.author = {"id": 3, "name": "Bob"}
note2.created_at = "2024-01-02T00:00:00Z"
note2.updated_at = "2024-01-02T01:00:00Z"
note2.system = True
epic.notes.list.return_value = [note1, note2]
group.epics.get.return_value = epic
mock_gitlab.return_value.groups.get.return_value = group
result = server.list_epic_comments("test-group", 101)
assert "comments" in result
assert len(result["comments"]) == 2
assert result["comments"][0]["body"] == "Epic comment 1"
assert result["comments"][1]["author"]["name"] == "Bob"
group.epics.get.assert_called_once_with(101)
epic.notes.list.assert_called_once_with(all=True)
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_list_epic_comments_error(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
mock_gitlab.return_value.groups.get.side_effect = Exception("Group not found")
result = server.list_epic_comments("bad-group", 101)
assert "error" in result
assert "Group not found" in result["error"]
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_create_epic_comment_success(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
group = MagicMock()
epic = MagicMock()
note = MagicMock()
note.id = 456
note.body = "A new epic comment"
note.author = {"id": 2, "name": "Alice"}
note.created_at = "2024-01-01T00:00:00Z"
note.updated_at = "2024-01-01T01:00:00Z"
note.system = False
epic.notes.create.return_value = note
group.epics.get.return_value = epic
mock_gitlab.return_value.groups.get.return_value = group
result = server.create_epic_comment("test-group", 101, "A new epic comment")
assert result["id"] == 456
assert result["body"] == "A new epic comment"
assert result["author"]["name"] == "Alice"
epic.notes.create.assert_called_once_with({"body": "A new epic comment"})
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_create_epic_comment_error(mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
mock_gitlab.return_value.groups.get.side_effect = Exception("Group not found")
result = server.create_epic_comment("bad-group", 101, "A new epic comment")
assert "error" in result
assert "Group not found" in result["error"]
|