Class: SlackIntegration

Inherits:
ApplicationRecord show all
Includes:
EachBatch, Gitlab::EncryptedAttribute
Defined in:
app/models/slack_integration.rb

Constant Summary collapse

ALL_FEATURES =
%i[commands notifications].freeze
SCOPE_COMMANDS =
'commands'
SCOPE_CHAT_WRITE =
'chat:write'
SCOPE_CHAT_WRITE_PUBLIC =
'chat:write.public'
ORGANIZATION_ALIAS =
'gitlab-organization'
SCOPES =

These scopes are requested when installing the app, additional scopes will need reauthorization. api.slack.com/authentication/oauth-v2#asking

[SCOPE_COMMANDS, SCOPE_CHAT_WRITE, SCOPE_CHAT_WRITE_PUBLIC].freeze
DATABASE_ATTRIBUTES =
%w[
  team_id team_name user_id bot_user_id encrypted_bot_access_token encrypted_bot_access_token_iv
].freeze

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

Instance 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

.organization_alias(organization_id) ⇒ Object

Raises:

  • (ArgumentError)


60
61
62
63
64
# File 'app/models/slack_integration.rb', line 60

def self.organization_alias(organization_id)
  raise ArgumentError, 'organization_id must be an Integer' unless organization_id.is_a?(Integer)

  [SlackIntegration::ORGANIZATION_ALIAS, organization_id].join('-')
end

Instance Method Details

#all_features_supported?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'app/models/slack_integration.rb', line 83

def all_features_supported?
  ALL_FEATURES.all? { |feature| feature_available?(feature) } # rubocop: disable Gitlab/FeatureAvailableUsage
end

#authorized_scope_namesObject



99
100
101
# File 'app/models/slack_integration.rb', line 99

def authorized_scope_names
  slack_api_scopes.pluck(:name)
end

#authorized_scope_names=(names) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/slack_integration.rb', line 87

def authorized_scope_names=(names)
  names = Array.wrap(names).flat_map { |name| name.split(',') }.map(&:strip)

  # TODO: get `organization_id_from_parent` directly from SlackIntegration.
  # The `organization_id_from_parent` should be moved to this model when sharding key is finalized
  # https://gitlab.com/gitlab-org/gitlab/-/work_items/582748
  scopes = ::Integrations::SlackWorkspace::ApiScope.find_or_initialize_by_names(
    names, organization_id: integration.organization_id_from_parent
  )
  self.slack_api_scopes = scopes
end

#feature_available?(feature_name) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/slack_integration.rb', line 66

def feature_available?(feature_name)
  case feature_name
  when :commands
    # The slash commands feature requires 'commands' scope.
    # All records will support this scope, as this was the original feature.
    true
  when :notifications
    scoped_to?(SCOPE_CHAT_WRITE, SCOPE_CHAT_WRITE_PUBLIC)
  else
    false
  end
end

#to_database_hashObject



103
104
105
# File 'app/models/slack_integration.rb', line 103

def to_database_hash
  attributes_for_database.slice(*DATABASE_ATTRIBUTES)
end

#upgrade_needed?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'app/models/slack_integration.rb', line 79

def upgrade_needed?
  !all_features_supported?
end