diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5b327249..ba0aca0f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -5,7 +5,7 @@ concurrency: cancel-in-progress: true on: - pull_request: + pull_request_target: paths: - "app/**" - "public/**" @@ -25,6 +25,9 @@ on: - "*config.*" workflow_dispatch: +permissions: + pull-requests: write + jobs: test: runs-on: ubuntu-latest @@ -42,4 +45,95 @@ jobs: run: bun i - name: Test app - run: bun test + run: | + bun test &> >(tee -p test.log) || echo STATUS=$? >> $GITHUB_ENV + { + echo 'RESULT<> $GITHUB_ENV + + - name: Create test comment + if: github.event_name == 'pull_request_target' && env.STATUS == 1 + env: + ACTOR: ${{ github.actor }} + REPO: ${{ github.repository }} + RUN_ID: ${{ github.run_id }} + SHA: ${{ github.event.pull_request.head.sha }} + WORKFLOW_REF: ${{ github.workflow_ref }} + run: | + { + echo 'TEST_COMMENT<' + echo 'Show Test Log' + echo '' + echo "\`\`\`shell" + echo "$RESULT" + echo "\`\`\`" + echo '' + echo '' + echo '' + echo "👀[View in Actions](https://github.com/$REPO/actions/runs/$RUN_ID)" + echo "" + echo 'EOF' + } >> $GITHUB_ENV + + - name: Add a test comment + if: github.event_name == 'pull_request_target' && env.STATUS == 1 + uses: actions/github-script@v7 + with: + script: | + const { data: comments } = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + }) + const testComment = comments.find(comment => { + return comment.user.type === 'Bot' && comment.body.includes('# ❌Test Failed') + }) + if (testComment) { + github.rest.issues.deleteComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: testComment.id, + }) + } + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: process.env.TEST_COMMENT + }) + + - name: Add a Fixed comment + if: github.event_name == 'pull_request_target' && env.STATUS == 0 + uses: actions/github-script@v7 + with: + script: | + const { data: comments } = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + }) + const testComment = comments.find(comment => { + return comment.user.type === 'Bot' && comment.body.includes('# ❌Test Failed') + }) + const body = `# ✅Test Passed + + Failed tests were fixed. + ` + + if (testComment) { + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: body + }) + } + + - name: Fail if test failed + if: env.STATUS == 1 + run: exit 1