summaryrefslogtreecommitdiff
path: root/app/services/ci/job_artifacts/bulk_delete_by_project_service.rb
blob: 738fa19e29bf1ad175dd82c22faa3379508998b2 (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
# frozen_string_literal: true

module Ci
  module JobArtifacts
    class BulkDeleteByProjectService
      include BaseServiceUtility

      JOB_ARTIFACTS_COUNT_LIMIT = 50

      def initialize(job_artifact_ids:, project:, current_user:)
        @job_artifact_ids = job_artifact_ids
        @project = project
        @current_user = current_user
      end

      def execute
        if exceeds_limits?
          return ServiceResponse.error(
            message: "Can only delete up to #{JOB_ARTIFACTS_COUNT_LIMIT} job artifacts per call"
          )
        end

        find_result = find_artifacts

        return ServiceResponse.error(message: find_result[:error_message]) if find_result[:error_message]

        @job_artifact_scope = find_result[:scope]

        unless all_job_artifacts_belong_to_project?
          return ServiceResponse.error(message: 'Not all artifacts belong to requested project')
        end

        result = Ci::JobArtifacts::DestroyBatchService.new(job_artifact_scope).execute

        destroyed_artifacts_count = result.fetch(:destroyed_artifacts_count)
        destroyed_ids = result.fetch(:destroyed_ids)

        ServiceResponse.success(
          payload: {
            destroyed_count: destroyed_artifacts_count,
            destroyed_ids: destroyed_ids,
            errors: []
          })
      end

      private

      def find_artifacts
        job_artifacts = ::Ci::JobArtifact.id_in(job_artifact_ids)

        error_message = nil
        if job_artifacts.count != job_artifact_ids.count
          not_found_artifacts = job_artifact_ids - job_artifacts.map(&:id)
          error_message = "Artifacts (#{not_found_artifacts.join(',')}) not found"
        end

        { scope: job_artifacts, error_message: error_message }
      end

      def exceeds_limits?
        job_artifact_ids.count > JOB_ARTIFACTS_COUNT_LIMIT
      end

      def all_job_artifacts_belong_to_project?
        # rubocop:disable CodeReuse/ActiveRecord
        job_artifact_scope.pluck(:project_id).all?(project.id)
        # rubocop:enable CodeReuse/ActiveRecord
      end

      attr_reader :job_artifact_ids, :job_artifact_scope, :current_user, :project
    end
  end
end