##// END OF EJS Templates
fixed issue with renaming repos group together with changing parents with multiple nested trees...
marcink -
r3472:702da441 default
parent child Browse files
Show More
@@ -1287,15 +1287,13 b' class RepoGroup(Base, BaseModel):'
1287
1287
1288 return cnt + children_count(self)
1288 return cnt + children_count(self)
1289
1289
1290 def recursive_groups_and_repos(self):
1290 def _recursive_objects(self, include_repos=True):
1291 """
1292 Recursive return all groups, with repositories in those groups
1293 """
1294 all_ = []
1291 all_ = []
1295
1292
1296 def _get_members(root_gr):
1293 def _get_members(root_gr):
1297 for r in root_gr.repositories:
1294 if include_repos:
1298 all_.append(r)
1295 for r in root_gr.repositories:
1296 all_.append(r)
1299 childs = root_gr.children.all()
1297 childs = root_gr.children.all()
1300 if childs:
1298 if childs:
1301 for gr in childs:
1299 for gr in childs:
@@ -1305,6 +1303,18 b' class RepoGroup(Base, BaseModel):'
1305 _get_members(self)
1303 _get_members(self)
1306 return [self] + all_
1304 return [self] + all_
1307
1305
1306 def recursive_groups_and_repos(self):
1307 """
1308 Recursive return all groups, with repositories in those groups
1309 """
1310 return self._recursive_objects()
1311
1312 def recursive_groups(self):
1313 """
1314 Returns all children groups for this group including children of children
1315 """
1316 return self._recursive_objects(include_repos=False)
1317
1308 def get_new_name(self, group_name):
1318 def get_new_name(self, group_name):
1309 """
1319 """
1310 returns new full group name based on parent and new name
1320 returns new full group name based on parent and new name
@@ -244,27 +244,37 b' class ReposGroupModel(BaseModel):'
244
244
245 # change properties
245 # change properties
246 repos_group.group_description = form_data['group_description']
246 repos_group.group_description = form_data['group_description']
247 repos_group.parent_group = RepoGroup.get(form_data['group_parent_id'])
248 repos_group.group_parent_id = form_data['group_parent_id']
247 repos_group.group_parent_id = form_data['group_parent_id']
249 repos_group.enable_locking = form_data['enable_locking']
248 repos_group.enable_locking = form_data['enable_locking']
249
250 repos_group.parent_group = RepoGroup.get(form_data['group_parent_id'])
250 repos_group.group_name = repos_group.get_new_name(form_data['group_name'])
251 repos_group.group_name = repos_group.get_new_name(form_data['group_name'])
251 new_path = repos_group.full_path
252 new_path = repos_group.full_path
252
253 self.sa.add(repos_group)
253 self.sa.add(repos_group)
254
254
255 # iterate over all members of this groups and set the locking !
255 # iterate over all members of this groups and do fixes
256 # set locking if given
257 # if obj is a repoGroup also fix the name of the group according
258 # to the parent
259 # if obj is a Repo fix it's name
256 # this can be potentially heavy operation
260 # this can be potentially heavy operation
257 for obj in repos_group.recursive_groups_and_repos():
261 for obj in repos_group.recursive_groups_and_repos():
258 #set the value from it's parent
262 #set the value from it's parent
259 obj.enable_locking = repos_group.enable_locking
263 obj.enable_locking = repos_group.enable_locking
264 if isinstance(obj, RepoGroup):
265 new_name = obj.get_new_name(obj.name)
266 log.debug('Fixing group %s to new name %s' \
267 % (obj.group_name, new_name))
268 obj.group_name = new_name
269 elif isinstance(obj, Repository):
270 # we need to get all repositories from this new group and
271 # rename them accordingly to new group path
272 new_name = obj.get_new_name(obj.just_name)
273 log.debug('Fixing repo %s to new name %s' \
274 % (obj.repo_name, new_name))
275 obj.repo_name = new_name
260 self.sa.add(obj)
276 self.sa.add(obj)
261
277
262 # we need to get all repositories from this new group and
263 # rename them accordingly to new group path
264 for r in repos_group.repositories:
265 r.repo_name = r.get_new_name(r.just_name)
266 self.sa.add(r)
267
268 self.__rename_group(old_path, new_path)
278 self.__rename_group(old_path, new_path)
269
279
270 return repos_group
280 return repos_group
@@ -47,7 +47,8 b' log = logging.getLogger(__name__)'
47 'TEST_USER_REGULAR2_PASS', 'TEST_USER_REGULAR2_EMAIL', 'TEST_HG_REPO',
47 'TEST_USER_REGULAR2_PASS', 'TEST_USER_REGULAR2_EMAIL', 'TEST_HG_REPO',
48 'TEST_HG_REPO_CLONE', 'TEST_HG_REPO_PULL', 'TEST_GIT_REPO',
48 'TEST_HG_REPO_CLONE', 'TEST_HG_REPO_PULL', 'TEST_GIT_REPO',
49 'TEST_GIT_REPO_CLONE', 'TEST_GIT_REPO_PULL', 'HG_REMOTE_REPO',
49 'TEST_GIT_REPO_CLONE', 'TEST_GIT_REPO_PULL', 'HG_REMOTE_REPO',
50 'GIT_REMOTE_REPO', 'SCM_TESTS', '_get_repo_create_params'
50 'GIT_REMOTE_REPO', 'SCM_TESTS', '_get_repo_create_params',
51 '_get_group_create_params'
51 ]
52 ]
52
53
53 # Invoke websetup with the current config file
54 # Invoke websetup with the current config file
@@ -182,3 +183,18 b' def _get_repo_create_params(**custom):'
182 defs.update({'repo_name_full': defs['repo_name']})
183 defs.update({'repo_name_full': defs['repo_name']})
183
184
184 return defs
185 return defs
186
187
188 def _get_group_create_params(**custom):
189 defs = dict(
190 group_name=None,
191 group_description='DESC',
192 group_parent_id=None,
193 perms_updates=[],
194 perms_new=[],
195 enable_locking=False,
196 recursive=False
197 )
198 defs.update(custom)
199
200 return defs
@@ -21,6 +21,33 b" def _make_group(path, desc='desc', paren"
21 return gr
21 return gr
22
22
23
23
24 def _update_group(id_, group_name, desc='desc', parent_id=None):
25 form_data = _get_group_create_params(group_name=group_name,
26 group_desc=desc,
27 group_parent_id=parent_id)
28 gr = ReposGroupModel().update(id_, form_data)
29 return gr
30
31
32 def _make_repo(name, **kwargs):
33 form_data = _get_repo_create_params(repo_name=name, **kwargs)
34 cur_user = User.get_by_username(TEST_USER_ADMIN_LOGIN)
35 r = RepoModel().create(form_data, cur_user)
36 return r
37
38
39 def _update_repo(name, **kwargs):
40 form_data = _get_repo_create_params(**kwargs)
41 if not 'repo_name' in kwargs:
42 form_data['repo_name'] = name
43 if not 'perms_new' in kwargs:
44 form_data['perms_new'] = []
45 if not 'perms_updates' in kwargs:
46 form_data['perms_updates'] = []
47 r = RepoModel().update(name, **form_data)
48 return r
49
50
24 class TestReposGroups(unittest.TestCase):
51 class TestReposGroups(unittest.TestCase):
25
52
26 def setUp(self):
53 def setUp(self):
@@ -32,7 +59,7 b' class TestReposGroups(unittest.TestCase)'
32 Session().commit()
59 Session().commit()
33
60
34 def tearDown(self):
61 def tearDown(self):
35 print 'out'
62 Session.remove()
36
63
37 def __check_path(self, *path):
64 def __check_path(self, *path):
38 """
65 """
@@ -48,21 +75,9 b' class TestReposGroups(unittest.TestCase)'
48 def __delete_group(self, id_):
75 def __delete_group(self, id_):
49 ReposGroupModel().delete(id_)
76 ReposGroupModel().delete(id_)
50
77
51 def __update_group(self, id_, path, desc='desc', parent_id=None):
52 form_data = dict(
53 group_name=path,
54 group_description=desc,
55 group_parent_id=parent_id,
56 perms_updates=[],
57 perms_new=[],
58 enable_locking=False,
59 recursive=False
60 )
61 gr = ReposGroupModel().update(id_, form_data)
62 return gr
63
64 def test_create_group(self):
78 def test_create_group(self):
65 g = _make_group('newGroup')
79 g = _make_group('newGroup')
80 Session().commit()
66 self.assertEqual(g.full_path, 'newGroup')
81 self.assertEqual(g.full_path, 'newGroup')
67
82
68 self.assertTrue(self.__check_path('newGroup'))
83 self.assertTrue(self.__check_path('newGroup'))
@@ -73,23 +88,27 b' class TestReposGroups(unittest.TestCase)'
73
88
74 def test_same_subgroup(self):
89 def test_same_subgroup(self):
75 sg1 = _make_group('sub1', parent_id=self.g1.group_id)
90 sg1 = _make_group('sub1', parent_id=self.g1.group_id)
91 Session().commit()
76 self.assertEqual(sg1.parent_group, self.g1)
92 self.assertEqual(sg1.parent_group, self.g1)
77 self.assertEqual(sg1.full_path, 'test1/sub1')
93 self.assertEqual(sg1.full_path, 'test1/sub1')
78 self.assertTrue(self.__check_path('test1', 'sub1'))
94 self.assertTrue(self.__check_path('test1', 'sub1'))
79
95
80 ssg1 = _make_group('subsub1', parent_id=sg1.group_id)
96 ssg1 = _make_group('subsub1', parent_id=sg1.group_id)
97 Session().commit()
81 self.assertEqual(ssg1.parent_group, sg1)
98 self.assertEqual(ssg1.parent_group, sg1)
82 self.assertEqual(ssg1.full_path, 'test1/sub1/subsub1')
99 self.assertEqual(ssg1.full_path, 'test1/sub1/subsub1')
83 self.assertTrue(self.__check_path('test1', 'sub1', 'subsub1'))
100 self.assertTrue(self.__check_path('test1', 'sub1', 'subsub1'))
84
101
85 def test_remove_group(self):
102 def test_remove_group(self):
86 sg1 = _make_group('deleteme')
103 sg1 = _make_group('deleteme')
104 Session().commit()
87 self.__delete_group(sg1.group_id)
105 self.__delete_group(sg1.group_id)
88
106
89 self.assertEqual(RepoGroup.get(sg1.group_id), None)
107 self.assertEqual(RepoGroup.get(sg1.group_id), None)
90 self.assertFalse(self.__check_path('deteteme'))
108 self.assertFalse(self.__check_path('deteteme'))
91
109
92 sg1 = _make_group('deleteme', parent_id=self.g1.group_id)
110 sg1 = _make_group('deleteme', parent_id=self.g1.group_id)
111 Session().commit()
93 self.__delete_group(sg1.group_id)
112 self.__delete_group(sg1.group_id)
94
113
95 self.assertEqual(RepoGroup.get(sg1.group_id), None)
114 self.assertEqual(RepoGroup.get(sg1.group_id), None)
@@ -97,24 +116,26 b' class TestReposGroups(unittest.TestCase)'
97
116
98 def test_rename_single_group(self):
117 def test_rename_single_group(self):
99 sg1 = _make_group('initial')
118 sg1 = _make_group('initial')
119 Session().commit()
100
120
101 new_sg1 = self.__update_group(sg1.group_id, 'after')
121 new_sg1 = _update_group(sg1.group_id, 'after')
102 self.assertTrue(self.__check_path('after'))
122 self.assertTrue(self.__check_path('after'))
103 self.assertEqual(RepoGroup.get_by_group_name('initial'), None)
123 self.assertEqual(RepoGroup.get_by_group_name('initial'), None)
104
124
105 def test_update_group_parent(self):
125 def test_update_group_parent(self):
106
126
107 sg1 = _make_group('initial', parent_id=self.g1.group_id)
127 sg1 = _make_group('initial', parent_id=self.g1.group_id)
128 Session().commit()
108
129
109 new_sg1 = self.__update_group(sg1.group_id, 'after', parent_id=self.g1.group_id)
130 new_sg1 = _update_group(sg1.group_id, 'after', parent_id=self.g1.group_id)
110 self.assertTrue(self.__check_path('test1', 'after'))
131 self.assertTrue(self.__check_path('test1', 'after'))
111 self.assertEqual(RepoGroup.get_by_group_name('test1/initial'), None)
132 self.assertEqual(RepoGroup.get_by_group_name('test1/initial'), None)
112
133
113 new_sg1 = self.__update_group(sg1.group_id, 'after', parent_id=self.g3.group_id)
134 new_sg1 = _update_group(sg1.group_id, 'after', parent_id=self.g3.group_id)
114 self.assertTrue(self.__check_path('test3', 'after'))
135 self.assertTrue(self.__check_path('test3', 'after'))
115 self.assertEqual(RepoGroup.get_by_group_name('test3/initial'), None)
136 self.assertEqual(RepoGroup.get_by_group_name('test3/initial'), None)
116
137
117 new_sg1 = self.__update_group(sg1.group_id, 'hello')
138 new_sg1 = _update_group(sg1.group_id, 'hello')
118 self.assertTrue(self.__check_path('hello'))
139 self.assertTrue(self.__check_path('hello'))
119
140
120 self.assertEqual(RepoGroup.get_by_group_name('hello'), new_sg1)
141 self.assertEqual(RepoGroup.get_by_group_name('hello'), new_sg1)
@@ -123,23 +144,17 b' class TestReposGroups(unittest.TestCase)'
123
144
124 g1 = _make_group('g1')
145 g1 = _make_group('g1')
125 g2 = _make_group('g2')
146 g2 = _make_group('g2')
126
147 Session().commit()
127 # create new repo
148 # create new repo
128 form_data = _get_repo_create_params(repo_name='john')
149 r = _make_repo('john')
129 cur_user = User.get_by_username(TEST_USER_ADMIN_LOGIN)
150 Session().commit()
130 r = RepoModel().create(form_data, cur_user)
131
132 self.assertEqual(r.repo_name, 'john')
151 self.assertEqual(r.repo_name, 'john')
133
134 # put repo into group
152 # put repo into group
135 form_data = form_data
153 r = _update_repo('john', repo_group=g1.group_id)
136 form_data['repo_group'] = g1.group_id
154 Session().commit()
137 form_data['perms_new'] = []
138 form_data['perms_updates'] = []
139 RepoModel().update(r.repo_name, **form_data)
140 self.assertEqual(r.repo_name, 'g1/john')
155 self.assertEqual(r.repo_name, 'g1/john')
141
156
142 self.__update_group(g1.group_id, 'g1', parent_id=g2.group_id)
157 _update_group(g1.group_id, 'g1', parent_id=g2.group_id)
143 self.assertTrue(self.__check_path('g2', 'g1'))
158 self.assertTrue(self.__check_path('g2', 'g1'))
144
159
145 # test repo
160 # test repo
@@ -155,7 +170,7 b' class TestReposGroups(unittest.TestCase)'
155 self.assertEqual(g2.full_path, 't11/t22')
170 self.assertEqual(g2.full_path, 't11/t22')
156 self.assertTrue(self.__check_path('t11', 't22'))
171 self.assertTrue(self.__check_path('t11', 't22'))
157
172
158 g2 = self.__update_group(g2.group_id, 'g22', parent_id=None)
173 g2 = _update_group(g2.group_id, 'g22', parent_id=None)
159 Session().commit()
174 Session().commit()
160
175
161 self.assertEqual(g2.group_name, 'g22')
176 self.assertEqual(g2.group_name, 'g22')
@@ -163,3 +178,65 b' class TestReposGroups(unittest.TestCase)'
163 self.assertEqual(g2.full_path, 'g22')
178 self.assertEqual(g2.full_path, 'g22')
164 self.assertFalse(self.__check_path('t11', 't22'))
179 self.assertFalse(self.__check_path('t11', 't22'))
165 self.assertTrue(self.__check_path('g22'))
180 self.assertTrue(self.__check_path('g22'))
181
182 def test_rename_top_level_group_in_nested_setup(self):
183 g1 = _make_group('L1')
184 Session().commit()
185 g2 = _make_group('L2', parent_id=g1.group_id)
186 Session().commit()
187 g3 = _make_group('L3', parent_id=g2.group_id)
188 Session().commit()
189
190 r = _make_repo('L1/L2/L3/L3_REPO', repo_group=g3.group_id)
191 Session().commit()
192
193 ##rename L1 all groups should be now changed
194 _update_group(g1.group_id, 'L1_NEW')
195 Session().commit()
196 self.assertEqual(g1.full_path, 'L1_NEW')
197 self.assertEqual(g2.full_path, 'L1_NEW/L2')
198 self.assertEqual(g3.full_path, 'L1_NEW/L2/L3')
199 self.assertEqual(r.repo_name, 'L1_NEW/L2/L3/L3_REPO')
200
201 def test_change_parent_of_top_level_group_in_nested_setup(self):
202 g1 = _make_group('R1')
203 Session().commit()
204 g2 = _make_group('R2', parent_id=g1.group_id)
205 Session().commit()
206 g3 = _make_group('R3', parent_id=g2.group_id)
207 Session().commit()
208
209 g4 = _make_group('R1_NEW')
210 Session().commit()
211
212 r = _make_repo('R1/R2/R3/R3_REPO', repo_group=g3.group_id)
213 Session().commit()
214 ##rename L1 all groups should be now changed
215 _update_group(g1.group_id, 'R1', parent_id=g4.group_id)
216 Session().commit()
217 self.assertEqual(g1.full_path, 'R1_NEW/R1')
218 self.assertEqual(g2.full_path, 'R1_NEW/R1/R2')
219 self.assertEqual(g3.full_path, 'R1_NEW/R1/R2/R3')
220 self.assertEqual(r.repo_name, 'R1_NEW/R1/R2/R3/R3_REPO')
221
222 def test_change_parent_of_top_level_group_in_nested_setup_with_rename(self):
223 g1 = _make_group('X1')
224 Session().commit()
225 g2 = _make_group('X2', parent_id=g1.group_id)
226 Session().commit()
227 g3 = _make_group('X3', parent_id=g2.group_id)
228 Session().commit()
229
230 g4 = _make_group('X1_NEW')
231 Session().commit()
232
233 r = _make_repo('X1/X2/X3/X3_REPO', repo_group=g3.group_id)
234 Session().commit()
235
236 ##rename L1 all groups should be now changed
237 _update_group(g1.group_id, 'X1_PRIM', parent_id=g4.group_id)
238 Session().commit()
239 self.assertEqual(g1.full_path, 'X1_NEW/X1_PRIM')
240 self.assertEqual(g2.full_path, 'X1_NEW/X1_PRIM/X2')
241 self.assertEqual(g3.full_path, 'X1_NEW/X1_PRIM/X2/X3')
242 self.assertEqual(r.repo_name, 'X1_NEW/X1_PRIM/X2/X3/X3_REPO')
General Comments 0
You need to be logged in to leave comments. Login now