Skip to content

Commit

Permalink
send additional tags from github actions provider when pull_request t…
Browse files Browse the repository at this point in the history
…rigger is used
  • Loading branch information
anmarchenko committed Sep 23, 2024
1 parent 84542ef commit 960d008
Show file tree
Hide file tree
Showing 4 changed files with 580 additions and 1 deletion.
24 changes: 24 additions & 0 deletions lib/datadog/ci/ext/environment/providers/github_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@ def ci_env_vars
}.reject { |_, v| v.nil? }.to_json
end

def additional_tags
base_ref = env["GITHUB_BASE_REF"]
return {} if base_ref.nil? || base_ref.empty?

# @type var result: Hash[String, String]
result = {
Git::TAG_PULL_REQUEST_BASE_BRANCH => base_ref
}

event_path = env["GITHUB_EVENT_PATH"]
event_json = JSON.parse(File.read(event_path))

head_sha = event_json.dig("pull_request", "head", "sha")
result[Git::TAG_COMMIT_HEAD_SHA] = head_sha if head_sha

base_sha = event_json.dig("pull_request", "base", "sha")
result[Git::TAG_PULL_REQUEST_BASE_BRANCH_SHA] = base_sha if base_sha

result
rescue => e
Datadog.logger.error("Failed to extract additional tags from GitHub Actions: #{e}")
{}
end

private

def github_server_url
Expand Down
2 changes: 1 addition & 1 deletion spec/datadog/ci/contrib/rspec/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ def rspec_skipped_session_run
let(:integration_options) { {service_name: "lspec"} }
end

it "retries test until it passes" do
it "does not report the test that failed when RSpec was quitting" do
rspec_session_run(with_canceled_test: true)

expect(test_spans).to have(2).items
Expand Down
65 changes: 65 additions & 0 deletions spec/support/fixtures/ci/github.json
Original file line number Diff line number Diff line change
Expand Up @@ -710,5 +710,70 @@
"git.commit.sha": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"git.repository_url": "https://1.1.1.1:1234/ghactions-repo.git"
}
],
[
{
"GITHUB_ACTION": "run",
"GITHUB_JOB": "github-job-name",
"GITHUB_REF": "master",
"GITHUB_REPOSITORY": "ghactions-repo",
"GITHUB_RUN_ATTEMPT": "ghactions-run-attempt",
"GITHUB_RUN_ID": "ghactions-pipeline-id",
"GITHUB_RUN_NUMBER": "ghactions-pipeline-number",
"GITHUB_SERVER_URL": "https://ghenterprise.com",
"GITHUB_SHA": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"GITHUB_WORKFLOW": "ghactions-pipeline-name",
"GITHUB_WORKSPACE": "/foo/bar",
"GITHUB_EVENT_PATH": "./spec/support/fixtures/github_actions/github_event.json",
"GITHUB_BASE_REF": "github-base-ref"
},
{
"_dd.ci.env_vars": "{\"GITHUB_SERVER_URL\":\"https://ghenterprise.com\",\"GITHUB_REPOSITORY\":\"ghactions-repo\",\"GITHUB_RUN_ID\":\"ghactions-pipeline-id\",\"GITHUB_RUN_ATTEMPT\":\"ghactions-run-attempt\"}",
"ci.job.name": "github-job-name",
"ci.job.url": "https://ghenterprise.com/ghactions-repo/commit/b9f0fb3fdbb94c9d24b2c75b49663122a529e123/checks",
"ci.pipeline.id": "ghactions-pipeline-id",
"ci.pipeline.name": "ghactions-pipeline-name",
"ci.pipeline.number": "ghactions-pipeline-number",
"ci.pipeline.url": "https://ghenterprise.com/ghactions-repo/actions/runs/ghactions-pipeline-id/attempts/ghactions-run-attempt",
"ci.provider.name": "github",
"ci.workspace_path": "/foo/bar",
"git.branch": "master",
"git.commit.sha": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"git.repository_url": "https://ghenterprise.com/ghactions-repo.git",
"git.commit.head_sha": "df289512a51123083a8e6931dd6f57bb3883d4c4",
"git.pull_request.base_branch": "github-base-ref",
"git.pull_request.base_branch_sha": "52e0974c74d41160a03d59ddc73bb9f5adab054b"
}
],
[
{
"GITHUB_ACTION": "run",
"GITHUB_JOB": "github-job-name",
"GITHUB_REF": "master",
"GITHUB_REPOSITORY": "ghactions-repo",
"GITHUB_RUN_ATTEMPT": "ghactions-run-attempt",
"GITHUB_RUN_ID": "ghactions-pipeline-id",
"GITHUB_RUN_NUMBER": "ghactions-pipeline-number",
"GITHUB_SERVER_URL": "https://ghenterprise.com",
"GITHUB_SHA": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"GITHUB_WORKFLOW": "ghactions-pipeline-name",
"GITHUB_WORKSPACE": "/foo/bar",
"GITHUB_EVENT_PATH": "./spec/support/fixtures/github_actions/no_such_file.json",
"GITHUB_BASE_REF": "github-base-ref"
},
{
"_dd.ci.env_vars": "{\"GITHUB_SERVER_URL\":\"https://ghenterprise.com\",\"GITHUB_REPOSITORY\":\"ghactions-repo\",\"GITHUB_RUN_ID\":\"ghactions-pipeline-id\",\"GITHUB_RUN_ATTEMPT\":\"ghactions-run-attempt\"}",
"ci.job.name": "github-job-name",
"ci.job.url": "https://ghenterprise.com/ghactions-repo/commit/b9f0fb3fdbb94c9d24b2c75b49663122a529e123/checks",
"ci.pipeline.id": "ghactions-pipeline-id",
"ci.pipeline.name": "ghactions-pipeline-name",
"ci.pipeline.number": "ghactions-pipeline-number",
"ci.pipeline.url": "https://ghenterprise.com/ghactions-repo/actions/runs/ghactions-pipeline-id/attempts/ghactions-run-attempt",
"ci.provider.name": "github",
"ci.workspace_path": "/foo/bar",
"git.branch": "master",
"git.commit.sha": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"git.repository_url": "https://ghenterprise.com/ghactions-repo.git"
}
]
]
Loading

0 comments on commit 960d008

Please sign in to comment.