From d6f8b9c20fd795d1510af21d7b8bb29ca10f1cfd Mon Sep 17 00:00:00 2001 From: Tyler Biethman Date: Wed, 26 Jul 2023 17:25:11 -0500 Subject: [PATCH 1/4] fix: updating branch name fallbacks for GitHub Actions recordings --- packages/server/lib/util/ci_provider.js | 11 ++++++- .../server/test/unit/util/ci_provider_spec.js | 29 +++++++++++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/packages/server/lib/util/ci_provider.js b/packages/server/lib/util/ci_provider.js index 68cc80bea5c0..ead06e4a122c 100644 --- a/packages/server/lib/util/ci_provider.js +++ b/packages/server/lib/util/ci_provider.js @@ -262,6 +262,9 @@ const _providerCiParams = () => { 'GITHUB_RUN_ID', 'GITHUB_RUN_ATTEMPT', 'GITHUB_REPOSITORY', + 'GITHUB_HEAD_REF', + 'GITHUB_REF_NAME', + 'GITHUB_REF', ]), // see https://docs.gitlab.com/ee/ci/variables/ gitlab: extract([ @@ -536,7 +539,13 @@ const _providerCommitParams = () => { }, githubActions: { sha: env.GITHUB_SHA, - branch: env.GH_BRANCH || env.GITHUB_REF, + // GH_BRANCH - populated with HEAD branch by cypress/github-action + // GITHUB_HEAD_REF - populated with the head ref or source branch + // of the pull request in a workflow run and is + // otherwise unset + // GITHUB_REF_NAME - populated with short ref name of the branch or + // tag that triggered the workflow run + branch: env.GH_BRANCH || env.GITHUB_HEAD_REF || env.GITHUB_REF_NAME, defaultBranch: env.GITHUB_BASE_REF, remoteBranch: env.GITHUB_HEAD_REF, runAttempt: env.GITHUB_RUN_ATTEMPT, diff --git a/packages/server/test/unit/util/ci_provider_spec.js b/packages/server/test/unit/util/ci_provider_spec.js index 892dd8bc0b59..c8fc3f228fa3 100644 --- a/packages/server/test/unit/util/ci_provider_spec.js +++ b/packages/server/test/unit/util/ci_provider_spec.js @@ -561,6 +561,7 @@ describe('lib/util/ci_provider', () => { }) it('github actions', () => { + // with GITHUB_HEAD_REF used as branch resetEnv = mockedEnv({ GITHUB_ACTIONS: 'true', @@ -575,9 +576,9 @@ describe('lib/util/ci_provider', () => { GITHUB_SHA: 'ciCommitSha', GITHUB_REF: 'ciCommitRef', - // only for forked repos GITHUB_HEAD_REF: 'ciHeadRef', GITHUB_BASE_REF: 'ciBaseRef', + GITHUB_REF_NAME: 'ciRefName', }, { clear: true }) expectsName('githubActions') @@ -588,6 +589,9 @@ describe('lib/util/ci_provider', () => { githubRepository: 'ciGithubRepository', githubRunAttempt: 'ciGithubRunAttempt', githubRunId: 'ciGithubRunId', + githubHeadRef: 'ciHeadRef', + githubRefName: 'ciRefName', + githubRef: 'ciCommitRef', }) expectsCommitParams({ @@ -595,19 +599,34 @@ describe('lib/util/ci_provider', () => { defaultBranch: 'ciBaseRef', runAttempt: 'ciGithubRunAttempt', remoteBranch: 'ciHeadRef', - branch: 'ciCommitRef', + branch: 'ciHeadRef', }) + // with GH_BRANCH used as branch resetEnv = mockedEnv({ GITHUB_ACTIONS: 'true', GITHUB_REF: 'ciCommitRef', GH_BRANCH: 'GHCommitBranch', - GITHUB_RUN_ATTEMPT: 'ciGithubRunAttempt', + GITHUB_HEAD_REF: 'ciHeadRef', + GITHUB_REF_NAME: 'ciRefName', }, { clear: true }) - return expectsCommitParams({ + expectsCommitParams({ branch: 'GHCommitBranch', - runAttempt: 'ciGithubRunAttempt', + remoteBranch: 'ciHeadRef', + }) + + // with GITHUB_REF_NAME used as branch + resetEnv = mockedEnv({ + GITHUB_ACTIONS: 'true', + GITHUB_REF: 'ciCommitRef', + GH_BRANCH: undefined, + GITHUB_HEAD_REF: undefined, + GITHUB_REF_NAME: 'ciRefName', + }, { clear: true }) + + return expectsCommitParams({ + branch: 'ciRefName', }) }) From df416bedaad6770c7640a85afea3da3bf30a4f44 Mon Sep 17 00:00:00 2001 From: Tyler Biethman Date: Thu, 27 Jul 2023 10:47:02 -0500 Subject: [PATCH 2/4] Updating test for clarity --- packages/server/lib/util/ci_provider.js | 1 + .../server/test/unit/util/ci_provider_spec.js | 20 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/server/lib/util/ci_provider.js b/packages/server/lib/util/ci_provider.js index ead06e4a122c..bf9015137ae7 100644 --- a/packages/server/lib/util/ci_provider.js +++ b/packages/server/lib/util/ci_provider.js @@ -262,6 +262,7 @@ const _providerCiParams = () => { 'GITHUB_RUN_ID', 'GITHUB_RUN_ATTEMPT', 'GITHUB_REPOSITORY', + 'GITHUB_BASE_REF', 'GITHUB_HEAD_REF', 'GITHUB_REF_NAME', 'GITHUB_REF', diff --git a/packages/server/test/unit/util/ci_provider_spec.js b/packages/server/test/unit/util/ci_provider_spec.js index c8fc3f228fa3..53286fd079c2 100644 --- a/packages/server/test/unit/util/ci_provider_spec.js +++ b/packages/server/test/unit/util/ci_provider_spec.js @@ -561,21 +561,18 @@ describe('lib/util/ci_provider', () => { }) it('github actions', () => { - // with GITHUB_HEAD_REF used as branch + // with GH_BRANCH used as branch resetEnv = mockedEnv({ GITHUB_ACTIONS: 'true', - GITHUB_WORKFLOW: 'ciGitHubWorkflowName', GITHUB_ACTION: 'ciGitHubActionId', GITHUB_EVENT_NAME: 'ciEventName', GITHUB_RUN_ID: 'ciGithubRunId', GITHUB_RUN_ATTEMPT: 'ciGithubRunAttempt', GITHUB_REPOSITORY: 'ciGithubRepository', - GH_BRANCH: '', - GITHUB_SHA: 'ciCommitSha', + GH_BRANCH: 'GHCommitBranch', GITHUB_REF: 'ciCommitRef', - GITHUB_HEAD_REF: 'ciHeadRef', GITHUB_BASE_REF: 'ciBaseRef', GITHUB_REF_NAME: 'ciRefName', @@ -589,6 +586,7 @@ describe('lib/util/ci_provider', () => { githubRepository: 'ciGithubRepository', githubRunAttempt: 'ciGithubRunAttempt', githubRunId: 'ciGithubRunId', + githubBaseRef: 'ciBaseRef', githubHeadRef: 'ciHeadRef', githubRefName: 'ciRefName', githubRef: 'ciCommitRef', @@ -599,30 +597,30 @@ describe('lib/util/ci_provider', () => { defaultBranch: 'ciBaseRef', runAttempt: 'ciGithubRunAttempt', remoteBranch: 'ciHeadRef', - branch: 'ciHeadRef', + branch: 'GHCommitBranch', }) - // with GH_BRANCH used as branch + // with GITHUB_HEAD_REF used as branch resetEnv = mockedEnv({ GITHUB_ACTIONS: 'true', - GITHUB_REF: 'ciCommitRef', - GH_BRANCH: 'GHCommitBranch', + GH_BRANCH: undefined, GITHUB_HEAD_REF: 'ciHeadRef', GITHUB_REF_NAME: 'ciRefName', + GITHUB_REF: 'ciCommitRef', }, { clear: true }) expectsCommitParams({ - branch: 'GHCommitBranch', + branch: 'ciHeadRef', remoteBranch: 'ciHeadRef', }) // with GITHUB_REF_NAME used as branch resetEnv = mockedEnv({ GITHUB_ACTIONS: 'true', - GITHUB_REF: 'ciCommitRef', GH_BRANCH: undefined, GITHUB_HEAD_REF: undefined, GITHUB_REF_NAME: 'ciRefName', + GITHUB_REF: 'ciCommitRef', }, { clear: true }) return expectsCommitParams({ From 5183f2faba44e6a6f8ad3209f34fd0844be86170 Mon Sep 17 00:00:00 2001 From: Tyler Biethman Date: Thu, 27 Jul 2023 10:58:59 -0500 Subject: [PATCH 3/4] Adding changelog entry --- cli/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index d7f1e30b9583..dadb9babaf0c 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -3,6 +3,10 @@ _Released 08/01/2023 (PENDING)_ +**Bugfixes:** + +- 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). + **Performance:** - 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). From 3114c3e0ff606f198614c3a3bf7b36e78c7238e3 Mon Sep 17 00:00:00 2001 From: Tyler Biethman Date: Thu, 27 Jul 2023 12:50:41 -0500 Subject: [PATCH 4/4] Updating telemetry too --- .../detectors/githubActionsDetectorSync.ts | 4 +-- .../githubActionsDetectorSync.spec.ts | 36 +++++++++++++------ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/packages/telemetry/src/detectors/githubActionsDetectorSync.ts b/packages/telemetry/src/detectors/githubActionsDetectorSync.ts index 0edfd6384b24..95463c2ffd8e 100644 --- a/packages/telemetry/src/detectors/githubActionsDetectorSync.ts +++ b/packages/telemetry/src/detectors/githubActionsDetectorSync.ts @@ -15,12 +15,12 @@ class GithubActionsDetectorSync implements DetectorSync { detect (): IResource { const attributes: ResourceAttributes = {} - const { GITHUB_ACTION, GH_BRANCH, GITHUB_REF, GITHUB_SHA, GITHUB_RUN_NUMBER } = process.env + const { GITHUB_ACTION, GH_BRANCH, GITHUB_HEAD_REF, GITHUB_REF_NAME, GITHUB_SHA, GITHUB_RUN_NUMBER } = process.env if (GITHUB_ACTION) { attributes['ci.github_action'] = GITHUB_ACTION attributes['ci.build-number'] = GITHUB_RUN_NUMBER - attributes['ci.branch'] = GH_BRANCH || GITHUB_REF + attributes['ci.branch'] = GH_BRANCH || GITHUB_HEAD_REF || GITHUB_REF_NAME attributes['SHA1'] = GITHUB_SHA } diff --git a/packages/telemetry/test/detectors/githubActionsDetectorSync.spec.ts b/packages/telemetry/test/detectors/githubActionsDetectorSync.spec.ts index 54bdac69787a..280cd2aaac56 100644 --- a/packages/telemetry/test/detectors/githubActionsDetectorSync.spec.ts +++ b/packages/telemetry/test/detectors/githubActionsDetectorSync.spec.ts @@ -10,14 +10,16 @@ describe('githubActionsDetectorSync', () => { // cache values processValues.GITHUB_ACTION = process.env.GITHUB_ACTION processValues.GH_BRANCH = process.env.GH_BRANCH - processValues.GITHUB_REF = process.env.GITHUB_REF + processValues.GITHUB_HEAD_REF = process.env.GITHUB_HEAD_REF + processValues.GITHUB_REF_NAME = process.env.GITHUB_REF_NAME processValues.GITHUB_RUN_NUMBER = process.env.GITHUB_RUN_NUMBER processValues.GITHUB_SHA = process.env.GITHUB_SHA //reset values delete process.env.GITHUB_ACTION delete process.env.GH_BRANCH - delete process.env.GITHUB_REF + delete process.env.GITHUB_HEAD_REF + delete process.env.GITHUB_REF_NAME delete process.env.GITHUB_RUN_NUMBER delete process.env.GITHUB_SHA }) @@ -26,7 +28,8 @@ describe('githubActionsDetectorSync', () => { // Replace values process.env.GITHUB_ACTION = processValues.GITHUB_ACTION process.env.GH_BRANCH = processValues.GH_BRANCH - process.env.GITHUB_REF = processValues.GITHUB_REF + process.env.GITHUB_HEAD_REF = processValues.GITHUB_HEAD_REF + process.env.GITHUB_REF_NAME = processValues.GITHUB_REF_NAME process.env.GITHUB_RUN_NUMBER = processValues.GITHUB_RUN_NUMBER process.env.GITHUB_SHA = processValues.GITHUB_SHA }) @@ -47,14 +50,16 @@ describe('githubActionsDetectorSync', () => { // cache values processValues.GITHUB_ACTION = process.env.GITHUB_ACTION processValues.GH_BRANCH = process.env.GH_BRANCH - processValues.GITHUB_REF = process.env.GITHUB_REF + processValues.GITHUB_HEAD_REF = process.env.GITHUB_HEAD_REF + processValues.GITHUB_REF_NAME = process.env.GITHUB_REF_NAME processValues.GITHUB_RUN_NUMBER = process.env.GITHUB_RUN_NUMBER processValues.GITHUB_SHA = process.env.GITHUB_SHA //reset values process.env.GITHUB_ACTION = 'githubAction' process.env.GH_BRANCH = 'ghBranch' - process.env.GITHUB_REF = 'ghRef' + process.env.GITHUB_HEAD_REF = 'ghHeadRef' + process.env.GITHUB_REF_NAME = 'ghRefName' process.env.GITHUB_RUN_NUMBER = 'ghRunNumber' process.env.GITHUB_SHA = 'ghSha' }) @@ -63,7 +68,8 @@ describe('githubActionsDetectorSync', () => { // Replace values process.env.GITHUB_ACTION = processValues.GITHUB_ACTION process.env.GH_BRANCH = processValues.GH_BRANCH - process.env.GITHUB_REF = processValues.GITHUB_REF + process.env.GITHUB_HEAD_REF = processValues.GITHUB_HEAD_REF + process.env.GITHUB_REF_NAME = processValues.GITHUB_REF_NAME process.env.GITHUB_RUN_NUMBER = processValues.GITHUB_RUN_NUMBER process.env.GITHUB_SHA = processValues.GITHUB_SHA }) @@ -72,23 +78,31 @@ describe('githubActionsDetectorSync', () => { it('returns a resource with attributes', () => { const resource = githubActionsDetectorSync.detect() - console.log(resource.attributes) - expect(resource.attributes['ci.github_action']).to.equal('githubAction') expect(resource.attributes['ci.branch']).to.equal('ghBranch') expect(resource.attributes['ci.build-number']).to.equal('ghRunNumber') expect(resource.attributes['SHA1']).to.equal('ghSha') }) - it('returns a resource with attributes when gh_branch is missing', () => { + it('returns a resource with attributes when GH_BRANCH is missing', () => { delete process.env.GH_BRANCH const resource = githubActionsDetectorSync.detect() - console.log(resource.attributes) + expect(resource.attributes['ci.github_action']).to.equal('githubAction') + expect(resource.attributes['ci.branch']).to.equal('ghHeadRef') + expect(resource.attributes['ci.build-number']).to.equal('ghRunNumber') + expect(resource.attributes['SHA1']).to.equal('ghSha') + }) + + it('returns a resource with attributes when GH_BRANCH and GITHUB_HEAD_REF is missing', () => { + delete process.env.GH_BRANCH + delete process.env.GITHUB_HEAD_REF + + const resource = githubActionsDetectorSync.detect() expect(resource.attributes['ci.github_action']).to.equal('githubAction') - expect(resource.attributes['ci.branch']).to.equal('ghRef') + expect(resource.attributes['ci.branch']).to.equal('ghRefName') expect(resource.attributes['ci.build-number']).to.equal('ghRunNumber') expect(resource.attributes['SHA1']).to.equal('ghSha') })