Files
gh-atournayre-claude-market…/skills/git-pr/scripts/create_pr.sh
2025-11-29 17:58:54 +08:00

62 lines
1.9 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# Création complète de PR: lit template, génère titre, crée PR
# Usage: create_pr.sh <branch_base> <pr_template_path>
# Output: PR_NUMBER (stdout) ou exit 1
set -euo pipefail
BRANCH_BASE="$1"
PR_TEMPLATE_PATH="$2"
SCRIPTS_DIR="$(dirname "$0")"
# Récupérer branche courante
BRANCH_NAME=$(git branch --show-current)
# Lire template PR
if [ ! -f "$PR_TEMPLATE_PATH" ]; then
echo "❌ Template PR absent: $PR_TEMPLATE_PATH" >&2
exit 1
fi
PR_TEMPLATE=$(cat "$PR_TEMPLATE_PATH")
# Détecter issue depuis nom de branche (ex: feat/123-description, fix/456-bug)
ISSUE_NUMBER=$(echo "$BRANCH_NAME" | grep -oE '[0-9]+' | head -1 || echo "")
# Générer titre PR
if [ -n "$ISSUE_NUMBER" ]; then
# Vérifier que l'issue existe et récupérer son titre
ISSUE_TITLE=$(gh issue view "$ISSUE_NUMBER" --json title -q '.title' 2>/dev/null || echo "")
if [ -n "$ISSUE_TITLE" ]; then
PR_TITLE="$ISSUE_TITLE / Issue #$ISSUE_NUMBER"
echo "✅ Titre PR basé sur issue #$ISSUE_NUMBER" >&2
else
# Issue non trouvée, utiliser nom de branche
echo "⚠️ Issue #$ISSUE_NUMBER non trouvée" >&2
# Nettoyer le nom de branche pour créer un titre
PR_TITLE=$(echo "$BRANCH_NAME" | sed 's|^[^/]*/||' | sed 's/-/ /g' | sed 's/[0-9]* *//')
PR_TITLE="${PR_TITLE^}" # Capitalize first letter
fi
else
# Pas de numéro d'issue, utiliser nom de branche
echo " Pas d'issue détectée dans '$BRANCH_NAME'" >&2
PR_TITLE=$(echo "$BRANCH_NAME" | sed 's|^[^/]*/||' | sed 's/-/ /g')
PR_TITLE="${PR_TITLE^}"
fi
# Créer fichier temporaire avec le body
PR_BODY_FILE="/tmp/pr_body_$(date +%s).md"
echo "$PR_TEMPLATE" > "$PR_BODY_FILE"
# Appeler le script de push sécurisé
PR_NUMBER=$(bash "$SCRIPTS_DIR/safe_push_pr.sh" "$BRANCH_BASE" "$BRANCH_NAME" "$PR_TITLE" "$PR_BODY_FILE")
EXIT_CODE=$?
# Nettoyer
rm -f "$PR_BODY_FILE"
if [ $EXIT_CODE -ne 0 ]; then
exit 1
fi
echo "$PR_NUMBER"