Skip to content

Commit f634c70

Browse files
author
Glenn Snyder
committed
adding script to show how you can delete project-versions system wide based on age, excluding those project-versions whose phase is set to RELEASED or ARCHIVED
1 parent 27604d6 commit f634c70

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'''
2+
Created on Feb 2, 2021
3+
4+
@author: gsnyder
5+
6+
Find and delete older project-versions system-wide based on version age
7+
excluding versions whose phase is equal to RELEASED (or ARCHIVED)
8+
9+
Copyright (C) 2021 Synopsys, Inc.
10+
http://www.synopsys.com/
11+
12+
Licensed to the Apache Software Foundation (ASF) under one
13+
or more contributor license agreements. See the NOTICE file
14+
distributed with this work for additional information
15+
regarding copyright ownership. The ASF licenses this file
16+
to you under the Apache License, Version 2.0 (the
17+
"License"); you may not use this file except in compliance
18+
with the License. You may obtain a copy of the License at
19+
20+
http://www.apache.org/licenses/LICENSE-2.0
21+
22+
Unless required by applicable law or agreed to in writing,
23+
software distributed under the License is distributed on an
24+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
25+
KIND, either express or implied. See the License for the
26+
specific language governing permissions and limitations
27+
under the License.
28+
'''
29+
import argparse
30+
import logging
31+
import sys
32+
33+
from blackduck.HubRestApi import HubInstance
34+
35+
import arrow
36+
37+
excluded_phases_defaults = ['RELEASED', 'ARCHIVED']
38+
39+
parser = argparse.ArgumentParser("Find and delete older project-versions system-wide based on age unless they are marked as RELEASED or ARCHIVED")
40+
# parser.add_argument("project_name")
41+
parser.add_argument("-e", "--excluded_phases", default=excluded_phases_defaults, help=f"Set the phases to exclude from deletion (defaults to {excluded_phases_defaults})")
42+
parser.add_argument("-a", "--age", type=int, help=f"Project-versions older than this age (days) will be deleted unless their phase is in the list of excluded phases ({excluded_phases_defaults})")
43+
parser.add_argument("-d", "--delete", action='store_true', help=f"Because this script can, and will, delete project-versions we require the caller to explicitly ask to delete things. Otherwise, the script runs in a 'test mode' and just says what it would do.")
44+
args = parser.parse_args()
45+
46+
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(module)s: %(message)s', stream=sys.stderr, level=logging.DEBUG)
47+
logging.getLogger("requests").setLevel(logging.WARNING)
48+
logging.getLogger("urllib3").setLevel(logging.WARNING)
49+
logging.getLogger("blackduck.HubRestApi").setLevel(logging.WARNING)
50+
51+
hub = HubInstance()
52+
53+
projects = hub.get_projects(limit=9999).get('items', [])
54+
55+
for project in projects:
56+
versions = hub.get_project_versions(project, limit=9999)
57+
sorted_versions = sorted(versions['items'], key = lambda i: i['createdAt'])
58+
59+
un_released_versions = list(filter(lambda v: v['phase'] not in args.excluded_phases, sorted_versions))
60+
logging.debug(f"Found {len(un_released_versions)} versions in project {project['name']} which are not in phase RELEASED or ARCHIVED")
61+
62+
for version in un_released_versions:
63+
version_age = (arrow.now() - arrow.get(version['createdAt'])).days
64+
if version_age > args.age:
65+
if args.delete:
66+
# TODO: What to do if/when this is the last version in a project to avoid having empty projects being left around
67+
logging.debug(f"Deleting version {version['versionName']} from project {project['name']} cause it is {version_age} days old which is greater than {args.age} days")
68+
url = version['_meta']['href']
69+
response = hub.execute_delete(url)
70+
if response.status_code == 204:
71+
logging.info(f"Successfully deleted version {version['versionName']} from project {project['name']}")
72+
else:
73+
logging.error(f"Failed to delete version {version['versionName']} from project {project['name']}. status code {response.status_code}")
74+
else:
75+
logging.debug(f"In test-mode. Version {version['versionName']} from project {project['name']} would be deleted cause it is {version_age} days old which is greater than {args.age} days. Use '--delete' to actually delete it.")
76+
else:
77+
logging.debug(f"Version {version['versionName']} from project {project['name']} will be retained because it is {version_age} days old which is less or equal to the threshold of {args.age} days")
78+
79+

0 commit comments

Comments
 (0)