@cypress/github-action 6.3.0 → 6.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +41 -0
  2. package/dist/index.js +76 -0
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1462,6 +1462,47 @@ jobs:
1462
1462
  publish-summary: false
1463
1463
  ```
1464
1464
 
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
+
1465
1506
  ## Node.js
1466
1507
 
1467
1508
  ### Support
package/dist/index.js CHANGED
@@ -74832,6 +74832,80 @@ const waitOnMaybe = () => {
74832
74832
 
74833
74833
  const I = (x) => x
74834
74834
 
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
+
74835
74909
  /**
74836
74910
  * Asks Cypress API if there were already builds for this commit.
74837
74911
  * In that case increments the count to get unique parallel id.
@@ -75065,6 +75139,8 @@ const runTests = async () => {
75065
75139
  core.exportVariable('CYPRESS_CACHE_FOLDER', CYPRESS_CACHE_FOLDER)
75066
75140
  core.exportVariable('TERM', 'xterm')
75067
75141
 
75142
+ await detectPrNumber()
75143
+
75068
75144
  if (customCommand) {
75069
75145
  console.log('Using custom test command: %s', customCommand)
75070
75146
  return execCommand(customCommand, true, 'run tests')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cypress/github-action",
3
- "version": "6.3.0",
3
+ "version": "6.4.0",
4
4
  "description": "GitHub Action for running Cypress end-to-end and component tests",
5
5
  "private": false,
6
6
  "main": "dist/index.js",