-
Notifications
You must be signed in to change notification settings - Fork 0
/
jiraCleanup.py
105 lines (77 loc) · 3.74 KB
/
jiraCleanup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import json
import os
from atlassian import jira as JIRA
# OS specific strings - find configs file and import values
sys_path = os.path.sep
# Path of the config file, relative to the working directory
dir_path = os.path.dirname(os.path.realpath(__file__))
config_path = f'{dir_path}{sys_path}configs{sys_path}config.json'
jira_config = json.load(open(config_path))["JIRA"]
# Add Jira connection
# #Use this method if you want to login with username and PW
# jira = JIRA.Jira(url=jira_config["JiraBaseURL"], username=jira_config["JiraUser"], password=jira_config["JiraPass"])
# Use this method if you want to login with a token
jira = JIRA.Jira(url=jira_config["JiraBaseURL"], token=jira_config["JiraToken"])
query = 'project=%s' % (jira_config['ProjectKey'])
# Fetches how many issues there are (mind you, this method only returns issues that are unresolved)
issue_count = jira.jql(query)["total"]
issues = jira.get_all_project_issues("WASP", fields='*all', limit=issue_count)
issue_dict = {} # Invokes a dictionary for the sorting
for issue in issues: # Let's sort through the issue summaries and list versions of each SW package
if issue["fields"]["status"]["name"] == "Production":
summary_split = issue["fields"]["summary"].split("@")
# ATTENTION -> You need this weird structure of the list in order for .sort() to work
buffer = [[int(item) for item in summary_split[1].split(".")]] + [issue["key"]]
try:
issue_dict[summary_split[0]].append(buffer)
except:
# ATTENTION -> Buffer needs to be added as a list with the [] to form the first element
issue_dict[summary_split[0]] = [buffer]
del summary_split, issue
'''
returns a dictionary with the structure:
issue_dict = {"SOFTWARE NAME": [["VERSION as [MAJOR, MINOR, DIST]",","JIRA ISSUE KEY"],["VERSION"...
"SOFTWARE NAME": [["VERSION","JIRA ISSUE KEY"], ... }
The issue key is needed for the deletion request
'''
to_be_deleted = [] # List of issue keys to be deleted
to_be_deleted_dict = {}
for software_name in issue_dict: # Takes each package to determine the issue keys to be deleted
issue_dict[software_name].sort(reverse=True) # Sorts the version numbers in descending order
if len(issue_dict[software_name])>jira_config['VersionsToKeep']:
to_be_deleted_dict[software_name] = []
buffer = issue_dict[software_name][jira_config['VersionsToKeep']:]
to_be_deleted.extend(buffer)
buffer2 = ""
for i in buffer:
buffer2 = ".".join([str(i) for i in i[0]])
to_be_deleted_dict[software_name].append(buffer2)
del buffer, buffer2, software_name, i
ans1 = input("Would you like to see the versions to be deleted [y/n] ?")
try:
if ans1.lower() == "y":
for software_name in to_be_deleted_dict:
printout = software_name + "\n"
for version in to_be_deleted_dict[software_name]:
printout += "\t\t" + version + "\n"
print(printout)
elif ans1.lower() == "n":
print("Maybe next time...")
else:
print("No valid answer...")
except:
print("No valid answer...")
# This will pull the trigger (deletes all in to_be_deleted) from the Jira Board
ans = input("Are you sure, that you want to delete old issue [y/n] ?")
try:
if ans.lower() == "y":
for issue in to_be_deleted:
jira.delete_issue(issue[1])
print("Done deleting")
print("Thanks for choosing the Jira cleaner, see you next time")
elif ans.lower() == "n":
print("Maybe next time...")
else:
print("No valid answer...")
except:
print("No valid answer...")