2020-03-30 05:07:58 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ContainerRegistry
|
|
|
|
class Event
|
2022-05-09 08:08:55 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2020-03-30 05:07:58 -04:00
|
|
|
ALLOWED_ACTIONS = %w(push delete).freeze
|
|
|
|
PUSH_ACTION = 'push'
|
2022-07-19 23:09:04 -04:00
|
|
|
DELETE_ACTION = 'delete'
|
2020-03-30 05:07:58 -04:00
|
|
|
EVENT_TRACKING_CATEGORY = 'container_registry:notification'
|
|
|
|
|
|
|
|
attr_reader :event
|
|
|
|
|
|
|
|
def initialize(event)
|
|
|
|
@event = event
|
|
|
|
end
|
|
|
|
|
|
|
|
def supported?
|
|
|
|
action.in?(ALLOWED_ACTIONS)
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle!
|
2022-05-09 08:08:55 -04:00
|
|
|
update_project_statistics
|
2020-03-30 05:07:58 -04:00
|
|
|
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)
|
|
|
|
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-07-19 23:09:04 -04:00
|
|
|
def target_digest?
|
|
|
|
event['target'].has_key?('digest')
|
|
|
|
end
|
|
|
|
|
2020-03-30 05:07:58 -04:00
|
|
|
def target_repository?
|
|
|
|
!target_tag? && event['target'].has_key?('repository')
|
|
|
|
end
|
|
|
|
|
|
|
|
def action
|
|
|
|
event['action']
|
|
|
|
end
|
|
|
|
|
|
|
|
def action_push?
|
|
|
|
PUSH_ACTION == action
|
|
|
|
end
|
|
|
|
|
2022-07-19 23:09:04 -04:00
|
|
|
def action_delete?
|
|
|
|
DELETE_ACTION == action
|
|
|
|
end
|
|
|
|
|
2020-03-30 05:07:58 -04:00
|
|
|
def container_repository_exists?
|
|
|
|
return unless container_registry_path
|
|
|
|
|
|
|
|
ContainerRepository.exists_by_path?(container_registry_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def container_registry_path
|
2022-05-09 08:08:55 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
def update_project_statistics
|
|
|
|
return unless supported?
|
2022-07-19 23:09:04 -04:00
|
|
|
return unless target_tag? || (action_delete? && target_digest?)
|
2022-05-09 08:08:55 -04:00
|
|
|
return unless project
|
2020-03-30 05:07:58 -04:00
|
|
|
|
2022-06-03 08:09:05 -04:00
|
|
|
Rails.cache.delete(project.root_ancestor.container_repositories_size_cache_key)
|
2022-05-09 08:08:55 -04:00
|
|
|
ProjectCacheWorker.perform_async(project.id, [], [:container_registry_size])
|
2020-03-30 05:07:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
::ContainerRegistry::Event.prepend_mod_with('ContainerRegistry::Event')
|