Commit 594d47fb authored by rossfuhrman's avatar rossfuhrman Committed by Robert Speicher

Add SAST UI Config telemetry

Track when the SAST UI Config form is submitted,
as well as wether it was a create or an update for the .gitlab-ci.yml
file, as well as wether default values were changed
parent 102c9fb4
...@@ -11,10 +11,12 @@ module Security ...@@ -11,10 +11,12 @@ module Security
end end
def execute def execute
result = ::Files::MultiService.new(@project, @current_user, attributes).execute attributes_for_commit = attributes
result = ::Files::MultiService.new(@project, @current_user, attributes_for_commit).execute
if result[:status] == :success if result[:status] == :success
result[:success_path] = successful_change_path result[:success_path] = successful_change_path
track_event(attributes_for_commit)
else else
result[:errors] = result[:message] result[:errors] = result[:message]
end end
...@@ -48,6 +50,14 @@ module Security ...@@ -48,6 +50,14 @@ module Security
merge_request_params = { source_branch: @branch_name, description: description } merge_request_params = { source_branch: @branch_name, description: description }
Gitlab::Routing.url_helpers.project_new_merge_request_url(@project, merge_request: merge_request_params) Gitlab::Routing.url_helpers.project_new_merge_request_url(@project, merge_request: merge_request_params)
end end
def track_event(attributes_for_commit)
action = attributes_for_commit[:actions].first
Gitlab::Tracking.event(
self.class.to_s, action[:action], { label: action[:default_values_overwritten].to_s }
)
end
end end
end end
end end
---
title: Add SAST UI Config telemetry
merge_request: 42720
author:
type: changed
...@@ -8,6 +8,7 @@ module Security ...@@ -8,6 +8,7 @@ module Security
@variables = variables(params) @variables = variables(params)
@existing_gitlab_ci_content = existing_gitlab_ci_content || {} @existing_gitlab_ci_content = existing_gitlab_ci_content || {}
@default_sast_values = default_sast_values(params) @default_sast_values = default_sast_values(params)
@default_values_overwritten = false
end end
def generate def generate
...@@ -15,7 +16,7 @@ module Security ...@@ -15,7 +16,7 @@ module Security
update_existing_content! update_existing_content!
[{ action: action, file_path: '.gitlab-ci.yml', content: prepare_existing_content }] [{ action: action, file_path: '.gitlab-ci.yml', content: prepare_existing_content, default_values_overwritten: @default_values_overwritten }]
end end
private private
...@@ -77,6 +78,7 @@ module Security ...@@ -77,6 +78,7 @@ module Security
variables.each do |key| variables.each do |key|
if @variables[key].present? && @variables[key].to_s != @default_sast_values[key].to_s if @variables[key].present? && @variables[key].to_s != @default_sast_values[key].to_s
hash_to_update['variables'][key] = @variables[key] hash_to_update['variables'][key] = @variables[key]
@default_values_overwritten = true
else else
hash_to_update['variables'].delete(key) hash_to_update['variables'].delete(key)
end end
......
...@@ -55,6 +55,10 @@ RSpec.describe Security::CiConfiguration::SastBuildActions do ...@@ -55,6 +55,10 @@ RSpec.describe Security::CiConfiguration::SastBuildActions do
expect(result.first[:action]).to eq('update') expect(result.first[:action]).to eq('update')
expect(result.first[:content]).to eq(sast_yaml_two_includes) expect(result.first[:content]).to eq(sast_yaml_two_includes)
end end
it 'reports defaults have been overwritten' do
expect(result.first[:default_values_overwritten]).to eq(true)
end
end end
end end
...@@ -78,6 +82,10 @@ RSpec.describe Security::CiConfiguration::SastBuildActions do ...@@ -78,6 +82,10 @@ RSpec.describe Security::CiConfiguration::SastBuildActions do
it 'generates the correct YML' do it 'generates the correct YML' do
expect(result.first[:content]).to eq(sast_yaml_with_no_variables_set) expect(result.first[:content]).to eq(sast_yaml_with_no_variables_set)
end end
it 'reports defaults have not been overwritten' do
expect(result.first[:default_values_overwritten]).to eq(false)
end
end end
context 'with update stage and SEARCH_MAX_DEPTH and set SECURE_ANALYZERS_PREFIX to default' do context 'with update stage and SEARCH_MAX_DEPTH and set SECURE_ANALYZERS_PREFIX to default' do
......
...@@ -2,20 +2,43 @@ ...@@ -2,20 +2,43 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Security::CiConfiguration::SastCreateService do RSpec.describe Security::CiConfiguration::SastCreateService, :snowplow do
describe '#execute' do describe '#execute' do
let_it_be(:project) { create(:project, :repository) } let_it_be(:project) { create(:project, :repository) }
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let(:params) { {} }
subject(:result) { described_class.new(project, user, params).execute } subject(:result) { described_class.new(project, user, params).execute }
context 'user does not belong to project' do
it 'returns an error status' do
expect(result[:status]).to eq(:error)
expect(result[:success_path]).to be_nil
end
it 'does not track a snowplow event' do
subject
expect_no_snowplow_event
end
end
context 'user belongs to project' do
before do before do
project.add_developer(user) project.add_developer(user)
end end
context 'with no parameters' do it 'does track the snowplow event' do
let(:params) { {} } subject
expect_snowplow_event(
category: 'Security::CiConfiguration::SastCreateService',
action: 'create',
label: 'false'
)
end
context 'with no parameters' do
it 'returns the path to create a new merge request' do it 'returns the path to create a new merge request' do
expect(result[:status]).to eq(:success) expect(result[:status]).to eq(:success)
expect(result[:success_path]).to match(/#{Gitlab::Routing.url_helpers.project_new_merge_request_url(project, {})}(.*)description(.*)source_branch/) expect(result[:success_path]).to match(/#{Gitlab::Routing.url_helpers.project_new_merge_request_url(project, {})}(.*)description(.*)source_branch/)
...@@ -36,4 +59,5 @@ RSpec.describe Security::CiConfiguration::SastCreateService do ...@@ -36,4 +59,5 @@ RSpec.describe Security::CiConfiguration::SastCreateService do
end end
end end
end end
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