Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. import csv
  2. import json
  3. from openpyxl.reader.excel import load_workbook
  4. from openpyxl.styles import PatternFill
  5.  
  6. dic_id_map = {}
  7. dic_parent_map = {}
  8. dic_assignee = {}
  9. dic_story_map = {}
  10. new_stories = []
  11.  
  12. with open('conf.json', encoding='utf-8') as config_file:
  13. conf_data = json.load(config_file)
  14.  
  15. def list_stories(key, id, parent, row):
  16. if parent == '':
  17. dic_story_map[key] = row
  18. map_parent(key, id)
  19. else:
  20. story_key = dic_id_map[parent]
  21. map_parent(story_key, id)
  22.  
  23.  
  24. def map_parent(key, id):
  25. if key not in dic_parent_map.keys():
  26. dic_parent_map[key] = []
  27. dic_parent_map[key].append(id)
  28.  
  29. def map_columns(array, name):
  30. for i in range(0, len(array)):
  31. if array[i] == name:
  32. return i
  33. break
  34.  
  35. def write_row(row_index, issue_key):
  36. write_assignee(row_index, issue_key)
  37. sheet.cell(row=row_index, column=2).value = dic_story_map[issue_key][status_col]
  38. sheet.cell(row=row_index, column=4).value = dic_story_map[issue_key][priority_col]
  39. sheet.cell(row=row_index, column=col_max).value = dic_story_map[issue_key][summary_col]
  40. if dic_story_map[issue_key][story_point_col] =='':
  41. sheet.cell(row=row_index, column=5).value = 0
  42. else:
  43. sheet.cell(row=row_index, column=5).value = float(dic_story_map[issue_key][story_point_col])
  44.  
  45.  
  46. def write_assignee(row_index,issue_key):
  47. for j in range(6, col_max):
  48. assignee = sheet.cell(row=1, column=j).value
  49. assignee = conf_data[assignee]
  50. is_assignee = test_assignee(assignee, issue_key)
  51. if is_assignee == True:
  52. sheet.cell(row=row_index, column=j).value = 'x'
  53.  
  54. def test_assignee(assignee, issue_key):
  55. issue_id = dic_story_map[issue_key][issue_id_col]
  56. is_assignee = None
  57. if dic_parent_map != {}:
  58. parent_id = dic_parent_map[issue_key]
  59. for item in dic_assignee[assignee]:
  60. if item == issue_id:
  61. is_assignee = True
  62. elif item in parent_id:
  63. is_assignee = True
  64. else:
  65. for item in dic_assignee[assignee]:
  66. if item == issue_id:
  67. is_assignee = True
  68. return is_assignee
  69.  
  70.  
  71. # file name JIRA.csv, separators ,
  72. with open('JIRA.csv') as csvfile:
  73. readCSV = csv.reader(csvfile, delimiter=',')
  74. data = list(readCSV)
  75. row_id = 1
  76. #maps each header to a column number based on the label
  77. for row in data:
  78. if row_id == 1:
  79. header = row
  80. issue_key_col = map_columns(row, 'Issue key')
  81. issue_id_col = map_columns(row, 'Issue id')
  82. parent_id_col = map_columns(row,'Parent id')
  83. summary_col = map_columns(row,'Summary')
  84. assignee_col = map_columns(row, 'Assignee')
  85. priority_col = map_columns(row, 'Priority')
  86. status_col = map_columns(row, 'Status')
  87. story_point_col = map_columns(row, 'Custom field (Story Points)')
  88. row_id += 1
  89. else:
  90. #associates each issue id to a key
  91. dic_id_map[row[issue_id_col]] = row[issue_key_col]
  92. if row[assignee_col] not in dic_assignee.keys():
  93. dic_assignee[row[assignee_col]] = []
  94. dic_assignee[row[assignee_col]].append(row[issue_id_col])
  95.  
  96. #build a list of the stories
  97. for row in data:
  98. if row == header:
  99. continue
  100. if parent_id_col != None:
  101. list_stories(row[issue_key_col], row[issue_id_col], row[parent_id_col], row)
  102. else:
  103. dic_story_map[row[issue_key_col]] = row
  104. row_id += 1
  105.  
  106. #generates the output file
  107. output_file = load_workbook('Sprint_matrix.xlsx')
  108. sheet = output_file.active
  109. row_max = sheet.max_row
  110. col_max = sheet.max_column
  111.  
  112. #checks for existing lines and updates them (specifically useful for update)
  113. for key in dic_story_map:
  114. for i in range(3, row_max):
  115. c = i
  116. issue_key = sheet.cell(row=i, column=3).value
  117. if issue_key != None:
  118. if issue_key == key:
  119. write_row(i, issue_key)
  120. break
  121. else:
  122. continue
  123. if c == row_max - 1:
  124. if key not in new_stories:
  125. new_stories.append(key)
  126.  
  127. #adds new lines in the end of the file (specifically useful for first creation)
  128. index = row_max
  129. for item in new_stories:
  130. if dic_story_map[item][status_col] == 'Done':
  131. sheet.insert_rows(4)
  132. sheet.cell(row=4, column=3).value = item
  133. write_row(4, item)
  134. else:
  135. write_row(index, item)
  136. sheet.cell(row=index, column=3).value = item
  137.  
  138. index += 1
  139.  
  140. #computes the total sums
  141. total_row = {}
  142. row_max = sheet.max_row
  143. for i in range(3, row_max):
  144. category = sheet.cell(row=i, column=1).value
  145. if category != None and category != '':
  146. total_row[category] = []
  147. total_row[category].append(i)
  148.  
  149. for key in total_row.keys():
  150. for i in range(total_row[key][0], row_max):
  151. total = sheet.cell(row=i, column=4).value
  152. if total == 'Total':
  153. total_row[key].append(i)
  154. break
  155.  
  156. for key in total_row.keys():
  157. if key != None and key != 'To be sorted':
  158. top_cell = 'E' + str(total_row[key][0])
  159. total_cell = 'E' + str(total_row[key][1] - 1)
  160. total = sheet.cell(row=total_row[key][1], column=5)
  161. total.value = '= SUM(' + top_cell + ':' + total_cell + ')'
  162.  
  163. #highlight the removed stories
  164. cell_fill = PatternFill(start_color="FFC000", end_color="FFC000", fill_type = "solid")
  165. for i in range(3, row_max):
  166. story_key = sheet.cell(row=i, column=3)
  167. if story_key.value != None:
  168. if story_key.value not in dic_story_map.keys():
  169. for j in range(1, col_max +1):
  170. sheet.cell(row=i, column=j).fill = cell_fill
  171.  
  172. output_file.save('Sprint_matrix_update.xlsx')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement