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

Feat: Sonar setup added to measure UT coverage #13

Closed
wants to merge 10 commits into from
Closed
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
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib
node_modules
18 changes: 18 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"rules": {}
}
72 changes: 72 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Coverage
on:
pull_request:
types:
- closed
push:
branches:
- master

env:
HOME: /actions-runner/_work
GITHUB_ACCESS_TOKEN: ${{ secrets.CI_BOT_TOKEN }}
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
REPORT_PORTAL_TOKEN: ${{ secrets.REPORT_PORTAL_TOKEN }}
REPORT_PORTAL_HOST: ${{ secrets.REPORT_PORTAL_HOST }}
REPORT_PORTAL_PROJECT: ${{ secrets.REPORT_PORTAL_PROJECT }}
COMMIT_ID: ${{ github.sha }}
NODE_VERSION: 20.3.1

# to cancel prev workflow if it is running
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
sonar:
name: Sonar Scanner
runs-on: ubuntu-latest
continue-on-error: true
if: ${{ github.ref == 'refs/heads/master' && github.event.head_commit.author.name != 'rzpcibot'}}
steps:
- name: Checkout Codebase
uses: actions/checkout@v3
with:
token: ${{ secrets.CI_BOT_TOKEN }}

- name: Get Pull Request Number
id: pr
run: |
commit_sha=${{ github.sha }}
repo_name=${{ github.repository }}
pr_number=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/$repo_name/pulls?head=$commit_sha" | \
jq -r '.[0].number')
echo "::set-output name=pr_number::$pr_number"

- name: Set PR Number
run: |
echo "PULL_REQUEST_NUMBER=${{ steps.pr.outputs.pr_number }}" >> $GITHUB_ENV

- name: Setup Node v20
uses: actions/setup-node@v3
with:
node-version: 20.3.1

- name: Setup Cache & Install Dependencies
uses: bahmutov/[email protected]
with:
install-command: yarn --frozen-lockfile

- name: Run Tests
run: yarn test

- name: SonarQube Scan
uses: sonarsource/[email protected]
env:
SONAR_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONARQUBE_HOST }}
with:
args: >
-Dsonar.host.url=${{ secrets.SONARQUBE_HOST }}
-Dsonar.login=${{ secrets.SONARQUBE_TOKEN }}
-Dsonar.projectVersion=${{ github.sha }}
73 changes: 73 additions & 0 deletions .github/workflows/sonar-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Sonar Code Coverage PR Analysis

on:
pull_request:
branches:
- master

env:
HOME: /actions-runner/_work
GITHUB_ACCESS_TOKEN: ${{ secrets.CI_BOT_TOKEN }}
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
REPORT_PORTAL_TOKEN: ${{ secrets.REPORT_PORTAL_TOKEN }}
REPORT_PORTAL_HOST: ${{ secrets.REPORT_PORTAL_HOST }}
REPORT_PORTAL_PROJECT: ${{ secrets.REPORT_PORTAL_PROJECT }}
COMMIT_ID: ${{ github.event.pull_request.head.sha }}
PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number}}
NODE_VERSION: 20.3.1
jobs:
coverage:
name: Sonar analysis on coverage
runs-on: ubuntu-latest
if: |
!(github.head_ref == 'changeset-release/master' && github.actor == 'rzpcibot') &&
!contains(github.event.head_commit.message, '[skip ci]')
steps:
- name: Checkout Codebase
uses: actions/checkout@v3
with:
token: ${{ secrets.CI_BOT_TOKEN }}
- name: Setup Node v20
uses: actions/setup-node@v3
with:
node-version: 20.3.1
- name: Setup Cache & Install Dependencies
uses: bahmutov/[email protected]
with:
install-command: yarn --frozen-lockfile
- name: Run Tests
run: yarn test
- name: Run SonarQube PR analysis on Dev Project
id: pr_sonarqube_analysis
uses: sonarsource/[email protected]
if: ${{ github.ref != 'refs/heads/master' }}
env:
SONAR_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
SONARCLOUD_URL: ${{ secrets.SONARQUBE_HOST }}
with:
args: >
-Dsonar.projectKey=I18NIFY_DEV
-Dsonar.projectName=I18NIFY_DEV
-Dsonar.host.url=${{ secrets.SONARQUBE_HOST }}
-Dsonar.login=${{ secrets.SONARQUBE_TOKEN }}
-Dsonar.projectVersion=${{ github.sha }}

# Perform code coverage check on PR changes vs master
- name: PR code coverage check
id: pr-code-coverage-check
if: ${{ github.ref != 'refs/heads/master' }}
run: cd .. && scripts/sonar-analysis.sh
continue-on-error: false
env:
SONAR_HOST: ${{ secrets.SONARQUBE_HOST }}
SONAR_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
PROJECT_KEY_DEV: I18NIFY_DEV
PROJECT_KEY_PROD: I18NIFY
CODE_COVERAGE_THRESHOLD: 0
COMMENT_FILE: comment.txt

- name: Update Sonar Analysis Comment
if: ${{ github.ref != 'refs/heads/master' }}
uses: marocchino/sticky-pull-request-comment@v2
with:
path: comment.txt
41 changes: 41 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Validate
on:
pull_request:
jobs:
lint:
name: Run eslint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v2
with:
node-version: 20
- name: Install dependencies
run: yarn
- name: run eslint
run: yarn lint
tsc:
name: Run tsc
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v2
with:
node-version: 20
- name: Install dependencies
run: yarn
- name: run tsc
run: yarn tsc
build:
name: Run Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v2
with:
node-version: 20
- name: Install dependencies
run: yarn
- name: run build
run: yarn build

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
dist
lib
.DS_Store
.DS_Store
coverage
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn lint-staged
3 changes: 3 additions & 0 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"**/*.{js,ts,mjs}": ["yarn format", "yarn validate"]
}
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib
node_modules
7 changes: 7 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"tabWidth": 2,
"bracketSpacing": true
}
4 changes: 0 additions & 4 deletions jest.config.js

This file was deleted.

17 changes: 17 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const config = {
collectCoverage: true,
preset: 'ts-jest',
testEnvironment: 'jsdom',
collectCoverageFrom: ['**/*.{ts,js}', '!coverage/**/*.{ts,tsx,js,jsx}'],
coverageThreshold: {
'./src/': {
statements: 0,
branches: 0,
functions: 0,
lines: 0,
},
},
rootDir: 'src',
};

export default config;
21 changes: 19 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@
}
},
"scripts": {
"lint": "eslint src",
"lint:fix": "eslint src --fix",
"tsc": "tsc --noEmit",
"validate": "yarn tsc && yarn lint:fix",
"format": "prettier src --write",
"format:check": "prettier src --check",
"clean": "rm -rf lib",
"build": "npm run clean && rollup -c",
"test": "jest"
"build": "yarn clean && rollup -c",
"test": "jest",
"prepare": "husky install",
"lint-staged": "lint-staged -c .lintstagedrc.json"
},
"devDependencies": {
"@rollup/plugin-babel": "^6.0.4",
Expand All @@ -34,13 +42,22 @@
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^11.1.5",
"@types/jest": "^29.5.7",
"@typescript-eslint/eslint-plugin": "^6.9.1",
"@typescript-eslint/parser": "^6.9.1",
"cross-env": "^7.0.3",
"esbuild": "^0.19.4",
"eslint": "^8.52.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.1",
"husky": "^8.0.0",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"lint-staged": "^15.0.2",
"prettier": "^3.0.3",
"rollup": "^4.0.2",
"rollup-plugin-dts": "^6.1.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"tslib": "^2.6.2",
"typescript": "^5.2.2"
}
Expand Down
Loading
Loading