|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +''' |
| 4 | +Copyright (C) 2021 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 | +''' |
| 25 | +import argparse |
| 26 | +import json |
| 27 | +import logging |
| 28 | +import sys |
| 29 | + |
| 30 | +from blackduck import Client |
| 31 | + |
| 32 | +parser = argparse.ArgumentParser("Get custom field value from a project version") |
| 33 | +parser.add_argument("bd_url") |
| 34 | +parser.add_argument("user_api_token") |
| 35 | +parser.add_argument("project") |
| 36 | +parser.add_argument("version") |
| 37 | +parser.add_argument("field_label") |
| 38 | +parser.add_argument("-v", "--verify", default=False, help="Set this to verify the SSL certificate. In production you should always use this. In dev/test the default (False) is ok.") |
| 39 | +args = parser.parse_args() |
| 40 | + |
| 41 | + |
| 42 | +logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG) |
| 43 | +logging.getLogger("requests").setLevel(logging.WARNING) |
| 44 | +logging.getLogger("urllib3").setLevel(logging.WARNING) |
| 45 | +logging.getLogger("blackduck").setLevel(logging.WARNING) |
| 46 | + |
| 47 | +def get_project_version_by_name(project_name, version_name): |
| 48 | + params = { |
| 49 | + 'q': [f"name:{project_name}"] |
| 50 | + } |
| 51 | + projects = [p for p in bd.get_resource('projects', params=params) if p['name'] == project_name] |
| 52 | + assert len(projects) == 1, f"There should be one, and only one project named {project_name}. We found {len(projects)}" |
| 53 | + project = projects[0] |
| 54 | + |
| 55 | + params = { |
| 56 | + 'q': [f"versionName:{version_name}"] |
| 57 | + } |
| 58 | + versions = [v for v in bd.get_resource('versions', project, params=params) if v['versionName'] == version_name] |
| 59 | + assert len(versions) == 1, f"There should be one, and only one version named {version_name}. We found {len(versions)}" |
| 60 | + version = versions[0] |
| 61 | + return project, version |
| 62 | + |
| 63 | + |
| 64 | +bd = Client( |
| 65 | + base_url=args.bd_url, |
| 66 | + token=args.user_api_token, |
| 67 | + verify=args.verify |
| 68 | +) |
| 69 | + |
| 70 | +project, version = get_project_version_by_name(args.project, args.version) |
| 71 | +custom_fields = [cf for cf in bd.get_resource("custom-fields", version)] |
| 72 | + |
| 73 | +cfs = list(filter(lambda cf: cf['label'] == args.field_label, custom_fields)) |
| 74 | +assert len(cfs) == 1, f"We did not find the field labeled {args.field_label} or we found more than one and that shouldn't happen!" |
| 75 | +custom_field = cfs[0] |
| 76 | +print(f"Custom field {args.field_label} on project-version {args.project}-{args.version} has value(s) {custom_field['values']}") |
| 77 | +print(f"Refer to the BD REST API doc for more details on how to interact with the different custom field types, {bd.base_url}/api-doc/public.html#_reading_a_single_project_version_custom_field") |
| 78 | + |
0 commit comments