forked from ekohe/gitlab-skill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlab_api.py
More file actions
447 lines (353 loc) · 16.5 KB
/
Copy pathgitlab_api.py
File metadata and controls
447 lines (353 loc) · 16.5 KB
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
#!/usr/bin/env python3
"""
GitLab API interaction script for fetching issues, merge requests, and posting comments.
Configuration:
- Config file: ./gitlab_config.json or ~/.gitlab/config.json
- Or environment variables: GITLAB_URL and GITLAB_TOKEN
"""
import os
import sys
import json
import requests
from pathlib import Path
from typing import Optional, Dict, List, Any
from datetime import datetime
def load_gitlab_config(instance_name: Optional[str] = None) -> tuple[str, str]:
"""
Load GitLab configuration from config file or environment variables.
Config file locations (in order):
1. ./gitlab_config.json
2. ~/.gitlab/config.json
3. <script_dir>/gitlab_config.json
Returns:
tuple: (base_url, token)
"""
config_paths = [
Path.cwd() / 'gitlab_config.json',
Path.home() / '.gitlab' / 'config.json',
Path(__file__).parent.parent / 'gitlab_config.json'
]
# Try to load from config file
for config_path in config_paths:
if config_path.exists():
try:
with open(config_path, 'r') as f:
config = json.load(f)
# If no instance specified, use default
if instance_name is None:
instance_name = config.get('default')
if not instance_name:
print("Warning: No default instance specified in config", file=sys.stderr)
continue
# Get instance config
instance = config.get('instances', {}).get(instance_name)
if not instance:
print(f"Error: Instance '{instance_name}' not found in config", file=sys.stderr)
print(f"Available instances: {', '.join(config.get('instances', {}).keys())}", file=sys.stderr)
sys.exit(1)
url = instance.get('url', '').rstrip('/')
token = instance.get('token', '')
if url and token:
return url, token
except json.JSONDecodeError as e:
print(f"Warning: Invalid JSON in {config_path}: {e}", file=sys.stderr)
continue
except Exception as e:
print(f"Warning: Error reading {config_path}: {e}", file=sys.stderr)
continue
# Fall back to environment variables
url = os.environ.get('GITLAB_URL', '').rstrip('/')
token = os.environ.get('GITLAB_TOKEN', '')
if not url or not token:
raise ValueError(
"GitLab configuration not found.\n\n"
"Option 1 - Config file:\n"
" Create gitlab_config.json with your GitLab instances\n"
" (see gitlab_config.json.template for example)\n\n"
"Option 2 - Environment variables:\n"
" export GITLAB_URL='https://gitlab.com'\n"
" export GITLAB_TOKEN='glpat-xxxxxxxxxxxx'"
)
return url, token
def get_config_file() -> Optional[Path]:
"""Get the config file path if it exists."""
config_paths = [
Path.cwd() / 'gitlab_config.json',
Path.home() / '.gitlab' / 'config.json',
Path(__file__).parent.parent / 'gitlab_config.json'
]
for config_path in config_paths:
if config_path.exists():
return config_path
return None
def load_config() -> Optional[Dict]:
"""Load the full config file."""
config_path = get_config_file()
if not config_path:
return None
try:
with open(config_path, 'r') as f:
return json.load(f)
except Exception as e:
print(f"Error reading config: {e}", file=sys.stderr)
return None
def resolve_project_alias(project_alias: str, instance_name: Optional[str] = None) -> tuple[str, Optional[str]]:
"""
Resolve a project alias to its full project ID and determine instance.
Args:
project_alias: Project alias or full project ID
instance_name: Optional instance name (overrides project config)
Returns:
tuple: (project_id, instance_name)
"""
config = load_config()
# If no config or no projects section, return as-is
if not config or 'projects' not in config:
return project_alias, instance_name
projects = config.get('projects', {})
# Check if this is a project alias
if project_alias in projects:
project_config = projects[project_alias]
project_id = project_config.get('project_id', project_alias)
# Use instance from project config if not explicitly specified
if instance_name is None:
instance_name = project_config.get('instance')
return project_id, instance_name
# Not an alias, return as-is
return project_alias, instance_name
def list_instances() -> None:
"""List all configured GitLab instances."""
config_path = get_config_file()
if not config_path:
print("No GitLab configuration file found.")
print("\nSearched locations:")
for path in [Path.cwd() / 'gitlab_config.json',
Path.home() / '.gitlab' / 'config.json',
Path(__file__).parent.parent / 'gitlab_config.json']:
print(f" - {path}")
print("\nCreate a gitlab_config.json file (see gitlab_config.json.template)")
return
try:
with open(config_path, 'r') as f:
config = json.load(f)
default = config.get('default', '')
instances = config.get('instances', {})
print(f"Config file: {config_path}")
print(f"Default instance: {default}\n")
print("Available instances:")
for name, instance in instances.items():
desc = instance.get('description', 'No description')
url = instance.get('url', 'No URL')
marker = ' (default)' if name == default else ''
print(f" - {name}{marker}")
print(f" URL: {url}")
print(f" Description: {desc}")
except Exception as e:
print(f"Error reading config: {e}", file=sys.stderr)
sys.exit(1)
def list_projects() -> None:
"""List all configured project aliases."""
config_path = get_config_file()
if not config_path:
print("No GitLab configuration file found.")
return
try:
with open(config_path, 'r') as f:
config = json.load(f)
projects = config.get('projects', {})
if not projects:
print("No project aliases configured.")
print("\nAdd a 'projects' section to your gitlab_config.json")
return
print(f"Config file: {config_path}\n")
print("Available project aliases:")
for alias, project in projects.items():
project_id = project.get('project_id', 'No ID')
instance = project.get('instance', 'default')
desc = project.get('description', 'No description')
print(f" - {alias}")
print(f" Project ID: {project_id}")
print(f" Instance: {instance}")
print(f" Description: {desc}")
print()
except Exception as e:
print(f"Error reading config: {e}", file=sys.stderr)
sys.exit(1)
class GitLabAPI:
"""GitLab API client for issues, merge requests, and comments."""
def __init__(self, instance_name: Optional[str] = None):
self.base_url, self.token = load_gitlab_config(instance_name)
self.headers = {
'PRIVATE-TOKEN': self.token,
'Content-Type': 'application/json'
}
self.api_url = f"{self.base_url}/api/v4"
def _request(self, method: str, endpoint: str, **kwargs) -> Any:
"""Make API request with error handling."""
url = f"{self.api_url}/{endpoint}"
try:
response = requests.request(method, url, headers=self.headers, **kwargs)
response.raise_for_status()
return response.json() if response.content else None
except requests.exceptions.HTTPError as e:
print(f"HTTP Error: {e}", file=sys.stderr)
print(f"Response: {e.response.text}", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
def get_project(self, project_id: str) -> Dict:
"""Get project details."""
return self._request('GET', f"projects/{requests.utils.quote(project_id, safe='')}")
def get_issue(self, project_id: str, issue_iid: int) -> Dict:
"""Get issue details including description and comments."""
issue = self._request('GET', f"projects/{requests.utils.quote(project_id, safe='')}/issues/{issue_iid}")
# Fetch comments/notes
notes = self._request('GET', f"projects/{requests.utils.quote(project_id, safe='')}/issues/{issue_iid}/notes")
issue['notes'] = notes
return issue
def list_issues(self, project_id: str, state: str = 'opened', labels: Optional[List[str]] = None) -> List[Dict]:
"""List issues in a project."""
params = {'state': state}
if labels:
params['labels'] = ','.join(labels)
return self._request('GET', f"projects/{requests.utils.quote(project_id, safe='')}/issues", params=params)
def get_merge_request(self, project_id: str, mr_iid: int) -> Dict:
"""Get merge request details including changes and comments."""
mr = self._request('GET', f"projects/{requests.utils.quote(project_id, safe='')}/merge_requests/{mr_iid}")
# Fetch changes (diffs)
changes = self._request('GET', f"projects/{requests.utils.quote(project_id, safe='')}/merge_requests/{mr_iid}/changes")
mr['changes'] = changes.get('changes', [])
# Fetch comments/notes
notes = self._request('GET', f"projects/{requests.utils.quote(project_id, safe='')}/merge_requests/{mr_iid}/notes")
mr['notes'] = notes
return mr
def list_merge_requests(self, project_id: str, state: str = 'opened') -> List[Dict]:
"""List merge requests in a project."""
params = {'state': state}
return self._request('GET', f"projects/{requests.utils.quote(project_id, safe='')}/merge_requests", params=params)
def post_issue_comment(self, project_id: str, issue_iid: int, body: str) -> Dict:
"""Post a comment on an issue."""
data = {'body': body}
return self._request('POST', f"projects/{requests.utils.quote(project_id, safe='')}/issues/{issue_iid}/notes", json=data)
def post_mr_comment(self, project_id: str, mr_iid: int, body: str) -> Dict:
"""Post a comment on a merge request."""
data = {'body': body}
return self._request('POST', f"projects/{requests.utils.quote(project_id, safe='')}/merge_requests/{mr_iid}/notes", json=data)
def get_diff_content(self, project_id: str, mr_iid: int) -> str:
"""Get merge request diff as unified diff format."""
changes = self._request('GET', f"projects/{requests.utils.quote(project_id, safe='')}/merge_requests/{mr_iid}/changes")
diff_content = []
for change in changes.get('changes', []):
diff_content.append(f"File: {change['old_path']} -> {change['new_path']}")
diff_content.append(change['diff'])
diff_content.append("\n")
return "\n".join(diff_content)
def list_project_issues_aggregate(self, project_id: str, days: int = 7) -> Dict[str, Any]:
"""Aggregate issue statistics for a project."""
all_issues = self._request('GET', f"projects/{requests.utils.quote(project_id, safe='')}/issues",
params={'per_page': 100})
stats = {
'total': len(all_issues),
'opened': sum(1 for i in all_issues if i['state'] == 'opened'),
'closed': sum(1 for i in all_issues if i['state'] == 'closed'),
'by_label': {},
'recent': []
}
# Count by labels
for issue in all_issues:
for label in issue.get('labels', []):
stats['by_label'][label] = stats['by_label'].get(label, 0) + 1
# Recent issues
stats['recent'] = sorted(
all_issues,
key=lambda x: x['updated_at'],
reverse=True
)[:10]
return stats
def main():
"""CLI interface for GitLab API operations."""
if len(sys.argv) < 2:
print("Usage: gitlab_api.py [--instance=<name>] <command> [args...]", file=sys.stderr)
print("\nCommands:", file=sys.stderr)
print(" list-instances - List configured GitLab instances", file=sys.stderr)
print(" list-projects - List configured project aliases", file=sys.stderr)
print(" get-issue <project> <issue_iid> - Fetch issue with comments", file=sys.stderr)
print(" list-issues <project> [state] [labels...] - List issues with filters", file=sys.stderr)
print(" get-mr <project> <mr_iid> - Fetch merge request with changes", file=sys.stderr)
print(" list-mrs <project> [state] - List merge requests", file=sys.stderr)
print(" post-issue-comment <project> <issue_iid> <comment>", file=sys.stderr)
print(" post-mr-comment <project> <mr_iid> <comment>", file=sys.stderr)
print(" get-diff <project> <mr_iid> - Get unified diff for MR", file=sys.stderr)
print(" aggregate-issues <project> [days] - Get issue statistics", file=sys.stderr)
print("\nOptions:", file=sys.stderr)
print(" --instance=<name> Specify which GitLab instance to use (from config file)", file=sys.stderr)
print("\nNote: <project> can be a project alias (from config) or full project ID", file=sys.stderr)
sys.exit(1)
# Parse instance flag
instance_name = None
args = sys.argv[1:]
if args[0].startswith('--instance='):
instance_name = args[0].split('=', 1)[1]
args = args[1:]
if not args:
print("Error: No command specified", file=sys.stderr)
sys.exit(1)
command = args[0]
# Handle list commands (don't need API initialization)
if command == 'list-instances':
list_instances()
return
if command == 'list-projects':
list_projects()
return
# Resolve project alias if a project argument is provided
if len(args) > 1:
project_arg = args[1]
resolved_project_id, resolved_instance = resolve_project_alias(project_arg, instance_name)
args[1] = resolved_project_id
# Use the instance from project config if not explicitly set
if instance_name is None and resolved_instance is not None:
instance_name = resolved_instance
# Initialize API with optional instance name
api = GitLabAPI(instance_name)
try:
if command == 'get-issue':
project_id, issue_iid = args[1], int(args[2])
result = api.get_issue(project_id, issue_iid)
elif command == 'list-issues':
project_id = args[1]
state = args[2] if len(args) > 2 else 'opened'
labels = args[3:] if len(args) > 3 else None
result = api.list_issues(project_id, state, labels)
elif command == 'get-mr':
project_id, mr_iid = args[1], int(args[2])
result = api.get_merge_request(project_id, mr_iid)
elif command == 'list-mrs':
project_id = args[1]
state = args[2] if len(args) > 2 else 'opened'
result = api.list_merge_requests(project_id, state)
elif command == 'post-issue-comment':
project_id, issue_iid, comment = args[1], int(args[2]), args[3]
result = api.post_issue_comment(project_id, issue_iid, comment)
elif command == 'post-mr-comment':
project_id, mr_iid, comment = args[1], int(args[2]), args[3]
result = api.post_mr_comment(project_id, mr_iid, comment)
elif command == 'get-diff':
project_id, mr_iid = args[1], int(args[2])
result = api.get_diff_content(project_id, mr_iid)
print(result)
return
elif command == 'aggregate-issues':
project_id = args[1]
days = int(args[2]) if len(args) > 2 else 7
result = api.list_project_issues_aggregate(project_id, days)
else:
print(f"Unknown command: {command}", file=sys.stderr)
sys.exit(1)
print(json.dumps(result, indent=2))
except IndexError:
print(f"Invalid arguments for command: {command}", file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
main()