sq_projects_list.py 2.55 KiB
#!/usr/bin/env python3
"""Filter JSON data based on the 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 json
import configparser
import requests
import datetime
from docopt import docopt
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 __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
Please create the config file {sq_ini}
with the following content:
[sq]
token = <your token>
You can generate a token in SonarQube under My Account > Security
''')
os.sys.exit(1)
config = configparser.RawConfigParser()
config.read(sq_ini)
sq_token = config.get('sq', 'token')
session = requests.Session()
session.auth = (sq_token, '')
req = session.get(
'https://sonarqube.software.geant.org/api/projects/search')
if req.status_code == 401:
print('Error: Invalid token')
os.sys.exit(1)
if req.status_code != 200:
print(f'Error: HTTP status code is {req.status_code}')
os.sys.exit(1)
proj_list = req.json()['components']
final_list = []
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'
})
months = int(args['--months'])
# filter the data based on the last_analysis_date
filtered_list = filter_data(final_list, int(months))
sorted_projects = sorted(
filtered_list, key=lambda d: d['last_analysis_date']
)
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('')