Skip to content
Snippets Groups Projects

New Merge Request Helper

Merged Nick Barry requested to merge merge-request-check-enhancements into develop
All threads resolved!
1 file
+ 141
0
Compare changes
  • Side-by-side
  • Inline
+ 141
0
#!/bin/bash
# Script to check changed file extensions and for code in changed lines of files
# Define variables for testing only (these are passed in via GitLab CI)
#CI_COMMIT_BEFORE_SHA=""
#CI_COMMIT_SHA=""
# Function to check for ASP.NET controls and directives
check_aspx() {
local content="$1"
# Check for ASP.NET server controls and directives (matches <asp:, <%@ Page, <%@ Control, etc.)
if echo "$content" | grep -qE '<asp:|<%|%>'; then
return 0 # ASP.NET control or directive found
fi
return 1 # No ASP.NET control or directive found
}
# Function to check for PHP code and Blade directives
check_php_blade() {
local content="$1"
# Check for PHP code (matches <?php ... ?> and <?= ... ?>)
if echo "$content" | grep -qE '<\?php|<\?='; then
return 0 # PHP code found
fi
# Check for Blade directives (matches {{ ... }}, {!! ... !!}, @if, @foreach, etc.)
if echo "$content" | grep -qE '{{.*}}|{!!.*!!}|@\w+'; then
return 0 # Blade directive found
fi
return 1 # No PHP code or Blade directive found
}
# Flag to track if code was found
code_found=0
# Loop through each changed file in the commit
tempfile=$(mktemp)
git diff --name-only $CI_COMMIT_BEFORE_SHA..$CI_COMMIT_SHA > "$tempfile"
while read -r file; do
case "$file" in
*.html)
echo "OK: HTML file change - $file"
;;
*.js)
echo "OK: JS file change - $file"
;;
*.css)
echo "OK: CSS file change - $file"
;;
*.pdf)
echo "OK: PDF file change - $file"
;;
*.doc)
echo "OK: DOC file change - $file"
;;
*.docx)
echo "OK: DOCX file change - $file"
;;
*.png)
echo "OK: PNG file change - $file"
;;
*.jpg)
echo "OK: JPG file change - $file"
;;
*.gif)
echo "OK: GIF file change - $file"
;;
*.svg)
echo "OK: SVG file change - $file"
;;
*.ico)
echo "OK: ICO file change - $file"
;;
*.md)
echo "OK: MD file change - $file"
;;
*.csproj)
echo "OK: CSPROJ file change - $file"
;;
*.sln)
echo "OK: SLN file change - $file"
;;
*.aspx)
# need to look for code within the HTML
echo "CHECKING: ASPX file - $file"
while IFS= read -r line; do
if [[ $line == \+\ * ]]; then
# Extract added lines, ignoring leading '+'
content=$(echo "$line" | cut -c2-)
# Check for ASP.NET code and elements
if check_aspx "$content"; then
trimmed=$(echo "$content" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
echo "FAIL: Found ASP.NET code or elements in $file: $trimmed"
code_found=1 # Set flag indicating code was found
fi
fi
done < <(git diff $CI_COMMIT_BEFORE_SHA..$CI_COMMIT_SHA -- "$file")
;;
*.blade.php)
# need to look for code within the HTML
echo "CHECKING: BLADE.PHP file - $file"
while IFS= read -r line; do
if [[ $line == \+\ * ]]; then
# Extract added lines, ignoring leading '+'
content=$(echo "$line" | cut -c2-)
# Check for PHP code and blade directives
if check_php_blade "$content"; then
trimmed=$(echo "$content" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
echo "FAIL: Found PHP code or Blade directives in $file: $trimmed"
code_found=1 # Set flag indicating code was found
fi
fi
done < <(git diff $CI_COMMIT_BEFORE_SHA..$CI_COMMIT_SHA -- "$file")
;;
*)
# All other files fail for manual review
echo "FAIL: File type needs reviewed - $file"
code_found=1 # Set flag indicating code could be present
;;
esac
done < "$tempfile"
rm "$tempfile"
# Fail the GitLab CI job if code was found
if [ $code_found -eq 1 ]; then
echo "Job failed. Files found that need to be reviewed. Please contact ITS-ADS to request a review of this deployment."
exit 1 # Exit with non-zero status to indicate failure
else
echo "Job succeeded. No files that need to be reviewed."
exit 0 # Exit with zero status to indicate success
fi
\ No newline at end of file
Loading