From 8078844cee63e94efdf0d3a078b54179e1792e0c Mon Sep 17 00:00:00 2001
From: Massimiliano Adamo <maxadamo@gmail.com>
Date: Thu, 27 Apr 2023 11:10:53 +0200
Subject: [PATCH] change ini file structure

---
 sq_projects_list.py | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/sq_projects_list.py b/sq_projects_list.py
index f57f880..327e45e 100755
--- a/sq_projects_list.py
+++ b/sq_projects_list.py
@@ -2,16 +2,19 @@
 """Filter JSON data based on the last_analysis_date.
 
 Usage:
-    sq_projects_list.py --months=<n>
+    sq_projects_list.py --months=<N>
+
+    0 or negative number means all projects
 
 Options:
-    --months=<n>   Show only projects with last_analysis_date older than n months ago.
+    -h --help     Show this help screen.
+    --months=<N>  Show only projects with last_analysis_date older than N months ago.
 """
 import os
 import json
 import configparser
-import requests
 import datetime
+import requests
 from docopt import docopt
 
 
@@ -21,7 +24,9 @@ def filter_data(data, months_nr):
     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)
+        months_diff = (
+            datetime.datetime.now(date_obj.tzinfo) - date_obj
+            ) // datetime.timedelta(days=30)
         if months_diff > months_nr:
             filtered_data.append(item)
     return filtered_data
@@ -30,6 +35,7 @@ def filter_data(data, months_nr):
 if __name__ == '__main__':
     # parse the command line arguments using docopt
     args = docopt(__doc__)
+    months = args['--months']
     sq_ini = os.path.expanduser('~/.config/sonarqube.ini')
 
     if not os.path.isfile(sq_ini):
@@ -40,6 +46,7 @@ with the following content:
 
 [sq]
 token = <your token>
+host = sonarqube.example.org
 
 You can generate a token in SonarQube under My Account > Security
 ''')
@@ -48,10 +55,11 @@ You can generate a token in SonarQube under My Account > Security
     config = configparser.RawConfigParser()
     config.read(sq_ini)
     sq_token = config.get('sq', 'token')
+    sq_host = config.get('sq', 'host')
 
     session = requests.Session()
     session.auth = (sq_token, '')
-    req = session.get('https://sonarqube.software.geant.org/api/projects/search')
+    req = session.get(f'https://{sq_host}/api/projects/search')
 
     if req.status_code == 401:
         print('Error: Invalid token')
@@ -76,7 +84,6 @@ You can generate a token in SonarQube under My Account > Security
                 '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))
 
-- 
GitLab