|
| 1 | +from atlassian import Jira |
| 2 | +import networkx as nx # for visualisation of the tree |
| 3 | +import matplotlib.pyplot as plt # for visualisation of the tree |
| 4 | + |
| 5 | +# use one of above objects depending on your instance type cloud or DC |
| 6 | +jira_cloud = Jira(url="<url>", username="username", password="password") |
| 7 | +jira_dc = Jira(url="url", token="<token>>") |
| 8 | + |
| 9 | +""" |
| 10 | +
|
| 11 | + Return list that contains the tree of the issue, with all subtasks and inward linked issues. |
| 12 | + be aware of following limitations: |
| 13 | + (!) Function only returns child issues from the same jira instance or from instance to which api key has access to. |
| 14 | + (!) User asssociated with API key must have access to the all child issues in order to get them. |
| 15 | +""" |
| 16 | +""" |
| 17 | + Let's say we have a tree of issues: |
| 18 | + INTEGRTEST-2 is the root issue and it has 1 subtask from project TEST - TEST1 |
| 19 | + and also two linked issues from project INTEGRTEST - INTEGRTEST-3 and INTEGRTEST-4. |
| 20 | + INTEGRTEST-4 has a subtask INTEGRTEST-6 |
| 21 | + -------------- graph representation of the tree ---------------- |
| 22 | +INTEGRTEST-2 |
| 23 | + TEST-1 |
| 24 | + INTEGRTEST-3 |
| 25 | + INTEGRTEST-4 |
| 26 | + INTEGRTEST-6 |
| 27 | + ---------------------------------------------------------------- |
| 28 | +""" |
| 29 | +output = jira_cloud.get_issue_tree_recursive("INTEGRTEST-2") |
| 30 | + |
| 31 | + |
| 32 | +# print(output) will return: |
| 33 | +# [{'INTEGRTEST-2': 'TEST-1'}, {'INTEGRTEST-2': 'INTEGRTEST-3'}, {'INTEGRTEST-2': 'INTEGRTEST-4'}, {'INTEGRTEST-4': 'INTEGRTEST-6'}] |
| 34 | +# now we can use this output to create a graph representation of the tree: |
| 35 | +def print_tree(node, dict_list, level=0): |
| 36 | + children = [value for dict_item in dict_list for key, value in dict_item.items() if key == node] |
| 37 | + print(" " * level + node) |
| 38 | + for child in children: |
| 39 | + print_tree(child, dict_list, level + 1) |
| 40 | + |
| 41 | + |
| 42 | +# or use this input to create a visualisation using networkx and matplotlib librarries or some js library like recharts or vis.js |
| 43 | +def make_graph(dict_list): |
| 44 | + # Create a new directed graph |
| 45 | + G = nx.DiGraph() |
| 46 | + # Add an edge to the graph for each key-value pair in each dictionary |
| 47 | + for d in dict_list: |
| 48 | + for key, value in d.items(): |
| 49 | + G.add_edge(key, value) |
| 50 | + |
| 51 | + # Generate a layout for the nodes |
| 52 | + pos = nx.spring_layout(G) |
| 53 | + |
| 54 | + # Define a color map for the nodes |
| 55 | + color_map = [] |
| 56 | + for node in G: |
| 57 | + if node.startswith("CYBER"): |
| 58 | + color_map.append("blue") |
| 59 | + else: |
| 60 | + color_map.append("red") |
| 61 | + |
| 62 | + # Draw the graph |
| 63 | + nx.draw(G, pos, node_color=color_map, with_labels=True, node_size=1500) |
| 64 | + |
| 65 | + # Display the graph |
| 66 | + plt.show() |
0 commit comments