Class: Projects::TransferService

Inherits:
BaseService show all
Includes:
Gitlab::ShellAdapter
Defined in:
app/services/projects/transfer_service.rb

Defined Under Namespace

Classes: TransferError

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods included from Gitlab::ShellAdapter

#gitlab_shell

Methods inherited from BaseService

#abilities, #can?, #deny_visibility_level, #event_service, #initialize, #log_info, #notification_service, #repository, #system_hook_service, #todo_service

Methods included from Gitlab::CurrentSettings

#current_application_settings, #fake_application_settings

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#allowed_transfer?(current_user, project, namespace) ⇒ Boolean


68
69
70
71
72
73
# File 'app/services/projects/transfer_service.rb', line 68

def allowed_transfer?(current_user, project, namespace)
  namespace &&
    can?(current_user, :change_namespace, project) &&
    namespace.id != project.namespace_id &&
    current_user.can?(:create_projects, namespace)
end

#execute(new_namespace) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/projects/transfer_service.rb', line 14

def execute(new_namespace)
  if allowed_transfer?(current_user, project, new_namespace)
    transfer(project, new_namespace)
  else
    project.errors.add(:new_namespace, 'is invalid')
    false
  end
rescue Projects::TransferService::TransferError => ex
  project.reload
  project.errors.add(:new_namespace, ex.message)
  false
end

#transfer(project, new_namespace) ⇒ Object


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
# File 'app/services/projects/transfer_service.rb', line 27

def transfer(project, new_namespace)
  Project.transaction do
    old_path = project.path_with_namespace
    old_namespace = project.namespace
    new_path = File.join(new_namespace.try(:path) || '', project.path)

    if Project.where(path: project.path, namespace_id: new_namespace.try(:id)).present?
      raise TransferError.new("Project with same path in target namespace already exists")
    end

    project.expire_caches_before_rename(old_path)

    # Apply new namespace id and visibility level
    project.namespace = new_namespace
    project.visibility_level = new_namespace.visibility_level unless project.visibility_level_allowed_by_group?
    project.save!

    # Notifications
    project.send_move_instructions(old_path)

    # Move main repository
    unless gitlab_shell.mv_repository(old_path, new_path)
      raise TransferError.new('Cannot move project')
    end

    # Move wiki repo also if present
    gitlab_shell.mv_repository("#{old_path}.wiki", "#{new_path}.wiki")

    # clear project cached events
    project.reset_events_cache

    # Move uploads
    Gitlab::UploadsTransfer.new.move_project(project.path, old_namespace.path, new_namespace.path)

    project.old_path_with_namespace = old_path

    SystemHooksService.new.execute_hooks_for(project, :transfer)
    true
  end
end