Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDTEST-528] Add pull_request extra tags for GitHub Actions #238

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not relevant to this PR's topic, I just noticed that one of the specs was copy-pasted without changing description

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"
}
],
[
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here are the actual test cases:

  1. happy path
  2. case where events file is not found

{
"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
Loading