1
+ '''
2
+ Created on Jan 29, 2024
3
+ @author: pedapati
4
+
5
+ Copyright (C) 2024 Synopsys, Inc.
6
+ https://www.synopsys.com
7
+
8
+ Licensed to the Apache Software Foundation (ASF) under one
9
+ or more contributor license agreements. See the NOTICE file
10
+ distributed with this work for additional information
11
+ regarding copyright ownership. The ASF licenses this file
12
+ to you under the Apache License, Version 2.0 (the
13
+ "License"); you may not use this file except in compliance
14
+ with the License. You may obtain a copy of the License at
15
+
16
+ http://www.apache.org/licenses/LICENSE-2.0
17
+
18
+ Unless required by applicable law or agreed to in writing,
19
+ software distributed under the License is distributed on an
20
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21
+ KIND, either express or implied. See the License for the
22
+ specific language governing permissions and limitations
23
+ under the License.
24
+
25
+ This program will Export KB & KB Modified Component Status updates from one Black Duck Server and Import onto another Black Duck Server
26
+ '''
27
+
28
+ from blackduck import Client
29
+
30
+ import requests
31
+ import argparse
32
+ import json
33
+ import logging
34
+ import sys
35
+ import time
36
+ from pprint import pprint
37
+
38
+
39
+ NAME = 'copy_kb_component_status_updates.py'
40
+ VERSION = '2024-01-29'
41
+
42
+ print (f'{ NAME } ({ VERSION } ). Copyright (c) 2023 Synopsys, Inc.' )
43
+
44
+
45
+ logging .basicConfig (
46
+ level = logging .DEBUG ,
47
+ format = "[%(asctime)s] {%(module)s:%(lineno)d} %(levelname)s - %(message)s"
48
+ )
49
+
50
+
51
+ logging .basicConfig (format = '%(asctime)s:%(levelname)s:%(message)s' , stream = sys .stderr , level = logging .DEBUG )
52
+ logging .getLogger ("requests" ).setLevel (logging .WARNING )
53
+ logging .getLogger ("urllib3" ).setLevel (logging .WARNING )
54
+ logging .getLogger ("blackduck" ).setLevel (logging .WARNING )
55
+
56
+
57
+ def parse_command_args ():
58
+
59
+ parser = argparse .ArgumentParser (sys .argv [0 ])
60
+ parser .add_argument ("-su" , "--source-bd-url" , required = True , help = "Source BD server URL to copy KB component adjustments from e.g. https://your.blackduck.url" )
61
+ parser .add_argument ("-st" , "--source-token-file" , required = True , help = "File containing Source BD access token" )
62
+ parser .add_argument ("-du" , "--dest-bd-url" , required = True , help = "Destination BD server URL to apply KB component adjustments to e.g. https://your.blackduck.url" )
63
+ parser .add_argument ("-dt" , "--dest-token-file" , required = True , help = "File containing Destination BD access token" )
64
+ parser .add_argument ("-nv" , "--no-verify" , action = 'store_false' , help = "Disable TLS certificate verification" )
65
+ return parser .parse_args ()
66
+
67
+
68
+ def main ():
69
+ args = parse_command_args ()
70
+ ## Step 1 Source Black Duck Server - Authentication
71
+ with open (args .source_token_file , 'r' ) as tf :
72
+ access_token = tf .readline ().strip ()
73
+ bd1 = Client (base_url = args .source_bd_url , token = access_token , verify = args .no_verify , timeout = 60.0 , retries = 4 )
74
+ ## Step 2 Destination Black Duck Server - Authentication
75
+ with open (args .dest_token_file , 'r' ) as tf :
76
+ access_token1 = tf .readline ().strip ()
77
+ bd2 = Client (base_url = args .dest_bd_url , token = access_token1 , verify = args .no_verify , timeout = 60.0 , retries = 4 )
78
+ ## Step 3 Source Black Duck Server - Get KB Components with Status Updates
79
+ headers = {'Accept' : 'application/vnd.blackducksoftware.internal-1+json' }
80
+ get_comp_url = f"{ bd1 .base_url } /api/components?filter=componentApprovalStatus%3Ain_review&filter=componentApprovalStatus%3Areviewed&filter=componentApprovalStatus%3Aapproved&filter=componentApprovalStatus%3Alimited_approval&filter=componentApprovalStatus%3Arejected&filter=componentApprovalStatus%3Adeprecated&filter=componentSource%3Akb_and_kb_modified&limit=25&offset=0"
81
+ get_comp_json = bd1 .session .get (get_comp_url , headers = headers ).json ()
82
+ total = str (get_comp_json ["totalCount" ])
83
+ print ("Found " + total + " KB components with status updates" )
84
+ print ()
85
+ for component in get_comp_json ["items" ]:
86
+ comp_name = component ['name' ]
87
+ # comp_url = component['url']
88
+ comp_status = component ['approvalStatus' ]
89
+ comp_url = component ['_meta' ]['href' ]
90
+ comp_id = comp_url .split ("/" )[- 1 ]
91
+ print ("Updating KB Component " + comp_name + " status to " + comp_status )
92
+ ## Step 4 Destination Black Duck Server - Update KB Components with Status Updates
93
+ headers = {'Content-Type' : 'application/vnd.blackducksoftware.component-detail-4+json' , 'Accept' : 'application/vnd.blackducksoftware.component-detail-4+json' }
94
+ put_data = {"name" : comp_name , "approvalStatus" : comp_status }
95
+ put_comp_url = f"{ bd2 .base_url } /api/components/{ comp_id } "
96
+ update_comp_results = bd2 .session .put (put_comp_url , headers = headers , data = json .dumps (put_data ))
97
+ if update_comp_results .status_code == 200 :
98
+ message = f"{ update_comp_results } "
99
+ print ("Successfully updated status of " + comp_name + " to " + comp_status )
100
+ print ()
101
+ else :
102
+ message = f"{ update_comp_results .json ()} "
103
+ print ("Updating status FAILED with error message:" )
104
+ print ()
105
+ logging .debug ({message })
106
+ print ()
107
+
108
+
109
+ if __name__ == "__main__" :
110
+ sys .exit (main ())
0 commit comments