|
| 1 | +name: Internal - Tests for checkout action (issue_comment trigger) |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created, edited] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + pull-requests: read |
| 10 | + issues: read |
| 11 | + |
| 12 | +jobs: |
| 13 | + test-checkout-issue-comment: |
| 14 | + # Only run on pull request comments |
| 15 | + if: github.event.issue.pull_request != null |
| 16 | + name: Test checkout action on issue_comment trigger |
| 17 | + runs-on: ubuntu-latest |
| 18 | + steps: |
| 19 | + - name: Arrange - Get PR information |
| 20 | + id: pr-info |
| 21 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 22 | + with: |
| 23 | + script: | |
| 24 | + const pr = await github.rest.pulls.get({ |
| 25 | + owner: context.repo.owner, |
| 26 | + repo: context.repo.repo, |
| 27 | + pull_number: context.issue.number, |
| 28 | + }); |
| 29 | + core.setOutput('head-sha', pr.data.head.sha); |
| 30 | + core.setOutput('head-ref', pr.data.head.ref); |
| 31 | + console.log(`PR Head SHA: ${pr.data.head.sha}`); |
| 32 | + console.log(`PR Head Ref: ${pr.data.head.ref}`); |
| 33 | +
|
| 34 | + - name: Act - Checkout using custom checkout action (issue_comment case) |
| 35 | + id: checkout |
| 36 | + uses: ./actions/checkout |
| 37 | + with: |
| 38 | + persist-credentials: true |
| 39 | + |
| 40 | + - name: Assert - Verify correct PR SHA was checked out |
| 41 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 42 | + env: |
| 43 | + EXPECTED_SHA: ${{ steps.pr-info.outputs.head-sha }} |
| 44 | + with: |
| 45 | + script: | |
| 46 | + const { execSync } = require('child_process'); |
| 47 | + const currentSha = execSync('git rev-parse HEAD').toString().trim(); |
| 48 | + const expectedSha = process.env.EXPECTED_SHA; |
| 49 | +
|
| 50 | + console.log(`Current SHA: ${currentSha}`); |
| 51 | + console.log(`Expected PR Head SHA: ${expectedSha}`); |
| 52 | +
|
| 53 | + if (currentSha !== expectedSha) { |
| 54 | + throw new Error(`Checked out SHA (${currentSha}) does not match PR head SHA (${expectedSha})`); |
| 55 | + } |
| 56 | +
|
| 57 | + console.log('✓ Verified: Checked out correct PR head SHA'); |
| 58 | +
|
| 59 | + - name: Assert - Verify not on main branch |
| 60 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 61 | + env: |
| 62 | + EXPECTED_BRANCH: ${{ steps.pr-info.outputs.head-ref }} |
| 63 | + with: |
| 64 | + script: | |
| 65 | + const { execSync } = require('child_process'); |
| 66 | + const currentBranch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim(); |
| 67 | + const expectedBranch = process.env.EXPECTED_BRANCH; |
| 68 | +
|
| 69 | + console.log(`Current branch/ref: ${currentBranch}`); |
| 70 | + console.log(`Expected branch: ${expectedBranch}`); |
| 71 | +
|
| 72 | + if (['HEAD', 'main', 'master'].includes(currentBranch)) { |
| 73 | + throw new Error(`Checked out main/master branch instead of PR branch`); |
| 74 | + } |
| 75 | +
|
| 76 | + console.log('✓ Verified: Not on main/master branch'); |
| 77 | +
|
| 78 | + - name: Assert - Verify credentials are persisted |
| 79 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 80 | + with: |
| 81 | + script: | |
| 82 | + const { execSync } = require('child_process'); |
| 83 | + try { |
| 84 | + execSync('git config --local --get-regexp "^url\\.https://.*\\.insteadOf"', { stdio: 'pipe' }); |
| 85 | + console.log('✓ Verified: Credentials are persisted'); |
| 86 | + } catch (error) { |
| 87 | + console.log('WARNING: Token credentials appear not to be persisted'); |
| 88 | + console.log('This may be expected depending on GitHub token availability in issue_comment context'); |
| 89 | + } |
| 90 | +
|
| 91 | + - name: Info - Display checkout details |
| 92 | + if: always() |
| 93 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 94 | + with: |
| 95 | + script: | |
| 96 | + const { execSync } = require('child_process'); |
| 97 | + console.log('=== Git Status ==='); |
| 98 | + console.log(execSync('git status').toString()); |
| 99 | + console.log(''); |
| 100 | + console.log('=== Git Log (last 3 commits) ==='); |
| 101 | + console.log(execSync('git log --oneline -3').toString()); |
0 commit comments