|
| 1 | +""" |
| 2 | +Created on july 11, 2024 |
| 3 | +@author: Dinesh Ravi |
| 4 | +Gather list of non matched components where blackduck could not able have match event with their kb for the bdio codelocation type |
| 5 | +""" |
| 6 | +from blackduck import Client |
| 7 | + |
| 8 | +import argparse |
| 9 | + |
| 10 | +import logging |
| 11 | +import json |
| 12 | +# py get_scan_missed_import_event.py --base-url=https://blackduck.company.com --token-file=.pt --project=ASTERIX2CLU3D_PR |
| 13 | +# OGRAM --version=AED2_ANDROID_S_2_2024-09-21_00-32 --company=company --no-verify > missing.txt |
| 14 | +logging.basicConfig( |
| 15 | + level=logging.INFO, |
| 16 | + format="[%(asctime)s] {%(module)s:%(lineno)d} %(levelname)s - %(message)s" |
| 17 | +) |
| 18 | + |
| 19 | +parser = argparse.ArgumentParser("Get the BOM components for a given project-version and the license details for each BOM component") |
| 20 | +parser.add_argument("--base-url", required=True, help="Hub server URL e.g. https://your.blackduck.url") |
| 21 | +parser.add_argument("--token-file", dest='token_file', required=True, help="containing access token") |
| 22 | +parser.add_argument("--project", dest='project_name', required=True, help="Project that contains the BOM components") |
| 23 | +parser.add_argument("--version", dest='version_name', required=True, help="Version that contains the BOM components") |
| 24 | +parser.add_argument("--company", dest='company_name', required=True, help="modules that contains the company name for separation") |
| 25 | +parser.add_argument("--no-verify", dest='verify', action='store_false', help="disable TLS certificate verification") |
| 26 | +args = parser.parse_args() |
| 27 | + |
| 28 | +with open(args.token_file, 'r') as tf: |
| 29 | + access_token = tf.readline().strip() |
| 30 | + |
| 31 | +bd = Client(base_url=args.base_url, token=access_token, verify=args.verify) |
| 32 | + |
| 33 | +params = { |
| 34 | + 'q': [f"name:{args.project_name}"] |
| 35 | +} |
| 36 | + |
| 37 | +projects = [p for p in bd.get_resource('projects', params=params) if p['name'] == args.project_name] |
| 38 | +assert len(projects) == 1, f"There should be one, and only one project named {args.project_name}. We found {len(projects)}" |
| 39 | +project = projects[0] |
| 40 | + |
| 41 | +params = { |
| 42 | + 'q': [f"versionName:{args.version_name}"] |
| 43 | +} |
| 44 | +versions = [v for v in bd.get_resource('versions', project, params=params) if v['versionName'] == args.version_name] |
| 45 | +assert len(versions) == 1, f"There should be one, and only one version named {args.version_name}. We found {len(versions)}" |
| 46 | +version = versions[0] |
| 47 | + |
| 48 | +print(f"Found {project['name']}:{version['versionName']}") |
| 49 | +hashset_company = set() |
| 50 | +hashset_other = set() |
| 51 | +# =================== |
| 52 | +# print(version) |
| 53 | +# params = { |
| 54 | +# 'q': [f"name:*bdio*"] |
| 55 | +# } |
| 56 | +codelocations=[codelocation for codelocation in bd.get_resource('codelocations',version) if 'bdio' in codelocation['name']] |
| 57 | +# print(codelocations[0]) |
| 58 | +# codelocation=codelocations[0] |
| 59 | +for codelocation in codelocations: |
| 60 | + scans=bd.get_resource('scans',codelocation) |
| 61 | + for scan in scans: |
| 62 | + events=[events for events in bd.get_resource('component-import-events',scan) if events['event']=="COMPONENT_MAPPING_FAILED"] |
| 63 | + if len(events)>0: |
| 64 | + print("============================") |
| 65 | + print(f"codelocation_name: {codelocation['name']}") |
| 66 | + print(f"matchCount: {scan['matchCount']}") |
| 67 | + print(f"missing: {len(events)}") |
| 68 | + |
| 69 | + for i,event in enumerate(events,start=1): |
| 70 | + print(f"--------------{i}") |
| 71 | + externalId=event['externalId'] |
| 72 | + print(f"externalId: {externalId}") |
| 73 | + print(f"importComponentName: {event['importComponentName']}") |
| 74 | + print(f"importComponentVersionName: {event['importComponentVersionName']}") |
| 75 | + if args.company_name in externalId: |
| 76 | + hashset_company.add(externalId) |
| 77 | + else: |
| 78 | + hashset_other.add(externalId) |
| 79 | + |
| 80 | + # {'event': 'COMPONENT_MAPPING_FAILED', |
| 81 | + # 'importComponentName': 'rsi-common-lib', |
| 82 | + # 'importComponentVersionName': '0.2.14', |
| 83 | + # 'externalId': 'com.company.aed2:rsi-common-lib:0.2.14', |
| 84 | + # 'failureReason': 'Unable to map scanned component version to Black Duck project version because no mapping is present for the given external identifier'} |
| 85 | +print("============================") |
| 86 | +sorted_company=sorted(hashset_company) |
| 87 | +sorted_other=sorted(hashset_other) |
| 88 | + |
| 89 | +print(f"========total missing Other components to get foss report: {len(hashset_other)}========") |
| 90 | +for i,missing in enumerate(sorted_other,start=1): |
| 91 | + print(f"{i} {missing}") |
| 92 | +print(f"========total missing {args.company_name} components to manually add: {len(hashset_company)}========") |
| 93 | +for i,missing in enumerate(sorted_company,start=1): |
| 94 | + print(f"{i} {missing}") |
| 95 | +total_missing=len(sorted_company)+len(sorted_other) |
| 96 | +print(f"========EB:{len(sorted_company)}+Other:{len(sorted_other)}={total_missing}========") |
0 commit comments