Skip to content

Commit a95a204

Browse files
author
Eric Lei
committed
Add feature to backup and delete multiple projects
1 parent 4014259 commit a95a204

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

blackduck/HubRestApi.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ def delete_project_version_by_name(self, project_name, version_name, save_scans=
851851
else:
852852
logging.debug("Did not find project with name {}".format(project_name))
853853

854-
def delete_project_by_name(self, project_name, save_scans=False):
854+
def delete_project_by_name(self, project_name, save_scans=False, backup_scans=False):
855855
project = self.get_project_by_name(project_name)
856856
if project:
857857
# get project versions
@@ -865,6 +865,9 @@ def delete_project_by_name(self, project_name, save_scans=False):
865865
if delete_scans:
866866
# delete all code locations associated with each version
867867
for version in versions:
868+
if backup_scans:
869+
logging.debug("Backup code locations (aka scans) for version {}".format(version['versionName']))
870+
self.download_project_scans(project_name, version['versionName'])
868871
logging.debug("Deleting code locations (aka scans) for version {}".format(version['versionName']))
869872
self.delete_project_version_codelocations(version)
870873

@@ -1245,7 +1248,9 @@ def download_project_scans(self, project_name,version_name, output_folder=None):
12451248
if output_folder:
12461249
pathname = os.path.join(output_folder, filename)
12471250
else:
1248-
pathname = filename
1251+
if not os.path.exists(project_name):
1252+
os.mkdir(project_name)
1253+
pathname = os.path.join(project_name, filename)
12491254
responce = requests.get(url, headers=self.get_headers(), stream=True, verify=False)
12501255
with open(pathname, "wb") as f:
12511256
for data in responce.iter_content():

examples/clean_project.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"clean_project" : [
3+
"proftpd",
4+
"libiec"
5+
]
6+
}

examples/delete_project.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
parser = argparse.ArgumentParser("A program that will delete a project along with its scans")
1717
parser.add_argument("project", help="Project name")
1818
parser.add_argument("-k", "--keep_scans", action = 'store_true', default=False, help="Use this option if you want to keep scans associated with the project-versions. Default is False, scans will be deleted.")
19+
parser.add_argument("-b", "--backup_scans", action = 'store_true', default=False, help="Use this option if you want to backup scans associated with the project-versions. Default is False, scans will not be backuped.")
1920
args = parser.parse_args()
2021

2122
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
@@ -24,4 +25,4 @@
2425

2526
hub = HubInstance()
2627

27-
hub.delete_project_by_name(args.project, save_scans=args.keep_scans)
28+
hub.delete_project_by_name(args.project, save_scans=args.keep_scans, backup_scans=args.backup_scans)

examples/delete_projects.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
Created on Nov 8, 2019
3+
4+
@author: ylei
5+
6+
Delete multiple projects and their scans. The project names to be deleted are kept in clean_project.json
7+
'''
8+
9+
import argparse
10+
import csv
11+
import logging
12+
import sys
13+
import json
14+
import arrow
15+
16+
from blackduck.HubRestApi import HubInstance
17+
18+
parser = argparse.ArgumentParser("A program that will delete projects along with their scans")
19+
#parser.add_argument("project", help="Project name")
20+
parser.add_argument("-f", "--filename", default="clean_project.json", help="File to keep Project names")
21+
parser.add_argument("-k", "--keep_scans", action = 'store_true', default=False, help="Use this option if you want to keep scans associated with the project-versions. Default is False, scans will be deleted.")
22+
parser.add_argument("-b", "--backup_scans", action = 'store_true', default=False, help="Use this option if you want to backup scans associated with the project-versions. Default is False, scans will not be backuped.")
23+
parser.add_argument("-a", "--age", default=3, help="The age, in days. If a project is older than this age it will be deleted.")
24+
args = parser.parse_args()
25+
26+
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
27+
logging.getLogger("requests").setLevel(logging.WARNING)
28+
logging.getLogger("urllib3").setLevel(logging.WARNING)
29+
30+
hub = HubInstance()
31+
32+
filename = args.filename
33+
with open(filename, 'r+') as f:
34+
d = json.load(f)
35+
print("Projects in maintain json file: ", d['clean_project'])
36+
for proj in d['clean_project']:
37+
now = arrow.now()
38+
clean_time = now.to('local').shift(days=-args.age)
39+
project = hub.get_project_by_name(proj)
40+
41+
if clean_time > arrow.get(project['createdAt']).to('local'):
42+
print("Project ", project['name'], ' last for more than 3 days. Will be backup abd deleted.')
43+
hub.delete_project_by_name(proj, save_scans=args.keep_scans, backup_scans=args.backup_scans)

0 commit comments

Comments
 (0)