Skip to content

Commit d0fbd29

Browse files
authored
fix: updating branch name fallbacks for GitHub Actions recordings (#27409)
* fix: updating branch name fallbacks for GitHub Actions recordings * Updating test for clarity * Adding changelog entry * Updating telemetry too --------- Co-authored-by: Tyler Biethman <tbiethman@users.noreply.github.com>
1 parent 152e4ff commit d0fbd29

File tree

5 files changed

+69
-24
lines changed

5 files changed

+69
-24
lines changed

cli/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
_Released 08/01/2023 (PENDING)_
55

6+
**Bugfixes:**
7+
8+
- Fixed an issue where unexpected branch names were being recorded for cypress runs when executed by GitHub Actions. The HEAD branch name will now be recorded by default for pull request workflows if a branch name cannot otherwise be detected from user overrides or from local git data. Fixes [#27389](https://github.com/cypress-io/cypress/issues/27389).
9+
610
**Performance:**
711

812
- Fixed an issue where unnecessary requests were being paused. No longer sends `X-Cypress-Is-XHR-Or-Fetch` header and infers resource type off of the server pre-request object. Fixes [#26620](https://github.com/cypress-io/cypress/issues/26620) and [#26622](https://github.com/cypress-io/cypress/issues/26622).

packages/server/lib/util/ci_provider.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ const _providerCiParams = () => {
262262
'GITHUB_RUN_ID',
263263
'GITHUB_RUN_ATTEMPT',
264264
'GITHUB_REPOSITORY',
265+
'GITHUB_BASE_REF',
266+
'GITHUB_HEAD_REF',
267+
'GITHUB_REF_NAME',
268+
'GITHUB_REF',
265269
]),
266270
// see https://docs.gitlab.com/ee/ci/variables/
267271
gitlab: extract([
@@ -536,7 +540,13 @@ const _providerCommitParams = () => {
536540
},
537541
githubActions: {
538542
sha: env.GITHUB_SHA,
539-
branch: env.GH_BRANCH || env.GITHUB_REF,
543+
// GH_BRANCH - populated with HEAD branch by cypress/github-action
544+
// GITHUB_HEAD_REF - populated with the head ref or source branch
545+
// of the pull request in a workflow run and is
546+
// otherwise unset
547+
// GITHUB_REF_NAME - populated with short ref name of the branch or
548+
// tag that triggered the workflow run
549+
branch: env.GH_BRANCH || env.GITHUB_HEAD_REF || env.GITHUB_REF_NAME,
540550
defaultBranch: env.GITHUB_BASE_REF,
541551
remoteBranch: env.GITHUB_HEAD_REF,
542552
runAttempt: env.GITHUB_RUN_ATTEMPT,

packages/server/test/unit/util/ci_provider_spec.js

+27-10
Original file line numberDiff line numberDiff line change
@@ -561,23 +561,21 @@ describe('lib/util/ci_provider', () => {
561561
})
562562

563563
it('github actions', () => {
564+
// with GH_BRANCH used as branch
564565
resetEnv = mockedEnv({
565566
GITHUB_ACTIONS: 'true',
566-
567567
GITHUB_WORKFLOW: 'ciGitHubWorkflowName',
568568
GITHUB_ACTION: 'ciGitHubActionId',
569569
GITHUB_EVENT_NAME: 'ciEventName',
570570
GITHUB_RUN_ID: 'ciGithubRunId',
571571
GITHUB_RUN_ATTEMPT: 'ciGithubRunAttempt',
572572
GITHUB_REPOSITORY: 'ciGithubRepository',
573-
GH_BRANCH: '',
574-
575573
GITHUB_SHA: 'ciCommitSha',
574+
GH_BRANCH: 'GHCommitBranch',
576575
GITHUB_REF: 'ciCommitRef',
577-
578-
// only for forked repos
579576
GITHUB_HEAD_REF: 'ciHeadRef',
580577
GITHUB_BASE_REF: 'ciBaseRef',
578+
GITHUB_REF_NAME: 'ciRefName',
581579
}, { clear: true })
582580

583581
expectsName('githubActions')
@@ -588,26 +586,45 @@ describe('lib/util/ci_provider', () => {
588586
githubRepository: 'ciGithubRepository',
589587
githubRunAttempt: 'ciGithubRunAttempt',
590588
githubRunId: 'ciGithubRunId',
589+
githubBaseRef: 'ciBaseRef',
590+
githubHeadRef: 'ciHeadRef',
591+
githubRefName: 'ciRefName',
592+
githubRef: 'ciCommitRef',
591593
})
592594

593595
expectsCommitParams({
594596
sha: 'ciCommitSha',
595597
defaultBranch: 'ciBaseRef',
596598
runAttempt: 'ciGithubRunAttempt',
597599
remoteBranch: 'ciHeadRef',
598-
branch: 'ciCommitRef',
600+
branch: 'GHCommitBranch',
599601
})
600602

603+
// with GITHUB_HEAD_REF used as branch
601604
resetEnv = mockedEnv({
602605
GITHUB_ACTIONS: 'true',
606+
GH_BRANCH: undefined,
607+
GITHUB_HEAD_REF: 'ciHeadRef',
608+
GITHUB_REF_NAME: 'ciRefName',
609+
GITHUB_REF: 'ciCommitRef',
610+
}, { clear: true })
611+
612+
expectsCommitParams({
613+
branch: 'ciHeadRef',
614+
remoteBranch: 'ciHeadRef',
615+
})
616+
617+
// with GITHUB_REF_NAME used as branch
618+
resetEnv = mockedEnv({
619+
GITHUB_ACTIONS: 'true',
620+
GH_BRANCH: undefined,
621+
GITHUB_HEAD_REF: undefined,
622+
GITHUB_REF_NAME: 'ciRefName',
603623
GITHUB_REF: 'ciCommitRef',
604-
GH_BRANCH: 'GHCommitBranch',
605-
GITHUB_RUN_ATTEMPT: 'ciGithubRunAttempt',
606624
}, { clear: true })
607625

608626
return expectsCommitParams({
609-
branch: 'GHCommitBranch',
610-
runAttempt: 'ciGithubRunAttempt',
627+
branch: 'ciRefName',
611628
})
612629
})
613630

packages/telemetry/src/detectors/githubActionsDetectorSync.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ class GithubActionsDetectorSync implements DetectorSync {
1515
detect (): IResource {
1616
const attributes: ResourceAttributes = {}
1717

18-
const { GITHUB_ACTION, GH_BRANCH, GITHUB_REF, GITHUB_SHA, GITHUB_RUN_NUMBER } = process.env
18+
const { GITHUB_ACTION, GH_BRANCH, GITHUB_HEAD_REF, GITHUB_REF_NAME, GITHUB_SHA, GITHUB_RUN_NUMBER } = process.env
1919

2020
if (GITHUB_ACTION) {
2121
attributes['ci.github_action'] = GITHUB_ACTION
2222
attributes['ci.build-number'] = GITHUB_RUN_NUMBER
23-
attributes['ci.branch'] = GH_BRANCH || GITHUB_REF
23+
attributes['ci.branch'] = GH_BRANCH || GITHUB_HEAD_REF || GITHUB_REF_NAME
2424
attributes['SHA1'] = GITHUB_SHA
2525
}
2626

packages/telemetry/test/detectors/githubActionsDetectorSync.spec.ts

+25-11
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ describe('githubActionsDetectorSync', () => {
1010
// cache values
1111
processValues.GITHUB_ACTION = process.env.GITHUB_ACTION
1212
processValues.GH_BRANCH = process.env.GH_BRANCH
13-
processValues.GITHUB_REF = process.env.GITHUB_REF
13+
processValues.GITHUB_HEAD_REF = process.env.GITHUB_HEAD_REF
14+
processValues.GITHUB_REF_NAME = process.env.GITHUB_REF_NAME
1415
processValues.GITHUB_RUN_NUMBER = process.env.GITHUB_RUN_NUMBER
1516
processValues.GITHUB_SHA = process.env.GITHUB_SHA
1617

1718
//reset values
1819
delete process.env.GITHUB_ACTION
1920
delete process.env.GH_BRANCH
20-
delete process.env.GITHUB_REF
21+
delete process.env.GITHUB_HEAD_REF
22+
delete process.env.GITHUB_REF_NAME
2123
delete process.env.GITHUB_RUN_NUMBER
2224
delete process.env.GITHUB_SHA
2325
})
@@ -26,7 +28,8 @@ describe('githubActionsDetectorSync', () => {
2628
// Replace values
2729
process.env.GITHUB_ACTION = processValues.GITHUB_ACTION
2830
process.env.GH_BRANCH = processValues.GH_BRANCH
29-
process.env.GITHUB_REF = processValues.GITHUB_REF
31+
process.env.GITHUB_HEAD_REF = processValues.GITHUB_HEAD_REF
32+
process.env.GITHUB_REF_NAME = processValues.GITHUB_REF_NAME
3033
process.env.GITHUB_RUN_NUMBER = processValues.GITHUB_RUN_NUMBER
3134
process.env.GITHUB_SHA = processValues.GITHUB_SHA
3235
})
@@ -47,14 +50,16 @@ describe('githubActionsDetectorSync', () => {
4750
// cache values
4851
processValues.GITHUB_ACTION = process.env.GITHUB_ACTION
4952
processValues.GH_BRANCH = process.env.GH_BRANCH
50-
processValues.GITHUB_REF = process.env.GITHUB_REF
53+
processValues.GITHUB_HEAD_REF = process.env.GITHUB_HEAD_REF
54+
processValues.GITHUB_REF_NAME = process.env.GITHUB_REF_NAME
5155
processValues.GITHUB_RUN_NUMBER = process.env.GITHUB_RUN_NUMBER
5256
processValues.GITHUB_SHA = process.env.GITHUB_SHA
5357

5458
//reset values
5559
process.env.GITHUB_ACTION = 'githubAction'
5660
process.env.GH_BRANCH = 'ghBranch'
57-
process.env.GITHUB_REF = 'ghRef'
61+
process.env.GITHUB_HEAD_REF = 'ghHeadRef'
62+
process.env.GITHUB_REF_NAME = 'ghRefName'
5863
process.env.GITHUB_RUN_NUMBER = 'ghRunNumber'
5964
process.env.GITHUB_SHA = 'ghSha'
6065
})
@@ -63,7 +68,8 @@ describe('githubActionsDetectorSync', () => {
6368
// Replace values
6469
process.env.GITHUB_ACTION = processValues.GITHUB_ACTION
6570
process.env.GH_BRANCH = processValues.GH_BRANCH
66-
process.env.GITHUB_REF = processValues.GITHUB_REF
71+
process.env.GITHUB_HEAD_REF = processValues.GITHUB_HEAD_REF
72+
process.env.GITHUB_REF_NAME = processValues.GITHUB_REF_NAME
6773
process.env.GITHUB_RUN_NUMBER = processValues.GITHUB_RUN_NUMBER
6874
process.env.GITHUB_SHA = processValues.GITHUB_SHA
6975
})
@@ -72,23 +78,31 @@ describe('githubActionsDetectorSync', () => {
7278
it('returns a resource with attributes', () => {
7379
const resource = githubActionsDetectorSync.detect()
7480

75-
console.log(resource.attributes)
76-
7781
expect(resource.attributes['ci.github_action']).to.equal('githubAction')
7882
expect(resource.attributes['ci.branch']).to.equal('ghBranch')
7983
expect(resource.attributes['ci.build-number']).to.equal('ghRunNumber')
8084
expect(resource.attributes['SHA1']).to.equal('ghSha')
8185
})
8286

83-
it('returns a resource with attributes when gh_branch is missing', () => {
87+
it('returns a resource with attributes when GH_BRANCH is missing', () => {
8488
delete process.env.GH_BRANCH
8589

8690
const resource = githubActionsDetectorSync.detect()
8791

88-
console.log(resource.attributes)
92+
expect(resource.attributes['ci.github_action']).to.equal('githubAction')
93+
expect(resource.attributes['ci.branch']).to.equal('ghHeadRef')
94+
expect(resource.attributes['ci.build-number']).to.equal('ghRunNumber')
95+
expect(resource.attributes['SHA1']).to.equal('ghSha')
96+
})
97+
98+
it('returns a resource with attributes when GH_BRANCH and GITHUB_HEAD_REF is missing', () => {
99+
delete process.env.GH_BRANCH
100+
delete process.env.GITHUB_HEAD_REF
101+
102+
const resource = githubActionsDetectorSync.detect()
89103

90104
expect(resource.attributes['ci.github_action']).to.equal('githubAction')
91-
expect(resource.attributes['ci.branch']).to.equal('ghRef')
105+
expect(resource.attributes['ci.branch']).to.equal('ghRefName')
92106
expect(resource.attributes['ci.build-number']).to.equal('ghRunNumber')
93107
expect(resource.attributes['SHA1']).to.equal('ghSha')
94108
})

0 commit comments

Comments
 (0)