Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SonarQube Scripts
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Geant DevOps
SonarQube Scripts
Commits
7e7bf468
Unverified
Commit
7e7bf468
authored
2 years ago
by
Max Adamo
Browse files
Options
Downloads
Patches
Plain Diff
add parameter to supply the number of months
parent
d0c3fd6f
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
sq_projects_list.py
+68
-35
68 additions, 35 deletions
sq_projects_list.py
with
68 additions
and
35 deletions
sq_projects_list.py
+
68
−
35
View file @
7e7bf468
#!/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
(
''
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment