Skip to content

Instantly share code, notes, and snippets.

@maitrungduc1410
Last active March 2, 2023 23:23
Show Gist options
  • Save maitrungduc1410/325df43a563b3bd84d6feb4595538560 to your computer and use it in GitHub Desktop.
Save maitrungduc1410/325df43a563b3bd84d6feb4595538560 to your computer and use it in GitHub Desktop.
Update Npm and SonarQube on Git commit (using Husky)
{
"name": "demo-project",
"version": "0.0.3",
"husky": {
"hooks": {
"pre-commit": "npm --no-git-tag-version version patch && sh updateSonarProps.sh && git add ."
}
}
}
# must be unique in a given SonarQube instance
sonar.projectKey=demo-project
# --- optional properties ---
# defaults to project key
sonar.projectName=Demo Project
# defaults to 'not provided'
sonar.projectVersion=0.0.1
#!/usr/bin/env bash
echo "Updating the SonarQube properties..."
# Get the version from package.json
PACKAGE_VERSION=$(awk -F'"' '/"version": ".+"/{ print $4; exit; }' package.json)
echo "Extracted version: ${PACKAGE_VERSION}"
# Get the project name from package.json
PACKAGE_NAME=$(awk -F'"' '/"name": ".+"/{ print $4; exit; }' package.json)
echo "Extracted project: ${PACKAGE_NAME}"
# Get the Sonar properties file
SONAR_FILE=$(find ./ -iname sonar*.properties -type f)
echo "Sonar file found: ${SONAR_FILE}"
# Update the version
REPLACE='^sonar.projectVersion=.*$'
WITH="sonar.projectVersion=${PACKAGE_VERSION}"
sed -i.bak "s#${REPLACE}#${WITH}#g" ${SONAR_FILE}
# Update the project name
REPLACE='^sonar.projectName=.*$'
WITH="sonar.projectName=${PACKAGE_NAME}"
sed -i.bak "s#${REPLACE}#${WITH}#g" ${SONAR_FILE}
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment