Commit ab1267ff authored by Shinya Maeda's avatar Shinya Maeda

Merge branch '296612-instrument-release-cli' into 'master'

Add snowplow tracking to Releases API

See merge request gitlab-org/gitlab!58221
parents 0b545cbf b4b93272
---
title: Add snoplow tracking to Releases API
merge_request: 58221
author:
type: added
...@@ -6,9 +6,12 @@ module API ...@@ -6,9 +6,12 @@ module API
RELEASE_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS RELEASE_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS
.merge(tag_name: API::NO_SLASH_URL_PART_REGEX) .merge(tag_name: API::NO_SLASH_URL_PART_REGEX)
RELEASE_CLI_USER_AGENT = 'GitLab-release-cli'
before { authorize_read_releases! } before { authorize_read_releases! }
after { track_release_event }
feature_category :release_orchestration feature_category :release_orchestration
params do params do
...@@ -17,6 +20,7 @@ module API ...@@ -17,6 +20,7 @@ module API
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
desc 'Get a project releases' do desc 'Get a project releases' do
detail 'This feature was introduced in GitLab 11.7.' detail 'This feature was introduced in GitLab 11.7.'
named 'get_releases'
success Entities::Release success Entities::Release
end end
params do params do
...@@ -34,6 +38,7 @@ module API ...@@ -34,6 +38,7 @@ module API
desc 'Get a single project release' do desc 'Get a single project release' do
detail 'This feature was introduced in GitLab 11.7.' detail 'This feature was introduced in GitLab 11.7.'
named 'get_release'
success Entities::Release success Entities::Release
end end
params do params do
...@@ -47,6 +52,7 @@ module API ...@@ -47,6 +52,7 @@ module API
desc 'Create a new release' do desc 'Create a new release' do
detail 'This feature was introduced in GitLab 11.7.' detail 'This feature was introduced in GitLab 11.7.'
named 'create_release'
success Entities::Release success Entities::Release
end end
params do params do
...@@ -84,6 +90,7 @@ module API ...@@ -84,6 +90,7 @@ module API
desc 'Update a release' do desc 'Update a release' do
detail 'This feature was introduced in GitLab 11.7.' detail 'This feature was introduced in GitLab 11.7.'
named 'update_release'
success Entities::Release success Entities::Release
end end
params do params do
...@@ -112,6 +119,7 @@ module API ...@@ -112,6 +119,7 @@ module API
desc 'Delete a release' do desc 'Delete a release' do
detail 'This feature was introduced in GitLab 11.7.' detail 'This feature was introduced in GitLab 11.7.'
named 'delete_release'
success Entities::Release success Entities::Release
end end
params do params do
...@@ -176,6 +184,21 @@ module API ...@@ -176,6 +184,21 @@ module API
def log_release_milestones_updated_audit_event def log_release_milestones_updated_audit_event
# extended in EE # extended in EE
end 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 end
end end
......
...@@ -1136,8 +1136,33 @@ RSpec.describe API::Releases do ...@@ -1136,8 +1136,33 @@ RSpec.describe API::Releases do
end end
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 def initialize_tags
project.repository.add_tag(maintainer, 'v0.1', commit.id) project.repository.add_tag(maintainer, 'v0.1', commit.id)
project.repository.add_tag(maintainer, 'v0.2', commit.id) project.repository.add_tag(maintainer, 'v0.2', commit.id)
end 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 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