Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: add coverage comment on pull request #684

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -24,7 +24,7 @@ export default {
'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 @@ -34,5 +34,7 @@ export default {
'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