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

Read pull_request and pull_request_target event info from GHA #1473

Merged
merged 1 commit into from
Oct 2, 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
70 changes: 70 additions & 0 deletions src/helpers/__tests__/ci-fixtures/github_event_payload.json
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 @@
{}
76 changes: 73 additions & 3 deletions src/helpers/__tests__/ci.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import path from 'path'

import {getCIEnv, getCIMetadata, getCISpanTags} from '../ci'
import {Metadata, SpanTags} from '../interfaces'
import {CI_NODE_LABELS, CI_ENV_VARS} from '../tags'
import {
CI_NODE_LABELS,
CI_ENV_VARS,
GIT_PULL_REQUEST_BASE_BRANCH,
GIT_PULL_REQUEST_BASE_BRANCH_SHA,
GIT_HEAD_SHA,
} from '../tags'
import {getUserCISpanTags, getUserGitSpanTags} from '../user-provided-git'

const CI_PROVIDERS = fs.readdirSync(path.join(__dirname, 'ci-env'))
Expand Down Expand Up @@ -169,9 +175,73 @@ describe('ci spec', () => {
})

CI_PROVIDERS.forEach((ciProvider) => {
const assertions = require(path.join(__dirname, 'ci-env', ciProvider))
const assertions = require(path.join(__dirname, 'ci-env', ciProvider)) as [
{[key: string]: string},
{[key: string]: string}
][]

if (ciProvider === 'github.json') {
describe('github actions pull request events', () => {
afterEach(() => {
delete process.env.GITHUB_BASE_REF
delete process.env.GITHUB_EVENT_PATH
})
// 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, 'ci-fixtures', 'github_event_payload.json')
const {
[GIT_PULL_REQUEST_BASE_BRANCH]: pullRequestBaseBranch,
[GIT_PULL_REQUEST_BASE_BRANCH_SHA]: pullRequestBaseBranchSha,
[GIT_HEAD_SHA]: headCommitSha,
} = getCISpanTags() as SpanTags

expect({
pullRequestBaseBranch,
pullRequestBaseBranchSha,
headCommitSha,
}).toEqual({
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_HEAD_SHA]: headCommitSha,
} = getCISpanTags() as SpanTags

expect(pullRequestBaseBranch).toEqual('datadog:main')
expect(pullRequestBaseBranchSha).toBeUndefined()
expect(headCommitSha).toBeUndefined()
})

it('does not crash if GITHUB_EVENT_PATH is not a file', () => {
process.env = env
process.env.GITHUB_BASE_REF = 'datadog:main'
process.env.GITHUB_EVENT_PATH = path.join(__dirname, 'fixtures', 'does_not_exist.json')
const {
[GIT_PULL_REQUEST_BASE_BRANCH]: pullRequestBaseBranch,
[GIT_PULL_REQUEST_BASE_BRANCH_SHA]: pullRequestBaseBranchSha,
[GIT_HEAD_SHA]: headCommitSha,
} = getCISpanTags() as SpanTags

expect(pullRequestBaseBranch).toEqual('datadog:main')
expect(pullRequestBaseBranchSha).toBeUndefined()
expect(headCommitSha).toBeUndefined()
})
})
}

assertions.forEach(([env, expectedSpanTags]: [{[key: string]: string}, {[key: string]: string}], index: number) => {
assertions.forEach(([env, expectedSpanTags], index) => {
test(`reads env info for spec ${index} from ${ciProvider}`, () => {
process.env = env
const tags = {
Expand Down
15 changes: 10 additions & 5 deletions src/helpers/ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ import {
GIT_TAG,
GIT_HEAD_SHA,
GIT_BASE_REF,
GIT_PULL_REQUEST_BASE_BRANCH,
GIT_PULL_REQUEST_BASE_BRANCH_SHA,
} from './tags'
import {getUserCISpanTags, getUserGitSpanTags} from './user-provided-git'
import {
normalizeRef,
removeEmptyValues,
removeUndefinedValues,
filterSensitiveInfoFromRepository,
getGitHeadShaFromGitHubWebhookPayload,
getGitHubEventPayload,
} from './utils'

export const CI_ENGINES = {
Expand Down Expand Up @@ -264,10 +266,13 @@ export const getCISpanTags = (): SpanTags | undefined => {
if (GITHUB_BASE_REF) {
// GITHUB_BASE_REF is defined if it's a pull_request or pull_request_target trigger
tags[GIT_BASE_REF] = GITHUB_BASE_REF
const headSha = getGitHeadShaFromGitHubWebhookPayload()

if (headSha) {
tags[GIT_HEAD_SHA] = headSha
tags[GIT_PULL_REQUEST_BASE_BRANCH] = GITHUB_BASE_REF
try {
const eventPayload = getGitHubEventPayload()
tags[GIT_HEAD_SHA] = eventPayload?.pull_request?.head?.sha
tags[GIT_PULL_REQUEST_BASE_BRANCH_SHA] = eventPayload?.pull_request?.base?.sha
} catch (e) {
// ignore malformed event content
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
GIT_HEAD_SHA,
SERVICE,
GIT_BASE_REF,
GIT_PULL_REQUEST_BASE_BRANCH,
GIT_PULL_REQUEST_BASE_BRANCH_SHA,
} from './tags'

export interface Metadata {
Expand Down Expand Up @@ -98,6 +100,8 @@ export type SpanTag =
| typeof SERVICE
| typeof GIT_HEAD_SHA
| typeof GIT_BASE_REF
| typeof GIT_PULL_REQUEST_BASE_BRANCH
| typeof GIT_PULL_REQUEST_BASE_BRANCH_SHA

export type SpanTags = Partial<Record<SpanTag, string>>

Expand Down
2 changes: 2 additions & 0 deletions src/helpers/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export const GIT_SHA = 'git.commit.sha'
export const GIT_TAG = 'git.tag'
export const GIT_HEAD_SHA = 'git.commit.head_sha'
export const GIT_BASE_REF = 'git.commit.base_ref'
export const GIT_PULL_REQUEST_BASE_BRANCH_SHA = 'git.pull_request.base_branch_sha'
export const GIT_PULL_REQUEST_BASE_BRANCH = 'git.pull_request.base_branch'

// General
export const SPAN_TYPE = 'span.type'
Expand Down
18 changes: 8 additions & 10 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,22 +414,20 @@ export const execute = (cmd: string, cwd?: string): Promise<{stderr: string; std
})

type GitHubWebhookPayload = {
pull_request: {
head: {
pull_request?: {
head?: {
sha: string
}
base?: {
sha: string
}
}
}

export const getGitHeadShaFromGitHubWebhookPayload = () => {
export const getGitHubEventPayload = () => {
if (!process.env.GITHUB_EVENT_PATH) {
return ''
return
}
try {
const parsedContents = JSON.parse(readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')) as GitHubWebhookPayload

return parsedContents.pull_request.head.sha
} catch (e) {
return ''
}
return JSON.parse(readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')) as GitHubWebhookPayload
}