Skip to content
Snippets Groups Projects
Unverified Commit 7e7bf468 authored by Max Adamo's avatar Max Adamo
Browse files

add parameter to supply the number of months

parent d0c3fd6f
Branches
Tags
No related merge requests found
#!/usr/bin/env python3 #!/usr/bin/env python3
""" """Filter JSON data based on the last_analysis_date.
This script will list all projects in SonarQube
and sort them by last analysis date Usage:
filter_json.py --months=<n>
Options:
--months=<n> Show only projects with last_analysis_date less than n months ago.
""" """
import os import os
import json import json
import configparser import configparser
import requests import requests
import datetime
from docopt import docopt
sq_ini = os.path.expanduser('~/.config/sonarqube.ini') def filter_data(data, months):
filtered_data = []
for item in data:
date_str = item['last_analysis_date']
date_obj = datetime.datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S%z')
months_diff = (datetime.datetime.now(date_obj.tzinfo) - date_obj) // datetime.timedelta(days=30)
if months_diff > months:
filtered_data.append(item)
return filtered_data
if not os.path.isfile(sq_ini):
print(f''' if __name__ == '__main__':
# parse the command line arguments using docopt
args = docopt(__doc__)
sq_ini = os.path.expanduser('~/.config/sonarqube.ini')
if not os.path.isfile(sq_ini):
print(f'''
Config file not found Config file not found
Please create the config file {sq_ini} Please create the config file {sq_ini}
with the following content: with the following content:
...@@ -21,41 +42,53 @@ token = <your token> ...@@ -21,41 +42,53 @@ token = <your token>
You can generate a token in SonarQube under My Account > Security You can generate a token in SonarQube under My Account > Security
''') ''')
os.sys.exit(1) os.sys.exit(1)
config = configparser.RawConfigParser()
config.read(sq_ini)
sq_token = config.get('sq', 'token')
config = configparser.RawConfigParser() session = requests.Session()
config.read(sq_ini) session.auth = (sq_token, '')
sq_token = config.get('sq', 'token') req = session.get(
'https://sonarqube.software.geant.org/api/projects/search')
session = requests.Session() if req.status_code == 401:
session.auth = (sq_token, '') print('Error: Invalid token')
req = session.get('https://sonarqube.software.geant.org/api/projects/search') os.sys.exit(1)
if req.status_code == 401: if req.status_code != 200:
print('Error: Invalid token') print(f'Error: HTTP status code is {req.status_code}')
os.sys.exit(1) os.sys.exit(1)
if req.status_code != 200: proj_list = req.json()['components']
print(f'Error: HTTP status code is {req.status_code}') final_list = []
os.sys.exit(1)
for project in proj_list:
if 'lastAnalysisDate' in project:
final_list.append({
'project_name': project['name'],
'last_analysis_date': project['lastAnalysisDate']
})
else:
final_list.append({
'project_name': project['name'],
'last_analysis_date': '1970-01-01T01:00:00+0200'
})
proj_list = req.json()['components'] months = int(args['--months'])
final_list = [] # filter the data based on the last_analysis_date
filtered_list = filter_data(final_list, int(months))
for project in proj_list: sorted_projects = sorted(
if 'lastAnalysisDate' in project: filtered_list, key=lambda d: d['last_analysis_date']
final_list.append({ )
'project_name': project['name'], pretty = json.dumps(sorted_projects, indent=4)
'last_analysis_date': project['lastAnalysisDate']
})
else:
final_list.append({
'project_name': project['name'],
'last_analysis_date': '1970-01-01T01:00:00+0200'
})
sorted_projects = sorted(final_list, key=lambda d: d['last_analysis_date']) print(pretty)
pretty = json.dumps(sorted_projects, indent=4)
print(pretty) print('')
print(f'Number of projects: {len(sorted_projects)}')
print(
f'This is a list of projects that have not been analyzed in the last {months} months.')
print('')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment