Skip to content

Commit

Permalink
Merge pull request #684 from terrestris/add-coverage-comment
Browse files Browse the repository at this point in the history
ci: add coverage comment on pull request
  • Loading branch information
dnlkoch authored Feb 5, 2025
2 parents 3d6df52 + a46ecb3 commit 7ec0832
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
63 changes: 63 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ jobs:
steps:
- name: Checkout sources 🔰
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Create local branch based on 'origin/main' 🚧
run: |
git fetch origin main
git checkout -b main origin/main
git checkout -
- name: Setup Node.js 20 🧮
uses: actions/setup-node@v4
Expand Down Expand Up @@ -38,3 +46,58 @@ jobs:
run: npm run test
env:
CI: true

- name: Run tests related to current changes ✅
run: |
npx jest --config=jest.config.js --coverage --coverageReporters json-summary --changedSince=main --coverageDirectory ./coverage/changed
- name: Check for test results ❓
id: check-results
run: |
if [ -f ./coverage/changed/coverage-summary.json ]; then
COVERAGE_CONTENT=$(cat ./coverage/changed/coverage-summary.json)
if echo "$COVERAGE_CONTENT" | jq '.total.lines.pct' | grep -q 'Unknown'; then
echo "no_tests_found=true" >> $GITHUB_ENV
else
echo "no_tests_found=false" >> $GITHUB_ENV
fi
else
echo "no_tests_found=true" >> $GITHUB_ENV
fi
- name: Construct jest coverage comment input 💬
id: construct-input
run: |
if [ "${{ env.no_tests_found }}" == "true" ]; then
echo "MULTIPLE_FILES=All, ./coverage/all/coverage-summary.json" >> $GITHUB_ENV
else
echo "MULTIPLE_FILES=All, ./coverage/all/coverage-summary.json\nonly changed, ./coverage/changed/coverage-summary.json" >> $GITHUB_ENV
fi
- name: Jest Coverage Comment 💬
uses: MishaKav/jest-coverage-comment@main
with:
hide-comment: false
create-new-comment: false
hide-summary: false
multiple-files: ${{ env.MULTIPLE_FILES }}

- name: Get Coverage Comment Id ➡️
if: env.no_tests_found == 'true'
id: get-comment-id
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
COMMENTS_JSON=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${PR_NUMBER}/comments")
COVERAGE_COMMENT_ID=$(echo "${COMMENTS_JSON}" | jq -r 'map(select(.user.login == "github-actions[bot]")) | sort_by(.created_at) | .[0].id')
echo "COVERAGE_COMMENT_ID=${COVERAGE_COMMENT_ID}" >> $GITHUB_ENV
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Post To No Tests Found Comment ⚠️
if: env.no_tests_found == 'true'
uses: peter-evans/create-or-update-comment@v4
with:
comment-id: ${{ env.COVERAGE_COMMENT_ID }}
body: |
❌ No tests were found related to the files changed
6 changes: 4 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = {
'shogunApplicationConfig': '<rootDir>/assets/fallbackConfig.js'
},
transformIgnorePatterns: [
'<rootDir>/node_modules/(?!@ngrx|(?!deck.gl)|ng-dynamic)'
'<rootDir>/node_modules/(?!@ngrx|(?!deck.gl)|ng-dynamic|@monaco-editor)'
],
moduleFileExtensions: [
'ts',
Expand All @@ -33,5 +33,7 @@ module.exports = {
'json'
],
testEnvironment: 'jsdom',
reporters: ['default', '@casualbot/jest-sonar-reporter']
reporters: ['default', '@casualbot/jest-sonar-reporter'],
coverageReporters: ['json-summary', 'lcov'],
coverageDirectory: 'coverage/all'
};
2 changes: 2 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ sonar.tests=src
sonar.sources=src
sonar.test.inclusions=**/*.spec.tsx,**/*.spec.ts
sonar.projectKey=shogun-admin
sonar.javascript.lcov.reportPaths=./coverage/all/lcov.info
sonar.host.url=https://sq.terrestris.de

0 comments on commit 7ec0832

Please sign in to comment.