Skip to content

Commit 7215fc1

Browse files
ax-vasquezMikeMcC399renovate[bot]
authoredSep 1, 2023
feat: [CYCLOUD-1447] Auto-detect PR numbers (#1009)
* WIP - need to ask for ideal way to check existing variables * check for existing variables * consolidate comments * remove yarn lock file * run commands from PR feedback * logging for testing purposes only * more logging * more logging * simplify URL logic * more logging * remove logging - fix url string * log response for fallback logic * docs: rewrite readme working directory description (#1005) * chore(deps): update dependency @types/node to v20.5.7 (#1008) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * docs: rework requirements for contributors (#1010) * feat(deps): update cypress to 13.0.0 (#1012) * logging for testing purposes only * test: run component test example from current branch (#1013) * more logging * more logging * simplify URL logic * more logging * remove logging - fix url string * log response for fallback logic * cleanup * doc update * Update README.md Co-authored-by: Mike McCready <66998419+MikeMcC399@users.noreply.github.com> * Update README.md Co-authored-by: Mike McCready <66998419+MikeMcC399@users.noreply.github.com> * exit detectPrNumber if variables already defined * Update README.md Co-authored-by: Mike McCready <66998419+MikeMcC399@users.noreply.github.com> * docs update - formatting fix for readability * Update README.md Co-authored-by: Mike McCready <66998419+MikeMcC399@users.noreply.github.com> * Update README.md Co-authored-by: Mike McCready <66998419+MikeMcC399@users.noreply.github.com> * try catch pr data request * Readme update * Update README.md Co-authored-by: Mike McCready <66998419+MikeMcC399@users.noreply.github.com> --------- Co-authored-by: Mike McCready <66998419+MikeMcC399@users.noreply.github.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
1 parent fd50b62 commit 7215fc1

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed
 

‎README.md

+41
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,47 @@ jobs:
14621462
publish-summary: false
14631463
```
14641464

1465+
### Automatic PR number & URL detection
1466+
1467+
When recording runs to Cypress Cloud, the PR number and URL can be automatically detected if you pass `GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}`
1468+
via the workflow `env`. When set, this value enables the Action to perform additional logic that grabs the related PR number and URL (if they
1469+
exist) and sets them in the environment variables `CYPRESS_PULL_REQUEST_ID` and `CYPRESS_PULL_REQUEST_URL`, respectively.
1470+
* See Cypress' documentation on [CI Build Information](https://on.cypress.io/guides/continuous-integration/introduction#CI-Build-Information)
1471+
1472+
Example workflow using the variables:
1473+
```yml
1474+
name: Example echo PR number and URL
1475+
on: push
1476+
jobs:
1477+
cypress-run:
1478+
runs-on: ubuntu-22.04
1479+
steps:
1480+
- name: Checkout
1481+
uses: actions/checkout@v3
1482+
- name: Cypress run
1483+
uses: cypress-io/github-action@v6
1484+
with:
1485+
record: true
1486+
- run: echo "PR number is $CYPRESS_PULL_REQUEST_ID"
1487+
- run: echo "PR URL is $CYPRESS_PULL_REQUEST_URL"
1488+
env:
1489+
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
1490+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1491+
```
1492+
1493+
#### Triggering event: `pull_request`/`pull_request_target`
1494+
1495+
For either of these events, we set `CYPRESS_PULL_REQUEST_ID` and `CYPRESS_PULL_REQUEST_URL` to that of the PR number and URL, respectively, of the
1496+
PR that triggered the workflow.
1497+
1498+
#### Triggering event: `push`
1499+
1500+
When a commit on a branch without a PR is made, the Cypress GitHub Action checks to see if the commit that triggered the workflow has a
1501+
related PR. If the commit exists in any other PRs, it's considered a related PR. When there are related PRs, we grab the first related PR
1502+
and use that PR's number and URL for `CYPRESS_PULL_REQUEST_ID` and `CYPRESS_PULL_REQUEST_URL`, respectively.
1503+
1504+
If no related PR is detected, `CYPRESS_PULL_REQUEST_ID` and `CYPRESS_PULL_REQUEST_URL` will be undefined.
1505+
14651506
## Node.js
14661507

14671508
### Support

‎dist/index.js

+76
Original file line numberDiff line numberDiff line change
@@ -74832,6 +74832,80 @@ const waitOnMaybe = () => {
7483274832

7483374833
const I = (x) => x
7483474834

74835+
const detectPrNumber = async () => {
74836+
const {
74837+
GITHUB_SHA,
74838+
GITHUB_TOKEN,
74839+
GITHUB_RUN_ID,
74840+
GITHUB_REPOSITORY,
74841+
GITHUB_HEAD_REF,
74842+
GITHUB_REF,
74843+
GITHUB_SERVER_URL,
74844+
CYPRESS_PULL_REQUEST_ID,
74845+
CYPRESS_PULL_REQUEST_URL
74846+
} = process.env
74847+
74848+
if (CYPRESS_PULL_REQUEST_ID && CYPRESS_PULL_REQUEST_URL) {
74849+
// Both pull request envs are already defined - no need to do anything else
74850+
return
74851+
}
74852+
74853+
const [owner, repo] = GITHUB_REPOSITORY.split('/')
74854+
let prNumber
74855+
74856+
if (GITHUB_TOKEN) {
74857+
debug(
74858+
`Detecting PR number by asking GitHub about run ${GITHUB_RUN_ID}`
74859+
)
74860+
74861+
const client = new Octokit({
74862+
auth: GITHUB_TOKEN
74863+
})
74864+
74865+
if (GITHUB_HEAD_REF) {
74866+
// GITHUB_HEAD_REF is only defined when the event that triggered it was 'pull_request' or 'pull_request_target' (meaning a PR number should be readily-available)
74867+
// should have format refs/pull/<pr_number>/merge when triggered by pull_request workflow
74868+
prNumber = parseInt(GITHUB_REF.split('/')[2])
74869+
} else {
74870+
try {
74871+
const resp = await client.request(
74872+
'GET /repos/:owner/:repo/commits/:commit_sha/pulls',
74873+
{
74874+
owner,
74875+
repo,
74876+
commit_sha: GITHUB_SHA
74877+
}
74878+
)
74879+
74880+
if (
74881+
resp &&
74882+
resp.data &&
74883+
resp.data[0] &&
74884+
resp.data[0].number
74885+
) {
74886+
prNumber = resp.data[0].number
74887+
}
74888+
} catch (e) {
74889+
console.error(
74890+
`Unable to fetch related PR data for commit: '${GITHUB_SHA}': `,
74891+
e
74892+
)
74893+
}
74894+
}
74895+
74896+
if (prNumber) {
74897+
if (!CYPRESS_PULL_REQUEST_ID) {
74898+
core.exportVariable('CYPRESS_PULL_REQUEST_ID', prNumber)
74899+
}
74900+
74901+
const url = `${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/pull/${prNumber}`
74902+
if (!CYPRESS_PULL_REQUEST_URL) {
74903+
core.exportVariable('CYPRESS_PULL_REQUEST_URL', url)
74904+
}
74905+
}
74906+
}
74907+
}
74908+
7483574909
/**
7483674910
* Asks Cypress API if there were already builds for this commit.
7483774911
* In that case increments the count to get unique parallel id.
@@ -75065,6 +75139,8 @@ const runTests = async () => {
7506575139
core.exportVariable('CYPRESS_CACHE_FOLDER', CYPRESS_CACHE_FOLDER)
7506675140
core.exportVariable('TERM', 'xterm')
7506775141

75142+
await detectPrNumber()
75143+
7506875144
if (customCommand) {
7506975145
console.log('Using custom test command: %s', customCommand)
7507075146
return execCommand(customCommand, true, 'run tests')

‎index.js

+76
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,80 @@ const waitOnMaybe = () => {
436436

437437
const I = (x) => x
438438

439+
const detectPrNumber = async () => {
440+
const {
441+
GITHUB_SHA,
442+
GITHUB_TOKEN,
443+
GITHUB_RUN_ID,
444+
GITHUB_REPOSITORY,
445+
GITHUB_HEAD_REF,
446+
GITHUB_REF,
447+
GITHUB_SERVER_URL,
448+
CYPRESS_PULL_REQUEST_ID,
449+
CYPRESS_PULL_REQUEST_URL
450+
} = process.env
451+
452+
if (CYPRESS_PULL_REQUEST_ID && CYPRESS_PULL_REQUEST_URL) {
453+
// Both pull request envs are already defined - no need to do anything else
454+
return
455+
}
456+
457+
const [owner, repo] = GITHUB_REPOSITORY.split('/')
458+
let prNumber
459+
460+
if (GITHUB_TOKEN) {
461+
debug(
462+
`Detecting PR number by asking GitHub about run ${GITHUB_RUN_ID}`
463+
)
464+
465+
const client = new Octokit({
466+
auth: GITHUB_TOKEN
467+
})
468+
469+
if (GITHUB_HEAD_REF) {
470+
// GITHUB_HEAD_REF is only defined when the event that triggered it was 'pull_request' or 'pull_request_target' (meaning a PR number should be readily-available)
471+
// should have format refs/pull/<pr_number>/merge when triggered by pull_request workflow
472+
prNumber = parseInt(GITHUB_REF.split('/')[2])
473+
} else {
474+
try {
475+
const resp = await client.request(
476+
'GET /repos/:owner/:repo/commits/:commit_sha/pulls',
477+
{
478+
owner,
479+
repo,
480+
commit_sha: GITHUB_SHA
481+
}
482+
)
483+
484+
if (
485+
resp &&
486+
resp.data &&
487+
resp.data[0] &&
488+
resp.data[0].number
489+
) {
490+
prNumber = resp.data[0].number
491+
}
492+
} catch (e) {
493+
console.error(
494+
`Unable to fetch related PR data for commit: '${GITHUB_SHA}': `,
495+
e
496+
)
497+
}
498+
}
499+
500+
if (prNumber) {
501+
if (!CYPRESS_PULL_REQUEST_ID) {
502+
core.exportVariable('CYPRESS_PULL_REQUEST_ID', prNumber)
503+
}
504+
505+
const url = `${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/pull/${prNumber}`
506+
if (!CYPRESS_PULL_REQUEST_URL) {
507+
core.exportVariable('CYPRESS_PULL_REQUEST_URL', url)
508+
}
509+
}
510+
}
511+
}
512+
439513
/**
440514
* Asks Cypress API if there were already builds for this commit.
441515
* In that case increments the count to get unique parallel id.
@@ -669,6 +743,8 @@ const runTests = async () => {
669743
core.exportVariable('CYPRESS_CACHE_FOLDER', CYPRESS_CACHE_FOLDER)
670744
core.exportVariable('TERM', 'xterm')
671745

746+
await detectPrNumber()
747+
672748
if (customCommand) {
673749
console.log('Using custom test command: %s', customCommand)
674750
return execCommand(customCommand, true, 'run tests')

0 commit comments

Comments
 (0)
Please sign in to comment.