From 417a1ecac93dcfaddd49f8217120a658512a774a Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Tue, 9 Jan 2024 19:48:08 +0100 Subject: [PATCH] remove branches when closing a cascade PR from a fork Fixes: https://code.forgejo.org/actions/cascading-pr/issues/17 --- cascading-pr.sh | 25 +++++++++++++------------ tests/run.sh | 24 +++++++++++++++++++++++- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/cascading-pr.sh b/cascading-pr.sh index babe8c0..f2ec3db 100755 --- a/cascading-pr.sh +++ b/cascading-pr.sh @@ -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 diff --git a/tests/run.sh b/tests/run.sh index 492671e..8215336 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -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() {