Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mark_packages_for_destruction_service.rb « packages « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 023392cf2d9b9242eed01922806fb6a7aed0ed69 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true

module Packages
  class MarkPackagesForDestructionService
    include BaseServiceUtility

    BATCH_SIZE = 20

    UNAUTHORIZED_RESPONSE = ServiceResponse.error(
      message: "You don't have the permission to perform this action",
      reason: :unauthorized
    ).freeze

    ERROR_RESPONSE = ServiceResponse.error(
      message: 'Failed to mark the packages as pending destruction'
    ).freeze

    SUCCESS_RESPONSE = ServiceResponse.success(
      message: 'Packages were successfully marked as pending destruction'
    ).freeze

    # Initialize this service with the given packages and user.
    #
    # * `packages`: must be an ActiveRecord relationship.
    # * `current_user`: an User object. Could be nil.
    def initialize(packages:, current_user: nil)
      @packages = packages
      @current_user = current_user
    end

    def execute(batch_size: BATCH_SIZE)
      no_access = false
      min_batch_size = [batch_size, BATCH_SIZE].min

      @packages.each_batch(of: min_batch_size) do |batched_packages|
        loaded_packages = batched_packages.including_project_route.to_a

        break no_access = true unless can_destroy_packages?(loaded_packages)

        ::Packages::Package.id_in(loaded_packages.map(&:id))
                           .update_all(status: :pending_destruction)

        sync_maven_metadata(loaded_packages)
        mark_package_files_for_destruction(loaded_packages)
      end

      return UNAUTHORIZED_RESPONSE if no_access

      SUCCESS_RESPONSE
    rescue StandardError
      ERROR_RESPONSE
    end

    private

    def mark_package_files_for_destruction(packages)
      ::Packages::MarkPackageFilesForDestructionWorker.bulk_perform_async_with_contexts(
        packages,
        arguments_proc: -> (package) { package.id },
        context_proc: -> (package) { { project: package.project, user: @current_user } }
      )
    end

    def sync_maven_metadata(packages)
      maven_packages_with_version = packages.select { |pkg| pkg.maven? && pkg.version? }
      ::Packages::Maven::Metadata::SyncWorker.bulk_perform_async_with_contexts(
        maven_packages_with_version,
        arguments_proc: -> (package) { [@current_user.id, package.project_id, package.name] },
        context_proc: -> (package) { { project: package.project, user: @current_user } }
      )
    end

    def can_destroy_packages?(packages)
      packages.all? do |package|
        can?(@current_user, :destroy_package, package)
      end
    end
  end
end