From 1ee6cb02978244e05ac3939c1133ed1ac3ebed70 Mon Sep 17 00:00:00 2001 From: Nick Taylor Date: Tue, 24 Sep 2024 10:05:14 -0400 Subject: [PATCH] feat: now the latest updates PR will close stale update PRs (#17) --- scripts/create-pr.sh | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/scripts/create-pr.sh b/scripts/create-pr.sh index 8da3330..c8fef66 100755 --- a/scripts/create-pr.sh +++ b/scripts/create-pr.sh @@ -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" @@ -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