forked from ruxailab/RUXAILAB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsonarcloud_to_github.py
More file actions
353 lines (294 loc) · 11.8 KB
/
sonarcloud_to_github.py
File metadata and controls
353 lines (294 loc) · 11.8 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
import requests
import json
import os
import re
import logging
from datetime import datetime, timedelta, timezone
from urllib.parse import quote
from typing import List, Dict, Any, Tuple
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
GITHUB_ACCEPT_HEADER = "application/vnd.github.v3+json"
class Config:
"""Configuration class for the integration"""
SONARCLOUD_URL = "https://sonarcloud.io"
PROJECT_KEY = "ruxailab_RUXAILAB"
ORGANIZATION_KEY = "ruxailab"
SONAR_TOKEN = os.environ.get("SONAR_TOKEN")
PAT_TOKEN = os.environ.get("PAT_TOKEN")
MIN_SEVERITY = "MAJOR"
GITHUB_REPO_OWNER = "ruxailab"
GITHUB_REPO_NAME = "RUXAILAB"
ISSUES_LOOKBACK_DAYS = 1
REQUEST_TIMEOUT = 30
MAX_RETRIES = 3
DUPLICATE_GITHUB_ISSUES_CHECK_COUNT = 500
SEVERITY_LEVELS = {
"BLOCKER": 5,
"CRITICAL": 4,
"MAJOR": 3,
"MINOR": 2,
"INFO": 1
}
def get_sonarcloud_issues() -> List[Dict[str, Any]]:
"""
Fetch new issues from SonarCloud.
Returns:
List of SonarCloud issues.
"""
issues, page, page_size = [], 1, 100
lookback_date = (datetime.now(timezone.utc) - timedelta(days=Config.ISSUES_LOOKBACK_DAYS)).date().isoformat()
severities = ",".join(sev for sev, level in Config.SEVERITY_LEVELS.items() if level >= Config.SEVERITY_LEVELS.get(Config.MIN_SEVERITY, 2))
url = f"{Config.SONARCLOUD_URL}/api/issues/search"
headers = {"Authorization": f"Bearer {Config.SONAR_TOKEN}"}
while True:
params = {
"componentKeys": Config.PROJECT_KEY,
"organization": Config.ORGANIZATION_KEY,
"resolved": "false",
"severities": severities,
"statuses": "OPEN,CONFIRMED",
"createdAfter": lookback_date,
"p": page,
"ps": page_size
}
try:
for attempt in range(Config.MAX_RETRIES):
try:
response = requests.get(url, params=params, headers=headers, timeout=Config.REQUEST_TIMEOUT)
response.raise_for_status()
break
except requests.RequestException as e:
if attempt < Config.MAX_RETRIES - 1:
logger.warning(f"Attempt {attempt + 1} failed: {str(e)}. Retrying...")
continue
raise
data = response.json()
issues.extend(data.get("issues", []))
if page * page_size >= data.get("total", 0):
break
page += 1
except requests.RequestException as e:
logger.error(f"Error fetching SonarCloud issues: {str(e)}")
break
return issues
def get_existing_github_issues() -> Dict[str, Any]:
"""
Get existing Github issues to check for duplicates.
Returns:
Dict mapping SonarCloud issue keys to GitHub issue data.
"""
existing_issues = {}
page, issue_count, per_page = 1, 0, 100
max_issues = Config.DUPLICATE_GITHUB_ISSUES_CHECK_COUNT
url = f"https://api.github.com/repos/{Config.GITHUB_REPO_OWNER}/{Config.GITHUB_REPO_NAME}/issues"
headers = {
"Authorization": f"token {Config.PAT_TOKEN}",
"Accept": GITHUB_ACCEPT_HEADER
}
while issue_count < max_issues:
try:
response = requests.get(
url, headers=headers,
params={"state": "all", "labels": "sonarcloud", "per_page": per_page, "page": page},
timeout=Config.REQUEST_TIMEOUT
)
response.raise_for_status()
issues = response.json()
if not issues:
break
for issue in issues:
body = issue.get("body", "")
match = re.search(r"issues=([^&]+)&", body)
if match:
existing_issues[match.group(1)] = issue
issue_count += len(issues)
if len(issues) < per_page:
break
page += 1
except requests.RequestException as e:
logger.error(f"Error fetching GitHub issues: {str(e)}")
break
logger.info(f"Found {len(existing_issues)} existing GitHub issues with SonarCloud labels.")
return existing_issues
def get_next_github_issue_number() -> int:
"""
Get the next GitHub issue number by finding the highest current issue number
Returns:
int: The next issue number (current highest + 1)
"""
url = f"https://api.github.com/repos/{Config.GITHUB_REPO_OWNER}/{Config.GITHUB_REPO_NAME}/issues"
params = {
"state": "all",
"per_page": 1,
"sort": "created",
"direction": "desc"
}
headers = {
"Authorization": f"token {Config.PAT_TOKEN}",
"Accept": GITHUB_ACCEPT_HEADER
}
try:
for attempt in range(Config.MAX_RETRIES):
try:
response = requests.get(
url,
params=params,
headers=headers,
timeout=Config.REQUEST_TIMEOUT
)
response.raise_for_status()
issues = response.json()
if issues:
return issues[0]["number"] + 1
else:
return 1
except requests.RequestException as e:
if attempt < Config.MAX_RETRIES - 1:
logger.warning(f"Attempt {attempt+1} failed when getting next issue number: {str(e)}. Retrying...")
continue
raise
except Exception as e:
logger.error(f"Error determining next GitHub issue number: {str(e)}")
return 0
def create_github_issue(issue: Dict[str, Any], next_number: int) -> Tuple[bool, int]:
"""
Create a Github issue from a SonarCloud issue
Args:
issue: SonarCloud issue data
next_number: Expected issue number to include in the title
Returns:
Tuple[bool, int]: Success status and actual issue number
"""
url = f"https://api.github.com/repos/{Config.GITHUB_REPO_OWNER}/{Config.GITHUB_REPO_NAME}/issues"
component = issue.get('component', '')
file_path = component.replace(f"{Config.PROJECT_KEY}:", "")
line = issue.get('line', 'Unknown')
issue_key = issue.get('key', '')
severity = issue.get('severity', '')
issue_type = issue.get('type', '')
message = issue.get('message', '')
rule = issue.get('rule', '')
title_prefix = f"#{next_number}" if next_number > 0 else ""
title = f"{title_prefix} [{severity}] {issue_type}: {message[:80]}{'...' if len(message) > 80 else ''}"
body = f"""
## SonarCloud Issue: {title_prefix}
### Issue Details:
- **Rule**: [{rule}]({Config.SONARCLOUD_URL}/organizations/{Config.ORGANIZATION_KEY}/rules?open={quote(rule, safe="")}&rule_key={quote(rule, safe="")})
- **Severity**: {severity}
- **Type**: {issue_type}
- **File**: `{file_path}`
- **Line**: {line}
- **SonarCloud Issue Key**: `{issue_key}`
### Issue Message:
> {message}
### Relevant Links:
- [View Issue in SonarCloud]({Config.SONARCLOUD_URL}/project/issues?id={Config.PROJECT_KEY}&issues={issue_key}&open={issue_key})
- [SonarCloud Rule Definition]({Config.SONARCLOUD_URL}/organizations/{Config.ORGANIZATION_KEY}/rules?open={quote(rule, safe="")}&rule_key={quote(rule, safe="")})
"""
data = {
"title": title,
"body": body,
"labels": ["sonarcloud", f"{severity.lower()}", f"{issue_type.lower()}"]
}
headers = {
"Authorization": f"token {Config.PAT_TOKEN}",
"Accept": GITHUB_ACCEPT_HEADER
}
try:
for attempt in range(Config.MAX_RETRIES):
try:
response = requests.post(
url,
headers=headers,
data=json.dumps(data),
timeout=Config.REQUEST_TIMEOUT
)
response.raise_for_status()
result = response.json()
issue_url = result.get('html_url', '')
actual_number = result.get('number', 0)
logger.info(f"Created GitHub issue #{actual_number}: {issue_url}")
return True, actual_number
except requests.RequestException as e:
if attempt < Config.MAX_RETRIES - 1:
logger.warning(f"Attempt {attempt+1} failed: {str(e)}. Retrying...")
continue
raise
except requests.RequestException as e:
logger.error(f"Error creating GitHub issue: {str(e)}")
return False, 0
def should_create_issue(issue: Dict[str, Any], existing_issues: Dict[str, Any]) -> bool:
"""
Determine if an issue should trigger GitHub issue creation
Args:
issue: SonarCloud issue data
existing_issues: Dictionary of existing GitHub issues keyed by SonarCloud issue key
Returns:
bool: True if issue should be created, False otherwise
"""
issue_key = issue.get('key', '')
# Check if we already have an issue for this SonarCloud issue
if issue_key in existing_issues:
logger.info(f"Skipping issue {issue_key} - Already exists as GitHub issue #{existing_issues[issue_key].get('number')}")
return False
return True
def check_prerequisites() -> bool:
"""
Check if all require environment variables are set
Returns:
bool: True if all prerequisites are met, else False
"""
missing = []
if not Config.SONAR_TOKEN:
missing.append("SONAR_TOKEN")
if not Config.PAT_TOKEN:
missing.append("PAT_TOKEN")
if missing:
logger.error(f"Missing required environment variables: {', '.join(missing)}")
return False
return True
def main() -> None:
"""
Main function to fetch issues from SonarCloud and create issues on Github
"""
logger.info("Starting SonarCloud to GitHub integration")
if not check_prerequisites():
return
try:
logger.info("Fetching existing GitHub issues for deduplication check")
existing_issues = get_existing_github_issues()
logger.info("Fetching issues from SonarCloud")
issues = get_sonarcloud_issues()
if not issues:
logger.info("No new issues found")
return
severities = ",".join(sev for sev, level in Config.SEVERITY_LEVELS.items() if level >= Config.SEVERITY_LEVELS.get(Config.MIN_SEVERITY, 2))
logger.info(f"Fetching SonarCloud issues with severities: {severities} (Min Severity: {Config.MIN_SEVERITY}) - Found {len(issues)} issues")
next_issue_number = get_next_github_issue_number()
logger.info(f"Next expected GitHub issue number: #{next_issue_number}")
created_count = 0
duplicate_count = 0
current_number = next_issue_number
for issue in issues:
if should_create_issue(issue, existing_issues):
logger.info(f"Creating Github issue (expected #{current_number}) for: {issue.get('message', '')[:80]}...")
success, actual_number = create_github_issue(issue, current_number)
if success:
created_count += 1
existing_issues[issue.get('key', '')] = {"number": actual_number}
if actual_number > 0:
current_number = actual_number + 1
else:
current_number += 1
else:
duplicate_count += 1
logger.debug(f"Skipping duplicate issue: {issue.get('message', '')[:80]}...")
logger.info(f"Process completed. Created {created_count} issues, found {duplicate_count} duplicate issues")
except Exception as e:
logger.error(f"Unexpected error: {str(e)}")
if __name__ == "__main__":
main()