1
+ #!/usr/bin/env python
2
+
3
+ '''
4
+ Copyright (C) 2023 Synopsys, Inc.
5
+ http://www.blackducksoftware.com/
6
+
7
+ Licensed to the Apache Software Foundation (ASF) under one
8
+ or more contributor license agreements. See the NOTICE file
9
+ distributed with this work for additional information
10
+ regarding copyright ownership. The ASF licenses this file
11
+ to you under the Apache License, Version 2.0 (the
12
+ "License"); you may not use this file except in compliance
13
+ with the License. You may obtain a copy of the License at
14
+
15
+ http://www.apache.org/licenses/LICENSE-2.0
16
+
17
+ Unless required by applicable law or agreed to in writing,
18
+ software distributed under the License is distributed on an
19
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20
+ KIND, either express or implied. See the License for the
21
+ specific language governing permissions and limitations
22
+ under the License.
23
+
24
+ List projects and project owners
25
+
26
+ usage: list_projects.py [-h] -u BASE_URL -t TOKEN_FILE [-nv]
27
+
28
+ options:
29
+ -h, --help show this help message and exit
30
+ -u BASE_URL, --base-url BASE_URL
31
+ Hub server URL e.g. https://your.blackduck.url
32
+ -t TOKEN_FILE, --token-file TOKEN_FILE
33
+ containing access token
34
+ -nv, --no-verify disable TLS certificate verification
35
+
36
+ '''
37
+ import argparse
38
+ import json
39
+ import logging
40
+ import sys
41
+ import arrow
42
+ import csv
43
+
44
+ from blackduck import Client
45
+ from pprint import pprint
46
+
47
+ parser = argparse .ArgumentParser ("List projects and project owners" )
48
+ parser .add_argument ("-u" , "--base-url" , required = True , help = "Hub server URL e.g. https://your.blackduck.url" )
49
+ parser .add_argument ("-t" , "--token-file" , dest = 'token_file' , required = True , help = "containing access token" )
50
+ parser .add_argument ("-nv" , "--no-verify" , dest = 'verify' , action = 'store_false' , help = "disable TLS certificate verification" )
51
+ args = parser .parse_args ()
52
+
53
+ logging .basicConfig (format = '%(asctime)s:%(levelname)s:%(message)s' , stream = sys .stderr , level = logging .DEBUG )
54
+ logging .getLogger ("requests" ).setLevel (logging .WARNING )
55
+ logging .getLogger ("urllib3" ).setLevel (logging .WARNING )
56
+ logging .getLogger ("blackduck" ).setLevel (logging .WARNING )
57
+
58
+ with open (args .token_file , 'r' ) as tf :
59
+ access_token = tf .readline ().strip ()
60
+
61
+ bd = Client (
62
+ base_url = args .base_url ,
63
+ token = access_token ,
64
+ verify = args .verify
65
+ )
66
+
67
+ headers = {}
68
+ projects = bd .get_resource ('projects' )
69
+
70
+ def get_user_name (url ):
71
+ if url :
72
+ data = bd .get_json (url )
73
+ return (data ['userName' ])
74
+ else :
75
+ return None
76
+
77
+ fieldnames = ['Project Name' ,
78
+ 'Project Owner' ,
79
+ 'Project Created By' ,
80
+ 'Project Updated By' ]
81
+
82
+ file_name = 'projects_by_owners.csv'
83
+
84
+ with open (file_name , 'w' ) as output :
85
+ writer = csv .DictWriter (output , fieldnames = fieldnames )
86
+ writer .writeheader ()
87
+
88
+ for project in projects :
89
+ project_name = project ['name' ]
90
+ project_owner = project .get ('projectOwner' , None )
91
+ project_owner_name = get_user_name (project_owner )
92
+ project_created_by = project .get ('createdByUser' , None )
93
+ project_created_by_name = get_user_name (project_created_by )
94
+ project_updated_by = project .get ('updatedByUser' , None )
95
+ project_updated_by_name = get_user_name (project_updated_by )
96
+ project_users = bd .get_resource ('users' , project )
97
+ row = dict ()
98
+ row ['Project Name' ] = project_name
99
+ row ['Project Owner' ] = project_owner_name
100
+ row ['Project Created By' ] = project_created_by_name
101
+ row ['Project Updated By' ] = project_updated_by_name
102
+ writer .writerow (row )
103
+
104
+ logging .info (f"Output file { file_name } written" )
0 commit comments