mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-07-17 17:41:50 +00:00
feat(hooks): integrate Python pre-commit with Husky for monorepo (#9279)
This commit is contained in:
@@ -3,52 +3,92 @@
|
||||
/**
|
||||
* Setup Git Hooks for Prowler UI
|
||||
*
|
||||
* This script configures Git to use Husky hooks located in ui/.husky
|
||||
* It runs automatically after npm install via the postinstall script.
|
||||
* This script checks if Python pre-commit is managing git hooks.
|
||||
* If not, it runs the repository's setup script to install pre-commit.
|
||||
*/
|
||||
|
||||
const { execSync } = require('child_process');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
try {
|
||||
// Get the git root directory
|
||||
const gitRoot = execSync('git rev-parse --show-toplevel', {
|
||||
encoding: 'utf8',
|
||||
stdio: ['pipe', 'pipe', 'pipe']
|
||||
}).trim();
|
||||
/**
|
||||
* Check if Python pre-commit framework is managing git hooks
|
||||
*/
|
||||
function isPreCommitInstalled(gitRoot) {
|
||||
const hookPath = path.join(gitRoot, '.git', 'hooks', 'pre-commit');
|
||||
|
||||
// Check if we're in a git repository
|
||||
if (!gitRoot) {
|
||||
console.log('⚠️ Not in a git repository. Skipping git hooks setup.');
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Get current hooks path
|
||||
let currentHooksPath;
|
||||
try {
|
||||
currentHooksPath = execSync('git config --get core.hooksPath', {
|
||||
if (!fs.existsSync(hookPath)) return false;
|
||||
|
||||
const content = fs.readFileSync(hookPath, 'utf8');
|
||||
return content.includes('pre-commit') || content.includes('INSTALL_PYTHON');
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get git repository root directory
|
||||
*/
|
||||
function getGitRoot() {
|
||||
try {
|
||||
return execSync('git rev-parse --show-toplevel', {
|
||||
encoding: 'utf8',
|
||||
stdio: ['pipe', 'pipe', 'pipe']
|
||||
}).trim();
|
||||
} catch {
|
||||
// core.hooksPath not set yet
|
||||
currentHooksPath = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
const expectedHooksPath = 'ui/.husky';
|
||||
|
||||
// Only configure if not already set correctly
|
||||
if (currentHooksPath !== expectedHooksPath) {
|
||||
execSync(`git config core.hooksPath "${expectedHooksPath}"`, {
|
||||
stdio: 'inherit'
|
||||
});
|
||||
console.log('✅ Git hooks configured: core.hooksPath = ui/.husky');
|
||||
} else {
|
||||
console.log('✅ Git hooks already configured correctly');
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
// Don't fail the installation if git hooks setup fails
|
||||
console.warn('⚠️ Could not setup git hooks:', error.message);
|
||||
console.warn(' You may need to run manually: git config core.hooksPath "ui/.husky"');
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the repository setup script
|
||||
*/
|
||||
function runSetupScript(gitRoot) {
|
||||
const setupScript = path.join(gitRoot, 'scripts', 'setup-git-hooks.sh');
|
||||
|
||||
if (!fs.existsSync(setupScript)) {
|
||||
throw new Error('Setup script not found');
|
||||
}
|
||||
|
||||
execSync(`bash "${setupScript}"`, {
|
||||
cwd: gitRoot,
|
||||
stdio: 'inherit'
|
||||
});
|
||||
}
|
||||
|
||||
// Main execution
|
||||
|
||||
// Skip in Docker/CI environments
|
||||
if (process.env.DOCKER || process.env.CI || process.env.KUBERNETES_SERVICE_HOST) {
|
||||
console.log('⚠️ Running in containerized environment. Skipping git hooks setup.');
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const gitRoot = getGitRoot();
|
||||
|
||||
if (!gitRoot) {
|
||||
console.log('⚠️ Not in a git repository. Skipping git hooks setup.');
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (isPreCommitInstalled(gitRoot)) {
|
||||
console.log('✅ Git hooks managed by Python pre-commit framework');
|
||||
console.log(' Husky hooks will be called automatically for UI files');
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Pre-commit not installed - set it up
|
||||
console.log('⚠️ Pre-commit hooks not installed');
|
||||
console.log('📦 Installing pre-commit hooks from project dependencies...');
|
||||
console.log('');
|
||||
|
||||
try {
|
||||
runSetupScript(gitRoot);
|
||||
console.log('');
|
||||
console.log('✅ Pre-commit hooks installed successfully');
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to setup git hooks');
|
||||
console.error(' Please run manually from repo root: ./scripts/setup-git-hooks.sh');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user