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
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
"""
This script will list all projects in SonarQube
and sort them by last analysis date
"""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
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
Please create the config file {sq_ini}
with the following content:
......@@ -21,41 +42,53 @@ token = <your token>
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()
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')
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 == 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)
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'
})
proj_list = req.json()['components']
final_list = []
months = int(args['--months'])
# filter the data based on the last_analysis_date
filtered_list = filter_data(final_list, int(months))
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'
})
sorted_projects = sorted(
filtered_list, key=lambda d: d['last_analysis_date']
)
pretty = json.dumps(sorted_projects, indent=4)
sorted_projects = sorted(final_list, key=lambda d: d['last_analysis_date'])
pretty = json.dumps(sorted_projects, indent=4)
print(pretty)
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