Skip to content

Commit 46e66a6

Browse files
authored
Create remap_codelocations.py
1 parent bd36209 commit 46e66a6

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"""
2+
Created on july 11, 2024
3+
4+
@author: Dinesh Ravi
5+
6+
Remap codelocations from a project version to another 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(
26+
"-u", "--bd_url", help="Hub server URL e.g. https://your.blackduck.url"
27+
)
28+
parser.add_argument(
29+
"-t", "--token-file", help="File name of a file containing access token"
30+
)
31+
parser.add_argument(
32+
"-nv",
33+
"--no-verify",
34+
dest="verify",
35+
action="store_false",
36+
help="disable TLS certificate verification",
37+
)
38+
parser.add_argument("project_name")
39+
parser.add_argument("version_name")
40+
parser.add_argument("update_pv_url")
41+
42+
43+
args = parser.parse_args()
44+
45+
logging.basicConfig(
46+
format="%(asctime)s:%(levelname)s:%(message)s",
47+
stream=sys.stderr,
48+
level=logging.DEBUG,
49+
)
50+
logging.getLogger("requests").setLevel(logging.WARNING)
51+
logging.getLogger("urllib3").setLevel(logging.WARNING)
52+
logging.getLogger("blackduck").setLevel(logging.WARNING)
53+
54+
with open(args.token_file, "r") as tf:
55+
access_token = tf.readline().strip()
56+
57+
bd = Client(base_url=args.bd_url, token=access_token, verify=args.verify)
58+
59+
params = {"q": [f"name:{args.project_name}"]}
60+
projects = [
61+
p
62+
for p in bd.get_resource("projects", params=params)
63+
if p["name"] == args.project_name
64+
]
65+
assert (
66+
len(projects) == 1
67+
), f"There should be one, and only one project named {args.project_name}. We found {len(projects)}"
68+
project = projects[0]
69+
70+
params = {"q": [f"versionName:{args.version_name}"]}
71+
versions = [
72+
v
73+
for v in bd.get_resource("versions", project, params=params)
74+
if v["versionName"] == args.version_name
75+
]
76+
assert (
77+
len(versions) == 1
78+
), f"There should be one, and only one version named {args.version_name}. We found {len(versions)}"
79+
version = versions[0]
80+
81+
logging.debug(f"Found {project['name']}:{version['versionName']}")
82+
83+
84+
codelocations = bd.get_resource("codelocations", version)
85+
# logging.info(f"Total Code locations '{len(list(codelocations))}'")
86+
for codelocation in codelocations:
87+
logging.debug(f"Un-mapping code location {codelocation['name']}")
88+
url = codelocation["_meta"]["href"]
89+
codelocation["mappedProjectVersion"] = args.update_pv_url
90+
result = bd.session.put(url, json=codelocation)
91+
logging.info(f"Code location '{codelocation['name']}' unmap status {result}")

0 commit comments

Comments
 (0)