gh-pr-create-wrapperโข7.54 kB
#!/bin/bash
# GitFlow Guardian - PR Creation Wrapper
# Prevents creating PRs to the wrong branch before they're submitted
# Colors
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
# Get current branch
CURRENT_BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null)
# Parse target branch from arguments
TARGET_BRANCH=""
ARGS=("$@")
for i in "${!ARGS[@]}"; do
if [[ "${ARGS[$i]}" == "--base" ]] && [[ $((i+1)) -lt ${#ARGS[@]} ]]; then
TARGET_BRANCH="${ARGS[$((i+1))]}"
break
fi
done
# If no --base specified, gh pr create defaults to main/master
if [[ -z "$TARGET_BRANCH" ]]; then
# Check default branch
DEFAULT_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@')
if [[ -z "$DEFAULT_BRANCH" ]]; then
DEFAULT_BRANCH="main"
fi
TARGET_BRANCH="$DEFAULT_BRANCH"
fi
# Function to check if branch follows GitFlow patterns
check_gitflow_compliance() {
local source_branch="$1"
local target_branch="$2"
# Rules for PRs to main
if [[ "$target_branch" == "main" ]] || [[ "$target_branch" == "master" ]]; then
if [[ "$source_branch" == "develop" ]] || \
[[ "$source_branch" =~ ^release/.+ ]] || \
[[ "$source_branch" =~ ^hotfix/.+ ]]; then
return 0 # Valid
else
echo -e "${RED}โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ${NC}"
echo -e "${RED}โ ๐จ GITFLOW VIOLATION DETECTED ๐จ โ${NC}"
echo -e "${RED}โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ${NC}"
echo -e "${RED}โ โ${NC}"
echo -e "${RED}โ You're trying to create a PR to: ${YELLOW}$target_branch${RED} โ${NC}"
echo -e "${RED}โ From branch: ${YELLOW}$source_branch${RED} โ${NC}"
echo -e "${RED}โ โ${NC}"
echo -e "${RED}โ ${YELLOW}PRs to main must come from:${RED} โ${NC}"
echo -e "${RED}โ โข develop (for releases) โ${NC}"
echo -e "${RED}โ โข release/* (for release candidates) โ${NC}"
echo -e "${RED}โ โข hotfix/* (for emergency fixes) โ${NC}"
echo -e "${RED}โ โ${NC}"
echo -e "${RED}โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ${NC}"
echo -e "${RED}โ โ${NC}"
echo -e "${RED}โ ${GREEN}โ
CORRECT ACTION:${RED} โ${NC}"
echo -e "${RED}โ โ${NC}"
echo -e "${RED}โ Create your PR to ${GREEN}develop${RED} instead: โ${NC}"
echo -e "${RED}โ โ${NC}"
echo -e "${RED}โ ${CYAN}gh pr create --base develop${RED} โ${NC}"
echo -e "${RED}โ โ${NC}"
echo -e "${RED}โ Or if this is a hotfix: โ${NC}"
echo -e "${RED}โ 1. Rename your branch: โ${NC}"
echo -e "${RED}โ ${CYAN}git branch -m hotfix/$(echo $source_branch | sed 's/.*\///')${RED} โ${NC}"
echo -e "${RED}โ 2. Push the renamed branch โ${NC}"
echo -e "${RED}โ 3. Create PR from hotfix branch โ${NC}"
echo -e "${RED}โ โ${NC}"
echo -e "${RED}โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ${NC}"
return 1 # Invalid
fi
fi
# Rules for PRs to develop - almost everything is allowed except main/master
if [[ "$target_branch" == "develop" ]]; then
if [[ "$source_branch" == "main" ]] || [[ "$source_branch" == "master" ]]; then
echo -e "${RED}โ ERROR: Cannot create PR from main/master to develop${NC}"
echo -e "${YELLOW} This would reverse the GitFlow direction${NC}"
return 1
fi
return 0 # Most branches can PR to develop
fi
# For other target branches, allow
return 0
}
# Check GitFlow compliance
if ! check_gitflow_compliance "$CURRENT_BRANCH" "$TARGET_BRANCH"; then
echo ""
echo -e "${YELLOW}โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ${NC}"
echo -e "${YELLOW}โ Override Options: โ${NC}"
echo -e "${YELLOW}โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค${NC}"
echo -e "${YELLOW}โ โข To force this PR anyway (NOT recommended): โ${NC}"
echo -e "${YELLOW}โ ${RED}GH_FORCE_PR=1 gh pr create $@${YELLOW} โ${NC}"
echo -e "${YELLOW}โ โ${NC}"
echo -e "${YELLOW}โ โข To use gh directly without checks: โ${NC}"
echo -e "${YELLOW}โ ${RED}command gh pr create $@${YELLOW} โ${NC}"
echo -e "${YELLOW}โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ${NC}"
# Check if force flag is set
if [[ "$GH_FORCE_PR" == "1" ]]; then
echo ""
echo -e "${RED}โ ๏ธ WARNING: Forcing PR creation despite GitFlow violation!${NC}"
echo -e "${RED} This PR will likely fail CI checks.${NC}"
echo ""
# Proceed with the original command
command gh "$@"
else
exit 1
fi
else
# GitFlow compliant - show success message and proceed
echo -e "${GREEN}โ
GitFlow Check Passed${NC}"
echo -e "${GREEN} Creating PR from ${CYAN}$CURRENT_BRANCH${GREEN} to ${CYAN}$TARGET_BRANCH${NC}"
echo ""
# Execute the original gh command
command gh "$@"
fi