Skip to content

Commit 898b20f

Browse files
authored
ci(librarian): close existing PR and delete branch in update workflow (#13689)
Close the existing pull request and delete its branch (instead of failing) to allow the daily update workflow to run continuously without manual intervention. Fixes googleapis/librarian#6687
1 parent a049999 commit 898b20f

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

.github/workflows/update_librarian_googleapis.yaml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,32 @@ jobs:
3434
path: google-cloud-java
3535
fetch-depth: 0
3636
persist-credentials: false
37-
- name: Check for Open PR
37+
- name: Close Existing PR and Branch
3838
env:
39-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39+
GH_TOKEN: ${{ secrets.CLOUD_JAVA_BOT_GITHUB_TOKEN }}
4040
run: |
4141
set -x
4242
current_branch="update-librarian-googleapis-main"
4343
4444
if [ "${{ github.event_name }}" = "pull_request" ]; then
45-
echo "PR Test: Skipping open PR check."
45+
echo "PR Test: Skipping cleanup."
4646
else
4747
# Try to find an open pull request associated with the branch
4848
pr_num=$(gh pr list -s open -H "${current_branch}" -q . --json number | jq ".[] | .number")
4949
5050
if [ -n "${pr_num}" ]; then
51-
echo "Error: An open Pull Request already exists for this update: PR #${pr_num}."
52-
echo "Please merge or close the existing PR before running this workflow again."
53-
exit 1 # Fails this step and the workflow
51+
echo "Closing existing PR #${pr_num}..."
52+
gh pr close "${pr_num}" --delete-branch || gh pr close "${pr_num}"
53+
fi
54+
55+
# Delete the remote branch if it still exists (e.g. if no PR was open)
56+
echo "Checking if remote branch ${current_branch} exists..."
57+
if gh api "repos/${{ github.repository }}/git/refs/heads/${current_branch}" --silent 2>/dev/null; then
58+
echo "Deleting remote branch ${current_branch}..."
59+
gh api -X DELETE "repos/${{ github.repository }}/git/refs/heads/${current_branch}" || {
60+
echo "Error: Failed to delete remote branch ${current_branch}."
61+
exit 1
62+
}
5463
fi
5564
fi
5665
- name: Set up Go
@@ -130,7 +139,7 @@ jobs:
130139
PR_BODY: |
131140
Updated googleapis commitish in librarian.yaml to https://github.com/googleapis/googleapis/commit/${{ steps.commit.outputs.new_commit }}
132141
133-
💡 **Note:** Please merge or close this PR so that the daily update workflow can continue to run successfully the next day. Alternatively, you can manually trigger the workflow once this PR is merged or closed.
142+
💡 **Note:** If this PR is still open when the daily update workflow runs next, it will be closed and replaced with a new PR containing the latest updates.
134143
run: |
135144
set -x
136145

0 commit comments

Comments
 (0)