forked from moimael/trac-to-gitlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connection.py
209 lines (181 loc) · 8.09 KB
/
Connection.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
# vim: autoindent tabstop=4 shiftwidth=4 expandtab softtabstop=4 filetype=python fileencoding=utf-8
'''
Copyright © 2013 - 2017
Eric van der Vlist <[email protected]>
Jens Neuhalfen <http://www.neuhalfen.name/>
See license information at the bottom of this file
'''
from peewee import PostgresqlDatabase
from .model import *
import os
import shutil
from datetime import datetime
from io import open
class Connection(object):
"""
Connection to the gitlab database
"""
def __init__(self, db_name, db_user, db_password, db_path, uploads_path):
"""
"""
db = PostgresqlDatabase(db_name, user= db_user, host=db_path)
database_proxy.initialize(db)
self.uploads_path = uploads_path
def clear_issues(self, project_id):
# Delete all the uses of the labels of the project.
for label in Labels.select().where( Labels.project == project_id ):
LabelLinks.delete().where( LabelLinks.label == label.id ).execute()
## You probably do not want to delete the labels themselves, otherwise you'd need to
## set their colour every time when you re-run the migration.
#label.delete_instance()
# Delete issues and everything that goes with them...
for issue in Issues.select().where(Issues.project == project_id):
for note in Notes.select().where( (Notes.project == project_id) & (Notes.noteable_type == 'Issue') & (Notes.noteable == issue.id)):
if note.attachment != None:
directory = os.path.join(self.uploads_path, 'note/attachment/%s' % note.id)
try:
shutil.rmtree(directory)
except:
pass
Events.delete().where( (Events.project == project_id) & (Events.target_type == 'Note' ) & (Events.target == note.id) ).execute()
note.delete_instance()
Events.delete().where( (Events.project == project_id) & (Events.target_type == 'Issue' ) & (Events.target == issue.id) ).execute()
issue.delete_instance()
Milestones.delete().where( Milestones.project == project_id ).execute()
def clear_wiki_attachments(self, project_id):
for note in Notes.select().where( (Notes.project == project_id) & (Notes.noteable_type >> None) & (Notes.note % 'Wiki attachment %')):
directory = os.path.join(self.uploads_path, 'note/attachment/%s' % note.id)
try:
shutil.rmtree(directory)
except:
pass
Events.delete().where( (Events.project == project_id) & (Events.target_type == 'Note' ) & (Events.target == note.id) ).execute()
note.delete_instance()
def milestone_by_name(self, project_id, milestone_name):
for milestone in Milestones.select().where((Milestones.title == milestone_name) & (Milestones.project == project_id)):
return milestone._data
return None
def project_by_name(self, project_name):
(namespace, name) = project_name.split('/')
print(name)
for project in Projects.select().join(Namespaces, on=(Projects.namespace == Namespaces.id )).where((Projects.path == name) & (Namespaces.path == namespace)):
print(project._data)
return project._data
return None
def get_user_id(self, username):
return Users.get(Users.username == username).id
def get_issues_iid(self, dest_project_id):
return Issues.select().where(Issues.project == dest_project_id).aggregate(fn.Count(Issues.id)) + 1
def create_milestone(self, dest_project_id, new_milestone):
try:
existing = Milestones.get((Milestones.title == new_milestone.title) & (Milestones.project == dest_project_id))
for k in new_milestone._data:
if (k not in ('id', 'iid')):
existing._data[k] = new_milestone._data[k]
new_milestone = existing
except:
new_milestone.iid = Milestones.select().where(Milestones.project == dest_project_id).aggregate(fn.Count(Milestones.id)) + 1
new_milestone.created_at = datetime.now()
new_milestone.updated_at = datetime.now()
new_milestone.save()
return new_milestone
def create_issue(self, dest_project_id, new_issue):
new_issue.save()
event = Events.create(
action = 1,
author = new_issue.author,
created_at = new_issue.created_at,
project = dest_project_id,
target = new_issue.id,
target_type = 'Issue',
updated_at = new_issue.created_at
)
event.save()
for title in set(new_issue.labels.split(',')):
if (title == ''):
continue
try:
label = Labels.get((Labels.title == title) & (Labels.project == dest_project_id))
except:
label = Labels.create(
title = title,
color = '#0000FF',
project = dest_project_id,
type = 'ProjectLabel',
created_at = new_issue.created_at,
update_at = new_issue.created_at
)
label.save()
label_link = LabelLinks.create(
label = label.id,
target = new_issue.id,
target_type = 'Issue',
created_at = new_issue.created_at,
update_at = new_issue.created_at
)
label_link.save()
return new_issue
def comment_issue(self ,project_id, ticket, note, binary_attachment):
note.project = project_id
note.noteable = ticket.id
note.noteable_type = 'Issue'
note.save()
if binary_attachment:
directory = os.path.join(self.uploads_path, 'note/attachment/%s' % note.id)
if not os.path.exists(directory):
os.makedirs(directory)
path = os.path.join(directory, note.attachment)
file = open(path.encode('utf-8'),"wb")
file.write(binary_attachment)
file.close()
event = Events.create(
action = 1,
author = note.author,
created_at = note.created_at,
project = project_id,
target = note.id,
target_type = 'Note',
updated_at = note.created_at
)
event.save()
def create_wiki_attachment(self, project_id, user, last_modified, path, binary):
note = Notes.create(
project = project_id,
note = 'Wiki attachment %s' % path,
user = user,
created_at = last_modified,
updated_at = last_modified,
attachment = path
)
note.save()
full_path = os.path.join(self.uploads_path, 'note/attachment/%s' % note.id, path)
directory = os.path.dirname(full_path)
if not os.path.exists(directory):
os.makedirs(directory)
file = open(full_path,"wb")
file.write(binary)
file.close()
event = Events.create(
action = 1,
author = note.author,
created_at = note.created_at,
project = project_id,
target = note.id,
target_type = 'Note',
updated_at = note.created_at
)
event.save()
return '/files/note/%s/%s' % (note.id, path)
'''
This file is part of <https://gitlab.dyomedea.com/vdv/trac-to-gitlab>.
This sotfware is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This sotfware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library. If not, see <http://www.gnu.org/licenses/>.
'''