Skip to content

Commit 1ee6cb0

Browse files
authored
feat: now the latest updates PR will close stale update PRs (#17)
1 parent 04485b4 commit 1ee6cb0

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

scripts/create-pr.sh

+30-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22

33
PR_TITLE="$CUSTOM_PR_TITLE"
44
BRANCH_NAME="chore_automated_open-sauced-updates_$(date +%s)"
5+
BODY="This is an automated PR generated by the OpenSauced pizza-action."
6+
7+
# Even though the bot account is set to the OpenSauced bot account, the author is still the GitHub Actions bot
8+
AUTHOR="app/github-actions"
9+
10+
# Function to close PRs with the same title
11+
close_duplicate_prs() {
12+
echo "Checking for PRs with previous updates"
13+
local new_pr_number=$1
14+
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")
15+
16+
# No previous update PRs found
17+
if [[ -z $prs_to_close ]]; then
18+
echo "No duplicate PRs found to close"
19+
return
20+
fi
21+
22+
for pr in $prs_to_close; do
23+
echo "Closing duplicate PR #$pr"
24+
gh pr close $pr --comment "Closing in favor of #$new_pr_number"
25+
done
26+
}
527

628
git checkout -b "$BRANCH_NAME"
729

@@ -10,15 +32,21 @@ git add .
1032

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

1739
# Attempt to create the PR (dry run)
18-
if gh pr create --title "$PR_TITLE" --body "This is an automated PR generated by the OpenSauced pizza-action." --dry-run; then
40+
if gh pr create --title "$PR_TITLE" --body "$BODY" --dry-run; then
1941
# If dry run is successful, create the actual PR
2042
PR_URL=$(gh pr create --title "$PR_TITLE" --body "This is an automated PR generated by the OpenSauced pizza-action.")
2143
echo "PR created successfully: $PR_URL"
44+
45+
# Extract PR number from the URL
46+
PR_NUMBER=$(echo $PR_URL | grep -oE '[0-9]+$')
47+
48+
# Close duplicate PRs
49+
close_duplicate_prs $PR_NUMBER
2250
else
2351
echo "Failed to create PR. There might be an issue with branch permissions or other repository settings."
2452
fi

0 commit comments

Comments
 (0)