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

Merge pull request 'close-merge is close and applies to destination-ref as well' (#19) from earl-warren/cascading-pr:wip-ref into main

Reviewed-on: https://code.forgejo.org/actions/cascading-pr/pulls/19
Reviewed-by: dachary <dachary@noreply.code.forgejo.org>
This commit is contained in:
earl-warren 2024-01-09 19:23:54 +00:00
commit 2ad02b3ff6
6 changed files with 60 additions and 29 deletions

View file

@ -88,7 +88,7 @@ not exist, it is created.
| destination-token | a token with write permission on destination-repo | `true` | |
| update | path to the script to update the content of the cascading PR | `true` | |
| prefix | prefix of the branch from which the cascading PR is created on {destination-repo} or {destination-fork-repo} (default to {origin-repo}) | `false` | |
| close-merge | if true the cascading PR will be closed and the branch deleted when the PR is merged | `false` | false |
| close | if true the cascading PR will be closed and the branch deleted when the PR is merged | `false` | false |
| verbose | if true print verbose information | `false` | false |
| debug | if true print debug information | `false` | false |
<!-- action-docs-inputs -->
@ -220,18 +220,21 @@ For instance:
* `./cascading-pr.sh --debug --origin-url ... upsert_branch` will only call the `upsert_branch`
function found in `cascading-pr.sh`.
## directories
The `tests/run.sh` script stores all its files in
`/tmp/cascading-pr-test`. The temporary directories created by
`cascading-pr.sh` are disposed of when the script ends.
## logging
If `--debug` is used a full debug log is displayed, very complete and
very verbose. Otherwise it is stashed in a temporary file and only
displayed if an error happens.
If all goes well, the runner logs are not displayed even with `--debug`. They
can be looked at in the web UI.
## directories
The `tests/run.sh` script stores all its files in
`/tmp/cascading-pr-test`. The temporary directories created by
`cascading-pr.sh` are disposed of when the script ends.
## snippets for copy/pasting
```sh

View file

@ -103,8 +103,8 @@ inputs:
required: true
prefix:
description: 'prefix of the branch from which the cascading PR is created on {destination-repo} or {destination-fork-repo} (default to {origin-repo})'
close-merge:
description: 'if true the cascading PR will be closed and the branch deleted when the PR is merged'
close:
description: 'if true the cascading PR will be closed and the branch deleted when (i) the {origin-pr} is merged or (ii) when the cascading PR status is success if {origin-ref} is set'
default: false
verbose:
description: 'if true print verbose information'
@ -145,5 +145,5 @@ runs:
--destination-branch "${{ inputs.destination-branch }}" \
--update "${{ inputs.update }}" \
--prefix "${{ inputs.prefix }}" \
--close-merge "${{ inputs.close-merge }}" \
--close "${{ inputs.close }}" \
run

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
@ -385,7 +386,7 @@ function finalize_options() {
options[destination_is_fork]=false
fi
: ${options[close_merge]:=false}
: ${options[close]:=false}
}
function run() {
@ -414,7 +415,10 @@ function run_origin_ref() {
log_info "cascade PR status failed"
status=1
fi
close_pr
if "${options[close]}" ; then
log_info "close the cascade PR and remove the branch"
close_pr
fi
return $status
fi
}
@ -436,7 +440,7 @@ function run_origin_pr() {
;;
closed)
if "$(pr_merged origin)"; then
if "${options[close_merge]}" ; then
if "${options[close]}" ; then
log_info "PR is merged, close the cascade PR and remove the branch"
close_pr
else
@ -528,9 +532,9 @@ function main() {
options[prefix]=$1
shift
;;
--close-merge)
--close)
shift
options[close_merge]=$1
options[close]=$1
shift
;;
*)

View file

@ -16,5 +16,6 @@ jobs:
destination-repo: user2/destinationrepo
destination-branch: main
destination-token: ${{ secrets.DESTINATION_TOKEN }}
close: true
update: ./upgraded
debug: true

View file

@ -21,5 +21,5 @@ jobs:
destination-branch: main
destination-token: ${{ secrets.DESTINATION_TOKEN }}
update: ./upgraded
close-merge: true
close: true
debug: true

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() {
@ -421,6 +443,7 @@ function run() {
function integration() {
run branch_and_success
run branch_and_fail
run no_change_no_cascade_pr
run create_in_destination_fork_and_close
run create_and_close