Skip to content

Commit

Permalink
Merge pull request #238 from DataDog/anmarchenko/gha_pull_request_tags
Browse files Browse the repository at this point in the history
[SDTEST-528] Add pull_request extra tags for GitHub Actions
  • Loading branch information
anmarchenko authored Sep 23, 2024
2 parents f3d84e6 + 960d008 commit 4871058
Show file tree
Hide file tree
Showing 9 changed files with 602 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/datadog/ci/ext/environment/extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ def tags
Git::TAG_COMMIT_SHA => @provider.git_commit_sha
}

# set additional tags if provider needs them
@provider.additional_tags.each do |key, value|
@tags[key] = value
end

# Normalize Git references and filter sensitive data
normalize_git!
# Expand ~
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/ext/environment/providers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ def git_commit_message
def git_commit_sha
end

def additional_tags
{}
end

private

def set_branch_and_tag
Expand Down
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
5 changes: 5 additions & 0 deletions lib/datadog/ci/ext/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ module Git
TAG_COMMIT_MESSAGE = "git.commit.message"
TAG_COMMIT_SHA = "git.commit.sha"

# additional tags that we use for github actions jobs with "pull_request" target
TAG_COMMIT_HEAD_SHA = "git.commit.head_sha"
TAG_PULL_REQUEST_BASE_BRANCH = "git.pull_request.base_branch"
TAG_PULL_REQUEST_BASE_BRANCH_SHA = "git.pull_request.base_branch_sha"

ENV_REPOSITORY_URL = "DD_GIT_REPOSITORY_URL"
ENV_COMMIT_SHA = "DD_GIT_COMMIT_SHA"
ENV_BRANCH = "DD_GIT_BRANCH"
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/ext/environment/providers/base.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ module Datadog

def git_commit_sha: () -> nil

def additional_tags: () -> Hash[String, String]

private

def set_branch_and_tag: () -> [String?, String?]
Expand Down
6 changes: 6 additions & 0 deletions sig/datadog/ci/ext/git.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ module Datadog

TAG_COMMIT_SHA: "git.commit.sha"

TAG_COMMIT_HEAD_SHA: "git.commit.head_sha"

TAG_PULL_REQUEST_BASE_BRANCH: "git.pull_request.base_branch"

TAG_PULL_REQUEST_BASE_BRANCH_SHA: "git.pull_request.base_branch_sha"

ENV_REPOSITORY_URL: "DD_GIT_REPOSITORY_URL"

ENV_COMMIT_SHA: "DD_GIT_COMMIT_SHA"
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 4871058

Please sign in to comment.