Skip to content

Commit

Permalink
[test visibility] Read pull_request and pull_request_target event…
Browse files Browse the repository at this point in the history
… info from GHA (#4745)
  • Loading branch information
juan-fernandez authored and bengl committed Oct 16, 2024
1 parent d5ff8d5 commit 944a513
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 2 deletions.
24 changes: 23 additions & 1 deletion packages/dd-trace/src/plugins/util/ci.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { readFileSync } = require('fs')
const {
GIT_BRANCH,
GIT_COMMIT_SHA,
Expand All @@ -6,6 +7,9 @@ const {
GIT_COMMIT_AUTHOR_NAME,
GIT_COMMIT_MESSAGE,
GIT_COMMIT_AUTHOR_DATE,
GIT_COMMIT_HEAD_SHA,
GIT_PULL_REQUEST_BASE_BRANCH_SHA,
GIT_PULL_REQUEST_BASE_BRANCH,
GIT_REPOSITORY_URL,
CI_PIPELINE_ID,
CI_PIPELINE_NAME,
Expand Down Expand Up @@ -77,6 +81,13 @@ function resolveTilde (filePath) {
return filePath
}

function getGitHubEventPayload () {
if (!process.env.GITHUB_EVENT_PATH) {
return
}
return JSON.parse(readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8'))
}

module.exports = {
normalizeRef,
getCIMetadata () {
Expand Down Expand Up @@ -241,7 +252,8 @@ module.exports = {
GITHUB_REPOSITORY,
GITHUB_SERVER_URL,
GITHUB_RUN_ATTEMPT,
GITHUB_JOB
GITHUB_JOB,
GITHUB_BASE_REF
} = env

const repositoryURL = `${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git`
Expand Down Expand Up @@ -277,6 +289,16 @@ module.exports = {
GITHUB_RUN_ATTEMPT
})
}
if (GITHUB_BASE_REF) { // `pull_request` or `pull_request_target` event
tags[GIT_PULL_REQUEST_BASE_BRANCH] = GITHUB_BASE_REF
try {
const eventContent = getGitHubEventPayload()
tags[GIT_PULL_REQUEST_BASE_BRANCH_SHA] = eventContent.pull_request.base.sha
tags[GIT_COMMIT_HEAD_SHA] = eventContent.pull_request.head.sha
} catch (e) {
// ignore malformed event content
}
}
}

if (env.APPVEYOR) {
Expand Down
7 changes: 7 additions & 0 deletions packages/dd-trace/src/plugins/util/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const GIT_COMMIT_COMMITTER_NAME = 'git.commit.committer.name'
const GIT_COMMIT_AUTHOR_DATE = 'git.commit.author.date'
const GIT_COMMIT_AUTHOR_EMAIL = 'git.commit.author.email'
const GIT_COMMIT_AUTHOR_NAME = 'git.commit.author.name'
const GIT_COMMIT_HEAD_SHA = 'git.commit.head_sha'

const GIT_PULL_REQUEST_BASE_BRANCH_SHA = 'git.pull_request.base_branch_sha'
const GIT_PULL_REQUEST_BASE_BRANCH = 'git.pull_request.base_branch'

const CI_PIPELINE_ID = 'ci.pipeline.id'
const CI_PIPELINE_NAME = 'ci.pipeline.name'
Expand Down Expand Up @@ -36,6 +40,9 @@ module.exports = {
GIT_COMMIT_AUTHOR_DATE,
GIT_COMMIT_AUTHOR_EMAIL,
GIT_COMMIT_AUTHOR_NAME,
GIT_COMMIT_HEAD_SHA,
GIT_PULL_REQUEST_BASE_BRANCH_SHA,
GIT_PULL_REQUEST_BASE_BRANCH,
CI_PIPELINE_ID,
CI_PIPELINE_NAME,
CI_PIPELINE_NUMBER,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"action": "synchronize",
"after": "df289512a51123083a8e6931dd6f57bb3883d4c4",
"before": "f659d2fdd7bedffb40d9ab223dbde6afa5eadc32",
"number": 1,
"pull_request": {
"_links": {},
"active_lock_reason": null,
"additions": 2,
"assignee": null,
"assignees": [],
"author_association": "OWNER",
"auto_merge": null,
"base": {
"label": "datadog:main",
"ref": "main",
"repo": {},
"sha": "52e0974c74d41160a03d59ddc73bb9f5adab054b",
"user": {}
},
"body": "# What Does This Do\r\n\r\n# Motivation\r\n\r\n# Additional Notes\r\n",
"changed_files": 3,
"closed_at": null,
"comments": 0,
"comments_url": "",
"commits": 2,
"commits_url": "",
"created_at": "2024-09-11T15:08:02Z",
"deletions": 0,
"diff_url": "",
"draft": false,
"head": {
"label": "forked_org:test-branch",
"ref": "test-branch",
"repo": {},
"sha": "df289512a51123083a8e6931dd6f57bb3883d4c4",
"user": {}
},
"html_url": "",
"id": 2066570986,
"issue_url": "",
"labels": [],
"locked": false,
"maintainer_can_modify": false,
"merge_commit_sha": "d9a3212d0d5d1483426dbbdf0beea32ee50abcde",
"mergeable": null,
"mergeable_state": "unknown",
"merged": false,
"merged_at": null,
"merged_by": null,
"milestone": null,
"node_id": "PR_kwDOIvpGAs57LV7q",
"number": 1,
"patch_url": "",
"rebaseable": null,
"requested_reviewers": [],
"requested_teams": [],
"review_comment_url": "",
"review_comments": 0,
"review_comments_url": "",
"state": "open",
"statuses_url": "",
"title": "Test commit",
"updated_at": "2024-09-11T15:12:26Z",
"url": "",
"user": {}
},
"repository": {},
"sender": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
46 changes: 45 additions & 1 deletion packages/dd-trace/test/plugins/util/test-environment.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ const proxyquire = require('proxyquire')
const execFileSyncStub = sinon.stub().returns('')

const { getCIMetadata } = require('../../../src/plugins/util/ci')
const { CI_ENV_VARS, CI_NODE_LABELS } = require('../../../src/plugins/util/tags')
const {
CI_ENV_VARS,
CI_NODE_LABELS,
GIT_PULL_REQUEST_BASE_BRANCH,
GIT_PULL_REQUEST_BASE_BRANCH_SHA,
GIT_COMMIT_HEAD_SHA
} = require('../../../src/plugins/util/tags')

const { getGitMetadata } = proxyquire('../../../src/plugins/util/git', {
child_process: {
Expand All @@ -36,6 +42,44 @@ describe('test environment data', () => {
const ciProviders = fs.readdirSync(path.join(__dirname, 'ci-env'))
ciProviders.forEach(ciProvider => {
const assertions = require(path.join(__dirname, 'ci-env', ciProvider))
if (ciProvider === 'github.json') {
// We grab the first assertion because we only need to test one
const [env] = assertions[0]
it('can read pull request data from GitHub Actions', () => {
process.env = env
process.env.GITHUB_BASE_REF = 'datadog:main'
process.env.GITHUB_EVENT_PATH = path.join(__dirname, 'fixtures', 'github_event_payload.json')
const {
[GIT_PULL_REQUEST_BASE_BRANCH]: pullRequestBaseBranch,
[GIT_PULL_REQUEST_BASE_BRANCH_SHA]: pullRequestBaseBranchSha,
[GIT_COMMIT_HEAD_SHA]: headCommitSha
} = getTestEnvironmentMetadata()

expect({
pullRequestBaseBranch,
pullRequestBaseBranchSha,
headCommitSha
}).to.eql({
pullRequestBaseBranch: 'datadog:main',
pullRequestBaseBranchSha: '52e0974c74d41160a03d59ddc73bb9f5adab054b',
headCommitSha: 'df289512a51123083a8e6931dd6f57bb3883d4c4'
})
})
it('does not crash if GITHUB_EVENT_PATH is not a valid JSON file', () => {
process.env = env
process.env.GITHUB_BASE_REF = 'datadog:main'
process.env.GITHUB_EVENT_PATH = path.join(__dirname, 'fixtures', 'github_event_payload_malformed.json')
const {
[GIT_PULL_REQUEST_BASE_BRANCH]: pullRequestBaseBranch,
[GIT_PULL_REQUEST_BASE_BRANCH_SHA]: pullRequestBaseBranchSha,
[GIT_COMMIT_HEAD_SHA]: headCommitSha
} = getTestEnvironmentMetadata()

expect(pullRequestBaseBranch).to.equal('datadog:main')
expect(pullRequestBaseBranchSha).to.be.undefined
expect(headCommitSha).to.be.undefined
})
}

assertions.forEach(([env, expectedSpanTags], index) => {
it(`reads env info for spec ${index} from ${ciProvider}`, () => {
Expand Down

0 comments on commit 944a513

Please sign in to comment.