Commit 54651f01 authored by Giorgenes Gelatti's avatar Giorgenes Gelatti Committed by ggelatti

Adds package event tracking

Adds package event model
and extend package event
tracking to support
existing tracking calls.
parent 05719e1e
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
module Groups module Groups
module Registry module Registry
class RepositoriesController < Groups::ApplicationController class RepositoriesController < Groups::ApplicationController
include PackagesHelper
before_action :verify_container_registry_enabled! before_action :verify_container_registry_enabled!
before_action :authorize_read_container_image! before_action :authorize_read_container_image!
...@@ -13,7 +15,7 @@ module Groups ...@@ -13,7 +15,7 @@ module Groups
.execute .execute
.with_api_entity_associations .with_api_entity_associations
track_event(:list_repositories) track_package_event(:list_repositories, :container)
serializer = ContainerRepositoriesSerializer serializer = ContainerRepositoriesSerializer
.new(current_user: current_user) .new(current_user: current_user)
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
module Projects module Projects
module Registry module Registry
class RepositoriesController < ::Projects::Registry::ApplicationController class RepositoriesController < ::Projects::Registry::ApplicationController
include PackagesHelper
before_action :authorize_update_container_image!, only: [:destroy] before_action :authorize_update_container_image!, only: [:destroy]
before_action :ensure_root_container_repository!, only: [:index] before_action :ensure_root_container_repository!, only: [:index]
...@@ -13,7 +15,7 @@ module Projects ...@@ -13,7 +15,7 @@ module Projects
@images = ContainerRepositoriesFinder.new(user: current_user, subject: project, params: params.slice(:name)) @images = ContainerRepositoriesFinder.new(user: current_user, subject: project, params: params.slice(:name))
.execute .execute
track_event(:list_repositories) track_package_event(:list_repositories, :container)
serializer = ContainerRepositoriesSerializer serializer = ContainerRepositoriesSerializer
.new(project: project, current_user: current_user) .new(project: project, current_user: current_user)
...@@ -31,7 +33,7 @@ module Projects ...@@ -31,7 +33,7 @@ module Projects
def destroy def destroy
image.delete_scheduled! image.delete_scheduled!
DeleteContainerRepositoryWorker.perform_async(current_user.id, image.id) # rubocop:disable CodeReuse/Worker DeleteContainerRepositoryWorker.perform_async(current_user.id, image.id) # rubocop:disable CodeReuse/Worker
track_event(:delete_repository) track_package_event(:delete_repository, :container)
respond_to do |format| respond_to do |format|
format.json { head :no_content } format.json { head :no_content }
......
...@@ -3,12 +3,15 @@ ...@@ -3,12 +3,15 @@
module Projects module Projects
module Registry module Registry
class TagsController < ::Projects::Registry::ApplicationController class TagsController < ::Projects::Registry::ApplicationController
include PackagesHelper
before_action :authorize_destroy_container_image!, only: [:destroy] before_action :authorize_destroy_container_image!, only: [:destroy]
LIMIT = 15 LIMIT = 15
def index def index
track_event(:list_tags) track_package_event(:list_tags, :tag)
respond_to do |format| respond_to do |format|
format.json do format.json do
render json: ContainerTagsSerializer render json: ContainerTagsSerializer
...@@ -23,7 +26,7 @@ module Projects ...@@ -23,7 +26,7 @@ module Projects
result = Projects::ContainerRepository::DeleteTagsService result = Projects::ContainerRepository::DeleteTagsService
.new(image.project, current_user, tags: [params[:id]]) .new(image.project, current_user, tags: [params[:id]])
.execute(image) .execute(image)
track_event(:delete_tag) track_package_event(:delete_tag, :tag)
respond_to do |format| respond_to do |format|
format.json { head(result[:status] == :success ? :ok : bad_request) } format.json { head(result[:status] == :success ? :ok : bad_request) }
...@@ -40,7 +43,7 @@ module Projects ...@@ -40,7 +43,7 @@ module Projects
result = Projects::ContainerRepository::DeleteTagsService result = Projects::ContainerRepository::DeleteTagsService
.new(image.project, current_user, tags: tag_names) .new(image.project, current_user, tags: tag_names)
.execute(image) .execute(image)
track_event(:delete_tag_bulk) track_package_event(:delete_tag_bulk, :tag)
respond_to do |format| respond_to do |format|
format.json { head(result[:status] == :success ? :no_content : :bad_request) } format.json { head(result[:status] == :success ? :no_content : :bad_request) }
......
...@@ -56,4 +56,9 @@ module PackagesHelper ...@@ -56,4 +56,9 @@ module PackagesHelper
coming_soon_json: packages_coming_soon_data(resource).to_json coming_soon_json: packages_coming_soon_data(resource).to_json
} }
end end
def track_package_event(event_name, scope, **args)
::Packages::CreateEventService.new(nil, current_user, event_name: event_name, scope: scope).execute
track_event(event_name, **args)
end
end end
# frozen_string_literal: true
class Packages::Event < ApplicationRecord
belongs_to :package, optional: true
# FIXME: Remove debian: 9 from here when it's added to the types in package.rb model
EVENT_SCOPES = ::Packages::Package.package_types.merge(debian: 9, container: 1000, tag: 1001).freeze
enum event_scope: EVENT_SCOPES
enum event_type: {
push_package: 0,
delete_package: 1,
pull_package: 2,
search_package: 3,
list_package: 4,
list_repositories: 5,
delete_repository: 6,
delete_tag: 7,
delete_tag_bulk: 8,
list_tags: 9,
cli_metadata: 10
}
enum originator_type: { user: 0, deploy_token: 1, guest: 2 }
end
# frozen_string_literal: true
module Packages
class CreateEventService < BaseService
def execute
event_scope = scope.is_a?(::Packages::Package) ? scope.package_type : scope
::Packages::Event.create!(
event_type: event_name,
originator: current_user&.id,
originator_type: originator_type,
event_scope: event_scope
)
end
private
def scope
params[:scope]
end
def event_name
params[:event_name]
end
def originator_type
case current_user
when User
:user
when DeployToken
:deploy_token
else
:guest
end
end
end
end
---
title: Adds package event tracking
merge_request: 41846
author:
type: added
# frozen_string_literal: true
class CreatePackageEvents < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def change
create_table :packages_events do |t|
t.integer :event_type, null: false, limit: 2
t.integer :event_scope, null: false, limit: 2
t.integer :originator_type, null: false, limit: 2
t.bigint :originator
t.datetime_with_timezone :created_at, null: false
t.references :package, primary_key: false, default: nil, index: true, foreign_key: { to_table: :packages_packages, on_delete: :nullify }, type: :bigint
end
end
end
f68d29be164299e5ccf73347841d27b17f028941e37e3510d3da9d513762a17f
\ No newline at end of file
...@@ -14070,6 +14070,25 @@ CREATE SEQUENCE packages_dependency_links_id_seq ...@@ -14070,6 +14070,25 @@ CREATE SEQUENCE packages_dependency_links_id_seq
ALTER SEQUENCE packages_dependency_links_id_seq OWNED BY packages_dependency_links.id; ALTER SEQUENCE packages_dependency_links_id_seq OWNED BY packages_dependency_links.id;
CREATE TABLE packages_events (
id bigint NOT NULL,
event_type smallint NOT NULL,
event_scope smallint NOT NULL,
originator_type smallint NOT NULL,
originator bigint,
created_at timestamp with time zone NOT NULL,
package_id bigint
);
CREATE SEQUENCE packages_events_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE packages_events_id_seq OWNED BY packages_events.id;
CREATE TABLE packages_maven_metadata ( CREATE TABLE packages_maven_metadata (
id bigint NOT NULL, id bigint NOT NULL,
package_id bigint NOT NULL, package_id bigint NOT NULL,
...@@ -17516,6 +17535,8 @@ ALTER TABLE ONLY packages_dependencies ALTER COLUMN id SET DEFAULT nextval('pack ...@@ -17516,6 +17535,8 @@ ALTER TABLE ONLY packages_dependencies ALTER COLUMN id SET DEFAULT nextval('pack
ALTER TABLE ONLY packages_dependency_links ALTER COLUMN id SET DEFAULT nextval('packages_dependency_links_id_seq'::regclass); ALTER TABLE ONLY packages_dependency_links ALTER COLUMN id SET DEFAULT nextval('packages_dependency_links_id_seq'::regclass);
ALTER TABLE ONLY packages_events ALTER COLUMN id SET DEFAULT nextval('packages_events_id_seq'::regclass);
ALTER TABLE ONLY packages_maven_metadata ALTER COLUMN id SET DEFAULT nextval('packages_maven_metadata_id_seq'::regclass); ALTER TABLE ONLY packages_maven_metadata ALTER COLUMN id SET DEFAULT nextval('packages_maven_metadata_id_seq'::regclass);
ALTER TABLE ONLY packages_package_files ALTER COLUMN id SET DEFAULT nextval('packages_package_files_id_seq'::regclass); ALTER TABLE ONLY packages_package_files ALTER COLUMN id SET DEFAULT nextval('packages_package_files_id_seq'::regclass);
...@@ -18709,6 +18730,9 @@ ALTER TABLE ONLY packages_dependencies ...@@ -18709,6 +18730,9 @@ ALTER TABLE ONLY packages_dependencies
ALTER TABLE ONLY packages_dependency_links ALTER TABLE ONLY packages_dependency_links
ADD CONSTRAINT packages_dependency_links_pkey PRIMARY KEY (id); ADD CONSTRAINT packages_dependency_links_pkey PRIMARY KEY (id);
ALTER TABLE ONLY packages_events
ADD CONSTRAINT packages_events_pkey PRIMARY KEY (id);
ALTER TABLE ONLY packages_maven_metadata ALTER TABLE ONLY packages_maven_metadata
ADD CONSTRAINT packages_maven_metadata_pkey PRIMARY KEY (id); ADD CONSTRAINT packages_maven_metadata_pkey PRIMARY KEY (id);
...@@ -20696,6 +20720,8 @@ CREATE UNIQUE INDEX index_packages_dependencies_on_name_and_version_pattern ON p ...@@ -20696,6 +20720,8 @@ CREATE UNIQUE INDEX index_packages_dependencies_on_name_and_version_pattern ON p
CREATE INDEX index_packages_dependency_links_on_dependency_id ON packages_dependency_links USING btree (dependency_id); CREATE INDEX index_packages_dependency_links_on_dependency_id ON packages_dependency_links USING btree (dependency_id);
CREATE INDEX index_packages_events_on_package_id ON packages_events USING btree (package_id);
CREATE INDEX index_packages_maven_metadata_on_package_id_and_path ON packages_maven_metadata USING btree (package_id, path); CREATE INDEX index_packages_maven_metadata_on_package_id_and_path ON packages_maven_metadata USING btree (package_id, path);
CREATE INDEX index_packages_nuget_dl_metadata_on_dependency_link_id ON packages_nuget_dependency_link_metadata USING btree (dependency_link_id); CREATE INDEX index_packages_nuget_dl_metadata_on_dependency_link_id ON packages_nuget_dependency_link_metadata USING btree (dependency_link_id);
...@@ -23477,6 +23503,9 @@ ALTER TABLE ONLY merge_request_user_mentions ...@@ -23477,6 +23503,9 @@ ALTER TABLE ONLY merge_request_user_mentions
ALTER TABLE ONLY ci_job_artifacts ALTER TABLE ONLY ci_job_artifacts
ADD CONSTRAINT fk_rails_c5137cb2c1 FOREIGN KEY (job_id) REFERENCES ci_builds(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_c5137cb2c1 FOREIGN KEY (job_id) REFERENCES ci_builds(id) ON DELETE CASCADE;
ALTER TABLE ONLY packages_events
ADD CONSTRAINT fk_rails_c6c20d0094 FOREIGN KEY (package_id) REFERENCES packages_packages(id) ON DELETE SET NULL;
ALTER TABLE ONLY project_settings ALTER TABLE ONLY project_settings
ADD CONSTRAINT fk_rails_c6df6e6328 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_c6df6e6328 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE;
......
...@@ -123,7 +123,7 @@ module API ...@@ -123,7 +123,7 @@ module API
bad_request! bad_request!
end end
package_event('push_package') track_package_event('push_package', :composer)
::Packages::Composer::CreatePackageService ::Packages::Composer::CreatePackageService
.new(authorized_user_project, current_user, declared_params) .new(authorized_user_project, current_user, declared_params)
......
...@@ -246,7 +246,7 @@ module API ...@@ -246,7 +246,7 @@ module API
delete do delete do
authorize!(:destroy_package, project) authorize!(:destroy_package, project)
package_event('delete_package', category: 'API::ConanPackages') track_package_event('delete_package', :conan, category: 'API::ConanPackages')
package.destroy package.destroy
end end
......
...@@ -31,7 +31,7 @@ module API ...@@ -31,7 +31,7 @@ module API
authorize_upload!(authorized_user_project) authorize_upload!(authorized_user_project)
bad_request!('File is too large') if authorized_user_project.actual_limits.exceeded?(:debian_max_file_size, params[:file].size) bad_request!('File is too large') if authorized_user_project.actual_limits.exceeded?(:debian_max_file_size, params[:file].size)
package_event('push_package') track_package_event('push_package', :debian)
created! created!
rescue ObjectStorage::RemoteStoreError => e rescue ObjectStorage::RemoteStoreError => e
......
...@@ -4,6 +4,8 @@ module API ...@@ -4,6 +4,8 @@ module API
class GroupContainerRepositories < Grape::API::Instance class GroupContainerRepositories < Grape::API::Instance
include PaginationParams include PaginationParams
helpers ::API::Helpers::PackagesHelpers
before { authorize_read_group_container_images! } before { authorize_read_group_container_images! }
REPOSITORY_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge( REPOSITORY_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(
...@@ -27,7 +29,7 @@ module API ...@@ -27,7 +29,7 @@ module API
user: current_user, subject: user_group user: current_user, subject: user_group
).execute ).execute
track_event('list_repositories') track_package_event('list_repositories', :container)
present paginate(repositories), with: Entities::ContainerRegistry::Repository, tags: params[:tags], tags_count: params[:tags_count] present paginate(repositories), with: Entities::ContainerRegistry::Repository, tags: params[:tags], tags_count: params[:tags_count]
end end
......
...@@ -158,7 +158,7 @@ module API ...@@ -158,7 +158,7 @@ module API
conan_package_reference: params[:conan_package_reference] conan_package_reference: params[:conan_package_reference]
).execute! ).execute!
package_event('pull_package', category: 'API::ConanPackages') if params[:file_name] == ::Packages::Conan::FileMetadatum::PACKAGE_BINARY track_package_event('pull_package', :conan, category: 'API::ConanPackages') if params[:file_name] == ::Packages::Conan::FileMetadatum::PACKAGE_BINARY
present_carrierwave_file!(package_file.file) present_carrierwave_file!(package_file.file)
end end
...@@ -169,7 +169,7 @@ module API ...@@ -169,7 +169,7 @@ module API
def track_push_package_event def track_push_package_event
if params[:file_name] == ::Packages::Conan::FileMetadatum::PACKAGE_BINARY && params[:file].size > 0 # rubocop: disable Style/ZeroLengthPredicate if params[:file_name] == ::Packages::Conan::FileMetadatum::PACKAGE_BINARY && params[:file].size > 0 # rubocop: disable Style/ZeroLengthPredicate
package_event('push_package', category: 'API::ConanPackages') track_package_event('push_package', :conan, category: 'API::ConanPackages')
end end
end end
......
...@@ -48,7 +48,8 @@ module API ...@@ -48,7 +48,8 @@ module API
require_gitlab_workhorse! require_gitlab_workhorse!
end end
def package_event(event_name, **args) def track_package_event(event_name, scope, **args)
::Packages::CreateEventService.new(nil, current_user, event_name: event_name, scope: scope).execute
track_event(event_name, **args) track_event(event_name, **args)
end end
end end
......
...@@ -107,7 +107,7 @@ module API ...@@ -107,7 +107,7 @@ module API
when 'sha1' when 'sha1'
package_file.file_sha1 package_file.file_sha1
else else
package_event('pull_package') if jar_file?(format) track_package_event('pull_package', :maven) if jar_file?(format)
present_carrierwave_file_with_head_support!(package_file.file) present_carrierwave_file_with_head_support!(package_file.file)
end end
end end
...@@ -145,7 +145,7 @@ module API ...@@ -145,7 +145,7 @@ module API
when 'sha1' when 'sha1'
package_file.file_sha1 package_file.file_sha1
else else
package_event('pull_package') if jar_file?(format) track_package_event('pull_package', :maven) if jar_file?(format)
present_carrierwave_file_with_head_support!(package_file.file) present_carrierwave_file_with_head_support!(package_file.file)
end end
...@@ -181,7 +181,7 @@ module API ...@@ -181,7 +181,7 @@ module API
when 'sha1' when 'sha1'
package_file.file_sha1 package_file.file_sha1
else else
package_event('pull_package') if jar_file?(format) track_package_event('pull_package', :maven) if jar_file?(format)
present_carrierwave_file_with_head_support!(package_file.file) present_carrierwave_file_with_head_support!(package_file.file)
end end
...@@ -233,7 +233,7 @@ module API ...@@ -233,7 +233,7 @@ module API
when 'md5' when 'md5'
nil nil
else else
package_event('push_package') if jar_file?(format) track_package_event('push_package', :maven) if jar_file?(format)
file_params = { file_params = {
file: params[:file], file: params[:file],
......
...@@ -141,7 +141,7 @@ module API ...@@ -141,7 +141,7 @@ module API
package_file = ::Packages::PackageFileFinder package_file = ::Packages::PackageFileFinder
.new(package, params[:file_name]).execute! .new(package, params[:file_name]).execute!
package_event('pull_package') track_package_event('pull_package', package)
present_carrierwave_file!(package_file.file) present_carrierwave_file!(package_file.file)
end end
...@@ -157,7 +157,7 @@ module API ...@@ -157,7 +157,7 @@ module API
put ':id/packages/npm/:package_name', requirements: NPM_ENDPOINT_REQUIREMENTS do put ':id/packages/npm/:package_name', requirements: NPM_ENDPOINT_REQUIREMENTS do
authorize_create_package!(user_project) authorize_create_package!(user_project)
package_event('push_package') track_package_event('push_package', :npm)
created_package = ::Packages::Npm::CreatePackageService created_package = ::Packages::Npm::CreatePackageService
.new(user_project, current_user, params.merge(build: current_authenticated_job)).execute .new(user_project, current_user, params.merge(build: current_authenticated_job)).execute
......
...@@ -73,7 +73,7 @@ module API ...@@ -73,7 +73,7 @@ module API
get 'index', format: :json do get 'index', format: :json do
authorize_read_package!(authorized_user_project) authorize_read_package!(authorized_user_project)
track_event('nuget_service_index') track_package_event('cli_metadata', :nuget)
present ::Packages::Nuget::ServiceIndexPresenter.new(authorized_user_project), present ::Packages::Nuget::ServiceIndexPresenter.new(authorized_user_project),
with: ::API::Entities::Nuget::ServiceIndex with: ::API::Entities::Nuget::ServiceIndex
...@@ -105,7 +105,7 @@ module API ...@@ -105,7 +105,7 @@ module API
package_file = ::Packages::CreatePackageFileService.new(package, file_params) package_file = ::Packages::CreatePackageFileService.new(package, file_params)
.execute .execute
package_event('push_package') track_package_event('push_package', :nuget)
::Packages::Nuget::ExtractionWorker.perform_async(package_file.id) # rubocop:disable CodeReuse/Worker ::Packages::Nuget::ExtractionWorker.perform_async(package_file.id) # rubocop:disable CodeReuse/Worker
...@@ -198,7 +198,7 @@ module API ...@@ -198,7 +198,7 @@ module API
not_found!('Package') unless package_file not_found!('Package') unless package_file
package_event('pull_package') track_package_event('pull_package', :nuget)
# nuget and dotnet don't support 302 Moved status codes, supports_direct_download has to be set to false # nuget and dotnet don't support 302 Moved status codes, supports_direct_download has to be set to false
present_carrierwave_file!(package_file.file, supports_direct_download: false) present_carrierwave_file!(package_file.file, supports_direct_download: false)
...@@ -233,7 +233,7 @@ module API ...@@ -233,7 +233,7 @@ module API
.new(authorized_user_project, params[:q], search_options) .new(authorized_user_project, params[:q], search_options)
.execute .execute
package_event('search_package') track_package_event('search_package', :nuget)
present ::Packages::Nuget::SearchResultsPresenter.new(search), present ::Packages::Nuget::SearchResultsPresenter.new(search),
with: ::API::Entities::Nuget::SearchResults with: ::API::Entities::Nuget::SearchResults
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
module API module API
class ProjectContainerRepositories < Grape::API::Instance class ProjectContainerRepositories < Grape::API::Instance
include PaginationParams include PaginationParams
helpers ::API::Helpers::PackagesHelpers
REPOSITORY_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge( REPOSITORY_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(
tag_name: API::NO_SLASH_URL_PART_REGEX) tag_name: API::NO_SLASH_URL_PART_REGEX)
...@@ -28,7 +29,7 @@ module API ...@@ -28,7 +29,7 @@ module API
user: current_user, subject: user_project user: current_user, subject: user_project
).execute ).execute
track_event( 'list_repositories') track_package_event('list_repositories', :container)
present paginate(repositories), with: Entities::ContainerRegistry::Repository, tags: params[:tags], tags_count: params[:tags_count] present paginate(repositories), with: Entities::ContainerRegistry::Repository, tags: params[:tags], tags_count: params[:tags_count]
end end
...@@ -43,7 +44,7 @@ module API ...@@ -43,7 +44,7 @@ module API
authorize_admin_container_image! authorize_admin_container_image!
DeleteContainerRepositoryWorker.perform_async(current_user.id, repository.id) # rubocop:disable CodeReuse/Worker DeleteContainerRepositoryWorker.perform_async(current_user.id, repository.id) # rubocop:disable CodeReuse/Worker
track_event('delete_repository') track_package_event('delete_repository', :container)
status :accepted status :accepted
end end
...@@ -60,7 +61,7 @@ module API ...@@ -60,7 +61,7 @@ module API
authorize_read_container_image! authorize_read_container_image!
tags = Kaminari.paginate_array(repository.tags) tags = Kaminari.paginate_array(repository.tags)
track_event('list_tags') track_package_event('list_tags', :container)
present paginate(tags), with: Entities::ContainerRegistry::Tag present paginate(tags), with: Entities::ContainerRegistry::Tag
end end
...@@ -89,7 +90,7 @@ module API ...@@ -89,7 +90,7 @@ module API
declared_params.except(:repository_id).merge(container_expiration_policy: false)) declared_params.except(:repository_id).merge(container_expiration_policy: false))
# rubocop:enable CodeReuse/Worker # rubocop:enable CodeReuse/Worker
track_event('delete_tag_bulk') track_package_event('delete_tag_bulk', :container)
status :accepted status :accepted
end end
...@@ -125,7 +126,7 @@ module API ...@@ -125,7 +126,7 @@ module API
.execute(repository) .execute(repository)
if result[:status] == :success if result[:status] == :success
track_event('delete_tag') track_package_event('delete_tag', :container)
status :ok status :ok
else else
......
...@@ -72,7 +72,7 @@ module API ...@@ -72,7 +72,7 @@ module API
package = packages_finder(project).by_file_name_and_sha256(filename, params[:sha256]) package = packages_finder(project).by_file_name_and_sha256(filename, params[:sha256])
package_file = ::Packages::PackageFileFinder.new(package, filename, with_file_name_like: false).execute package_file = ::Packages::PackageFileFinder.new(package, filename, with_file_name_like: false).execute
package_event('pull_package') track_package_event('pull_package', :pypi)
present_carrierwave_file!(package_file.file, supports_direct_download: true) present_carrierwave_file!(package_file.file, supports_direct_download: true)
end end
...@@ -91,7 +91,7 @@ module API ...@@ -91,7 +91,7 @@ module API
get 'simple/*package_name', format: :txt do get 'simple/*package_name', format: :txt do
authorize_read_package!(authorized_user_project) authorize_read_package!(authorized_user_project)
package_event('list_package') track_package_event('list_package', :pypi)
packages = find_package_versions packages = find_package_versions
presenter = ::Packages::Pypi::PackagePresenter.new(packages, authorized_user_project) presenter = ::Packages::Pypi::PackagePresenter.new(packages, authorized_user_project)
...@@ -122,7 +122,7 @@ module API ...@@ -122,7 +122,7 @@ module API
authorize_upload!(authorized_user_project) authorize_upload!(authorized_user_project)
bad_request!('File is too large') if authorized_user_project.actual_limits.exceeded?(:pypi_max_file_size, params[:content].size) bad_request!('File is too large') if authorized_user_project.actual_limits.exceeded?(:pypi_max_file_size, params[:content].size)
package_event('push_package') track_package_event('push_package', :pypi)
::Packages::Pypi::CreatePackageService ::Packages::Pypi::CreatePackageService
.new(authorized_user_project, current_user, declared_params) .new(authorized_user_project, current_user, declared_params)
......
...@@ -87,7 +87,7 @@ RSpec.describe Groups::Registry::RepositoriesController do ...@@ -87,7 +87,7 @@ RSpec.describe Groups::Registry::RepositoriesController do
it_behaves_like 'with name parameter' it_behaves_like 'with name parameter'
it_behaves_like 'a gitlab tracking event', described_class.name, 'list_repositories' it_behaves_like 'a package tracking event', described_class.name, 'list_repositories'
context 'with project in subgroup' do context 'with project in subgroup' do
let_it_be(:test_group) { create(:group, parent: group ) } let_it_be(:test_group) { create(:group, parent: group ) }
......
...@@ -109,7 +109,7 @@ RSpec.describe Projects::Registry::TagsController do ...@@ -109,7 +109,7 @@ RSpec.describe Projects::Registry::TagsController do
it 'tracks the event' do it 'tracks the event' do
expect_delete_tags(%w[test.]) expect_delete_tags(%w[test.])
expect(controller).to receive(:track_event).with(:delete_tag) expect(controller).to receive(:track_event).with(:delete_tag, {})
destroy_tag('test.') destroy_tag('test.')
end end
......
...@@ -44,7 +44,7 @@ RSpec.describe API::GroupContainerRepositories do ...@@ -44,7 +44,7 @@ RSpec.describe API::GroupContainerRepositories do
let(:object) { group } let(:object) { group }
end end
it_behaves_like 'a gitlab tracking event', described_class.name, 'list_repositories' it_behaves_like 'a package tracking event', described_class.name, 'list_repositories'
context 'with invalid group id' do context 'with invalid group id' do
let(:url) { "/groups/#{non_existing_record_id}/registry/repositories" } let(:url) { "/groups/#{non_existing_record_id}/registry/repositories" }
......
...@@ -36,7 +36,7 @@ RSpec.describe API::MavenPackages do ...@@ -36,7 +36,7 @@ RSpec.describe API::MavenPackages do
context 'with jar file' do context 'with jar file' do
let_it_be(:package_file) { jar_file } let_it_be(:package_file) { jar_file }
it_behaves_like 'a gitlab tracking event', described_class.name, 'pull_package' it_behaves_like 'a package tracking event', described_class.name, 'pull_package'
end end
end end
...@@ -571,7 +571,7 @@ RSpec.describe API::MavenPackages do ...@@ -571,7 +571,7 @@ RSpec.describe API::MavenPackages do
context 'event tracking' do context 'event tracking' do
subject { upload_file_with_token(params) } subject { upload_file_with_token(params) }
it_behaves_like 'a gitlab tracking event', described_class.name, 'push_package' it_behaves_like 'a package tracking event', described_class.name, 'push_package'
end end
it 'creates package and stores package file' do it 'creates package and stores package file' do
......
...@@ -197,7 +197,7 @@ RSpec.describe API::NpmPackages do ...@@ -197,7 +197,7 @@ RSpec.describe API::NpmPackages do
expect(response.media_type).to eq('application/octet-stream') expect(response.media_type).to eq('application/octet-stream')
end end
it_behaves_like 'a gitlab tracking event', described_class.name, 'pull_package' it_behaves_like 'a package tracking event', described_class.name, 'pull_package'
end end
context 'private project' do context 'private project' do
...@@ -305,7 +305,7 @@ RSpec.describe API::NpmPackages do ...@@ -305,7 +305,7 @@ RSpec.describe API::NpmPackages do
context 'with access token' do context 'with access token' do
subject { upload_package_with_token(package_name, params) } subject { upload_package_with_token(package_name, params) }
it_behaves_like 'a gitlab tracking event', described_class.name, 'push_package' it_behaves_like 'a package tracking event', described_class.name, 'push_package'
it 'creates npm package with file' do it 'creates npm package with file' do
expect { subject } expect { subject }
......
...@@ -45,7 +45,7 @@ RSpec.describe API::ProjectContainerRepositories do ...@@ -45,7 +45,7 @@ RSpec.describe API::ProjectContainerRepositories do
it_behaves_like 'rejected container repository access', :guest, :forbidden it_behaves_like 'rejected container repository access', :guest, :forbidden
it_behaves_like 'rejected container repository access', :anonymous, :not_found it_behaves_like 'rejected container repository access', :anonymous, :not_found
it_behaves_like 'a gitlab tracking event', described_class.name, 'list_repositories' it_behaves_like 'a package tracking event', described_class.name, 'list_repositories'
it_behaves_like 'returns repositories for allowed users', :reporter, 'project' do it_behaves_like 'returns repositories for allowed users', :reporter, 'project' do
let(:object) { project } let(:object) { project }
...@@ -57,7 +57,7 @@ RSpec.describe API::ProjectContainerRepositories do ...@@ -57,7 +57,7 @@ RSpec.describe API::ProjectContainerRepositories do
it_behaves_like 'rejected container repository access', :developer, :forbidden it_behaves_like 'rejected container repository access', :developer, :forbidden
it_behaves_like 'rejected container repository access', :anonymous, :not_found it_behaves_like 'rejected container repository access', :anonymous, :not_found
it_behaves_like 'a gitlab tracking event', described_class.name, 'delete_repository' it_behaves_like 'a package tracking event', described_class.name, 'delete_repository'
context 'for maintainer' do context 'for maintainer' do
let(:api_user) { maintainer } let(:api_user) { maintainer }
...@@ -86,7 +86,7 @@ RSpec.describe API::ProjectContainerRepositories do ...@@ -86,7 +86,7 @@ RSpec.describe API::ProjectContainerRepositories do
stub_container_registry_tags(repository: root_repository.path, tags: %w(rootA latest)) stub_container_registry_tags(repository: root_repository.path, tags: %w(rootA latest))
end end
it_behaves_like 'a gitlab tracking event', described_class.name, 'list_tags' it_behaves_like 'a package tracking event', described_class.name, 'list_tags'
it 'returns a list of tags' do it 'returns a list of tags' do
subject subject
...@@ -114,7 +114,7 @@ RSpec.describe API::ProjectContainerRepositories do ...@@ -114,7 +114,7 @@ RSpec.describe API::ProjectContainerRepositories do
it_behaves_like 'rejected container repository access', :developer, :forbidden it_behaves_like 'rejected container repository access', :developer, :forbidden
it_behaves_like 'rejected container repository access', :anonymous, :not_found it_behaves_like 'rejected container repository access', :anonymous, :not_found
it_behaves_like 'a gitlab tracking event', described_class.name, 'delete_tag_bulk' it_behaves_like 'a package tracking event', described_class.name, 'delete_tag_bulk'
end end
context 'for maintainer' do context 'for maintainer' do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Packages::CreateEventService do
let(:scope) { 'container' }
let(:event_name) { 'push_package' }
let(:params) do
{
scope: scope,
event_name: event_name
}
end
subject { described_class.new(nil, user, params).execute }
describe '#execute' do
shared_examples 'package event creation' do |originator_type, expected_scope|
it 'creates the event' do
expect { subject }.to change { Packages::Event.count }.by(1)
expect(subject.originator_type).to eq(originator_type)
expect(subject.originator).to eq(user&.id)
expect(subject.event_scope).to eq(expected_scope)
expect(subject.event_type).to eq(event_name)
end
end
context 'with a user' do
let(:user) { create(:user) }
it_behaves_like 'package event creation', 'user', 'container'
end
context 'with a deploy token' do
let(:user) { create(:deploy_token) }
it_behaves_like 'package event creation', 'deploy_token', 'container'
end
context 'with no user' do
let(:user) { nil }
it_behaves_like 'package event creation', 'guest', 'container'
end
context 'with a package as scope' do
let(:user) { nil }
let(:scope) { create(:npm_package) }
it_behaves_like 'package event creation', 'guest', 'npm'
end
end
end
...@@ -75,7 +75,7 @@ RSpec.shared_examples 'Composer package creation' do |user_type, status, add_mem ...@@ -75,7 +75,7 @@ RSpec.shared_examples 'Composer package creation' do |user_type, status, add_mem
expect(response).to have_gitlab_http_status(status) expect(response).to have_gitlab_http_status(status)
end end
it_behaves_like 'a gitlab tracking event', described_class.name, 'push_package' it_behaves_like 'a package tracking event', described_class.name, 'push_package'
end end
end end
......
...@@ -79,11 +79,3 @@ RSpec.shared_examples 'returns repositories for allowed users' do |user_type, sc ...@@ -79,11 +79,3 @@ RSpec.shared_examples 'returns repositories for allowed users' do |user_type, sc
end end
end end
end end
RSpec.shared_examples 'a gitlab tracking event' do |category, action|
it "creates a gitlab tracking event #{action}" do
expect(Gitlab::Tracking).to receive(:event).with(category, action, {})
subject
end
end
...@@ -128,7 +128,7 @@ RSpec.shared_examples 'Debian project repository PUT request' do |user_role, add ...@@ -128,7 +128,7 @@ RSpec.shared_examples 'Debian project repository PUT request' do |user_role, add
expect(response.body).to eq(body) expect(response.body).to eq(body)
end end
end end
it_behaves_like 'a gitlab tracking event', described_class.name, 'push_package' it_behaves_like 'a package tracking event', described_class.name, 'push_package'
else else
it "returns #{status}#{and_body}" do it "returns #{status}#{and_body}" do
subject subject
......
...@@ -26,7 +26,7 @@ RSpec.shared_examples 'process nuget service index request' do |user_type, statu ...@@ -26,7 +26,7 @@ RSpec.shared_examples 'process nuget service index request' do |user_type, statu
it_behaves_like 'returning response status', status it_behaves_like 'returning response status', status
it_behaves_like 'a gitlab tracking event', described_class.name, 'nuget_service_index' it_behaves_like 'a package tracking event', described_class.name, 'cli_metadata'
it 'returns a valid json response' do it 'returns a valid json response' do
subject subject
...@@ -169,7 +169,7 @@ RSpec.shared_examples 'process nuget upload' do |user_type, status, add_member = ...@@ -169,7 +169,7 @@ RSpec.shared_examples 'process nuget upload' do |user_type, status, add_member =
context 'with correct params' do context 'with correct params' do
it_behaves_like 'package workhorse uploads' it_behaves_like 'package workhorse uploads'
it_behaves_like 'creates nuget package files' it_behaves_like 'creates nuget package files'
it_behaves_like 'a gitlab tracking event', described_class.name, 'push_package' it_behaves_like 'a package tracking event', described_class.name, 'push_package'
end end
end end
...@@ -286,7 +286,7 @@ RSpec.shared_examples 'process nuget download content request' do |user_type, st ...@@ -286,7 +286,7 @@ RSpec.shared_examples 'process nuget download content request' do |user_type, st
it_behaves_like 'returning response status', status it_behaves_like 'returning response status', status
it_behaves_like 'a gitlab tracking event', described_class.name, 'pull_package' it_behaves_like 'a package tracking event', described_class.name, 'pull_package'
it 'returns a valid package archive' do it 'returns a valid package archive' do
subject subject
...@@ -336,7 +336,7 @@ RSpec.shared_examples 'process nuget search request' do |user_type, status, add_ ...@@ -336,7 +336,7 @@ RSpec.shared_examples 'process nuget search request' do |user_type, status, add_
it_behaves_like 'returns a valid json search response', status, 4, [1, 5, 5, 1] it_behaves_like 'returns a valid json search response', status, 4, [1, 5, 5, 1]
it_behaves_like 'a gitlab tracking event', described_class.name, 'search_package' it_behaves_like 'a package tracking event', described_class.name, 'search_package'
context 'with skip set to 2' do context 'with skip set to 2' do
let(:skip) { 2 } let(:skip) { 2 }
......
...@@ -126,3 +126,11 @@ RSpec.shared_examples 'job token for package uploads' do ...@@ -126,3 +126,11 @@ RSpec.shared_examples 'job token for package uploads' do
end end
end end
end end
RSpec.shared_examples 'a package tracking event' do |category, action|
it "creates a gitlab tracking event #{action}" do
expect(Gitlab::Tracking).to receive(:event).with(category, action, {})
expect { subject }.to change { Packages::Event.count }.by(1)
end
end
...@@ -52,7 +52,7 @@ RSpec.shared_examples 'PyPi package creation' do |user_type, status, add_member ...@@ -52,7 +52,7 @@ RSpec.shared_examples 'PyPi package creation' do |user_type, status, add_member
context 'with correct params' do context 'with correct params' do
it_behaves_like 'package workhorse uploads' it_behaves_like 'package workhorse uploads'
it_behaves_like 'creating pypi package files' it_behaves_like 'creating pypi package files'
it_behaves_like 'a gitlab tracking event', described_class.name, 'push_package' it_behaves_like 'a package tracking event', described_class.name, 'push_package'
end end
end end
...@@ -119,7 +119,7 @@ RSpec.shared_examples 'PyPi package versions' do |user_type, status, add_member ...@@ -119,7 +119,7 @@ RSpec.shared_examples 'PyPi package versions' do |user_type, status, add_member
end end
it_behaves_like 'returning response status', status it_behaves_like 'returning response status', status
it_behaves_like 'a gitlab tracking event', described_class.name, 'list_package' it_behaves_like 'a package tracking event', described_class.name, 'list_package'
end end
end end
...@@ -136,7 +136,7 @@ RSpec.shared_examples 'PyPi package download' do |user_type, status, add_member ...@@ -136,7 +136,7 @@ RSpec.shared_examples 'PyPi package download' do |user_type, status, add_member
end end
it_behaves_like 'returning response status', status it_behaves_like 'returning response status', status
it_behaves_like 'a gitlab tracking event', described_class.name, 'pull_package' it_behaves_like 'a package tracking event', described_class.name, 'pull_package'
end end
end end
......
# frozen_string_literal: true
RSpec.shared_examples 'a gitlab tracking event' do |category, action|
it "creates a gitlab tracking event #{action}" do
expect(Gitlab::Tracking).to receive(:event).with(category, action, {})
subject
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment