Commit cba6e9243620e2ddcb15c749d626156c5ce1c063
1 parent
0cf0487d
Exists in
master
and in
4 other branches
move transfer logic out of project to service
Showing
2 changed files
with
38 additions
and
26 deletions
Show diff stats
app/models/project.rb
| ... | ... | @@ -247,32 +247,6 @@ class Project < ActiveRecord::Base |
| 247 | 247 | users_projects.find_by_user_id(user_id) |
| 248 | 248 | end |
| 249 | 249 | |
| 250 | - def transfer(new_namespace) | |
| 251 | - Project.transaction do | |
| 252 | - old_namespace = namespace | |
| 253 | - self.namespace = new_namespace | |
| 254 | - | |
| 255 | - old_dir = old_namespace.try(:path) || '' | |
| 256 | - new_dir = new_namespace.try(:path) || '' | |
| 257 | - | |
| 258 | - old_repo = if old_dir.present? | |
| 259 | - File.join(old_dir, self.path) | |
| 260 | - else | |
| 261 | - self.path | |
| 262 | - end | |
| 263 | - | |
| 264 | - if Project.where(path: self.path, namespace_id: new_namespace.try(:id)).present? | |
| 265 | - raise TransferError.new("Project with same path in target namespace already exists") | |
| 266 | - end | |
| 267 | - | |
| 268 | - Gitlab::ProjectMover.new(self, old_dir, new_dir).execute | |
| 269 | - | |
| 270 | - save! | |
| 271 | - end | |
| 272 | - rescue Gitlab::ProjectMover::ProjectMoveError => ex | |
| 273 | - raise Project::TransferError.new(ex.message) | |
| 274 | - end | |
| 275 | - | |
| 276 | 250 | def name_with_namespace |
| 277 | 251 | @name_with_namespace ||= begin |
| 278 | 252 | if namespace |
| ... | ... | @@ -295,6 +269,10 @@ class Project < ActiveRecord::Base |
| 295 | 269 | end |
| 296 | 270 | end |
| 297 | 271 | |
| 272 | + def transfer(new_namespace) | |
| 273 | + ProjectTransferService.new.transfer(self, new_namespace) | |
| 274 | + end | |
| 275 | + | |
| 298 | 276 | def execute_hooks(data) |
| 299 | 277 | hooks.each { |hook| hook.async_execute(data) } |
| 300 | 278 | end | ... | ... |
| ... | ... | @@ -0,0 +1,34 @@ |
| 1 | +# ProjectTransferService class | |
| 2 | +# | |
| 3 | +# Used for transfer project to another namespace | |
| 4 | +# | |
| 5 | +class ProjectTransferService | |
| 6 | + attr_accessor :project | |
| 7 | + | |
| 8 | + def transfer(project, new_namespace) | |
| 9 | + Project.transaction do | |
| 10 | + old_namespace = project.namespace | |
| 11 | + project.namespace = new_namespace | |
| 12 | + | |
| 13 | + old_dir = old_namespace.try(:path) || '' | |
| 14 | + new_dir = new_namespace.try(:path) || '' | |
| 15 | + | |
| 16 | + old_repo = if old_dir.present? | |
| 17 | + File.join(old_dir, project.path) | |
| 18 | + else | |
| 19 | + project.path | |
| 20 | + end | |
| 21 | + | |
| 22 | + if Project.where(path: project.path, namespace_id: new_namespace.try(:id)).present? | |
| 23 | + raise TransferError.new("Project with same path in target namespace already exists") | |
| 24 | + end | |
| 25 | + | |
| 26 | + Gitlab::ProjectMover.new(project, old_dir, new_dir).execute | |
| 27 | + | |
| 28 | + save! | |
| 29 | + end | |
| 30 | + rescue Gitlab::ProjectMover::ProjectMoveError => ex | |
| 31 | + raise Project::TransferError.new(ex.message) | |
| 32 | + end | |
| 33 | +end | |
| 34 | + | ... | ... |