Skip to content

Commit 4582c73

Browse files
committed
unmap all codelocations from a project version example
1 parent 51d1513 commit 4582c73

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'''
2+
Created on Jan 18, 2024
3+
4+
@author: kumykov
5+
6+
Unmap codelocations from a project version
7+
8+
'''
9+
10+
from blackduck import Client
11+
12+
import argparse
13+
import json
14+
import logging
15+
import sys
16+
import time
17+
from pprint import pprint
18+
19+
logging.basicConfig(
20+
level=logging.DEBUG,
21+
format="[%(asctime)s] {%(module)s:%(lineno)d} %(levelname)s - %(message)s"
22+
)
23+
24+
parser = argparse.ArgumentParser(sys.argv[0])
25+
parser.add_argument("-u", "--bd-url", help="Hub server URL e.g. https://your.blackduck.url")
26+
parser.add_argument("-t", "--token-file", help="File name of a file containing access token")
27+
parser.add_argument("-nv", '--no-verify', dest='verify', action='store_false', help="disable TLS certificate verification")
28+
parser.add_argument("project_name")
29+
parser.add_argument("version_name")
30+
31+
args = parser.parse_args()
32+
33+
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)
34+
logging.getLogger("requests").setLevel(logging.WARNING)
35+
logging.getLogger("urllib3").setLevel(logging.WARNING)
36+
logging.getLogger("blackduck").setLevel(logging.WARNING)
37+
38+
with open(args.token_file, 'r') as tf:
39+
access_token = tf.readline().strip()
40+
41+
bd = Client(base_url=args.bd_url, token=access_token, verify=args.verify)
42+
43+
params = {
44+
'q': [f"name:{args.project_name}"]
45+
}
46+
projects = [p for p in bd.get_resource('projects', params=params) if p['name'] == args.project_name]
47+
assert len(projects) == 1, f"There should be one, and only one project named {args.project_name}. We found {len(projects)}"
48+
project = projects[0]
49+
50+
params = {
51+
'q': [f"versionName:{args.version_name}"]
52+
}
53+
versions = [v for v in bd.get_resource('versions', project, params=params) if v['versionName'] == args.version_name]
54+
assert len(versions) == 1, f"There should be one, and only one version named {args.version_name}. We found {len(versions)}"
55+
version = versions[0]
56+
57+
logging.debug(f"Found {project['name']}:{version['versionName']}")
58+
59+
codelocations = bd.get_resource('codelocations', version)
60+
61+
for codelocation in codelocations:
62+
logging.debug(f"Un-mapping code location {codelocation['name']}")
63+
url = codelocation['_meta']['href']
64+
codelocation['mappedProjectVersion'] = None
65+
result = bd.session.put(url, json=codelocation)
66+
logging.info(f"Code location '{codelocation['name']}' unmap status {result}")

0 commit comments

Comments
 (0)