diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-30 09:07:58 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-30 09:07:58 +0000 |
commit | 45b4df3e57c949c88107840c44ccbfaf2eabdf26 (patch) | |
tree | f73c1533a75b03d2ceb1361644e0d8ab97568a8f /app/models | |
parent | 7421e6f9f2b5889b05738af7eba568af6ae3fcbc (diff) | |
download | gitlab-ce-45b4df3e57c949c88107840c44ccbfaf2eabdf26.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/container_registry/event.rb | 69 | ||||
-rw-r--r-- | app/models/container_repository.rb | 7 |
2 files changed, 76 insertions, 0 deletions
diff --git a/app/models/container_registry/event.rb b/app/models/container_registry/event.rb new file mode 100644 index 00000000000..109fda675a2 --- /dev/null +++ b/app/models/container_registry/event.rb @@ -0,0 +1,69 @@ +# frozen_string_literal: true + +module ContainerRegistry + class Event + ALLOWED_ACTIONS = %w(push delete).freeze + PUSH_ACTION = 'push' + EVENT_TRACKING_CATEGORY = 'container_registry:notification' + + attr_reader :event + + def initialize(event) + @event = event + end + + def supported? + action.in?(ALLOWED_ACTIONS) + end + + def handle! + # no op + 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 + + def target_repository? + !target_tag? && event['target'].has_key?('repository') + end + + def action + event['action'] + end + + def action_push? + PUSH_ACTION == action + end + + def container_repository_exists? + return unless container_registry_path + + ContainerRepository.exists_by_path?(container_registry_path) + end + + def container_registry_path + path = event.dig('target', 'repository') + return unless path + + ContainerRegistry::Path.new(path) + end + end +end + +::ContainerRegistry::Event.prepend_if_ee('EE::ContainerRegistry::Event') diff --git a/app/models/container_repository.rb b/app/models/container_repository.rb index fcbfda8fbc2..b74c044b687 100644 --- a/app/models/container_repository.rb +++ b/app/models/container_repository.rb @@ -16,6 +16,13 @@ class ContainerRepository < ApplicationRecord where(project_id: Project.for_group_and_its_subgroups(group).with_container_registry.select(:id)) end + def self.exists_by_path?(path) + where( + project: path.repository_project, + name: path.repository_name + ).exists? + end + # rubocop: disable CodeReuse/ServiceClass def registry @registry ||= begin |