-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathticket_copy.py
227 lines (192 loc) · 9.73 KB
/
ticket_copy.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from redminelib import Redmine
import redminelib
import tkinter as tk
import tkinter.filedialog as fd
import tkinter.messagebox as msgbox
from tkinter import ttk
import logging
import datetime
import sys, os
import copy
output_logging = 20
class Redmine_Copy(tk.Tk):
def __init__(self, top=None):
super().__init__()
redmine = ""
model_list = []
x_loc = 0.01
y_loc = 0.01
x_gap = 0.2
y_gap = 0.03
self.start_logging()
self.geometry("600x800+250+50")
self.title("Redmine Ticket Copy Utility v1.0 (2023/2/2)")
self.redmine = self.redmine_connect()
self.LoopLabel = tk.Label(self, text='Redmine Ticket List:', anchor='w', justify='left')
self.LoopLabel.place(relx=x_loc + 0.05, rely=y_loc, height=20, width=200)
y_loc += y_gap
self.TicketList = tk.Text(self)
self.TicketList.place(relx=x_loc + 0.05, rely=y_loc, height=250, width=500)
y_loc += y_gap * 11
self.LoopLabel = tk.Label(self, text='Model List:', anchor='w', justify='left')
self.LoopLabel.place(relx=x_loc + 0.05, rely=y_loc, height=20, width=200)
y_loc += y_gap
self.ModelList = tk.Listbox(self, selectmode=tk.EXTENDED)
self.ModelList.place(relx=x_loc + 0.05, rely=y_loc, height=250, width=500)
model_list = self.query_model()
for model in model_list:
self.ModelList.insert(tk.END, model)
y_loc += y_gap * 11
self.version_list = []
self.version_dict = []
self.version_dict = self.query_version()
for version in self.version_dict:
self.version_list.append(version["name"])
self.TargetVersion_Label = tk.Label(self, text='Target:')
self.TargetVersion_Label.place(relx=x_loc-0.1, rely=y_loc, height=32, width=250)
self.TargetVersion = ttk.Combobox(self, width=600, values=self.version_list)
self.TargetVersion.current(0)
self.TargetVersion.place(relx=x_loc + x_gap, rely=y_loc, height=32, width=400)
#self.TestType.bind("<<ComboboxSelected>>", self.test_profile_change)
x_loc = 0.05
y_loc += y_gap * 2
self.StartButton = tk.Button(self, pady="0", text='Copy', command=self.start_copy)
self.StartButton.place(relx=x_loc, rely=y_loc, height=31, width=150)
# Configure the scrollbars
#self.ResponseText = tk.Text(self, font=("Helvetica", 8))
#self.ResponseText.place(relx=x_loc + 0.05, rely=y_loc, height=250, width=800)
#self.ScrollBar = tk.Scrollbar(self.ResponseText)
#self.ScrollBar.pack(side=tk.RIGHT, fill=tk.Y)
#self.ScrollBar.config(command=self.ResponseText.yview)
def query_version(self):
version_list = []
versions = self.redmine.version.filter(project_id="emea-bu-bug-report")
#for testing
#versions = self.redmine.version.filter(project_id="redmine-evaluatoin")
for version_id in versions:
if "OPAL" in version_id.name or "opal" in version_id.name:
version_list.append({"name": version_id.name, "id": version_id.id})
return version_list
def redmine_connect(self):
# Redmine setting
redmine_version = '3.4.6-stable'
key = '9e162fa25b0267706cd589c99817b66403a8edfe'
server = 'http://172.20.0.37'
redmine = Redmine(server, key=key, version=redmine_version)
return redmine
def query_model(self):
name_list = []
select_model = ["VMG", "DX", "AX", "EX", "WX", "PX", "PE", "EE"]
# issues = redmine.issue.filter(project_id='Telefonica', tracker_id=1, status_id='*')
# ticket = redmine.issue.get(126600)
model_field = self.redmine.custom_field.get(14)
for model in model_field.possible_values:
for selective in select_model:
if selective in model["value"]:
name_list.append(model["value"])
return name_list
def start_copy(self):
model_list = []
tickets = self.TicketList.get("1.0", tk.END)
ticket_list = tickets.split("\n")
ticket_list = list(filter(None, ticket_list))
for i in self.ModelList.curselection():
model_list.append(self.ModelList.get(i))
target_version_name = self.TargetVersion.get()
version_id = 0
for version in self.version_dict:
if target_version_name==version["name"]:
version_id = version["id"]
break
// create a function to copy ticket
def copy_ticket(ticket_content, model_list, target_version_name, version_id):
#print (tickets)
for ticket in ticket_list:
if ticket !="":
ticket_content = self.redmine.issue.get(int(ticket))
file_list = []
for item in ticket_content.attachments:
filepath = item.download(savepath=os. getcwd(), filename=item.filename)
file_list.append({'path': filepath, 'filename': item.filename})
for model in model_list:
#print(ticket_content.custom_fields.get(14)['value'][0])
if ticket_content.custom_fields.get(14)['value'][0] == model:
logging.info("Copy ticket from ID#%s on model#%s is failed, target:%s, reason: exist model", \
ticket_content.id, model, target_version_name)
continue
else:
issue = self.redmine.issue.new()
issue.project_id = ticket_content['project']['id']
issue.subject = ticket_content['subject']
issue.tracker_id = ticket_content['tracker_id']
issue.description = ticket_content['description']
issue.status_id = 1
issue.fixed_version_id = version_id
try:
issue.custom_fields = [{'id': 14, 'value': model},
{'id': 5, 'value': ticket_content.custom_fields.get(5)['value']},
{'id': 36, 'value': ticket_content.custom_fields.get(36)['value']},
{'id': 21, 'value': ticket_content.custom_fields.get(21)['value']},
{'id': 19, 'value': "CPE"},
{'id': 6, 'value': ticket_content.custom_fields.get(6)['value']},
{'id': 34, 'value': ticket_content.custom_fields.get(34)['value']},
{'id': 18, 'value': ticket_content.custom_fields.get(18)['value']},
{'id': 38, 'value': ticket_content.custom_fields.get(38)['value']}]
except Exception as e:
logging.info(
"Copy ticket from ID#%s on model#%s is failed on new ID#%s, target:%s, reason:%s", \
ticket_content.id, model, issue.id, target_version_name, e)
continue
issue.priority_id = self.priority_convert(ticket_content['priority']['id'])
#report_date = datetime.datetime.strptime(datetime, "%Y/%m/%d").date()
#issue.start_date = report_date
copy_file_list = copy.deepcopy(file_list)
if ticket_content['attachments'] != "N/A":
issue.uploads = copy_file_list
try:
issue.save()
relation = self.redmine.issue_relation.new()
relation.issue_id = issue.id
relation.issue_to_id = ticket_content.id
relation.relation_type = 'copied_from'
relation.save()
logging.info("Copy ticket from ID#%s on model#%s is created on new ID#%s, target:%s", \
ticket_content.id, model ,issue.id, target_version_name)
except Exception as e:
#print(e)
logging.info("Copy ticket from ID#%s on model#%s is failed on new ID#%s, target:%s, reason:%s", \
ticket_content.id, model ,issue.id, target_version_name , e)
continue
#path = os. getcwd()
for file in file_list:
os.remove( os. getcwd() + "\\" + file['filename'])
tk.messagebox.showinfo("INFO", "Copy Ticket Is Finished")
return
def start_logging(self):
# Enable the logging to console and file
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
logging.basicConfig(level=output_logging,
format='%(asctime)s: [%(levelname)s] %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='redmine_copy.log',
filemode='a')
console = logging.StreamHandler()
console.setLevel(output_logging)
formatter = logging.Formatter('%(levelname)-4s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
def priority_convert(self, level):
if level == 6:
val = 19
elif level == 5:
val = 20
elif level == 4:
val = 21
else:
val = level
return int(val)
if __name__ == '__main__':
app = Redmine_Copy()
app.mainloop()
sys.exit(0)