File: cancel_project_import_service.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (42 lines) | stat: -rw-r--r-- 1,074 bytes parent folder | download
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
# frozen_string_literal: true

module Import
  module Github
    class CancelProjectImportService < ::BaseService
      def execute
        return error('Not Found', :not_found) unless authorized_to_read?
        return error('Unauthorized access', :forbidden) unless authorized_to_cancel?

        if project.import_state.completed?
          error(cannot_cancel_error_message, :bad_request)
        else
          project.import_state.cancel
          metrics.track_canceled_import

          success(project: project)
        end
      end

      private

      def authorized_to_read?
        can?(current_user, :read_project, project)
      end

      def authorized_to_cancel?
        can?(current_user, :owner_access, project)
      end

      def cannot_cancel_error_message
        format(
          _('The import cannot be canceled because it is %{project_status}'),
          project_status: project.import_state.status
        )
      end

      def metrics
        @metrics ||= Gitlab::Import::Metrics.new(:github_importer, project)
      end
    end
  end
end