fix(create_pr_from_files): improved robustness che prima faceva schifo

This commit is contained in:
Giulio De Pasquale 2024-12-05 15:02:19 +00:00
parent d91b3bc9a9
commit 5ebc68ad80

View File

@ -1,27 +1,23 @@
create_pr_from_files() { create_pr_from_files() {
# Constants
local TIMESTAMP=$(date +%Y%m%d%H%M%S) local TIMESTAMP=$(date +%Y%m%d%H%M%S)
# Branch names
local temp_branch="pr-${TIMESTAMP}-temp" local temp_branch="pr-${TIMESTAMP}-temp"
local pr_branch="pr-${TIMESTAMP}" local pr_branch="pr-${TIMESTAMP}"
local origin_branch="development" local base_branch="development"
local current_branch=$(git rev-parse --abbrev-ref HEAD) local current_branch=$(git rev-parse --abbrev-ref HEAD)
local files=()
cleanup() { cleanup() {
git checkout "${current_branch}" git checkout "${current_branch}"
git checkout "${temp_branch}" -- "${all_files[@]}" git checkout "${temp_branch}" -- "${all_files[@]}"
git restore --staged . git restore --staged .
git branch -D "${temp_branch}" git branch -D "${temp_branch}"
git branch -D "${pr_branch}"
} }
# Function to handle errors
handle_error() { handle_error() {
local error_msg="$1" local error_msg="$1"
echo "Error: ${error_msg}" echo "Error: ${error_msg}"
cleanup cleanup
return 1 return 1
} }
@ -29,7 +25,7 @@ create_pr_from_files() {
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case $1 in case $1 in
-b | --base) -b | --base)
origin_branch="$2" base_branch="$2"
shift 2 shift 2
;; ;;
*) *)
@ -44,50 +40,54 @@ create_pr_from_files() {
return 1 return 1
fi fi
asd=($(for f in "${files[@]}"; do find "${f}" | tr -s '/'; done)) # Backup all modified files
# Initialize an empty array local all_files=()
files_to_keep=()
# Use a while loop to read each line from the command substitution
while IFS= read -r line; do
files_to_keep+=("$line")
done < <(git status --porcelain | grep -f <(printf "%s\n" "${asd[@]}") | cut -d " " -f 3)
# Initialize an empty array
all_files=()
# Use a while loop to read each line from the command substitution
while IFS= read -r line; do while IFS= read -r line; do
all_files+=("$line") all_files+=("$line")
done < <(git status --porcelain | cut -d " " -f 3) done < <(git status -s | cut -d " " -f 3)
# Switch to origin branch if not already there echo Our: "${files[@]}"
if [ "$current_branch" != "$origin_branch" ]; then echo All: "${all_files[@]}"
git checkout "${origin_branch}" # Switch to base branch and create temporary branch
if [ "$current_branch" != "$base_branch" ]; then
git checkout "$base_branch" || handle_error "Failed to checkout base branch"
git pull || handle_error "Failed to sync base branch"
fi fi
git checkout -b "${temp_branch}" && git commit -am "Saving changes" || cleanup git checkout -b "$temp_branch" || handle_error "Failed to create temporary branch"
git commit -am "Backup changes" || handle_error "Failed to commit changes to temporary branch"
git checkout "${origin_branch}" && git checkout -b "${pr_branch}" && git checkout "${temp_branch}" -- "${files_to_keep[@]}" || cleanup # Create PR branch from base branch
git checkout "$base_branch" || handle_error "Failed to checkout base branch"
git checkout -b "$pr_branch" || handle_error "Failed to create PR branch"
# Restore specified files from temporary branch
git checkout "$temp_branch" -- "${files[@]}" || handle_error "Failed to restore specified files"
git add "${files[@]}" || handle_error "Failed to stage specified files"
# # Generate commit message
echo "Generating commit message..." echo "Generating commit message..."
local commit_message=$(git diff --staged | aichat -m ollama:pino-coder -r commitmessage || handle_error "Failed to generate commit message.") local commit_message=$(git diff --staged | aichat -m ollama:pino-coder -r commitmessage || handle_error "Failed to generate commit message.")
# Extract title and body from the generated commit message
local commit_subject=$(echo "$commit_message" | head -n 1) local commit_subject=$(echo "$commit_message" | head -n 1)
local commit_body=$(echo "$commit_message" | tail -n +2) local commit_body=$(echo "$commit_message" | tail -n +2)
git commit -m "$commit_subject"$'\n\n'"$commit_body" || handle_error "Committing files failed." # # # Commit the specified files
git commit --edit -m "$commit_subject"$'\n\n'"$commit_body" || handle_error "Committing files failed."
if [ $? -ne 0 ]; then
handle_error "Committing files failed."
fi
git push origin "${pr_branch}" # Push the PR branch to the remote repository
git push origin "$pr_branch" || handle_error "Failed to push PR branch"
# Create PR # Create the pull request
gh pr create \ gh pr create \
--base "${origin_branch}" \ --base "$base_branch" \
--head "${pr_branch}" \ --head "$pr_branch" || handle_error "Failed to create pull request"
--title "${commit_subject}" \
--body "${commit_body}" ||
handle_error "Failed to create pull request"
echo "Pull request created successfully." echo "Pull request created successfully."
# Cleanup
cleanup cleanup
} }