Terms Of Use

Privacy Policy

Featured snippets are MIT license

Gears & Masters

DevOpsnipp.com © 2020

medium.png

delete_old_sonar_projects.py

Deletes old sonar projects (last analysis date older than 60 days)

2

Votes

import json
import requests
import iso8601
import pytz
from datetime import datetime, timedelta


url_projects = 'http://srvpic.intra.sada.fr:9000/sonar/api/resources'
url_delete = 'http://srvpic.intra.sada.fr:9000/sonar/api/projects/destroy'

params = dict(
    qualifiers='TRK',
    format='json'
)

resp = requests.get(url=url_projects, params=params)
data = json.loads(resp.text)
project_keep_date = datetime.now(pytz.UTC) - timedelta(days=60)

for project in data:
    last_analysis_date = iso8601.parse_date(project['date'])
    if ((last_analysis_date < project_keep_date) and (project['branch'] != 'master')):
        delete_params = dict(
            id=project['id']
        )
        result = requests.post(url=url_delete, params=delete_params, auth=('admin', 'admin'))

Vote Here

You must earn at least 1 vote on your snippets to be allowed to vote