Skip to content

Commit

Permalink
♻️ci: comment test report
Browse files Browse the repository at this point in the history
  • Loading branch information
Marukome0743 committed Oct 23, 2024
1 parent e694854 commit 5ade738
Showing 1 changed file with 96 additions and 2 deletions.
98 changes: 96 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ concurrency:
cancel-in-progress: true

on:
pull_request:
pull_request_target:
paths:
- "app/**"
- "public/**"
Expand All @@ -25,6 +25,9 @@ on:
- "*config.*"
workflow_dispatch:

permissions:
pull-requests: write

jobs:
test:
runs-on: ubuntu-latest
Expand All @@ -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<<EOF'
cat test.log
echo EOF
} >> $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<<EOF'
echo '# ❌Test Failed'
echo "@$ACTOR, your commit $SHA failed the test."
echo '<details>'
echo '<summary><strong>Show Test Log</strong></summary>'
echo ''
echo "\`\`\`shell"
echo "$RESULT"
echo "\`\`\`"
echo ''
echo '</details>'
echo ''
echo "👀[View in Actions](https://github.com/$REPO/actions/runs/$RUN_ID)"
echo "<!-- generated-comment [Test]($WORKFLOW_REF) -->"
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.
<!-- generated-comment [Test]($WORKFLOW_REF) -->`
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

0 comments on commit 5ade738

Please sign in to comment.