Skip to content

Commit

Permalink
CI visibility: validate DD_GIT_REPOSITORY_URL and log error if not pr…
Browse files Browse the repository at this point in the history
…esent
  • Loading branch information
anmarchenko committed Sep 1, 2023
1 parent 90cf733 commit 534ddb0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 37 deletions.
20 changes: 16 additions & 4 deletions lib/datadog/ci/ext/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ def tags(env)
tags[key] ||= value
end

tags.reject { |_, v| v.nil? }
output = tags.reject { |_, v| v.nil? }

ensure_post_conditions(output)

output
end

def normalize_ref(name)
Expand Down Expand Up @@ -166,9 +170,7 @@ def extract_bitbucket(env)
pipeline_url = "https://bitbucket.org/#{env['BITBUCKET_REPO_FULL_NAME']}/addon/pipelines/home#" \
"!/results/#{env['BITBUCKET_BUILD_NUMBER']}"

repository_url = filter_sensitive_info(
env['BITBUCKET_GIT_SSH_ORIGIN'] || env['BITBUCKET_GIT_HTTP_ORIGIN']
)
repository_url = env['BITBUCKET_GIT_SSH_ORIGIN'] || env['BITBUCKET_GIT_HTTP_ORIGIN']

{
Core::Git::Ext::TAG_BRANCH => env['BITBUCKET_BRANCH'],
Expand Down Expand Up @@ -574,6 +576,16 @@ def extract_name_email(name_and_email)

[nil, name_and_email]
end

def ensure_post_conditions(tags)
validate_repository_url_present(tags)
end

def validate_repository_url_present(tags)
return if !tags[Core::Git::Ext::TAG_REPOSITORY_URL].nil? && !tags[Core::Git::Ext::TAG_REPOSITORY_URL].empty?

Datadog.logger.error('DD_GIT_REPOSITORY_URL is not set or empty; no repo URL was automatically extracted')
end
end
end
end
Expand Down
93 changes: 60 additions & 33 deletions spec/datadog/ci/ext/environment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
RSpec.describe Datadog::CI::Ext::Environment do
FIXTURE_DIR = "#{File.dirname(__FILE__)}/fixtures/" # rubocop:disable all

let(:logger) { instance_double(Datadog::Core::Logger) }
before do
allow(Datadog).to receive(:logger).and_return(logger)
allow(logger).to receive(:debug)
allow(logger).to receive(:error)
end

describe '.tags' do
subject(:tags) do
ClimateControl.modify(environment_variables) { described_class.tags(env) }
Expand Down Expand Up @@ -110,49 +117,69 @@
include_context 'without git installed'

it 'does not fail' do
allow(Datadog.logger).to receive(:debug)

is_expected.to eq({})

expect(Datadog.logger).to have_received(:debug).with(/No such file or directory - git/).at_least(1).time
expect(logger).to have_received(:debug).with(/No such file or directory - git/).at_least(1).time
end
end

context 'user provided metadata' do
include_context 'with git fixture', 'gitdir_with_commit'
let(:env) do
{
'DD_GIT_REPOSITORY_URL' => 'https://datadoghq.com/git/user-provided.git',
'DD_GIT_COMMIT_SHA' => '9322ca1d57975b49b8c00b449d21b06660ce8b5c',
'DD_GIT_BRANCH' => 'my-branch',
'DD_GIT_TAG' => 'my-tag',
'DD_GIT_COMMIT_MESSAGE' => 'provided message',
'DD_GIT_COMMIT_AUTHOR_NAME' => 'user',
'DD_GIT_COMMIT_AUTHOR_EMAIL' => 'user@provided.com',
'DD_GIT_COMMIT_AUTHOR_DATE' => '2021-06-18T18:35:10+00:00',
'DD_GIT_COMMIT_COMMITTER_NAME' => 'user committer',
'DD_GIT_COMMIT_COMMITTER_EMAIL' => 'user-committer@provided.com',
'DD_GIT_COMMIT_COMMITTER_DATE' => '2021-06-19T18:35:10+00:00',
}
context 'when required values are present' do
include_context 'with git fixture', 'gitdir_with_commit'

let(:env) do
{
'DD_GIT_REPOSITORY_URL' => 'https://datadoghq.com/git/user-provided.git',
'DD_GIT_COMMIT_SHA' => '9322ca1d57975b49b8c00b449d21b06660ce8b5c',
'DD_GIT_BRANCH' => 'my-branch',
'DD_GIT_TAG' => 'my-tag',
'DD_GIT_COMMIT_MESSAGE' => 'provided message',
'DD_GIT_COMMIT_AUTHOR_NAME' => 'user',
'DD_GIT_COMMIT_AUTHOR_EMAIL' => 'user@provided.com',
'DD_GIT_COMMIT_AUTHOR_DATE' => '2021-06-18T18:35:10+00:00',
'DD_GIT_COMMIT_COMMITTER_NAME' => 'user committer',
'DD_GIT_COMMIT_COMMITTER_EMAIL' => 'user-committer@provided.com',
'DD_GIT_COMMIT_COMMITTER_DATE' => '2021-06-19T18:35:10+00:00',
}
end

it 'returns user provided metadata' do
is_expected.to eq(
{
'ci.workspace_path' => "#{Dir.pwd}/spec/datadog/ci/ext/fixtures/git",
'git.branch' => env['DD_GIT_BRANCH'],
'git.tag' => env['DD_GIT_TAG'],
'git.commit.author.date' => env['DD_GIT_COMMIT_AUTHOR_DATE'],
'git.commit.author.email' => env['DD_GIT_COMMIT_AUTHOR_EMAIL'],
'git.commit.author.name' => env['DD_GIT_COMMIT_AUTHOR_NAME'],
'git.commit.committer.date' => env['DD_GIT_COMMIT_COMMITTER_DATE'],
'git.commit.committer.email' => env['DD_GIT_COMMIT_COMMITTER_EMAIL'],
'git.commit.committer.name' => env['DD_GIT_COMMIT_COMMITTER_NAME'],
'git.commit.message' => env['DD_GIT_COMMIT_MESSAGE'],
'git.commit.sha' => env['DD_GIT_COMMIT_SHA'],
'git.repository_url' => env['DD_GIT_REPOSITORY_URL']
}
)
end
end

it 'returns user provided metadata' do
is_expected.to eq(
context 'when DD_GIT_REPOSITORY_URL is missing' do
include_context 'without git installed'
let(:env) do
{
'ci.workspace_path' => "#{Dir.pwd}/spec/datadog/ci/ext/fixtures/git",
'git.branch' => env['DD_GIT_BRANCH'],
'git.tag' => env['DD_GIT_TAG'],
'git.commit.author.date' => env['DD_GIT_COMMIT_AUTHOR_DATE'],
'git.commit.author.email' => env['DD_GIT_COMMIT_AUTHOR_EMAIL'],
'git.commit.author.name' => env['DD_GIT_COMMIT_AUTHOR_NAME'],
'git.commit.committer.date' => env['DD_GIT_COMMIT_COMMITTER_DATE'],
'git.commit.committer.email' => env['DD_GIT_COMMIT_COMMITTER_EMAIL'],
'git.commit.committer.name' => env['DD_GIT_COMMIT_COMMITTER_NAME'],
'git.commit.message' => env['DD_GIT_COMMIT_MESSAGE'],
'git.commit.sha' => env['DD_GIT_COMMIT_SHA'],
'git.repository_url' => env['DD_GIT_REPOSITORY_URL']
'DD_GIT_COMMIT_SHA' => '9322ca1d57975b49b8c00b449d21b06660ce8b5c',
}
)
end

it 'returns user provided metadata' do
is_expected.to eq(
{
'git.commit.sha' => env['DD_GIT_COMMIT_SHA'],
}
)

expect(logger).to have_received(:error).with(/DD_GIT_REPOSITORY_URL is not set or empty/)
end
end
end
end
Expand Down

0 comments on commit 534ddb0

Please sign in to comment.