Test automation #40
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: PR Comment Test Runner | |
on: | |
issue_comment: | |
types: [created] | |
workflow_dispatch: | |
permissions: | |
statuses: write | |
pull-requests: read | |
jobs: | |
test: | |
if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, 'run tests') }} | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
ref: refs/pull/${{ github.event.issue.number }}/head | |
- name: Verify branch | |
run: | | |
echo "Current branch:" | |
git branch --show-current | |
echo "Current commit:" | |
git rev-parse HEAD | |
- name: Get PR SHA | |
id: get-pr-info | |
run: | | |
PR_SHA=$(git rev-parse HEAD) | |
echo "sha=$PR_SHA" >> $GITHUB_OUTPUT | |
- name: Setup pnpm | |
uses: pnpm/action-setup@v2 | |
with: | |
version: 8 | |
run_install: false | |
- name: Setup Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '18' | |
cache: 'pnpm' | |
- name: Get target branch | |
id: get-target-branch | |
run: | | |
TARGET_BRANCH=$(gh pr view ${{ github.event.issue.number }} --json baseRefName -q .baseRefName) | |
echo "target_branch=$TARGET_BRANCH" >> $GITHUB_OUTPUT | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Install dependencies | |
run: pnpm install --frozen-lockfile | |
- name: Run tests | |
id: run-tests | |
run: | | |
set +e # Don't exit on error | |
pnpm test 2>&1 | tee test_output.txt | |
TEST_EXIT_CODE=${PIPESTATUS[0]} | |
# Prepare the output, ensuring proper escaping | |
{ | |
echo "output<<EOF" | |
cat test_output.txt | |
echo "EOF" | |
if [ $TEST_EXIT_CODE -eq 0 ]; then | |
echo "status=success" | |
else | |
echo "status=failure" | |
fi | |
echo "exit_code=$TEST_EXIT_CODE" | |
} >> $GITHUB_OUTPUT | |
- name: Create Status Check | |
if: always() | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
try { | |
const testStatus = '${{ steps.run-tests.outputs.status }}'; | |
const exitCode = '${{ steps.run-tests.outputs.exit_code }}'; | |
const statusData = { | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
sha: '${{ steps.get-pr-info.outputs.sha }}', | |
state: testStatus === 'success' ? 'success' : 'failure', | |
target_url: `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`, | |
description: testStatus === 'success' | |
? 'Tests passed successfully!' | |
: `Tests failed with exit code ${exitCode}`, | |
context: 'PR Tests' | |
}; | |
await github.rest.repos.createCommitStatus(statusData); | |
} catch (error) { | |
console.error('Error creating status check:', error); | |
core.setFailed(error.message); | |
} |