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

migrate_links_for_vulnerability_findings.rb « background_migration « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0e38be3b4c9ce6e0ddc750c18bd6f9714b555aaf (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
80
81
82
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    # The class to migrate the link data into their own records from the json attribute
    class MigrateLinksForVulnerabilityFindings < BatchedMigrationJob
      feature_category :vulnerability_management
      operation_name :migrate_links_for_vulnerability_findings

      # The class is mimicking Vulnerabilites::Finding
      class Finding < ApplicationRecord
        self.table_name = 'vulnerability_occurrences'

        validates :details, json_schema: { filename: 'vulnerability_finding_details', draft: 7 }, if: false
      end

      # The class is mimicking Vulnerabilites::FindingLink
      class Link < ApplicationRecord
        self.table_name = 'vulnerability_finding_links'
      end

      def perform
        each_sub_batch do |sub_batch|
          migrate_remediations(sub_batch)
        end
      end

      private

      def migrate_remediations(sub_batch)
        sub_batch.each do |finding|
          links = extract_links(finding.raw_metadata)

          list_of_attrs = links.map do |link|
            build_link(finding, link)
          end

          next unless list_of_attrs.present?

          begin
            create_links(list_of_attrs)
          rescue ActiveRecord::RecordNotUnique
          rescue StandardError => e
            logger.error(
              message: e.message,
              class: self.class.name,
              model_id: finding.id
            )
          end
        end
      end

      def build_link(finding, link)
        current_time = Time.current
        {
          vulnerability_occurrence_id: finding.id,
          name: link['name'],
          url: link['url'],
          created_at: current_time,
          updated_at: current_time
        }
      end

      def create_links(attributes)
        Link.upsert_all(attributes, returning: false)
      end

      def extract_links(metadata)
        parsed_metadata = Gitlab::Json.parse(metadata)
        parsed_links = Array.wrap(parsed_metadata['links'])

        return [] if parsed_links.blank?

        parsed_links.select { |link| link.try(:[], 'url').present? }.uniq
      end

      def logger
        @logger ||= ::Gitlab::AppLogger
      end
    end
  end
end