Skip to content

Commit de9eca9

Browse files
jwendellclaude
andauthored
actions/pr: Auto-delete head branch after PR merge (#4641)
Add `--delete-branch` to `gh pr create` so that head branches are automatically deleted when the PR is merged. It's configurable via the input `delete-branch` (`true` by default). Signed-off-by: Jonh Wendell <jwendell@redhat.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 941333d commit de9eca9

4 files changed

Lines changed: 80 additions & 2 deletions

File tree

actions/github/pr/action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ inputs:
3232
diff-show:
3333
type: boolean
3434
default: false
35+
delete-branch:
36+
type: boolean
37+
default: true
3538
dry-run:
3639
type: boolean
3740
default: false
@@ -47,6 +50,7 @@ inputs:
4750
export PR_COMMITTER_EMAIL='\($committer_email)'
4851
export PR_COMMITTER_NAME='\($committer_name)'
4952
export PR_COMMIT_MESSAGE='\($commit_message)'
53+
export PR_DELETE_BRANCH='\(.delete_branch)'
5054
cd \(.working_directory)
5155
${PR_ACTION_PATH}/create.sh
5256
wip:
@@ -73,6 +77,7 @@ runs:
7377
committer_name: ${{ inputs.committer-name }}
7478
committer_email: ${{ inputs.committer-email }}
7579
commit_message: ${{ toJSON(inputs.commit-message) }}
80+
delete_branch: ${{ inputs.delete-branch }}
7681
dry_run: ${{ inputs.dry-run }}
7782
title: ${{ toJSON(inputs.title) }}
7883
working_directory: ${{ inputs.working-directory }}

actions/github/pr/create.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,15 @@ PR_BODY="${PR_BODY}
2525
${SIGNOFF}"
2626

2727
git push --no-verify --set-upstream origin "$PR_BRANCH"
28+
29+
DELETE_BRANCH_FLAG=()
30+
if [[ "$PR_DELETE_BRANCH" == "true" ]]; then
31+
DELETE_BRANCH_FLAG=(--delete-branch)
32+
fi
33+
2834
gh pr create \
2935
-B "$PR_BASE" \
3036
-H "$PR_BRANCH" \
37+
"${DELETE_BRANCH_FLAG[@]}" \
3138
--title "$PR_TITLE" \
3239
--body "$PR_BODY"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
uses: ./actions/github/pr
2+
id: pr
3+
with:
4+
base: main
5+
branch: ${{ env.TEST_BRANCH }}
6+
title: ${{ env.TEST_TITLE }}
7+
body: ${{ env.TEST_BODY }}
8+
commit: true
9+
committer-name: ${{ env.TEST_COMMITTER_NAME }}
10+
committer-email: ${{ env.TEST_COMMITTER_EMAIL }}
11+
commit-message: ${{ env.TEST_COMMIT_MESSAGE }}
12+
delete-branch: false
13+
GITHUB_TOKEN: "VERYSECRET"
14+
15+
before:
16+
- shell: bash
17+
env:
18+
TEST_BODY: This PR tests no delete-branch functionality
19+
TEST_BRANCH: test/pr-action-no-delete-branch
20+
TEST_COMMIT_MESSAGE: >-
21+
chore: Test no-delete-branch from action
22+
TEST_COMMITTER_EMAIL: bot@example.com
23+
TEST_COMMITTER_NAME: PR Bot
24+
TEST_TITLE: Test PR without delete-branch
25+
run: |
26+
. "${ACTION_TEST_PATH}/test_fun.sh"
27+
setup_test
28+
29+
after:
30+
- shell: bash
31+
run: |
32+
. "${ACTION_TEST_PATH}/test_fun.sh"
33+
test_no_delete_branch_output_log \
34+
"${TEST_COMMITTER_NAME}" \
35+
"${TEST_COMMITTER_EMAIL}" \
36+
"${TEST_COMMIT_MESSAGE}" \
37+
"${TEST_BRANCH}" \
38+
"${TEST_TITLE}" \
39+
"${TEST_BODY}"

actions/github/pr/tests/test_fun.sh

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ git config --global user.email ${committer_email}
1515
git commit . -m "${commit_message}" --signoff
1616
git log -1 --pretty=%B
1717
git push --no-verify --set-upstream origin ${branch}
18-
gh pr create -B main -H ${branch} --title "${title}" --body "${body}
18+
gh pr create -B main -H ${branch} --delete-branch --title "${title}" --body "${body}
1919
2020
2121
Signed-off-by: Mock User <mock@example.com>"
@@ -40,7 +40,7 @@ git checkout -b ${branch}
4040
git commit . -m "${commit_message}" --signoff
4141
git log -1 --pretty=%B
4242
git push --no-verify --set-upstream origin ${branch}
43-
gh pr create -B main -H ${branch} --title "${title}" --body "${body}
43+
gh pr create -B main -H ${branch} --delete-branch --title "${title}" --body "${body}
4444
4545
4646
Signed-off-by: Mock User <mock@example.com>"
@@ -64,6 +64,33 @@ test_nocommit_output_log () {
6464
git checkout -b ${branch}
6565
git log -1 --pretty=%B
6666
git push --no-verify --set-upstream origin ${branch}
67+
gh pr create -B main -H ${branch} --delete-branch --title "${title}" --body "${body}
68+
69+
70+
Signed-off-by: Mock User <mock@example.com>"
71+
EOF
72+
cmp -s /tmp/output.log "$MOCK_LOG" || {
73+
echo "fail:Output does not match" >> "$TEST_OUTPUT"
74+
diff -u /tmp/output.log "$MOCK_LOG"
75+
return
76+
}
77+
echo "success:Output matches" >> "$TEST_OUTPUT"
78+
}
79+
80+
test_no_delete_branch_output_log () {
81+
local committer_name="$1"
82+
local committer_email="$2"
83+
local commit_message="$3"
84+
local branch="$4"
85+
local title="$5"
86+
local body="$6"
87+
cat << EOF > /tmp/output.log
88+
git checkout -b ${branch}
89+
git config --global user.name "${committer_name}"
90+
git config --global user.email ${committer_email}
91+
git commit . -m "${commit_message}" --signoff
92+
git log -1 --pretty=%B
93+
git push --no-verify --set-upstream origin ${branch}
6794
gh pr create -B main -H ${branch} --title "${title}" --body "${body}
6895
6996

0 commit comments

Comments
 (0)