|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +''' |
| 4 | +Created on July 27, 2020 |
| 5 | +
|
| 6 | +@author: gsnyder |
| 7 | +
|
| 8 | +Set (or clear) the component adjustments flag on projects |
| 9 | +
|
| 10 | +''' |
| 11 | + |
| 12 | +import argparse |
| 13 | +import json |
| 14 | +import logging |
| 15 | +import sys |
| 16 | + |
| 17 | +from blackduck.HubRestApi import HubInstance, object_id |
| 18 | + |
| 19 | +parser = argparse.ArgumentParser("Set (or clear) the component adjustments flag on project(s)") |
| 20 | +parser.add_argument("-p", "--project", help="If supplied, set (or clear) the component adjustments flag for a specific project. Otherwise, perform the action on all projects on the system.") |
| 21 | +parser.add_argument("-c", "--clear", action='store_true', help="Default action is to set the component adjustments flag on the project (or all projects). Use this flag to clear the component adjustments flag") |
| 22 | +parser.add_argument("-t", "--test", action='store_true', help="Test mode. Will only list the projects and action to be taken") |
| 23 | + |
| 24 | +args = parser.parse_args() |
| 25 | + |
| 26 | +logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG) |
| 27 | +logging.getLogger("requests").setLevel(logging.WARNING) |
| 28 | +logging.getLogger("urllib3").setLevel(logging.WARNING) |
| 29 | + |
| 30 | +hub = HubInstance() |
| 31 | + |
| 32 | +if args.project: |
| 33 | + projects = [hub.get_project_by_name(args.project)] |
| 34 | +else: |
| 35 | + projects = hub.get_projects().get('items', []) |
| 36 | + |
| 37 | +action = "Setting" if not args.clear else "Clearing" |
| 38 | +project_names = ",".join([p['name'] for p in projects]) |
| 39 | +logging.debug(f"{action} the following projects: {project_names}") |
| 40 | + |
| 41 | +component_adjustments = not args.clear |
| 42 | +status = "set" if not args.clear else "cleared" |
| 43 | +for project in projects: |
| 44 | + if project['projectLevelAdjustments'] == component_adjustments: |
| 45 | + logging.debug(f"Project {project['name']}'s component adjustment flag is already {status}") |
| 46 | + else: |
| 47 | + project['projectLevelAdjustments'] = component_adjustments |
| 48 | + url = project['_meta']['href'] |
| 49 | + |
| 50 | + if not args.test: |
| 51 | + response = hub.execute_put(url, data=project) |
| 52 | + if response.status_code == 200: |
| 53 | + logging.info(f"Successfully {status} projectLevelAdjustments for project {project['name']}") |
| 54 | + else: |
| 55 | + logging.error(f"Failed to {status} projectLevelAdjustments for project {project['name']}, status code returned was {response.status_code}") |
| 56 | + else: |
| 57 | + logging.debug(f"Test mode: Would have {status} projectLevelAdjustments for project {project['name']}") |
0 commit comments