Commit 1686da10 authored by Ash McKenzie's avatar Ash McKenzie

Merge branch '35590-track-container-registry-events-in-api-endpoints' into 'master'

Track container registry events in API endpoints

See merge request gitlab-org/gitlab!19591
parents c94c53b6 73601955
...@@ -26,6 +26,8 @@ module API ...@@ -26,6 +26,8 @@ module API
user: current_user, subject: user_group user: current_user, subject: user_group
).execute ).execute
track_event('list_repositories')
present paginate(repositories), with: Entities::ContainerRegistry::Repository, tags: params[:tags] present paginate(repositories), with: Entities::ContainerRegistry::Repository, tags: params[:tags]
end end
end end
......
...@@ -455,6 +455,17 @@ module API ...@@ -455,6 +455,17 @@ module API
end end
end end
def track_event(action = action_name, **args)
category = args.delete(:category) || self.options[:for].name
raise "invalid category" unless category
::Gitlab::Tracking.event(category, action.to_s, **args)
rescue => error
Rails.logger.warn( # rubocop:disable Gitlab/RailsLogger
"Tracking event failed for action: #{action}, category: #{category}, message: #{error.message}"
)
end
protected protected
def project_finder_params_ce def project_finder_params_ce
......
...@@ -27,6 +27,8 @@ module API ...@@ -27,6 +27,8 @@ module API
user: current_user, subject: user_project user: current_user, subject: user_project
).execute ).execute
track_event( 'list_repositories')
present paginate(repositories), with: Entities::ContainerRegistry::Repository, tags: params[:tags] present paginate(repositories), with: Entities::ContainerRegistry::Repository, tags: params[:tags]
end end
...@@ -40,6 +42,7 @@ module API ...@@ -40,6 +42,7 @@ module API
authorize_admin_container_image! authorize_admin_container_image!
DeleteContainerRepositoryWorker.perform_async(current_user.id, repository.id) DeleteContainerRepositoryWorker.perform_async(current_user.id, repository.id)
track_event('delete_repository')
status :accepted status :accepted
end end
...@@ -56,6 +59,8 @@ module API ...@@ -56,6 +59,8 @@ 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')
present paginate(tags), with: Entities::ContainerRegistry::Tag present paginate(tags), with: Entities::ContainerRegistry::Tag
end end
...@@ -77,6 +82,8 @@ module API ...@@ -77,6 +82,8 @@ module API
CleanupContainerRepositoryWorker.perform_async(current_user.id, repository.id, CleanupContainerRepositoryWorker.perform_async(current_user.id, repository.id,
declared_params.except(:repository_id)) declared_params.except(:repository_id))
track_event('delete_tag_bulk')
status :accepted status :accepted
end end
...@@ -111,6 +118,8 @@ module API ...@@ -111,6 +118,8 @@ module API
.execute(repository) .execute(repository)
if result[:status] == :success if result[:status] == :success
track_event('delete_tag')
status :ok status :ok
else else
status :bad_request status :bad_request
......
...@@ -174,4 +174,18 @@ describe API::Helpers do ...@@ -174,4 +174,18 @@ describe API::Helpers do
end end
end end
end end
describe '#track_event' do
it "creates a gitlab tracking event" do
expect(Gitlab::Tracking).to receive(:event).with('foo', 'my_event', {})
subject.track_event('my_event', category: 'foo')
end
it "logs an exception" do
expect(Rails.logger).to receive(:warn).with(/Tracking event failed/)
subject.track_event('my_event', category: nil)
end
end
end end
...@@ -44,6 +44,8 @@ describe API::GroupContainerRepositories do ...@@ -44,6 +44,8 @@ describe API::GroupContainerRepositories do
let(:object) { group } let(:object) { group }
end end
it_behaves_like 'a gitlab tracking event', described_class.name, 'list_repositories'
context 'with invalid group id' do context 'with invalid group id' do
let(:url) { '/groups/123412341234/registry/repositories' } let(:url) { '/groups/123412341234/registry/repositories' }
......
...@@ -46,6 +46,7 @@ describe API::ProjectContainerRepositories do ...@@ -46,6 +46,7 @@ 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 '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,6 +58,7 @@ describe API::ProjectContainerRepositories do ...@@ -57,6 +58,7 @@ 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'
context 'for maintainer' do context 'for maintainer' do
let(:api_user) { maintainer } let(:api_user) { maintainer }
...@@ -85,6 +87,8 @@ describe API::ProjectContainerRepositories do ...@@ -85,6 +87,8 @@ 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 'returns a list of tags' do it 'returns a list of tags' do
subject subject
...@@ -111,6 +115,7 @@ describe API::ProjectContainerRepositories do ...@@ -111,6 +115,7 @@ 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'
end end
context 'for maintainer' do context 'for maintainer' do
...@@ -222,6 +227,7 @@ describe API::ProjectContainerRepositories do ...@@ -222,6 +227,7 @@ describe API::ProjectContainerRepositories do
it 'properly removes tag' do it 'properly removes tag' do
expect(service).to receive(:execute).with(root_repository) { { status: :success } } expect(service).to receive(:execute).with(root_repository) { { status: :success } }
expect(Projects::ContainerRepository::DeleteTagsService).to receive(:new).with(root_repository.project, api_user, tags: %w[rootA]) { service } expect(Projects::ContainerRepository::DeleteTagsService).to receive(:new).with(root_repository.project, api_user, tags: %w[rootA]) { service }
expect(Gitlab::Tracking).to receive(:event).with(described_class.name, 'delete_tag', {})
subject subject
...@@ -237,6 +243,7 @@ describe API::ProjectContainerRepositories do ...@@ -237,6 +243,7 @@ describe API::ProjectContainerRepositories do
it 'properly removes tag' do it 'properly removes tag' do
expect(service).to receive(:execute).with(root_repository) { { status: :success } } expect(service).to receive(:execute).with(root_repository) { { status: :success } }
expect(Projects::ContainerRepository::DeleteTagsService).to receive(:new).with(root_repository.project, api_user, tags: %w[rootA]) { service } expect(Projects::ContainerRepository::DeleteTagsService).to receive(:new).with(root_repository.project, api_user, tags: %w[rootA]) { service }
expect(Gitlab::Tracking).to receive(:event).with(described_class.name, 'delete_tag', {})
subject subject
......
...@@ -56,3 +56,11 @@ shared_examples 'returns repositories for allowed users' do |user_type, scope| ...@@ -56,3 +56,11 @@ shared_examples 'returns repositories for allowed users' do |user_type, scope|
end end
end end
end end
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