summaryrefslogtreecommitdiff
path: root/app/workers/quality/test_data_cleanup_worker.rb
blob: 68b36cacbbf66abe00d5c87bde19076769e790d7 (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
# frozen_string_literal: true

module Quality
  class TestDataCleanupWorker
    include ApplicationWorker

    data_consistency :always
    feature_category :quality_management
    urgency :low

    include CronjobQueue
    idempotent!

    KEEP_RECENT_DATA_DAY = 3
    GROUP_PATH_PATTERN = 'test-group-fulfillment'
    GROUP_OWNER_EMAIL_PATTERN = %w(test-user- gitlab-qa-user qa-user-).freeze

    # Remove test groups generated in E2E tests on gstg
    # rubocop: disable CodeReuse/ActiveRecord
    def perform
      return unless Gitlab.staging?

      Group.where('path like ?', "#{GROUP_PATH_PATTERN}%").where('created_at < ?', KEEP_RECENT_DATA_DAY.days.ago).each do |group|
        next unless GROUP_OWNER_EMAIL_PATTERN.any? { |pattern| group.owners.first.email.include?(pattern) }

        with_context(namespace: group, user: group.owners.first) do
          Groups::DestroyService.new(group, group.owners.first).execute
        end
      end
    end
    # rubocop: enable CodeReuse/ActiveRecord
  end
end