File: project_import_data.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 (53 lines) | stat: -rw-r--r-- 1,687 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
43
44
45
46
47
48
49
50
51
52
53
# frozen_string_literal: true

require 'carrierwave/orm/activerecord'

class ProjectImportData < ApplicationRecord
  prepend_mod_with('ProjectImportData') # rubocop: disable Cop/InjectEnterpriseEditionModule

  # Timeout strategy can only be changed via API, currently only with GitHub and BitBucket Server
  OPTIMISTIC_TIMEOUT = "optimistic"
  PESSIMISTIC_TIMEOUT = "pessimistic"
  TIMEOUT_STRATEGIES = [OPTIMISTIC_TIMEOUT, PESSIMISTIC_TIMEOUT].freeze

  belongs_to :project, inverse_of: :import_data
  attr_encrypted :credentials,
    key: Settings.attr_encrypted_db_key_base,
    marshal: true,
    encode: true,
    mode: :per_attribute_iv_and_salt,
    insecure_mode: true,
    algorithm: 'aes-256-cbc'

  # NOTE
  # We are serializing a project as `data` in an "unsafe" way here
  # because the credentials are necessary for a successful import.
  # This is safe because the serialization is only going between rails
  # and the database, never to any end users.
  serialize :data, Serializers::UnsafeJson # rubocop:disable Cop/ActiveRecordSerialize

  validates :project, presence: true

  before_validation :symbolize_credentials

  def symbolize_credentials
    # bang doesn't work here - attr_encrypted makes it not to work
    self.credentials = self.credentials.deep_symbolize_keys unless self.credentials.blank?
  end

  def merge_data(hash)
    self.data = data.to_h.merge(hash) unless hash.empty?
  end

  def merge_credentials(hash)
    self.credentials = credentials.to_h.merge(hash) unless hash.empty?
  end

  def clear_credentials
    self.credentials = {}
  end

  def user_mapping_enabled?
    self.data&.dig('user_contribution_mapping_enabled') || false
  end
end