Skip to content

Commit ba8988b

Browse files
committed
fix: Correct YAML parser, PHP warnings, add debug mode and improve token handling
- Fix YAML parser to remove comments from parsed values (# and everything after) - Fix PHP version check to redirect stderr and avoid warnings in output - Add --debug mode with debug() function for verbose output - Improve token handling: automatically add to .env.dist if it exists - Add informative message when token is added to .env.dist
1 parent f426b88 commit ba8988b

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

bin/code-review-guardian.sh

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ and any Git provider (GitHub, GitLab, Bitbucket, etc.)
3333
OPTIONS:
3434
--post-comment Post review comment to PR/MR (requires Git provider token)
3535
--dry-run Show what would be executed without running
36+
--debug Enable debug mode with verbose output
3637
-h, --help Show this help message
3738
3839
EXAMPLES:
3940
$0 # Run code review
4041
$0 --post-comment # Post comment to PR/MR
4142
$0 --dry-run # Show what would be executed
43+
$0 --debug # Run with debug output
4244
4345
CONFIGURATION:
4446
Configuration file: $CONFIG_FILE
@@ -93,7 +95,8 @@ check_dependencies() {
9395
echo "$E_ERROR PHP is not installed. Please install PHP >= 7.4 to use Code Review Guardian."
9496
MISSING_DEPS=1
9597
else
96-
PHP_VERSION=$(php -r 'echo PHP_VERSION;' 2>/dev/null || echo "unknown")
98+
# Get PHP version, redirecting stderr to avoid warnings
99+
PHP_VERSION=$(php -r 'echo PHP_VERSION;' 2>/dev/null | head -1 | tr -d '\n' || echo "unknown")
97100
echo "$E_OK PHP found: $PHP_VERSION"
98101

99102
# Check PHP version (7.4+)
@@ -155,9 +158,9 @@ parse_yaml_value() {
155158
local default="${3:-}"
156159

157160
if [ -n "$section" ]; then
158-
grep -A 20 "^${section}:" "$CONFIG_FILE" 2>/dev/null | grep "^\s*${key}:" | head -1 | awk -F: '{print $2}' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | sed 's/^"//;s/"$//' | sed "s/^'//;s/'$//" || echo "$default"
161+
grep -A 20 "^${section}:" "$CONFIG_FILE" 2>/dev/null | grep "^\s*${key}:" | head -1 | awk -F: '{print $2}' | sed 's/#.*$//' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | sed 's/^"//;s/"$//' | sed "s/^'//;s/'$//" || echo "$default"
159162
else
160-
grep "^${key}:" "$CONFIG_FILE" 2>/dev/null | head -1 | awk -F: '{print $2}' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | sed 's/^"//;s/"$//' | sed "s/^'//;s/'$//" || echo "$default"
163+
grep "^${key}:" "$CONFIG_FILE" 2>/dev/null | head -1 | awk -F: '{print $2}' | sed 's/#.*$//' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | sed 's/^"//;s/"$//' | sed "s/^'//;s/'$//" || echo "$default"
161164
fi
162165
}
163166

@@ -184,6 +187,7 @@ load_config() {
184187
fi
185188

186189
echo "$E_INFO Loading configuration from $CONFIG_FILE..."
190+
debug "Config file path: $CONFIG_FILE"
187191

188192
# Load GGA settings
189193
CONFIG_GGA_ENABLED=$(parse_yaml_value "enabled" "gga" "true")
@@ -343,6 +347,17 @@ EOF
343347
} >> "$env_target"
344348

345349
echo "$E_OK Added $TOKEN_ENV_NAME to $env_target"
350+
351+
# Also add to .env.dist if it exists
352+
if [ -f ".env.dist" ] && ! grep -q "^${TOKEN_ENV_NAME}=" .env.dist 2>/dev/null; then
353+
{
354+
echo ""
355+
echo "# Git Provider API Token (required for posting comments to PRs/MRs)"
356+
echo "$TOKEN_ENV_NAME=your_token_here"
357+
} >> ".env.dist"
358+
echo "$E_OK Also added $TOKEN_ENV_NAME to .env.dist"
359+
fi
360+
346361
echo "$E_INFO Please edit $env_target and set $TOKEN_ENV_NAME with your Git provider token."
347362
else
348363
echo "$E_INFO Variable $TOKEN_ENV_NAME exists in .env file but is empty or commented out."
@@ -725,9 +740,17 @@ post_comment() {
725740
return 0
726741
}
727742

743+
# Debug function (outputs to stderr to not interfere with normal output)
744+
debug() {
745+
if [ "${DEBUG:-false}" = "true" ]; then
746+
echo "$E_INFO [DEBUG] $*" >&2
747+
fi
748+
}
749+
728750
# Parse command line arguments
729751
POST_COMMENT=false
730752
DRY_RUN=false
753+
DEBUG=false
731754

732755
for arg in "$@"; do
733756
case "$arg" in
@@ -741,6 +764,9 @@ for arg in "$@"; do
741764
--dry-run)
742765
DRY_RUN=true
743766
;;
767+
--debug)
768+
DEBUG=true
769+
;;
744770
*)
745771
echo "$E_ERROR Unknown option: $arg"
746772
echo "Run '$0 --help' for usage information."

0 commit comments

Comments
 (0)