Skip to content

Commit

Permalink
Update workflow to run coverage for all projects
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed Oct 12, 2023
1 parent 5aa1d8a commit df21817
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ on:

jobs:
build:
defaults:
run:
working-directory: ./packages/massa-web3/examples/smartContracts/
strategy:
matrix:
project: [massa-web3, web3-utils]

runs-on: ubuntu-latest

Expand Down Expand Up @@ -44,28 +44,22 @@ jobs:
npm run build
working-directory: ./

- name: Execute unit tests
run: |
npm ci
npm run build
npm run fmt:check
npm run test
- name: allow access to coverage.sh
run: chmod +x ./scripts/coverage.sh
shell: bash

- name: Extract coverage
id: coverage
run: |
value=$(npm run test:cov | awk '/All files/ {print $10}' | tr -d '%')
value=$(npm run test:cov -w ${{ matrix.project }} | awk '/All files/ {print $10}' | tr -d '%')
echo "coverage=$value" >> $GITHUB_OUTPUT
- name: Add test coverage to README
run: ./scripts/coverage.sh
shell: bash
env:
COVERAGE: ${{ steps.coverage.outputs.coverage }}
PROJECT: ${{ matrix.project }}

- name: Check if coverage changed
id: check_coverage_changed
Expand Down
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Massa Web3 Workspace

Welcome to the Massa Web3 Workspace. This space houses a collection of projects designed to bolster interaction with the Massa blockchain through TypeScript. Below is an overview of each project with links to their respective READMEs for more in-depth information.

## Projects within the Workspace

### 1. **Massa-web3**

![check-code-coverage](https://img.shields.io/badge/coverage-95.25%25-green)

**Description:**
Massa-web3 is a TypeScript library that facilitates communication with the Massa blockchain. It's your key to extracting data, interfacing with smart contracts, monitoring blockchain events, and much more.

- [📖 Read the full `Massa-web3` README](https://github.com/massalabs/massa-web3#readme) for detailed installation instructions, prerequisites, and additional resources.

### 2. **Web3-Utils**

![check-code-coverage](https://img.shields.io/badge/coverage-40%25-red)

**Description:**
Web3-Utils is a toolkit crafted for the Massa TypeScript projects. Although it currently lacks a dedicated README, this utility provides an array of essential tools to streamline and enhance your blockchain interactions.

- README for `Web3-Utils` will be available soon.

## Contributing

Community contributions are the backbone of our projects. If you wish to be a part of our growth and improve our tools, we'd love to have you! Please consult the [CONTRIBUTING guidelines](CONTRIBUTING.md) of the respective project.

## License

All projects within the Massa Web3 Workspace are released under the [MIT License](LICENSE).

## Powered By

These tools are developed with ❤️ by MassaLabs. Check out the [Powered By](powered-by.md) section to see the open-source projects that make our tools possible.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "index.js",
"scripts": {
"test": "npm run test --ws --if-present",
"test:cov": "npm run test:cov --ws --if-present",
"build": "npm run build --ws --if-present",
"prettier": "npm run prettier --ws --if-present",
"prettier:fix": "npm run prettier:fix --ws --if-present",
Expand Down
2 changes: 1 addition & 1 deletion packages/massa-web3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"test": "jest ----detectOpenHandles --forceExit",
"test:watch": "jest --watch",
"test:watch:all": "jest --watchAll",
"test:cov": "jest --coverage",
"test:cov": "jest --coverage --silent",
"test-smart-contract-example": "ts-node ./examples/smartContracts/index.ts",
"test-wallet": "ts-node ./examples/wallet/index.ts",
"lint": "eslint .",
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build-commonjs": "tsc --project tsconfig.commonjs.json",
"build": "npm-run-all clean-dist build-*",
"test": "jest --passWithNoTests",
"test:cov": "jest --coverage --passWithNoTests",
"test:cov": "jest --coverage --silent --passWithNoTests",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"prettier": "prettier --check .",
Expand Down
69 changes: 56 additions & 13 deletions scripts/coverage.sh
Original file line number Diff line number Diff line change
@@ -1,28 +1,71 @@
#!/bin/bash

color="red"
# Define the color based on coverage threshold
determine_color() {
local coverage=$1

if [ "$(echo "$COVERAGE >= 80" | bc -l)" -eq 1 ]; then
color="green"
elif [ "$(echo "$COVERAGE >= 70" | bc -l)" -eq 1 ]; then
color="orange"
fi
if (( $(echo "$coverage >= 80" | bc -l) )); then
echo "green"
elif (( $(echo "$coverage >= 70" | bc -l) )); then
echo "orange"
else
echo "red"
fi
}

filename="README.md"

coverageLine=$(sed -n '3p' $filename)
# Indexed arrays for projects and their corresponding line numbers
projects=("massa-web3" "web3-utils")
lines=(9 18)

# Get line number for the given project
get_line_number() {
local project=$1
local i=0
for p in "${projects[@]}"; do
if [ "$p" == "$project" ]; then
echo "${lines[$i]}"
return
fi
((i++))
done
echo ""
}

regex="coverage-([0-9]+([.][0-9]+)?)%"
line_number=$(get_line_number $PROJECT)

# If no line number found, exit the script
if [ -z "$line_number" ]; then
echo "Project '$PROJECT' not found in the map."
exit 1
fi

# If no line number found, exit the script
if [ -z "$line_number" ]; then
echo "Project '$PROJECT' not found in the map."
exit 1
fi

# Extract the current coverage badge from the specified line
coverageLine=$(sed -n "${line_number}p" $filename)

# Regex to match the coverage percentage
regex="${PROJECT}:\s*([0-9]+([.][0-9]+)?)%"

if [[ $coverageLine =~ $regex ]]; then
oldCoverage="${BASH_REMATCH[1]}"
echo "Coverage is $COVERAGE%"
echo "Coverage for $PROJECT is $COVERAGE%"
else
echo "No coverage found"
echo "No coverage found for $PROJECT."
fi

if [ -z "$oldCoverage" ] || [ "$(echo "$COVERAGE - $oldCoverage >= 1" | bc -l)" -eq 1 ] || [ "$(echo "$oldCoverage - $COVERAGE >= 1" | bc -l)" -eq 1 ]; then
echo "Updating badge"
# Determine the color for the badge based on the coverage
color=$(determine_color $COVERAGE)

# Update the coverage badge if the difference in coverage is greater than or equal to 1%
if [ -z "$oldCoverage" ] || (( $(echo "$COVERAGE - $oldCoverage >= 1" | bc -l) )) || (( $(echo "$oldCoverage - $COVERAGE >= 1" | bc -l) )); then
echo "Updating badge for $PROJECT."
newLine="![check-code-coverage](https://img.shields.io/badge/coverage-$COVERAGE%25-$color)"
sed -i "3s#.*#${newLine}#" $filename
sed -i "" "${line_number}s#.*#${newLine}#" $filename
fi

0 comments on commit df21817

Please sign in to comment.