Skip to content

Commit

Permalink
feat: now the latest updates PR will close stale update PRs (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickytonline authored Sep 24, 2024
1 parent 04485b4 commit 1ee6cb0
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions scripts/create-pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

PR_TITLE="$CUSTOM_PR_TITLE"
BRANCH_NAME="chore_automated_open-sauced-updates_$(date +%s)"
BODY="This is an automated PR generated by the OpenSauced pizza-action."

# Even though the bot account is set to the OpenSauced bot account, the author is still the GitHub Actions bot
AUTHOR="app/github-actions"

# Function to close PRs with the same title
close_duplicate_prs() {
echo "Checking for PRs with previous updates"
local new_pr_number=$1
local prs_to_close=$(gh pr list --state open --json number,title,author --jq ".[] | select(.title == \"$PR_TITLE\" and .number != $new_pr_number and .author.is_bot == "true" and .author.login == \"$AUTHOR\") | .number")

# No previous update PRs found
if [[ -z $prs_to_close ]]; then
echo "No duplicate PRs found to close"
return
fi

for pr in $prs_to_close; do
echo "Closing duplicate PR #$pr"
gh pr close $pr --comment "Closing in favor of #$new_pr_number"
done
}

git checkout -b "$BRANCH_NAME"

Expand All @@ -10,15 +32,21 @@ git add .

# Check if there are any changes to commit
if [[ -n "$(git status --porcelain)" ]]; then
echo "Creating PR \"$PR_TITLE\" for branch "$BRANCH_NAME""
echo "Creating PR \"$PR_TITLE\" for branch \"$BRANCH_NAME\""
git commit -m "$PR_TITLE"
git push origin "$BRANCH_NAME"

# Attempt to create the PR (dry run)
if gh pr create --title "$PR_TITLE" --body "This is an automated PR generated by the OpenSauced pizza-action." --dry-run; then
if gh pr create --title "$PR_TITLE" --body "$BODY" --dry-run; then
# If dry run is successful, create the actual PR
PR_URL=$(gh pr create --title "$PR_TITLE" --body "This is an automated PR generated by the OpenSauced pizza-action.")
echo "PR created successfully: $PR_URL"

# Extract PR number from the URL
PR_NUMBER=$(echo $PR_URL | grep -oE '[0-9]+$')

# Close duplicate PRs
close_duplicate_prs $PR_NUMBER
else
echo "Failed to create PR. There might be an issue with branch permissions or other repository settings."
fi
Expand Down

0 comments on commit 1ee6cb0

Please sign in to comment.