Class: Projects::ImportExport::RelationImportService

Inherits:
Object
  • Object
show all
Includes:
Services::ReturnServiceResponses
Defined in:
app/services/projects/import_export/relation_import_service.rb

Constant Summary collapse

IMPORTABLE_RELATIONS =
%w[issues merge_requests milestones ci_pipelines].freeze

Instance Method Summary collapse

Methods included from Services::ReturnServiceResponses

#error, #success

Constructor Details

#initialize(current_user:, params:) ⇒ RelationImportService

Creates a new RelationImportService.

Parameters:

  • current_user (User)
  • params (Hash)

Options Hash (params:):

  • path (String)

    The full path of the project

  • relation (String)

    The relation to import. See IMPORTABLE_RELATIONS for permitted values.

  • file (UploadedFile)

    The export archive containing the data to import



19
20
21
22
# File 'app/services/projects/import_export/relation_import_service.rb', line 19

def initialize(current_user:, params:)
  @current_user = current_user
  @params = params
end

Instance Method Details

#executeServices::ServiceResponse

Checks the validity of the chosen project and triggers the re-import of the chosen relation.

Returns:

  • (Services::ServiceResponse)


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
# File 'app/services/projects/import_export/relation_import_service.rb', line 28

def execute
  return error(_('Project not found'), :not_found) unless project

  unless relation_valid?
    return error(
      format(
        _('Imported relation must be one of %{relations}'),
        relations: IMPORTABLE_RELATIONS.to_sentence(last_word_connector: ', or ')
      ),
      :bad_request
    )
  end

  return error(_('You are not authorized to perform this action'), :forbidden) unless user_permitted?
  return error(_('A relation import is already in progress for this project'), :conflict) if import_in_progress?

  tracker = create_status_tracker

  unless tracker.persisted?
    return error(
      format(
        _('Relation import could not be created: %{errors}'),
        errors: tracker.errors.full_messages.to_sentence
      ),
      :bad_request
    )
  end

  attach_import_file

  Projects::ImportExport::RelationImportWorker.perform_async(
    tracker.id,
    current_user.id
  )

  success(tracker)
end