Class: Integrations::SlackWorkspace::IntegrationApiScope

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/integrations/slack_workspace/integration_api_scope.rb

Constant Summary

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Class Method Summary collapse

Methods inherited from ApplicationRecord

===, cached_column_list, #create_or_load_association, current_transaction, declarative_enum, default_select_columns, delete_all_returning, #deleted_from_database?, id_in, id_not_in, iid_in, nullable_column?, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from Organizations::Sharding

#sharding_organization

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.update_scopes(slack_integrations, scopes) ⇒ Object

Efficient scope propagation



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
# File 'app/models/integrations/slack_workspace/integration_api_scope.rb', line 19

def self.update_scopes(slack_integrations, scopes)
  return if slack_integrations.empty?

  slack_integrations = slack_integrations.preload_integration_organization
  organization_ids_by_integration = slack_integrations.each_with_object({}) do |slack_integration, result|
    result[slack_integration.id] = slack_integration.integration.organization_id_from_parent
  end

  scopes_by_organization = ApiScope.find_or_initialize_by_names_and_organizations(
    scopes.pluck(:name),
    organization_ids_by_integration.values.uniq
  )

  attrs = slack_integrations.flat_map do |slack_integration|
    organization_id = organization_ids_by_integration[slack_integration.id]
    scopes_by_organization[organization_id].map do |scope|
      {
        slack_integration_id: slack_integration.id,
        slack_api_scope_id: scope.id,
        organization_id: slack_integration.integration.organization_id,
        group_id: slack_integration.integration.group_id,
        project_id: slack_integration.integration.project_id
      }
    end
  end

  # We don't know which ones to preserve - so just delete them all in a single query
  transaction do
    where(slack_integration_id: slack_integrations.pluck_primary_key).delete_all
    upsert_all(attrs, on_duplicate: :skip)
  end
end