1
0
Fork 0
mirror of https://code.forgejo.org/actions/cascading-pr synced 2025-03-14 22:36:58 +01:00

remove branches when closing a cascade PR from a fork

Fixes: https://code.forgejo.org/actions/cascading-pr/issues/17
This commit is contained in:
Earl Warren 2024-01-09 19:48:08 +01:00
parent 09dc15f425
commit 417a1ecac9
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
2 changed files with 36 additions and 13 deletions

View file

@ -44,21 +44,22 @@ function destination_updated_at() {
jq --raw-output .updated_at < $TMPDIR/updated_at.json
}
function exists_branch() {
local direction=$1
function delete_branch_destination() {
local branch=${options[destination_head]}
local repo=${options[destination_repo]}
local api=${options[destination_api]}
repo_curl ${options[${direction}_repo]} api_json ${options[${direction}_api]}/branches/${options[${direction}_head]} >& /dev/null
}
if ${options[destination_is_fork]} ; then
repo=${options[destination_fork_repo]}
api=${options[destination_fork_api]}
fi
function delete_branch() {
local direction=$1
if ! $(exists_branch $direction) ; then
log_info "branch ${options[${direction}_head]} does not exists"
if ! repo_curl ${options[destination_repo]} api_json $api/branches/$branch >& /dev/null ; then
log_info "branch $branch does not exists in $repo"
return
fi
repo_curl ${options[${direction}_repo]} api_json -X DELETE ${options[${direction}_api]}/branches/${options[${direction}_head]}
log_info "branch ${options[${direction}_head]} deleted"
repo_curl ${options[destination_repo]} api_json -X DELETE $api/branches/$branch
log_info "branch $branch deleted in $repo"
}
function pr_origin_comment_body() {
@ -115,7 +116,7 @@ function close_pr() {
log_info "closing $(pr_url ${direction})"
local number=$(pr_number $direction)
repo_curl ${options[${direction}_repo]} api_json -X PATCH --data '{"state":"closed"}' ${options[${direction}_api]}/issues/$number
delete_branch ${direction}
delete_branch_destination
else
log_info "no open PR found"
fi

View file

@ -110,6 +110,12 @@ function cascade_pull_request() {
forgejo-curl.sh api_json ${options[url]}/api/v1/repos/user2/${repo}/pulls
}
function branches_count() {
local owner="$1" repo="$2"
forgejo-curl.sh api_json ${options[url]}/api/v1/repos/${owner}/${repo}/branches | jq '. | length'
}
function create_branch1() {
local owner=$1 repo=$2 modify=$3
@ -282,7 +288,9 @@ function branch_and_success() {
create_branch1 user1 ${origin_repo}
wait_success ${options[url]}/api/v1/repos/user1/${origin_repo} $(cat $TMPDIR/user1-${origin_repo}.sha)
test $(cascade_pull_request_count ${destination_repo}) = 1
# origin-branch-fail/.forgejo/workflows/test.yml has `close: true`
has_no_cascade_pull_request ${destination_repo}
test 1 = "$(branches_count user2 ${destination_repo})"
}
function branch_and_fail() {
@ -296,7 +304,9 @@ function branch_and_fail() {
create_branch1 user1 ${origin_repo}
wait_failure ${options[url]}/api/v1/repos/user1/${origin_repo} $(cat $TMPDIR/user1-${origin_repo}.sha)
test $(cascade_pull_request_count ${destination_repo}) = 1
has_no_cascade_pull_request ${destination_repo}
# origin-branch-fail/.forgejo/workflows/test.yml has `close: false`
has_cascade_pull_request ${destination_repo}
test 2 = "$(branches_count user2 ${destination_repo})"
}
function create_and_close() {
@ -306,8 +316,10 @@ function create_and_close() {
create_pull_request_case1 user1 user1 originrepo
wait_success ${options[url]}/api/v1/repos/user1/originrepo $(cat $TMPDIR/user1-originrepo.sha)
has_cascade_pull_request
test 2 = "$(branches_count user2 destinationrepo)"
close_pull_request originrepo
wait_success ${options[url]}/api/v1/repos/user1/originrepo $(cat $TMPDIR/user1-originrepo.sha)
test 1 = "$(branches_count user2 destinationrepo)"
}
function taint_update() {
@ -340,9 +352,11 @@ function create_and_merge() {
wait_success ${options[url]}/api/v1/repos/user1/originrepo $(cat $TMPDIR/user1-originrepo.sha)
has_cascade_pull_request
test 2 = "$(branches_count user2 destinationrepo)"
merge_pull_request originrepo
wait_success ${options[url]}/api/v1/repos/user1/originrepo $(cat $TMPDIR/user1-originrepo.sha)
has_cascade_pull_request
test 2 = "$(branches_count user2 destinationrepo)"
}
function create_in_destination_fork_and_close() {
@ -353,8 +367,10 @@ function create_in_destination_fork_and_close() {
wait_success ${options[url]}/api/v1/repos/user1/origin-fork-destination $(cat $TMPDIR/user1-origin-fork-destination.sha)
has_cascade_pull_request
test 2 = "$(branches_count user3 destinationrepo)"
close_pull_request origin-fork-destination
wait_success ${options[url]}/api/v1/repos/user1/origin-fork-destination $(cat $TMPDIR/user1-origin-fork-destination.sha)
test 1 = "$(branches_count user3 destinationrepo)"
}
function create_in_existing_destination_fork_and_close() {
@ -367,8 +383,10 @@ function create_in_existing_destination_fork_and_close() {
wait_success ${options[url]}/api/v1/repos/user1/origin-organization-fork-destination $(cat $TMPDIR/user1-origin-organization-fork-destination.sha)
has_cascade_pull_request
test 2 = "$(branches_count destination-fork destinationrepo)"
close_pull_request origin-organization-fork-destination
wait_success ${options[url]}/api/v1/repos/user1/origin-organization-fork-destination $(cat $TMPDIR/user1-origin-organization-fork-destination.sha)
test 1 = "$(branches_count destination-fork destinationrepo)"
}
function create_in_organization_destination_fork_and_close() {
@ -379,8 +397,10 @@ function create_in_organization_destination_fork_and_close() {
wait_success ${options[url]}/api/v1/repos/user1/origin-organization-fork-destination $(cat $TMPDIR/user1-origin-organization-fork-destination.sha)
has_cascade_pull_request
test 2 = "$(branches_count destination-fork destinationrepo)"
close_pull_request origin-organization-fork-destination
wait_success ${options[url]}/api/v1/repos/user1/origin-organization-fork-destination $(cat $TMPDIR/user1-origin-organization-fork-destination.sha)
test 1 = "$(branches_count destination-fork destinationrepo)"
}
function create_and_merge_close() {
@ -391,9 +411,11 @@ function create_and_merge_close() {
wait_success ${options[url]}/api/v1/repos/user1/originrepo-close-merge $(cat $TMPDIR/user1-originrepo-close-merge.sha)
has_cascade_pull_request
test 2 = "$(branches_count user2 destinationrepo)"
merge_pull_request originrepo-close-merge
wait_success ${options[url]}/api/v1/repos/user1/originrepo-close-merge $(cat $TMPDIR/user1-originrepo-close-merge.sha)
has_no_cascade_pull_request
test 1 = "$(branches_count user2 destinationrepo)"
}
function run() {