Skip to content
Snippets Groups Projects
Unverified Commit 8078844c authored by Max Adamo's avatar Max Adamo
Browse files

change ini file structure

parent 86239745
No related branches found
No related tags found
No related merge requests found
...@@ -2,16 +2,19 @@ ...@@ -2,16 +2,19 @@
"""Filter JSON data based on the last_analysis_date. """Filter JSON data based on the last_analysis_date.
Usage: Usage:
sq_projects_list.py --months=<n> sq_projects_list.py --months=<N>
0 or negative number means all projects
Options: 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 os
import json import json
import configparser import configparser
import requests
import datetime import datetime
import requests
from docopt import docopt from docopt import docopt
...@@ -21,7 +24,9 @@ def filter_data(data, months_nr): ...@@ -21,7 +24,9 @@ def filter_data(data, months_nr):
for item in data: for item in data:
date_str = item['last_analysis_date'] date_str = item['last_analysis_date']
date_obj = datetime.datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S%z') 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: if months_diff > months_nr:
filtered_data.append(item) filtered_data.append(item)
return filtered_data return filtered_data
...@@ -30,6 +35,7 @@ def filter_data(data, months_nr): ...@@ -30,6 +35,7 @@ def filter_data(data, months_nr):
if __name__ == '__main__': if __name__ == '__main__':
# parse the command line arguments using docopt # parse the command line arguments using docopt
args = docopt(__doc__) args = docopt(__doc__)
months = args['--months']
sq_ini = os.path.expanduser('~/.config/sonarqube.ini') sq_ini = os.path.expanduser('~/.config/sonarqube.ini')
if not os.path.isfile(sq_ini): if not os.path.isfile(sq_ini):
...@@ -40,6 +46,7 @@ with the following content: ...@@ -40,6 +46,7 @@ with the following content:
[sq] [sq]
token = <your token> token = <your token>
host = sonarqube.example.org
You can generate a token in SonarQube under My Account > Security 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 ...@@ -48,10 +55,11 @@ You can generate a token in SonarQube under My Account > Security
config = configparser.RawConfigParser() config = configparser.RawConfigParser()
config.read(sq_ini) config.read(sq_ini)
sq_token = config.get('sq', 'token') sq_token = config.get('sq', 'token')
sq_host = config.get('sq', 'host')
session = requests.Session() session = requests.Session()
session.auth = (sq_token, '') 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: if req.status_code == 401:
print('Error: Invalid token') print('Error: Invalid token')
...@@ -76,7 +84,6 @@ You can generate a token in SonarQube under My Account > Security ...@@ -76,7 +84,6 @@ You can generate a token in SonarQube under My Account > Security
'last_analysis_date': '1970-01-01T01:00:00+0200' 'last_analysis_date': '1970-01-01T01:00:00+0200'
}) })
months = int(args['--months'])
# filter the data based on the last_analysis_date # filter the data based on the last_analysis_date
filtered_list = filter_data(final_list, int(months)) filtered_list = filter_data(final_list, int(months))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment