From 5ff9d46b55265a0c9a8afd53f037df8cb4ce5156 Mon Sep 17 00:00:00 2001
From: Massimiliano Adamo <massimiliano.adamo@geant.org>
Date: Wed, 26 Apr 2023 15:28:29 +0000
Subject: [PATCH] Add sq_projects_list.py

---
 sq_projects_list.py | 52 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
 create mode 100644 sq_projects_list.py

diff --git a/sq_projects_list.py b/sq_projects_list.py
new file mode 100644
index 0000000..154947d
--- /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)
-- 
GitLab