Skip to content

Commit

Permalink
add githooks
Browse files Browse the repository at this point in the history
  • Loading branch information
kairu-ms committed Nov 11, 2024
1 parent 468a011 commit 0a2f946
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .githooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Git Hooks for Azure CLI Extension Development

## Setup

Please run the following command to enable the hooks.

```bash
azdev setup -c {azure_cli_repo_path} -r {azure_cli_extension_repo_path}

# if you install azdev which version is less than 0.1.84, you need to run the following command to enable the hooks
git config --local core.hooksPath .githooks
```

## Usage

Every time you git commit or git push, please make sure you have activated the python environment and completed the azdev setup.

If you want to skip the verification, you can add `--no-verify` to the git command.
39 changes: 39 additions & 0 deletions .githooks/azdev_active.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Check if in the python environment
$pythonPath = (Get-Command python -ErrorAction SilentlyContinue).Path
Write-Host "PYTHON_PATH: $pythonPath"

if (-not $pythonPath) {
Write-Host "Error: Python not found in PATH" -ForegroundColor Red
exit 1
}

$pythonEnvFolder = Split-Path -Parent (Split-Path -Parent $pythonPath)
$pythonActiveFile = Join-Path $pythonEnvFolder "Scripts\activate.ps1"

if (-not (Test-Path $pythonActiveFile)) {
Write-Host "Python active file does not exist: $pythonActiveFile" -ForegroundColor Red
Write-Host "Error: Please activate the python environment first." -ForegroundColor Red
exit 1
}

# Construct the full path to the .azdev\env_config directory
$azdevEnvConfigFolder = Join-Path $env:USERPROFILE ".azdev\env_config"
Write-Host "AZDEV_ENV_CONFIG_FOLDER: $azdevEnvConfigFolder"

# Check if the directory exists
if (-not (Test-Path $azdevEnvConfigFolder)) {
Write-Host "AZDEV_ENV_CONFIG_FOLDER does not exist: $azdevEnvConfigFolder" -ForegroundColor Red
Write-Host "Error: azdev environment is not completed, please run 'azdev setup' first." -ForegroundColor Red
exit 1
}

$configFile = Join-Path $azdevEnvConfigFolder ($pythonEnvFolder.Substring(2) + "\config")
if (-not (Test-Path $configFile)) {
Write-Host "CONFIG_FILE does not exist: $configFile" -ForegroundColor Red
Write-Host "Error: azdev environment is not completed, please run 'azdev setup' first." -ForegroundColor Red
exit 1
}

Write-Host "CONFIG_FILE: $configFile"

exit 0
41 changes: 41 additions & 0 deletions .githooks/azdev_active.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

# Check if in the python environment
PYTHON_FILE=$(which python)
echo "PYTHON_PATH: $PYTHON_FILE"

if [ -z "$PYTHON_FILE" ]; then
echo "\033[0;31mError: Python not found in PATH\033[0m"
exit 1
fi

PYTHON_ENV_FOLDER=$(dirname "$PYTHON_FILE")
PYTHON_ACTIVE_FILE="$PYTHON_ENV_FOLDER/activate"

if [ ! -f "$PYTHON_ACTIVE_FILE" ]; then
echo "Python active file does not exist: $PYTHON_ACTIVE_FILE"
echo "\033[0;31mError: Please activate the python environment first.\033[0m"
exit 1
fi

# Construct the full path to the .azdev/env_config directory
AZDEV_ENV_CONFIG_FOLDER="$HOME/.azdev/env_config"
echo "AZDEV_ENV_CONFIG_FOLDER: $AZDEV_ENV_CONFIG_FOLDER"

# Check if the directory exists
if [ ! -d "$AZDEV_ENV_CONFIG_FOLDER" ]; then
echo "AZDEV_ENV_CONFIG_FOLDER does not exist: $AZDEV_ENV_CONFIG_FOLDER"
echo "\033[0;31mError: azdev environment is not completed, please run 'azdev setup' first.\033[0m"
exit 1
fi

PYTHON_ENV_FOLDER=$(dirname "$PYTHON_ENV_FOLDER")

CONFIG_FILE="$AZDEV_ENV_CONFIG_FOLDER${PYTHON_ENV_FOLDER}/config"
if [ ! -f "$CONFIG_FILE" ]; then
echo "CONFIG_FILE does not exist: $CONFIG_FILE"
echo "\033[0;31mError: azdev environment is not completed, please run 'azdev setup' first.\033[0m"
exit 1
fi

echo "CONFIG_FILE: $CONFIG_FILE"
13 changes: 13 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env sh
":" //; if command -v pwsh >/dev/null 2>&1; then pwsh -ExecutionPolicy Bypass -File .githooks/pre-commit.ps1; else sh .githooks/pre-commit.sh; fi; exit $? # Try PowerShell Core first, then sh on Unix
":" //; exit # Skip rest on Unix

@echo off
powershell -NoProfile -Command "if (Get-Command powershell -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }"
if %errorlevel% equ 0 (
powershell -ExecutionPolicy Bypass -File .githooks\pre-commit.ps1
) else (
echo Error: PowerShell is not available. Please install PowerShell.
exit /b 1
)
exit /b %errorlevel%
43 changes: 43 additions & 0 deletions .githooks/pre-commit.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env pwsh
Write-Host "Running pre-commit hook in powershell..." -ForegroundColor Green

# run azdev_active script
$scriptPath = Join-Path $PSScriptRoot "azdev_active.ps1"
. $scriptPath
if ($LASTEXITCODE -ne 0) {
exit 1
}

# Run command azdev scan
Write-Host "Running azdev scan..." -ForegroundColor Green

# Check if we have a previous commit to compare against
if (git rev-parse --verify HEAD 2>$null) {
Write-Host "Using HEAD as the previous commit"
$against = "HEAD"
}
else {
# Initial commit: diff against an empty tree object
Write-Host "Using empty tree object as the previous commit"
$against = $(git hash-object -t tree /dev/null)
}

$hasSecrets = 0
$files = $(git diff --cached --name-only --diff-filter=AM $against)

foreach ($file in $files) {
# Check if the file contains secrets
$detected = $(azdev scan -f $file | ConvertFrom-Json).secrets_detected
if ($detected -eq "True") {
Write-Host "Detected secrets from $file. You can run 'azdev mask' to remove secrets before commit." -ForegroundColor Red
$hasSecrets = 1
}
}

if ($hasSecrets -eq 1) {
Write-Host "Secret detected. If you want to skip that, run add '--no-verify' in the end of 'git commit' command." -ForegroundColor Red
exit 1
}

Write-Host "Pre-commit hook passed." -ForegroundColor Green
exit 0
38 changes: 38 additions & 0 deletions .githooks/pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
echo "\033[0;32mRunning pre-commit hook in bash ...\033[0m"

# run azdev_active script
SCRIPT_PATH="$(dirname "$0")/azdev_active.sh"
. "$SCRIPT_PATH"
if [ $? -ne 0 ]; then
exit 1
fi

# Run command azdev scan
echo "\033[0;32mRunning azdev scan...\033[0m"

if git rev-parse --verify HEAD >/dev/null 2>&1
then
echo "Using HEAD as the previous commit"
against=HEAD
else
echo "Using empty tree object as the previous commit"
against=$(git hash-object -t tree /dev/null)
fi
has_secrets=0
for FILE in `git diff --cached --name-only --diff-filter=AM $against` ; do
# Check if the file contains secrets
detected=$(azdev scan -f "$FILE" | python -c "import sys, json; print(json.load(sys.stdin)['secrets_detected'])")
if [ "$detected" = "True" ]; then
echo "\033[0;31mDetected secrets from $FILE, You can run 'azdev mask' to remove secrets before commit.\033[0m"
has_secrets=1
fi
done

if [ $has_secrets -eq 1 ]; then
echo "\033[0;31mSecret detected. If you want to skip that, run add '--no-verify' in the end of 'git commit' command.\033[0m"
exit 1
fi

echo "\033[0;32mPre-commit hook passed.\033[0m"
exit 0
13 changes: 13 additions & 0 deletions .githooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env sh
":" //; if command -v pwsh >/dev/null 2>&1; then pwsh -ExecutionPolicy Bypass -File .githooks/pre-push.ps1; else sh .githooks/pre-push.sh; fi; exit $? # Try PowerShell Core first, then sh on Unix
":" //; exit # Skip rest on Unix

@echo off
powershell -NoProfile -Command "if (Get-Command powershell -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }"
if %errorlevel% equ 0 (
powershell -ExecutionPolicy Bypass -File .githooks\pre-push.ps1
) else (
echo Error: PowerShell is not available. Please install PowerShell.
exit /b 1
)
exit /b %errorlevel%
51 changes: 51 additions & 0 deletions .githooks/pre-push.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Write-Host "Running pre-push hook in powershell..." -ForegroundColor Green

# run azdev_active script
$scriptPath = Join-Path $PSScriptRoot "azdev_active.ps1"
. $scriptPath
if ($LASTEXITCODE -ne 0) {
exit 1
}

# Fetch upstream/dev branch
Write-Host "Fetching upstream/dev branch..." -ForegroundColor Green
git fetch upstream dev
if ($LASTEXITCODE -ne 0) {
Write-Host "Error: Failed to fetch upstream/dev branch. Please run 'git remote add upstream https://github.com/Azure/azure-cli.git' first." -ForegroundColor Red
exit 1
}

# get the current branch name
$currentBranch = git branch --show-current

# Run command azdev lint
Write-Host "Running azdev lint..." -ForegroundColor Green
azdev linter --repo ./ --tgt $currentBranch --src upstream/dev
if ($LASTEXITCODE -ne 0) {
Write-Host "Error: azdev lint check failed." -ForegroundColor Red
exit 1
}

# Run command azdev style
Write-Host "Running azdev style..." -ForegroundColor Green
azdev style --repo ./ --tgt $currentBranch --src upstream/dev
if ($LASTEXITCODE -ne 0) {
$error_msg = azdev style --repo ./ --tgt $currentBranch --src upstream/dev 2>&1
if ($error_msg -like "*No modules*") {
Write-Host "Pre-push hook passed." -ForegroundColor Green
exit 0
}
Write-Host "Error: azdev style check failed." -ForegroundColor Red
exit 1
}

# Run command azdev test
Write-Host "Running azdev test..." -ForegroundColor Green
azdev test --repo ./ --tgt $currentBranch --src upstream/dev
if ($LASTEXITCODE -ne 0) {
Write-Host "Error: azdev test check failed." -ForegroundColor Red
exit 1
}

Write-Host "Pre-push hook passed." -ForegroundColor Green
exit 0
54 changes: 54 additions & 0 deletions .githooks/pre-push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

echo "\033[0;32mRunning pre-push hook in bash ...\033[0m"

# run azdev_active script
SCRIPT_PATH="$(dirname "$0")/azdev_active.sh"
. "$SCRIPT_PATH"
if [ $? -ne 0 ]; then
exit 1
fi

# Fetch upstream/dev branch
echo "\033[0;32mFetching upstream/dev branch...\033[0m"
git fetch upstream dev
if [ $? -ne 0 ]; then
echo "\033[0;31mError: Failed to fetch upstream/dev branch. Please run 'git remote add upstream https://github.com/Azure/azure-cli.git' first.\033[0m"
exit 1
fi

# get the current branch name
currentBranch=$(git branch --show-current)

# Run command azdev lint
echo "\033[0;32mRunning azdev lint...\033[0m"
azdev linter --repo ./ --tgt $currentBranch --src upstream/dev
if [ $? -ne 0 ]; then
echo "\033[0;31mError: azdev lint check failed.\033[0m"
exit 1
fi

# Run command azdev style
echo "\033[0;32mRunning azdev style...\033[0m"
azdev style --repo ./ --tgt $currentBranch --src upstream/dev
if [ $? -ne 0 ]; then
error_msg=$(azdev style --repo ./ --tgt $currentBranch --src upstream/dev 2>&1)
if echo "$error_msg" | grep -q "No modules"; then
echo "\033[0;32mPre-push hook passed.\033[0m"
exit 0
fi
echo "\033[0;31mError: azdev style check failed.\033[0m"
exit 1
fi

# Run command azdev test
echo "\033[0;32mRunning azdev test...\033[0m"
azdev test --repo ./ --tgt $currentBranch --src upstream/dev
if [ $? -ne 0 ]; then
echo "\033[0;31mError: azdev test check failed.\033[0m"
exit 1
fi

echo "\033[0;32mPre-push hook passed.\033[0m"
exit 0

0 comments on commit 0a2f946

Please sign in to comment.