|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +################################################################################################# |
| 4 | +# |
| 5 | +# crbug.py -- Create a Bug, populate the Abundanza multi-select field in the 'Jira 7 Testing' |
| 6 | +# workspace |
| 7 | +# |
| 8 | +USAGE = """ |
| 9 | +Usage: crbug.py |
| 10 | +""" |
| 11 | +################################################################################################# |
| 12 | + |
| 13 | +import sys, os |
| 14 | +from pyral import Rally, rallyWorkset |
| 15 | + |
| 16 | +################################################################################################# |
| 17 | + |
| 18 | +errout = sys.stderr.write |
| 19 | + |
| 20 | +################################################################################################# |
| 21 | + |
| 22 | +def main(args): |
| 23 | + options = [opt for opt in args if opt.startswith('--')] |
| 24 | + args = [arg for arg in args if arg not in options] |
| 25 | + |
| 26 | + server, user, password, apikey, workspace, project = rallyWorkset(options) |
| 27 | + if apikey: |
| 28 | + rally = Rally(server, apikey=apikey, workspace=workspace, project=project) |
| 29 | + else: |
| 30 | + rally = Rally(server, user=user, password=password, workspace=workspace, project=project) |
| 31 | +# rally.enableLogging("rally.history.crbug") |
| 32 | + |
| 33 | + target_project = rally.getProject() |
| 34 | + target_entity_name = 'Defect' |
| 35 | + target_attribute_name = 'Abundanza' |
| 36 | + allowed_values = [] |
| 37 | + candidate_values = ['bosch', 'rybosome', 'stihl', 'snap-on'] |
| 38 | + value_refs = [] |
| 39 | + |
| 40 | + type_schema_attrs = rally.typedef(target_entity_name).Attributes |
| 41 | + ts_hits = [attr_schema for attr_schema in type_schema_attrs |
| 42 | + if attr_schema.ElementName == f'c_{target_attribute_name}'] |
| 43 | + if ts_hits: |
| 44 | + tsa = ts_hits[0] |
| 45 | + allowed_values = tsa.AllowedValues |
| 46 | + |
| 47 | + def notFound(target_value, allowed_values): |
| 48 | + hits = [aavs.value for aavs in allowed_values |
| 49 | + if aavs.value == target_value] |
| 50 | + return True if not hits else False |
| 51 | + |
| 52 | + if allowed_values: |
| 53 | + disallowed_values = [cand_val for cand_val in candidate_values |
| 54 | + if notFound(cand_val, allowed_values)] |
| 55 | + if disallowed_values: |
| 56 | + for dav in disallowed_values: |
| 57 | + print(f'WARNING: {dav} is not an allowed value for the {target_entity_name}.{target_attribute_name} field') |
| 58 | + candidate_values = list(set(candidate_values) - set(disallowed_values)) |
| 59 | + value_refs = getAllowedValueRefs(candidate_values, allowed_values) |
| 60 | + |
| 61 | + info = { |
| 62 | + "Project" : target_project.ref, |
| 63 | + "Name" : "Mushy fries sicken our customers", |
| 64 | + "State" : "Submitted", |
| 65 | + "ScheduleState" : "Defined", |
| 66 | + "Description" : "revolt is soon to arrive at your franchise", |
| 67 | + "Notes" : "I have really only done some daydreaming wrt this defect", |
| 68 | + #"Abundanza" : "bosch,stihl,snap-on" |
| 69 | + "Abundanza" : value_refs |
| 70 | + } |
| 71 | + |
| 72 | + print("Creating Defect ...") |
| 73 | + defect = rally.put('Defect', info) |
| 74 | + print("Created Defect: %s OID: %s" % (defect.FormattedID, defect.oid)) |
| 75 | + #abund = defect.Abundanza |
| 76 | + abund = defect.c_Abundanza |
| 77 | + if abund: |
| 78 | + abund = ", ".join([aav.value for aav in abund]) |
| 79 | + else: |
| 80 | + abund = '' |
| 81 | + |
| 82 | + print(f' Abundanza: {abund}') |
| 83 | + #print(repr(defect.__dict__)) |
| 84 | + #print(defect.details()) |
| 85 | + |
| 86 | +################################################################################################# |
| 87 | + |
| 88 | +def getAllowedValueRefs(specified_values, allowed_values_schema): |
| 89 | + """ |
| 90 | + Turn the specified values into refs to the corresponding |
| 91 | + allowedattributevalue items. |
| 92 | + have to return a structure like: |
| 93 | + [ |
| 94 | + {'_ref' : 'allowedattributevalue/875421254' }, |
| 95 | + {'_ref' : 'allowedattributevalue/875432439' }, |
| 96 | + ... |
| 97 | + ] |
| 98 | + """ |
| 99 | + ref_box = [] |
| 100 | + for aav_text in specified_values: |
| 101 | + hits = [aavs.ref for aavs in allowed_values_schema |
| 102 | + if aavs.value == aav_text] |
| 103 | + if hits: |
| 104 | + ref = hits[0] |
| 105 | + ref_box.append({'_ref' : ref}) |
| 106 | + return ref_box |
| 107 | + |
| 108 | +################################################################################################# |
| 109 | + |
| 110 | + |
| 111 | +def emptyDefect(): |
| 112 | + task = {'Workspace' : '', |
| 113 | + 'Project' : '', |
| 114 | + 'Name' : '', |
| 115 | + 'State' : '', |
| 116 | + 'ScheduleState' : '', |
| 117 | + } |
| 118 | + return task |
| 119 | + |
| 120 | +################################################################################################# |
| 121 | + |
| 122 | +def queryForDefects(rally): |
| 123 | + response = rally.get('Defect', fetch=True) |
| 124 | + # a response has status_code, content and data attributes |
| 125 | + |
| 126 | + for defect in response: |
| 127 | + #print "%s %s %s %s" % (defect.__class__.__name__, defect.oid, defect.name, defect._ref) |
| 128 | + print("%s %s %s %s %s %s" % (defect.FormattedID, defect.Name, |
| 129 | + defect.Workspace.Name, defect.Project.Name, |
| 130 | + defect.Release.Name, defect.Iteration.Name)) |
| 131 | + |
| 132 | +################################################################################################# |
| 133 | +################################################################################################# |
| 134 | + |
| 135 | +if __name__ == '__main__': |
| 136 | + main(sys.argv[1:]) |
0 commit comments