def add_label_to_platform_version(parser, scenario, config, queries, search, log=None): """Update the Label field, based on the UCIS and E-Feature entries of the source platform""" preq_source_query = get_query('preq_source_query', queries, add_label_to_platform_version.__name__, params=scenario, log=log) areq_source_e_feature_query = get_query( 'areq_source_e_feature', queries, add_label_to_platform_version.__name__, params=scenario, log=log) log.logger.info( "Labeling source platform {splatform}, source android version {sversion} with {label}" .format_map(scenario)) verify = scenario['verify'] update = scenario['update'] log.logger.info("Verify is %s and Update is %s", verify, update) log.logger.info( "=================================================================") # -- Get and format it: jira = Jira(scenario['name'], search, log=log.logger) source_preq_scanned = 0 source_areq_scanned = 0 update_count = 0 added_count = 0 # -- Label preqs: if 'label_preq' not in scenario or scenario[ 'label_preq']: # e.g., copy_preq is undefined or copy_preq = True for source_preq in jira.do_query(preq_source_query): updated = False source_preq_scanned += 1 log.logger.info("Source: %s '%s'", source_preq.key, source_preq.fields.summary) if 'UPDATE_FIELDS' in scenario and scenario['UPDATE_FIELDS']: updated = update_labels(jira, scenario, source_preq, update, 0, log) if updated: update_count += 1 if scenario['createmax'] and update_count >= scenario['createmax']: break added_count += update_count update_count = 0 # -- copy source e-features to output # This keeps having an exception because the total number of items seems to be changing... if 'label_areq' not in scenario or scenario[ 'label_areq']: # e.g., copy_areq is undefined or copy_areq = True e_features = [ e_feature for e_feature in jira.do_query(areq_source_e_feature_query) ] for source_e_feature in e_features: updated = False source_areq_scanned += 1 log.logger.info("Source: %s '%s'", source_e_feature.key, source_e_feature.fields.summary) if 'UPDATE_FIELDS' in scenario and scenario['UPDATE_FIELDS']: updated = update_labels(jira, scenario, source_e_feature, update, 0, log) if updated: update_count += 1 if scenario['createmax'] and update_count >= scenario['createmax']: break added_count += update_count log.logger.info( "-----------------------------------------------------------------") log.logger.info("%s UCIS source entries were considered. ", source_preq_scanned) log.logger.info("%s E-Feature source entries were considered. ", source_areq_scanned) log.logger.info("%s labels added. ", added_count) log.logger.info("")
class LabelFromLabeledItems: def __init__(self, parser, scenario, config, queries, search, log=None, jira=None): self.parser = parser self.jira = jira self.scenario = scenario self.config = config self.queries = queries self.search = search self.logger = log.logger self.update_count = 0 self.added_count = 0 self.createmax = self.scenario[ 'createmax'] if 'createmax' in self.scenario else 0 self.verify = self.scenario[ 'verify'] if 'verify' in self.scenario else False self.update = self.scenario[ 'update'] if 'update' in self.scenario else False # -- Authenticate to Jira server self.jira = Jira(self.scenario['name'], self.search, self.logger) pass def update_labels_field(self, target_preq, target_field="Label", delete_labels=[], add_labels=[]): updated = False delete_list = [x for x in self.scenario['delete_labels']] \ if 'delete_labels' in self.scenario else delete_labels.copy() add_list = [x for x in self.scenario['add_labels']] \ if 'add_labels' in self.scenario else add_labels.copy() label_field_name = self.jira.get_field_name(target_field) label_field = getattr(target_preq.fields, label_field_name) result_labels = {x: x for x in label_field} if isinstance( label_field, list) else {} original_labels = result_labels.copy() # -- Remove labels in delete list from source_labels: for regex in delete_list: for key, item in result_labels.copy().items(): if search(regex, item): self.logger.info( "Removing label %s (found by regex '%s') from item %s", key, regex, target_preq.key) del result_labels[key] # -- amd insert those in the add list: for label in add_list: self.logger.info("Adding label %s to item %s", label, target_preq.key) result_labels[label] = label if result_labels != original_labels: update_fields = { 'labels': [key for key, item in result_labels.items()] } comment_text = ("The Label on %s was updated from %s to %s by {command}.\n" + "\n" + "%s").format_map(self.scenario) \ % (target_preq.key, [key for key,value in original_labels.items()], [key for key, value in result_labels.items()], self.scenario['comment'] if self.scenario['comment'] is not None else "") if self.update: # -- only update if we're going to change something... self.logger.info( "Updating %s with %s, was %s", target_preq.key, [key for key, value in result_labels.items()], [key for key, value in original_labels.items()]) target_preq.update(notify=False, fields=update_fields) self.jira.jira_client.add_comment(target_preq, comment_text) updated = True else: self.logger.info( "%s: NO LABEL UPDATE; labels change from from %s, to %s; Comment would be\n%s", target_preq.key, [key for key, value in original_labels.items()], [key for key, value in result_labels.items()], comment_text) if updated: self.update_count += 1 return self.update_count def label_from_labeled_items(self): """Find the labeled AREQ items and add the label to corresponding target items""" self.logger.info( "Labeling target platform {tplatform}, source android version {tversion} with {tlabel}" .format_map(self.scenario)) self.logger.info("Update is %s", self.update) self.logger.info( "=================================================================" ) # -- Query to get the list of source items with the target label items_with_label_query = get_query( 'items_with_label_query', self.queries, LabelFromLabeledItems.label_from_labeled_items.__name__, params=self.scenario, log=self.logger) items_with_label_query = items_with_label_query.format_map( self.scenario) # items_with_label_list = [item for item in self.jira.do_query(items_with_label_query)] # -- Note: parent Feature could occur 1 or more times. (Also zero, but that doesn't matter in this case) by_parent_key = { item.fields.parent.key: item for item in self.jira.do_query(items_with_label_query) } # -- Query to get the (total) list of potential targets targets_for_label_query = get_query( 'targets_for_label_query', self.queries, LabelFromLabeledItems.label_from_labeled_items.__name__, params=self.scenario, log=self.logger) targets_for_label_query = targets_for_label_query.format_map( self.scenario) target_item_list = [ item for item in self.jira.do_query(targets_for_label_query) ] # -- Build a list of E-Features that share a parent Feature that's labeled in the source query target_items_with_same_parent = [ item for item in target_item_list if item.fields.parent.key in by_parent_key ] for target in target_items_with_same_parent: self.update_labels_field(target, target_field="Labels", add_labels=[self.scenario['tlabel']]) if self.createmax and self.update_count >= self.createmax: break self.logger.info( "-----------------------------------------------------------------" ) self.logger.info("%s E-Feature labels were updated. ", self.update_count) self.logger.info("")
class ProjectToCSV: def __init__(self, parser, scenario, config, queries, search, log=None, jira=None): self.parser = parser self.jira = jira self.scenario = scenario self.config = config self.queries = queries self.search = search self.logger = log.logger self.update_count = 0 self.added_count = 0 self.createmax = self.scenario[ 'createmax'] if 'createmax' in self.scenario else 0 self.verify = self.scenario[ 'verify'] if 'verify' in self.scenario else False self.update = self.scenario[ 'update'] if 'update' in self.scenario else False # -- Authenticate to Jira server self.jira = Jira(self.scenario['name'], self.search, self.logger) pass def run(self): """Iterate platforms, then items found by platform query""" for key, platform in self.scenario['platforms'].items(): self.logger.info(f"Processing {platform['splatform']}") query_values = { 'sproject': '"' + platform['sproject'] + '"', 'splatform': '"' + platform['splatform'] + '"', 'sexists_on': '"' + platform['sexists_on'] + '"', 'sversion': platform['sversion'] } field_list = platform['fields'] items_query = get_query('items_query', self.queries, __name__, params=query_values, log=self.logger) # items_query = items_query.format_map(query_values) with open(platform['splatform'] + '.csv', "w") as f: f.write(f"{platform['splatform']}\n") f.write(f"\n") item_count = 0 for item in self.jira.do_query(items_query): value_list = [] for field_name in field_list: if hasattr(item, field_name): local_value = getattr(item, field_name) elif hasattr(item.fields, field_name): local_value = getattr(item.fields, field_name) else: local_name = self.jira.get_field_name(field_name) if hasattr(item, local_name): local_value = getattr(item, local_name) elif hasattr(item.fields, local_name): local_value = getattr(item.fields, local_name) else: local_value = None value_list.append(local_value) result = ",".join(value_list) f.write(f"{result}\n") item_count += 1 self.logger.info( f"{item_count} entries written to {platform['splatform']}")