Add git hooks support (#8251)
* test * Setup pre-push hooks * POC for githooks in windows * modify pre-push for unix * fix hooks for linux * Add green color in echo * test * test * test * test * Using diff to check * remove change in elastic-san * skip hooks when no modules * Add pre-commit hooks * Add README.md file * Add success messages * fix error message * fix error message * fix error message * fix error message * Update readme.md * Update README.md * Add echo for azdev scan * Add echo when no module * Update .githooks/azdev_active.sh Co-authored-by: ZelinWang <zelinwang@microsoft.com> * Update .gitattributes Co-authored-by: ZelinWang <zelinwang@microsoft.com> * Update .githooks/README.md Co-authored-by: ZelinWang <zelinwang@microsoft.com> * Update .githooks/azdev_active.ps1 Co-authored-by: ZelinWang <zelinwang@microsoft.com> * Fix srt and tgt * test * test * add echo in pre-commit for head * Add code to check azure-cli and azure-cli-extension repo codes * add extension repo verification * Auto detect and add extensions * fix issue in pre-push.sh * fix issue in pre-push.sh * test * export tests in test_result.xml file * ignore the output of test error stream * remove test line * remove the test_result.xml file if test success * Add docs for .githooks * test * test * use medium min-servity for azdev linter. --------- Co-authored-by: ZelinWang <zelinwang@microsoft.com>
This commit is contained in:
Родитель
8660356607
Коммит
fd0946c1ee
|
@ -1 +1,10 @@
|
|||
# Set default behavior to automatically normalize line endings
|
||||
* text=auto
|
||||
|
||||
# Force bash scripts to use LF
|
||||
*.sh text eol=lf
|
||||
|
||||
# Force batch scripts to use CRLF
|
||||
*.bat text eol=crlf
|
||||
|
||||
src/vmware/azext_vmware/tests/latest/recordings/*.yaml linguist-generated=true
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
# 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.
|
||||
This option will bypass any pre-commit or pre-push hooks that are configured in the repository, allowing you to commit or push changes without running these checks.
|
||||
For example:
|
||||
Skipping verification during commit
|
||||
git commit -m "your commit message" --no-verify
|
||||
Skipping verification during push
|
||||
git push origin main --no-verify
|
||||
|
||||
## Note
|
||||
|
||||
### pre-commit
|
||||
|
||||
The pre-commit hook (`pre-commit.ps1`) performs the following checks:
|
||||
|
||||
1. Verifies that azdev is active in your current environment
|
||||
2. Runs `azdev scan` on all staged files to detect potential secrets
|
||||
3. If any secrets are detected, the commit will be blocked
|
||||
- You can use `azdev mask` to remove secrets before committing
|
||||
- Alternatively, use `git commit --no-verify` to bypass the check
|
||||
|
||||
### pre-push
|
||||
|
||||
The pre-push hooks (`pre-push.sh` for bash and `pre-push.ps1` for PowerShell) perform several quality checks:
|
||||
|
||||
1. Verifies that azdev is active in your current environment
|
||||
2. Detects which repository you're working in:
|
||||
- For azure-cli (when installed in editable mode):
|
||||
* Checks if your branch needs rebasing against upstream/dev
|
||||
* If rebasing is needed, displays instructions and provides a 5-second window to cancel
|
||||
- For extensions:
|
||||
* Automatically detects modified extensions
|
||||
* Adds them to the test scope using `azdev extension add`
|
||||
3. Runs the following quality checks on changed files:
|
||||
- `azdev lint`: Checks for linting issues
|
||||
- `azdev style`: Verifies code style compliance
|
||||
- `azdev test`: Runs tests for modified code/extensions
|
||||
4. If any check fails, the push will be blocked
|
||||
- Use `git push --no-verify` to bypass these checks (not recommended)
|
||||
|
||||
The hooks support both Windows (PowerShell) and Unix-like systems (Bash), automatically using the appropriate script for your environment.
|
|
@ -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
|
|
@ -0,0 +1,41 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Check if in the python environment
|
||||
PYTHON_FILE=$(which python)
|
||||
printf "PYTHON_PATH: %s\n" "$PYTHON_FILE"
|
||||
|
||||
if [ -z "$PYTHON_FILE" ]; then
|
||||
printf "\033[0;31mError: Python not found in PATH\033[0m\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
PYTHON_ENV_FOLDER=$(dirname "$PYTHON_FILE")
|
||||
PYTHON_ACTIVE_FILE="$PYTHON_ENV_FOLDER/activate"
|
||||
|
||||
if [ ! -f "$PYTHON_ACTIVE_FILE" ]; then
|
||||
printf "Python active file does not exist: %s\n" "$PYTHON_ACTIVE_FILE"
|
||||
printf "\033[0;31mError: Please activate the python environment first.\033[0m\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Construct the full path to the .azdev/env_config directory
|
||||
AZDEV_ENV_CONFIG_FOLDER="$HOME/.azdev/env_config"
|
||||
printf "AZDEV_ENV_CONFIG_FOLDER: %s\n" "$AZDEV_ENV_CONFIG_FOLDER"
|
||||
|
||||
# Check if the directory exists
|
||||
if [ ! -d "$AZDEV_ENV_CONFIG_FOLDER" ]; then
|
||||
printf "AZDEV_ENV_CONFIG_FOLDER does not exist: %s\n" "$AZDEV_ENV_CONFIG_FOLDER"
|
||||
printf "\033[0;31mError: azdev environment is not completed, please run 'azdev setup' first.\033[0m\n"
|
||||
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
|
||||
printf "CONFIG_FILE does not exist: %s\n" "$CONFIG_FILE"
|
||||
printf "\033[0;31mError: azdev environment is not completed, please run 'azdev setup' first.\033[0m\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
printf "CONFIG_FILE: %s\n" "$CONFIG_FILE"
|
|
@ -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%
|
|
@ -0,0 +1,42 @@
|
|||
#!/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 {
|
||||
Write-Host "Using an 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
|
|
@ -0,0 +1,38 @@
|
|||
#!/bin/bash
|
||||
printf "\033[0;32mRunning pre-commit hook in bash ...\033[0m\n"
|
||||
|
||||
# run azdev_active script
|
||||
SCRIPT_PATH="$(dirname "$0")/azdev_active.sh"
|
||||
. "$SCRIPT_PATH"
|
||||
if [ $? -ne 0 ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Run command azdev scan
|
||||
printf "\033[0;32mRunning azdev scan...\033[0m\n"
|
||||
|
||||
if git rev-parse --verify HEAD >/dev/null 2>&1
|
||||
then
|
||||
printf "Using HEAD as the previous commit\n"
|
||||
against=HEAD
|
||||
else
|
||||
printf "Using an empty tree object as the previous commit\n"
|
||||
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
|
||||
printf "\033[0;31mDetected secrets from %s, You can run 'azdev mask' to remove secrets before commit.\033[0m\n" "$FILE"
|
||||
has_secrets=1
|
||||
fi
|
||||
done
|
||||
|
||||
if [ $has_secrets -eq 1 ]; then
|
||||
printf "\033[0;31mSecret detected. If you want to skip that, run add '--no-verify' in the end of 'git commit' command.\033[0m\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
printf "\033[0;32mPre-commit hook passed.\033[0m\n"
|
||||
exit 0
|
|
@ -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%
|
|
@ -0,0 +1,160 @@
|
|||
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
|
||||
}
|
||||
|
||||
# Check if azure-cli is installed in editable mode
|
||||
$pipShowOutput = pip show azure-cli 2>&1
|
||||
$editableLocation = if ($pipShowOutput) {
|
||||
$match = $pipShowOutput | Select-String "Editable project location: (.+)"
|
||||
if ($match) {
|
||||
$match.Matches.Groups[1].Value
|
||||
}
|
||||
}
|
||||
if ($editableLocation) {
|
||||
# get the parent of parent directory of the editable location
|
||||
$AZURE_CLI_FOLDER = Split-Path -Parent (Split-Path -Parent $editableLocation)
|
||||
}
|
||||
|
||||
$ExtensionRepo = Split-Path -Parent $PSScriptRoot
|
||||
|
||||
# verify if the $ExtensionRepo is in the output of azdev extension repo list
|
||||
$Extensions = (azdev extension repo list -o tsv) -join ' '
|
||||
if ($Extensions -notlike "*$ExtensionRepo*") {
|
||||
Write-Host "The current repo is not added as an extension repo. Please run the following command to add it:" -ForegroundColor Red
|
||||
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Red
|
||||
Write-Host "azdev extension repo add $ExtensionRepo" -ForegroundColor Red
|
||||
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Fetch upstream/main branch
|
||||
Write-Host "Fetching upstream/main branch..." -ForegroundColor Green
|
||||
git fetch upstream main
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Host "Error: Failed to fetch upstream/main branch. Please run the following command to add the upstream remote:" -ForegroundColor Red
|
||||
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Red
|
||||
Write-Host "git remote add upstream https://github.com/Azure/azure-cli-extensions.git" -ForegroundColor Red
|
||||
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
if ($AZURE_CLI_FOLDER) {
|
||||
# run git fetch upstream/dev for the AZURE_CLI_FOLDER and check if it is successful
|
||||
Write-Host "Fetching $AZURE_CLI_FOLDER upstream/dev branch..." -ForegroundColor Green
|
||||
git -C $AZURE_CLI_FOLDER fetch upstream dev
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Host "Error: Failed to fetch $AZURE_CLI_FOLDER upstream/dev branch. Please run the following command to add the upstream remote:" -ForegroundColor Red
|
||||
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Red
|
||||
Write-Host "git -C $AZURE_CLI_FOLDER remote add upstream https://github.com/Azure/azure-cli.git" -ForegroundColor Red
|
||||
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if current branch needs rebasing
|
||||
$cliMergeBase = git -C $AZURE_CLI_FOLDER merge-base HEAD upstream/dev
|
||||
$cliUpstreamHead = git -C $AZURE_CLI_FOLDER rev-parse upstream/dev
|
||||
if ($cliMergeBase -ne $cliUpstreamHead) {
|
||||
Write-Host ""
|
||||
Write-Host "Your $AZURE_CLI_FOLDER repo code is not up to date with upstream/dev. Please run the following commands to rebase and setup:" -ForegroundColor Yellow
|
||||
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Yellow
|
||||
Write-Host "git -C $AZURE_CLI_FOLDER rebase upstream/dev" -ForegroundColor Yellow
|
||||
if ($Extensions) {
|
||||
Write-Host "azdev setup -c $AZURE_CLI_FOLDER -r $Extensions" -ForegroundColor Yellow
|
||||
} else {
|
||||
Write-Host "azdev setup -c $AZURE_CLI_FOLDER" -ForegroundColor Yellow
|
||||
}
|
||||
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Yellow
|
||||
Write-Host ""
|
||||
Write-Host "You have 5 seconds to stop the push (Ctrl+C)..." -ForegroundColor Yellow
|
||||
for ($i = 5; $i -gt 0; $i--) {
|
||||
Write-Host "`rTime remaining: $i seconds..." -NoNewline -ForegroundColor Yellow
|
||||
Start-Sleep -Seconds 1
|
||||
}
|
||||
Write-Host "`rContinuing without rebase..."
|
||||
}
|
||||
}
|
||||
|
||||
# Check if current branch needs rebasing
|
||||
$mergeBase = git merge-base HEAD upstream/main
|
||||
|
||||
# get the current branch name
|
||||
$currentBranch = git branch --show-current
|
||||
|
||||
# detect all extension folder names changed under src/
|
||||
$changedFiles = git diff --name-only $mergeBase $currentBranch
|
||||
$changedExtensions = $changedFiles |
|
||||
Where-Object { $_ -like "src/*" } |
|
||||
ForEach-Object {
|
||||
$parts = $_ -split '/'
|
||||
if ($parts.Length -gt 1) { $parts[1] }
|
||||
} |
|
||||
Select-Object -Unique
|
||||
|
||||
if ($changedExtensions) {
|
||||
Write-Host "Changed extensions: $($changedExtensions -join ', ')" -ForegroundColor Green
|
||||
|
||||
# Add each changed extension using azdev extension add
|
||||
foreach ($extension in $changedExtensions) {
|
||||
Write-Host "Adding extension: $extension"
|
||||
azdev extension add $extension
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Host "Error: Failed to add extension $extension" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Run command azdev lint
|
||||
Write-Host "Running azdev lint..." -ForegroundColor Green
|
||||
azdev linter --min-severity medium --repo ./ --src $currentBranch --tgt $mergeBase
|
||||
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 ./ --src $currentBranch --tgt $mergeBase
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
$error_msg = azdev style --repo ./ --src $currentBranch --tgt $mergeBase 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 ./ --src $currentBranch --tgt $mergeBase --discover --no-exitfirst --xml-path test_results.xml 2>$null
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Host "Error: azdev test check failed. You can check the test logs in the 'test_results.xml' file." -ForegroundColor Red
|
||||
exit 1
|
||||
} else {
|
||||
# remove the test_results.xml file
|
||||
Remove-Item -Path test_results.xml
|
||||
}
|
||||
|
||||
Write-Host "Pre-push hook passed." -ForegroundColor Green
|
||||
|
||||
if ($AZURE_CLI_FOLDER) {
|
||||
if ($cliMergeBase -ne $cliUpstreamHead) {
|
||||
Write-Host ""
|
||||
Write-Host "Your $AZURE_CLI_FOLDER repo code is not up to date with upstream/dev. Please run the following commands to rebase and setup:" -ForegroundColor Yellow
|
||||
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Yellow
|
||||
Write-Host "git -C $AZURE_CLI_FOLDER rebase upstream/dev" -ForegroundColor Yellow
|
||||
if ($Extensions) {
|
||||
Write-Host "azdev setup -c $AZURE_CLI_FOLDER -r $Extensions" -ForegroundColor Yellow
|
||||
} else {
|
||||
Write-Host "azdev setup -c $AZURE_CLI_FOLDER" -ForegroundColor Yellow
|
||||
}
|
||||
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Yellow
|
||||
}
|
||||
}
|
||||
exit 0
|
|
@ -0,0 +1,153 @@
|
|||
#!/bin/bash
|
||||
|
||||
printf "\033[0;32mRunning pre-push hook in bash ...\033[0m\n"
|
||||
|
||||
# run azdev_active script
|
||||
SCRIPT_PATH="$(dirname "$0")/azdev_active.sh"
|
||||
. "$SCRIPT_PATH"
|
||||
if [ $? -ne 0 ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if azure-cli is installed in editable mode
|
||||
EDITABLE_LOCATION=$(pip show azure-cli 2>/dev/null | grep "Editable project location" | cut -d" " -f4)
|
||||
if [ ! -z "$EDITABLE_LOCATION" ]; then
|
||||
AZURE_CLI_FOLDER=$(dirname $(dirname "$EDITABLE_LOCATION"))
|
||||
fi
|
||||
|
||||
# Get extension repo paths and join them with spaces
|
||||
EXTENSIONS=$(azdev extension repo list -o tsv | tr '\n' ' ')
|
||||
|
||||
# Verify if current repo is in extension repo list
|
||||
CURRENT_REPO=$(pwd)
|
||||
if [ -z "$(echo "$EXTENSIONS" | grep "$CURRENT_REPO")" ]; then
|
||||
printf "\033[0;31mThe current repo is not added as an extension repo. Please run the following command to add it:\033[0m\n"
|
||||
printf "\033[0;31m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
|
||||
printf "\033[0;31mazdev extension repo add %s\033[0m\n" "$CURRENT_REPO"
|
||||
printf "\033[0;31m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Fetch upstream/main branch
|
||||
printf "\033[0;32mFetching upstream/main branch...\033[0m\n"
|
||||
git fetch upstream main
|
||||
if [ $? -ne 0 ]; then
|
||||
printf "\033[0;31mError: Failed to fetch upstream/main branch. Please run the following command to add the upstream remote:\033[0m\n"
|
||||
printf "\033[0;31m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
|
||||
printf "\033[0;31mgit remote add upstream https://github.com/Azure/azure-cli-extensions.git\033[0m\n"
|
||||
printf "\033[0;31m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -z "$AZURE_CLI_FOLDER" ]; then
|
||||
printf "\033[0;32mFetching %s upstream/dev branch...\033[0m\n" "$AZURE_CLI_FOLDER"
|
||||
git -C "$AZURE_CLI_FOLDER" fetch upstream dev
|
||||
if [ $? -ne 0 ]; then
|
||||
printf "\033[0;31mError: Failed to fetch %s upstream/dev branch. Please run the following command to add the upstream remote:\033[0m\n" "$AZURE_CLI_FOLDER"
|
||||
printf "\033[0;31m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
|
||||
printf "\033[0;31mgit -C %s remote add upstream https://github.com/Azure/azure-cli.git\033[0m\n" "$AZURE_CLI_FOLDER"
|
||||
printf "\033[0;31m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if current branch needs rebasing
|
||||
CLI_MERGE_BASE=$(git -C "$AZURE_CLI_FOLDER" merge-base HEAD upstream/dev)
|
||||
CLI_UPSTREAM_HEAD=$(git -C "$AZURE_CLI_FOLDER" rev-parse upstream/dev)
|
||||
if [ "$CLI_MERGE_BASE" != "$CLI_UPSTREAM_HEAD" ]; then
|
||||
printf "\n"
|
||||
printf "\033[0;33mYour %s repo code is not up to date with upstream/dev. Please run the following commands to rebase and setup:\033[0m\n" "$AZURE_CLI_FOLDER"
|
||||
printf "\033[0;33m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
|
||||
printf "\033[0;33mgit -C %s rebase upstream/dev\033[0m\n" "$AZURE_CLI_FOLDER"
|
||||
if [ ! -z "$EXTENSIONS" ]; then
|
||||
printf "\033[0;33mazdev setup -c %s -r %s\033[0m\n" "$AZURE_CLI_FOLDER" "$EXTENSIONS"
|
||||
else
|
||||
printf "\033[0;33mazdev setup -c %s\033[0m\n" "$AZURE_CLI_FOLDER"
|
||||
fi
|
||||
printf "\033[0;33m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
|
||||
printf "\n"
|
||||
printf "\033[0;33mYou have 5 seconds to stop the push (Ctrl+C)...\033[0m\n"
|
||||
|
||||
# Using a C-style for loop instead of seq
|
||||
i=5
|
||||
while [ $i -ge 1 ]; do
|
||||
printf "\r\033[K\033[1;33mTime remaining: %d seconds...\033[0m" $i
|
||||
sleep 1
|
||||
i=$((i-1))
|
||||
done
|
||||
printf "\rContinuing without rebase...\n"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Check the merge base
|
||||
MERGE_BASE=$(git merge-base HEAD upstream/main)
|
||||
|
||||
# get the current branch name
|
||||
currentBranch=$(git branch --show-current)
|
||||
|
||||
# Detect changed extensions
|
||||
changedFiles=$(git diff --name-only $MERGE_BASE $currentBranch)
|
||||
changedExtensions=$(echo "$changedFiles" | grep "^src/" | cut -d'/' -f2 | sort -u)
|
||||
|
||||
if [ ! -z "$changedExtensions" ]; then
|
||||
printf "\033[0;32mChanged extensions: %s\033[0m\n" "$(echo $changedExtensions | tr '\n' ', ')"
|
||||
|
||||
# Add each changed extension using azdev extension add
|
||||
for extension in $changedExtensions; do
|
||||
printf "Adding extension: %s\n" "$extension"
|
||||
azdev extension add "$extension"
|
||||
if [ $? -ne 0 ]; then
|
||||
printf "\033[0;31mError: Failed to add extension %s\033[0m\n" "$extension"
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
# Run command azdev lint
|
||||
printf "\033[0;32mRunning azdev lint...\033[0m\n"
|
||||
azdev linter --min-severity medium --repo ./ --src $currentBranch --tgt $MERGE_BASE
|
||||
if [ $? -ne 0 ]; then
|
||||
printf "\033[0;31mError: azdev lint check failed.\033[0m\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Run command azdev style
|
||||
printf "\033[0;32mRunning azdev style...\033[0m\n"
|
||||
azdev style --repo ./ --src $currentBranch --tgt $MERGE_BASE
|
||||
if [ $? -ne 0 ]; then
|
||||
error_msg=$(azdev style --repo ./ --src $currentBranch --tgt $MERGE_BASE 2>&1)
|
||||
if echo "$error_msg" | grep -q "No modules"; then
|
||||
printf "\033[0;32mPre-push hook passed.\033[0m\n"
|
||||
exit 0
|
||||
fi
|
||||
printf "\033[0;31mError: azdev style check failed.\033[0m\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Run command azdev test
|
||||
printf "\033[0;32mRunning azdev test...\033[0m\n"
|
||||
azdev test --repo ./ --src $currentBranch --tgt $MERGE_BASE --discover --no-exitfirst --xml-path test_results.xml 2>/dev/null
|
||||
if [ $? -ne 0 ]; then
|
||||
printf "\033[0;31mError: azdev test check failed. You can check the test logs in the 'test_results.xml' file.\033[0m\n"
|
||||
exit 1
|
||||
else
|
||||
# remove the test_results.xml file
|
||||
rm -f test_results.xml
|
||||
fi
|
||||
|
||||
printf "\033[0;32mPre-push hook passed.\033[0m\n"
|
||||
|
||||
if [ ! -z "$AZURE_CLI_FOLDER" ]; then
|
||||
if [ "$CLI_MERGE_BASE" != "$CLI_UPSTREAM_HEAD" ]; then
|
||||
printf "\n"
|
||||
printf "\033[0;33mYour %s repo code is not up to date with upstream/dev. Please run the following commands to rebase and setup:\033[0m\n" "$AZURE_CLI_FOLDER"
|
||||
printf "\033[0;33m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
|
||||
printf "\033[0;33mgit -C %s rebase upstream/dev\033[0m\n" "$AZURE_CLI_FOLDER"
|
||||
if [ ! -z "$EXTENSIONS" ]; then
|
||||
printf "\033[0;33mazdev setup -c %s -r %s\033[0m\n" "$AZURE_CLI_FOLDER" "$EXTENSIONS"
|
||||
else
|
||||
printf "\033[0;33mazdev setup -c %s\033[0m\n" "$AZURE_CLI_FOLDER"
|
||||
fi
|
||||
printf "\033[0;33m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
|
||||
fi
|
||||
fi
|
||||
exit 0
|
|
@ -124,3 +124,6 @@ _az_debug/
|
|||
|
||||
# Ignore temporary test output files.
|
||||
/temp/
|
||||
|
||||
# Ignore test results
|
||||
test_results.xml
|
||||
|
|
Загрузка…
Ссылка в новой задаче