Commit b4b93272 authored by Jaime Martinez's avatar Jaime Martinez Committed by Shinya Maeda

Add snowplow tracking to Releases API

parent 4f178c56
---
title: Add snoplow tracking to Releases API
merge_request: 58221
author:
type: added
......@@ -6,9 +6,12 @@ module API
RELEASE_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS
.merge(tag_name: API::NO_SLASH_URL_PART_REGEX)
RELEASE_CLI_USER_AGENT = 'GitLab-release-cli'
before { authorize_read_releases! }
after { track_release_event }
feature_category :release_orchestration
params do
......@@ -17,6 +20,7 @@ module API
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
desc 'Get a project releases' do
detail 'This feature was introduced in GitLab 11.7.'
named 'get_releases'
success Entities::Release
end
params do
......@@ -34,6 +38,7 @@ module API
desc 'Get a single project release' do
detail 'This feature was introduced in GitLab 11.7.'
named 'get_release'
success Entities::Release
end
params do
......@@ -47,6 +52,7 @@ module API
desc 'Create a new release' do
detail 'This feature was introduced in GitLab 11.7.'
named 'create_release'
success Entities::Release
end
params do
......@@ -84,6 +90,7 @@ module API
desc 'Update a release' do
detail 'This feature was introduced in GitLab 11.7.'
named 'update_release'
success Entities::Release
end
params do
......@@ -112,6 +119,7 @@ module API
desc 'Delete a release' do
detail 'This feature was introduced in GitLab 11.7.'
named 'delete_release'
success Entities::Release
end
params do
......@@ -176,6 +184,21 @@ module API
def log_release_milestones_updated_audit_event
# extended in EE
end
def release_cli?
request.env['HTTP_USER_AGENT']&.include?(RELEASE_CLI_USER_AGENT) == true
end
def event_context
{
release_cli: release_cli?
}
end
def track_release_event
Gitlab::Tracking.event(options[:for].name, options[:route_options][:named],
project: user_project, user: current_user, **event_context)
end
end
end
end
......
......@@ -1136,8 +1136,33 @@ RSpec.describe API::Releases do
end
end
describe 'Track API events', :snowplow do
context 'when tracking event with labels from User-Agent' do
it 'adds the tracked User-Agent to the label of the tracked event' do
get api("/projects/#{project.id}/releases", maintainer), headers: { 'User-Agent' => described_class::RELEASE_CLI_USER_AGENT }
assert_snowplow_event('get_releases', true)
end
it 'skips label when User-Agent is invalid' do
get api("/projects/#{project.id}/releases", maintainer), headers: { 'User-Agent' => 'invalid_user_agent' }
assert_snowplow_event('get_releases', false)
end
end
end
def initialize_tags
project.repository.add_tag(maintainer, 'v0.1', commit.id)
project.repository.add_tag(maintainer, 'v0.2', commit.id)
end
def assert_snowplow_event(action, release_cli, user = maintainer)
expect_snowplow_event(
category: described_class.name,
action: action,
project: project,
user: user,
release_cli: release_cli
)
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