Last active
January 12, 2023 22:09
-
-
Save cierzniak/1c4d367eabec6b6cf48faab61ed9c11b to your computer and use it in GitHub Desktop.
Initialize SonarQube on project with Git history
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import subprocess | |
# Enter config variables here | |
start_date = '2023-01-01' | |
log_dir = '.sonarlogs' | |
branch = 'main' | |
# Get all commits (last commit of day) and run sonar-scanner | |
git_log = subprocess.run([ | |
'git', | |
'log', | |
branch, | |
'--after="{} 00:00"'.format(start_date), | |
'--pretty=format:"%H %as"', | |
'--reverse', | |
], stdout=subprocess.PIPE) | |
commits = git_log.stdout.decode('utf-8').split('\n') | |
commits = list(map(lambda x: x.strip('"\''), commits)) | |
to_scan = dict() | |
for commit in commits: | |
[commit_sha, commit_date] = commit.split() | |
to_scan[commit_date] = commit_sha | |
if not os.path.exists(log_dir): | |
os.makedirs(log_dir) | |
for commit_date, commit_sha in to_scan.items(): | |
result_file = open('{}/{}_{}.log'.format(log_dir, commit_date, commit_sha), 'w') | |
error_file = open('{}/{}_{}_error.log'.format(log_dir, commit_date, commit_sha), 'w') | |
print('Scan {} from {}'.format(commit_sha, commit_date)) | |
subprocess.run(['git', 'checkout', commit_sha], stdout=subprocess.PIPE) | |
sonar_scanner = subprocess.run([ | |
'sonar-scanner', | |
'-Dsonar.projectVersion={}'.format(commit_sha), | |
'-Dsonar.projectDate={}'.format(commit_date), | |
], stdout=result_file, stderr=error_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment