Refactoring the code base for better Quality #65
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: Publish Abfy Package | |
on: | |
pull_request: | |
types: [synchronize] | |
jobs: | |
Setup: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Code | |
uses: actions/checkout@v3 | |
- name: Setup Node | |
uses: actions/setup-node@v3 | |
with: | |
node-version: "18" | |
registry-url: https://registry.npmjs.org/ | |
scope: "@eyeamkd" | |
- name: Install Dependencies | |
run: npm install | |
- name: Run Tests with Coverage | |
run: npm run test:cov | |
- name: Upload Coverage Report | |
if: always() | |
uses: actions/upload-artifact@v3 | |
with: | |
name: coverage-report | |
path: coverage | |
- name: Publish | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
if: github.event_name == 'push' | |
run: | | |
cd packages/abfy | |
npm version patch | |
npm publish --access public | |
- name: Publish Test Version | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
if: github.event_name == 'pull_request' | |
run: | | |
cd packages/abfy | |
npm version prerelease --preid alpha-${{ github.sha }} | |
npm publish --access public --tag alpha | |
TestCoverage: | |
needs: Setup | |
name: Test Coverage | |
runs-on: ubuntu-latest | |
if: github.event_name == 'pull_request' | |
steps: | |
- name: Download Coverage Report | |
uses: actions/download-artifact@v3 | |
with: | |
name: coverage-report | |
path: coverage | |
- name: Convert Coverage Report to JSON | |
run: | | |
npm install -g jest-junit | |
npx jest --coverageReporters=json-summary | |
- name: Extract Coverage Summary | |
id: coverage | |
run: | | |
cat coverage/coverage-summary.json | jq '.total' > coverage-summary.json | |
echo "::set-output name=summary::$(cat coverage-summary.json)" | |
- name: Post or Update Coverage Comment | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const fs = require('fs'); | |
const coverage = JSON.parse(fs.readFileSync('coverage-summary.json', 'utf8')); | |
const commentBody = ` | |
## Test Coverage Report | |
**Lines:** ${coverage.lines.pct}% (${coverage.lines.covered}/${coverage.lines.total}) | |
**Statements:** ${coverage.statements.pct}% (${coverage.statements.covered}/${coverage.statements.total}) | |
**Functions:** ${coverage.functions.pct}% (${coverage.functions.covered}/${coverage.functions.total}) | |
**Branches:** ${coverage.branches.pct}% (${coverage.branches.covered}/${coverage.branches.total}) | |
`; | |
const { data: comments } = await github.rest.issues.listComments({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo | |
}); | |
const existingComment = comments.find(comment => | |
comment.user.login === 'github-actions[bot]' && | |
comment.body.startsWith('## Test Coverage Report') | |
); | |
if (existingComment) { | |
await github.rest.issues.updateComment({ | |
comment_id: existingComment.id, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: commentBody | |
}); | |
} else { | |
await github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: commentBody | |
}); | |
} |