diff --git a/sq_projects_list.py b/sq_projects_list.py new file mode 100644 index 0000000000000000000000000000000000000000..154947dbd0c879359304ccdec8c683f013049eb3 --- /dev/null +++ b/sq_projects_list.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +""" +This script will list all projects in SonarQube +and sort them by last analysis date +""" +import os +import json +import configparser +import requests + +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(allow_no_value=True) +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') + +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' + }) + +sorted_projects = sorted(final_list, key=lambda d: d['last_analysis_date']) +pretty = json.dumps(sorted_projects, indent=4) + +print(pretty)