debian-mirror-gitlab/app/models/container_registry/event.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

145 lines
3.4 KiB
Ruby
Raw Normal View History

2020-04-22 19:07:51 +05:30
# frozen_string_literal: true
module ContainerRegistry
class Event
2022-07-16 23:28:13 +05:30
include Gitlab::Utils::StrongMemoize
2020-04-22 19:07:51 +05:30
ALLOWED_ACTIONS = %w(push delete).freeze
PUSH_ACTION = 'push'
2022-08-13 15:12:31 +05:30
DELETE_ACTION = 'delete'
2020-04-22 19:07:51 +05:30
EVENT_TRACKING_CATEGORY = 'container_registry:notification'
2023-05-27 22:25:52 +05:30
EVENT_PREFIX = 'i_container_registry'
2023-04-23 21:23:45 +05:30
ALLOWED_ACTOR_TYPES = %w(
personal_access_token
build
gitlab_or_ldap
).freeze
TRACKABLE_ACTOR_EVENTS = %w(
push_tag
delete_tag
push_repository
delete_repository
create_repository
).freeze
2020-04-22 19:07:51 +05:30
attr_reader :event
def initialize(event)
@event = event
end
def supported?
action.in?(ALLOWED_ACTIONS)
end
def handle!
2022-07-16 23:28:13 +05:30
update_project_statistics
2020-04-22 19:07:51 +05:30
end
def track!
tracked_target = target_tag? ? :tag : :repository
tracking_action = "#{action}_#{tracked_target}"
if target_repository? && action_push? && !container_repository_exists?
tracking_action = "create_repository"
end
::Gitlab::Tracking.event(EVENT_TRACKING_CATEGORY, tracking_action)
2023-04-23 21:23:45 +05:30
2023-05-27 22:25:52 +05:30
if manifest_delete_event?
::Gitlab::UsageDataCounters::ContainerRegistryEventCounter.count("#{EVENT_PREFIX}_delete_manifest")
else
event = usage_data_event_for(tracking_action)
::Gitlab::UsageDataCounters::HLLRedisCounter.track_event(event, values: originator.id) if event
end
2020-04-22 19:07:51 +05:30
end
private
def target_tag?
# There is no clear indication in the event structure when we delete a top-level manifest
# except existance of "tag" key
event['target'].has_key?('tag')
end
2022-08-13 15:12:31 +05:30
def target_digest?
event['target'].has_key?('digest')
end
2020-04-22 19:07:51 +05:30
def target_repository?
!target_tag? && event['target'].has_key?('repository')
end
def action
event['action']
end
def action_push?
PUSH_ACTION == action
end
2022-08-13 15:12:31 +05:30
def action_delete?
DELETE_ACTION == action
end
2020-04-22 19:07:51 +05:30
def container_repository_exists?
return unless container_registry_path
ContainerRepository.exists_by_path?(container_registry_path)
end
def container_registry_path
2022-07-16 23:28:13 +05:30
strong_memoize(:container_registry_path) do
path = event.dig('target', 'repository')
next unless path
ContainerRegistry::Path.new(path)
end
end
def project
container_registry_path&.repository_project
end
2023-04-23 21:23:45 +05:30
# counter name for unique user tracking (for MAU)
def usage_data_event_for(tracking_action)
return unless originator
return unless TRACKABLE_ACTOR_EVENTS.include?(tracking_action)
"#{EVENT_PREFIX}_#{tracking_action}_user"
end
def originator_type
event.dig('actor', 'user_type')
end
def originator
return unless ALLOWED_ACTOR_TYPES.include?(originator_type)
username = event.dig('actor', 'name')
return unless username
strong_memoize(:originator) do
User.find_by_username(username)
end
end
2023-05-27 22:25:52 +05:30
def manifest_delete_event?
action_delete? && target_digest?
end
2022-07-16 23:28:13 +05:30
def update_project_statistics
return unless supported?
2023-05-27 22:25:52 +05:30
return unless target_tag? || manifest_delete_event?
2022-07-16 23:28:13 +05:30
return unless project
2020-04-22 19:07:51 +05:30
2022-07-23 23:45:48 +05:30
Rails.cache.delete(project.root_ancestor.container_repositories_size_cache_key)
2022-07-16 23:28:13 +05:30
ProjectCacheWorker.perform_async(project.id, [], [:container_registry_size])
2020-04-22 19:07:51 +05:30
end
end
end
2021-06-08 01:23:25 +05:30
::ContainerRegistry::Event.prepend_mod_with('ContainerRegistry::Event')