gitlab-watcher 1.0.0__py3-none-any.whl

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.
@@ -0,0 +1,364 @@
1
+ """GitLab API client with retry logic."""
2
+
3
+ import time
4
+ from dataclasses import dataclass
5
+ from typing import Any, Optional
6
+ from urllib.parse import quote
7
+
8
+ import requests
9
+ from requests.adapters import HTTPAdapter
10
+ from urllib3.util.retry import Retry
11
+
12
+ from .cache import TimedCache
13
+ from .exceptions import (
14
+ GitLabAPIError,
15
+ GitLabAuthenticationError,
16
+ GitLabConnectionError,
17
+ GitLabForbiddenError,
18
+ GitLabNotFoundError,
19
+ GitLabRateLimitError,
20
+ )
21
+
22
+
23
+ # Default configuration values
24
+ DEFAULT_TIMEOUT = 30.0
25
+ DEFAULT_MAX_RETRIES = 3
26
+ DEFAULT_RETRY_DELAY = 1.0
27
+ DEFAULT_POOL_CONNECTIONS = 10
28
+ DEFAULT_POOL_MAXSIZE = 20
29
+ DEFAULT_CACHE_TTL = 30.0
30
+
31
+
32
+ @dataclass
33
+ class Issue:
34
+ """Represents a GitLab issue."""
35
+
36
+ iid: int
37
+ title: str
38
+ description: str
39
+ web_url: str
40
+ labels: list[str]
41
+
42
+
43
+ @dataclass
44
+ class MergeRequest:
45
+ """Represents a GitLab merge request."""
46
+
47
+ iid: int
48
+ title: str
49
+ web_url: str
50
+ source_branch: str
51
+ state: str
52
+
53
+
54
+ @dataclass
55
+ class Note:
56
+ """Represents a GitLab note (comment)."""
57
+
58
+ id: int
59
+ body: str
60
+ author_username: str
61
+
62
+
63
+ class GitLabClient:
64
+ """GitLab API client with automatic retries, connection pooling, and caching."""
65
+
66
+ def __init__(
67
+ self,
68
+ url: str,
69
+ token: str,
70
+ max_retries: int = DEFAULT_MAX_RETRIES,
71
+ retry_delay: float = DEFAULT_RETRY_DELAY,
72
+ timeout: float = DEFAULT_TIMEOUT,
73
+ pool_connections: int = DEFAULT_POOL_CONNECTIONS,
74
+ pool_maxsize: int = DEFAULT_POOL_MAXSIZE,
75
+ cache_ttl: float = DEFAULT_CACHE_TTL,
76
+ ) -> None:
77
+ """Initialize GitLab client.
78
+
79
+ Args:
80
+ url: GitLab instance URL (e.g., https://git.example.com)
81
+ token: Personal access token
82
+ max_retries: Maximum number of retries on 5xx errors
83
+ retry_delay: Delay between retries in seconds
84
+ timeout: Request timeout in seconds
85
+ pool_connections: Number of connection pool connections
86
+ pool_maxsize: Maximum connections in pool
87
+ cache_ttl: Cache time-to-live in seconds
88
+ """
89
+ self.base_url = url.rstrip("/")
90
+ self._token = token # Private to avoid accidental logging
91
+ self.max_retries = max_retries
92
+ self.retry_delay = retry_delay
93
+ self.timeout = timeout
94
+ self._cache: TimedCache[dict[str, Any]] = TimedCache(ttl_seconds=cache_ttl)
95
+
96
+ # Configure session with connection pooling and retry strategy
97
+ self.session = requests.Session()
98
+ self.session.headers.update({"PRIVATE-TOKEN": token})
99
+
100
+ # Configure retry strategy for HTTP errors
101
+ retry_strategy = Retry(
102
+ total=max_retries,
103
+ backoff_factor=retry_delay,
104
+ status_forcelist=[429, 500, 502, 503, 504],
105
+ )
106
+
107
+ # Configure adapter with connection pooling
108
+ adapter = HTTPAdapter(
109
+ max_retries=retry_strategy,
110
+ pool_connections=pool_connections,
111
+ pool_maxsize=pool_maxsize,
112
+ )
113
+ self.session.mount("https://", adapter)
114
+ self.session.mount("http://", adapter)
115
+
116
+ def __repr__(self) -> str:
117
+ """Return string representation without sensitive data."""
118
+ return f"GitLabClient(url={self.base_url!r})"
119
+
120
+ def _api_url(self, project_id: int, endpoint: str) -> str:
121
+ """Build full API URL for a project endpoint."""
122
+ return f"{self.base_url}/api/v4/projects/{project_id}{endpoint}"
123
+
124
+ def _request(self, method: str, url: str, **kwargs: Any) -> requests.Response:
125
+ """Make HTTP request with timeout and retry logic for 5xx errors."""
126
+ # Set default timeout if not provided
127
+ kwargs.setdefault("timeout", self.timeout)
128
+
129
+ last_error: Optional[Exception] = None
130
+
131
+ for attempt in range(self.max_retries):
132
+ try:
133
+ response = self.session.request(method, url, **kwargs)
134
+
135
+ # Retry on 5xx errors
136
+ if response.status_code >= 500:
137
+ last_error = Exception(
138
+ f"Server error {response.status_code}: {response.text}"
139
+ )
140
+ time.sleep(self.retry_delay * (attempt + 1))
141
+ continue
142
+
143
+ if response.status_code == 401:
144
+ raise GitLabAuthenticationError()
145
+ elif response.status_code == 403:
146
+ raise GitLabForbiddenError(url)
147
+ elif response.status_code == 404:
148
+ raise GitLabNotFoundError("Resource", url)
149
+ elif response.status_code == 429:
150
+ retry_after_str = response.headers.get("Retry-After")
151
+ retry_after = int(retry_after_str) if retry_after_str and retry_after_str.isdigit() else None
152
+ raise GitLabRateLimitError(retry_after)
153
+ elif response.status_code >= 400:
154
+ raise GitLabAPIError(response.status_code, response.text)
155
+
156
+ return response
157
+
158
+ except requests.RequestException as e:
159
+ last_error = e
160
+ time.sleep(self.retry_delay * (attempt + 1))
161
+
162
+ raise GitLabConnectionError(
163
+ f"Request failed after {self.max_retries} retries. Last error: {last_error}"
164
+ )
165
+
166
+ def get_issues(
167
+ self,
168
+ project_id: int,
169
+ state: str = "opened",
170
+ assignee_username: Optional[str] = None,
171
+ ) -> list[Issue]:
172
+ """Get issues for a project."""
173
+ endpoint = f"/issues?state={state}"
174
+ if assignee_username:
175
+ endpoint += f"&assignee_username={quote(assignee_username)}"
176
+
177
+ response = self._request("GET", self._api_url(project_id, endpoint))
178
+ data = response.json()
179
+
180
+ return [
181
+ Issue(
182
+ iid=item["iid"],
183
+ title=item.get("title", ""),
184
+ description=item.get("description", "") or "",
185
+ web_url=item.get("web_url", ""),
186
+ labels=item.get("labels", []),
187
+ )
188
+ for item in data
189
+ ]
190
+
191
+ def get_merge_requests(
192
+ self,
193
+ project_id: int,
194
+ state: str = "opened",
195
+ author_username: Optional[str] = None,
196
+ ) -> list[MergeRequest]:
197
+ """Get merge requests for a project."""
198
+ endpoint = f"/merge_requests?state={state}"
199
+ if author_username:
200
+ endpoint += f"&author_username={quote(author_username)}"
201
+
202
+ response = self._request("GET", self._api_url(project_id, endpoint))
203
+ data = response.json()
204
+
205
+ return [
206
+ MergeRequest(
207
+ iid=item["iid"],
208
+ title=item.get("title", ""),
209
+ web_url=item.get("web_url", ""),
210
+ source_branch=item.get("source_branch", ""),
211
+ state=item.get("state", ""),
212
+ )
213
+ for item in data
214
+ ]
215
+
216
+ def get_merge_request(self, project_id: int, mr_iid: int) -> Optional[MergeRequest]:
217
+ """Get a specific merge request with caching."""
218
+ cache_key = f"mr_{project_id}_{mr_iid}"
219
+ cached = self._cache.get(cache_key)
220
+
221
+ if cached is not None:
222
+ return MergeRequest(
223
+ iid=cached["iid"],
224
+ title=cached.get("title", ""),
225
+ web_url=cached.get("web_url", ""),
226
+ source_branch=cached.get("source_branch", ""),
227
+ state=cached.get("state", ""),
228
+ )
229
+
230
+ try:
231
+ response = self._request("GET", self._api_url(project_id, f"/merge_requests/{mr_iid}"))
232
+ except GitLabNotFoundError:
233
+ return None
234
+
235
+ data = response.json()
236
+
237
+ if "iid" not in data:
238
+ return None
239
+
240
+ # Cache the raw response
241
+ self._cache.set(cache_key, data)
242
+
243
+ return MergeRequest(
244
+ iid=data["iid"],
245
+ title=data.get("title", ""),
246
+ web_url=data.get("web_url", ""),
247
+ source_branch=data.get("source_branch", ""),
248
+ state=data.get("state", ""),
249
+ )
250
+
251
+ def get_notes(
252
+ self,
253
+ project_id: int,
254
+ mr_iid: int,
255
+ sort: str = "desc",
256
+ ) -> list[Note]:
257
+ """Get notes (comments) for a merge request with caching."""
258
+ cache_key = f"notes_{project_id}_{mr_iid}"
259
+ cached = self._cache.get(cache_key)
260
+
261
+ if cached is not None:
262
+ return [
263
+ Note(
264
+ id=item["id"],
265
+ body=item.get("body", ""),
266
+ author_username=item.get("author", {}).get("username", ""),
267
+ )
268
+ for item in cached
269
+ ]
270
+
271
+ endpoint = f"/merge_requests/{mr_iid}/notes?sort={sort}"
272
+
273
+ response = self._request("GET", self._api_url(project_id, endpoint))
274
+ data = response.json()
275
+
276
+ # Cache the raw response
277
+ self._cache.set(cache_key, data)
278
+
279
+ return [
280
+ Note(
281
+ id=item["id"],
282
+ body=item.get("body", ""),
283
+ author_username=item.get("author", {}).get("username", ""),
284
+ )
285
+ for item in data
286
+ ]
287
+
288
+ def update_issue_labels(
289
+ self,
290
+ project_id: int,
291
+ issue_iid: int,
292
+ labels: list[str],
293
+ ) -> bool:
294
+ """Update labels on an issue."""
295
+ labels_str = ",".join(labels)
296
+ endpoint = f"/issues/{issue_iid}"
297
+
298
+ response = self._request(
299
+ "PUT",
300
+ self._api_url(project_id, endpoint),
301
+ data={"labels": labels_str},
302
+ )
303
+
304
+ return response.status_code == 200
305
+
306
+ def create_merge_request(
307
+ self,
308
+ project_id: int,
309
+ source_branch: str,
310
+ target_branch: str,
311
+ title: str,
312
+ description: str,
313
+ ) -> Optional[MergeRequest]:
314
+ """Create a new merge request."""
315
+ endpoint = "/merge_requests"
316
+
317
+ response = self._request(
318
+ "POST",
319
+ self._api_url(project_id, endpoint),
320
+ data={
321
+ "source_branch": source_branch,
322
+ "target_branch": target_branch,
323
+ "title": title,
324
+ "description": description,
325
+ },
326
+ )
327
+
328
+ data = response.json()
329
+
330
+ if "iid" not in data:
331
+ return None
332
+
333
+ return MergeRequest(
334
+ iid=data["iid"],
335
+ title=data.get("title", ""),
336
+ web_url=data.get("web_url", ""),
337
+ source_branch=data.get("source_branch", ""),
338
+ state=data.get("state", ""),
339
+ )
340
+
341
+ def invalidate_cache(self, key: Optional[str] = None) -> None:
342
+ """Invalidate cache entries.
343
+
344
+ Args:
345
+ key: Specific cache key to invalidate, or None to clear all
346
+ """
347
+ if key is not None:
348
+ self._cache.invalidate(key)
349
+ else:
350
+ self._cache.clear()
351
+
352
+
353
+ __all__ = [
354
+ "GitLabClient",
355
+ "Issue",
356
+ "MergeRequest",
357
+ "Note",
358
+ "DEFAULT_TIMEOUT",
359
+ "DEFAULT_MAX_RETRIES",
360
+ "DEFAULT_RETRY_DELAY",
361
+ "DEFAULT_POOL_CONNECTIONS",
362
+ "DEFAULT_POOL_MAXSIZE",
363
+ "DEFAULT_CACHE_TTL",
364
+ ]
@@ -0,0 +1,56 @@
1
+ """Logging utilities for sensitive data filtering."""
2
+
3
+ import logging
4
+ import re
5
+ from typing import Any
6
+
7
+
8
+ class SensitiveDataFilter(logging.Filter):
9
+ """Filter to mask sensitive data in log messages."""
10
+
11
+ SENSITIVE_PATTERNS = [
12
+ # GitLab tokens (typically 20+ alphanumeric characters, sometimes with hyphens/underscores)
13
+ (r'([a-zA-Z0-9_-]{20,})', '***TOKEN***'),
14
+ # URLs with authentication (user:pass@ or token@)
15
+ (r'https://[^:]+:[^@]+@', 'https://***:***@'),
16
+ # URLs with token only (token@)
17
+ (r'https://[^@]+@(?=[^/])', 'https://***@'),
18
+ ]
19
+
20
+ def filter(self, record: logging.LogRecord) -> bool:
21
+ """Filter sensitive data from log record.
22
+
23
+ Args:
24
+ record: The log record to filter
25
+
26
+ Returns:
27
+ Always True (filtering is done in-place)
28
+ """
29
+ # Filter the main message
30
+ if record.msg:
31
+ record.msg = self._mask_sensitive(str(record.msg))
32
+
33
+ # Filter args if present
34
+ if record.args:
35
+ record.args = tuple(
36
+ self._mask_sensitive(str(arg)) if isinstance(arg, str) else arg
37
+ for arg in record.args
38
+ )
39
+
40
+ return True
41
+
42
+ def _mask_sensitive(self, text: str) -> str:
43
+ """Mask sensitive data in text.
44
+
45
+ Args:
46
+ text: The text to mask
47
+
48
+ Returns:
49
+ Text with sensitive data masked
50
+ """
51
+ for pattern, replacement in self.SENSITIVE_PATTERNS:
52
+ text = re.sub(pattern, replacement, text)
53
+ return text
54
+
55
+
56
+ __all__ = ["SensitiveDataFilter"]