diff --git a/README.md b/README.md index afb7e20..d823cec 100644 --- a/README.md +++ b/README.md @@ -151,19 +151,8 @@ displayed if an error happens. ## snippets for copy/pasting ```sh -git clone https://code.forgejo.org/actions/setup-forgejo -export PATH=$(pwd)/setup-forgejo:$PATH -git clone https://code.forgejo.org/actions/cascading-pr -cd cascading-pr -forgejo-curl.sh logout -forgejo-runner.sh teardown -forgejo.sh teardown -forgejo.sh setup root admin1234 codeberg.org/forgejo/forgejo 1.21 -FORGEJO_RUNNER_CONFIG=$(pwd)/tests/runner-config.yaml forgejo-runner.sh setup -url=http://$(cat forgejo-ip):3000 -firefox $url tests/run.sh --debug -tests/run.sh --debug create_pull_request +tests/run.sh --debug no_change_no_cascade_pr ./cascading-pr.sh --debug --origin-url "$url" --origin-repo "user1/originrepo" --origin-token "$(cat /tmp/cascading-pr-test/user1/repo-token)" --origin-pr 1 --destination-url "$url" --destination-repo "user2/destinationrepo" --destination-token "$(cat /tmp/cascading-pr-test/user2/repo-token)" --destination-branch "main" --update "upgraded" run ``` diff --git a/cascading-pr.sh b/cascading-pr.sh index 09a6aa6..2e42a83 100755 --- a/cascading-pr.sh +++ b/cascading-pr.sh @@ -43,7 +43,7 @@ function delete_branch() { } function pr_origin_comment_body() { - echo "cascading-pr created at ${options[destination_url]}/${options[destination_repo]}/pulls/$(pr_number destination)" + echo "cascading-pr updated at ${options[destination_url]}/${options[destination_repo]}/pulls/$(pr_number destination)" } function comment_origin_pr() { @@ -121,31 +121,39 @@ function pr_get_destination() { repo_curl ${options[destination_repo]} api --get --data state=open --data type=pulls --data-urlencode q="$title" ${options[destination_api]}/issues | jq --raw-output .[0] > $TMPDIR/destination-pr.json } -function pr() { +function pr_get() { local direction=$1 if ! test -f $TMPDIR/${direction}-pr.json; then pr_get_$direction fi - cat $TMPDIR/${direction}-pr.json +} + +function pr() { + cat $TMPDIR/$1-pr.json } function pr_state() { + pr_get $1 pr $1 | jq --raw-output .state } function pr_url() { + pr_get $1 pr $1 | jq --raw-output .url } function pr_number() { + pr_get $1 pr $1 | jq --raw-output .number } function pr_head() { + pr_get $1 pr $1 | jq --raw-output .head.ref } function pr_merged() { + pr_get $1 pr $1 | jq --raw-output .merged } @@ -168,6 +176,13 @@ function upsert_clone() { ) } +function sha_pushed() { + local direction=$1 + if test -f $TMPDIR/$direction.sha ; then + cat $TMPDIR/$direction.sha + fi +} + function push() { local direction=$1 branch=$2 clone=$3 @@ -176,11 +191,11 @@ function push() { git add . if git commit -m 'cascading-pr update'; then git push --force origin $branch + git rev-parse HEAD > ../$direction.sha log_info "pushed" else log_info "nothing to push" fi - git rev-parse HEAD > ../$direction.sha ) } @@ -240,18 +255,21 @@ function run() { log_info "PR is open, update or create the cascade branch and PR" repo_login ${options[destination_repo]} upsert_destination_branch - upsert_destination_pr - repo_login ${options[origin_repo]} - comment_origin_pr update - wait_destination_ci $(cat $TMPDIR/destination.sha) + local sha=$(sha_pushed destination) + if test "$sha" ; then + upsert_destination_pr + repo_login ${options[origin_repo]} + comment_origin_pr + wait_destination_ci "$sha" + fi ;; closed) if "$(pr_merged origin)"; then log_info "PR was merged, update the cascade PR" repo_login ${options[destination_repo]} - pr origin - pr destination + pr_get origin + pr_get destination update else log_info "PR is closed, close the cascade PR and remove the branch" diff --git a/tests/originrepo-do-nothing/.forgejo/workflows/test.yml b/tests/originrepo-do-nothing/.forgejo/workflows/test.yml new file mode 100644 index 0000000..1d9eafa --- /dev/null +++ b/tests/originrepo-do-nothing/.forgejo/workflows/test.yml @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: MIT +name: test +on: + pull_request: + types: + - opened + - synchronize + - closed +jobs: + test: + runs-on: docker + steps: + - uses: actions/checkout@v4 + - uses: SELF@vTest + with: + origin-url: ${{ env.GITHUB_SERVER_URL }} + origin-repo: user1/originrepo-do-nothing + origin-token: ${{ secrets.ORIGIN_TOKEN }} + origin-pr: ${{ github.event.pull_request.number }} + destination-url: ${{ env.GITHUB_SERVER_URL }} + destination-repo: user2/destinationrepo + destination-branch: main + destination-token: ${{ secrets.DESTINATION_TOKEN }} + update: /bin/true + debug: true diff --git a/tests/originrepo-do-nothing/README b/tests/originrepo-do-nothing/README new file mode 100644 index 0000000..d2b2178 --- /dev/null +++ b/tests/originrepo-do-nothing/README @@ -0,0 +1 @@ +originrepo diff --git a/tests/originrepo/upgraded b/tests/originrepo/upgraded index 8b37734..f53c79f 100755 --- a/tests/originrepo/upgraded +++ b/tests/originrepo/upgraded @@ -7,7 +7,6 @@ destination_pr_json="$2" origin_pr_json="$3" test -d $destination_checkout -test -f $destination_pr_json test -f $origin_pr_json date +%s > $destination_checkout/last diff --git a/tests/run.sh b/tests/run.sh index 45dff2a..5ae2e6e 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -38,7 +38,7 @@ function user_token() { function user_secret() { local username=$1 name=$2 token=$3 - user_curl $username api_json -X PUT --data '{"data":"'$token'"}' ${options[url]}/api/v1/repos/user1/originrepo/actions/secrets/$name + user_curl $username api_json -X PUT --data '{"data":"'$token'"}' ${options[url]}/api/v1/user/actions/secrets/$name } function user_create() { @@ -60,27 +60,34 @@ function merge_pull_request() { done } +function has_cascade_pull_request() { + pr_count=$(forgejo-curl.sh api_json ${options[url]}/api/v1/repos/user2/destinationrepo/pulls | jq '. | length') + test $pr_count -gt 0 +} + function create_pull_request() { - forgejo-curl.sh api_json ${options[url]}/api/v1/repos/user1/originrepo/pulls | jq --raw-output '.[] | .number' | while read pr ; do - forgejo-curl.sh api_json -X DELETE ${options[url]}/api/v1/repos/user1/originrepo/issues/$pr + local originrepo=$1 + + forgejo-curl.sh api_json ${options[url]}/api/v1/repos/user1/${originrepo}/pulls | jq --raw-output '.[] | .number' | while read pr ; do + forgejo-curl.sh api_json -X DELETE ${options[url]}/api/v1/repos/user1/${originrepo}/issues/$pr done - forgejo-curl.sh api_json -X DELETE ${options[url]}/api/v1/repos/user1/originrepo/branches/branch1 >& /dev/null || true - forgejo-curl.sh api_json --data '{"new_branch_name":"branch1"}' ${options[url]}/api/v1/repos/user1/originrepo/branches + forgejo-curl.sh api_json -X DELETE ${options[url]}/api/v1/repos/user1/${originrepo}/branches/branch1 >& /dev/null || true + forgejo-curl.sh api_json --data '{"new_branch_name":"branch1"}' ${options[url]}/api/v1/repos/user1/${originrepo}/branches ( cd $TMPDIR - rm -fr originrepo - git clone -b branch1 http://user1:admin1234@${options[host_port]}/user1/originrepo - cd originrepo + rm -fr ${originrepo} + git clone -b branch1 http://user1:admin1234@${options[host_port]}/user1/${originrepo} + cd ${originrepo} echo CONTENT > README git config user.email root@example.com git config user.name username git add . git commit -m 'update' git push origin branch1 - git rev-parse HEAD > ../originrepo.sha + git rev-parse HEAD > ../${originrepo}.sha ) - forgejo-curl.sh api_json --data '{"title":"PR","base":"main","head":"branch1"}' ${options[url]}/api/v1/repos/user1/originrepo/pulls + forgejo-curl.sh api_json --data '{"title":"PR","base":"main","head":"branch1"}' ${options[url]}/api/v1/repos/user1/${originrepo}/pulls } function finalize_options() { @@ -143,34 +150,55 @@ function unit_retry() { grep --quiet 'retry failed' $TMPDIR/retry.log } -function integration() { +function fixture() { + local origin=$1 + local destination=$2 + user_create user2 user2@example.com - forgejo-test-helper.sh push tests/destinationrepo http://user2:admin1234@${options[host_port]} user2 destinationrepo + forgejo-test-helper.sh push tests/${destination} http://user2:admin1234@${options[host_port]} user2 ${destination} user_create user1 user1@example.com - forgejo-test-helper.sh push tests/originrepo http://user1:admin1234@${options[host_port]} user1 originrepo cascading-pr + forgejo-test-helper.sh push tests/${origin} http://user1:admin1234@${options[host_port]} user1 ${origin} cascading-pr user_secret user1 ORIGIN_TOKEN $(user_token user1 ORIGIN_TOKEN) user_secret user1 DESTINATION_TOKEN $(user_token user2 DESTINATION_TOKEN) push_self +} - # - # create & close - # - create_pull_request +function no_change_no_cascade_pr() { + fixture originrepo-do-nothing destinationrepo + + create_pull_request originrepo-do-nothing + wait_success ${options[url]}/api/v1/repos/user1/originrepo-do-nothing $(cat $TMPDIR/originrepo-do-nothing.sha) + ! has_cascade_pull_request +} + +function create_and_close() { + fixture originrepo destinationrepo + + create_pull_request originrepo wait_success ${options[url]}/api/v1/repos/user1/originrepo $(cat $TMPDIR/originrepo.sha) + has_cascade_pull_request close_pull_request wait_success ${options[url]}/api/v1/repos/user1/originrepo $(cat $TMPDIR/originrepo.sha) +} - # - # create & merge - # - create_pull_request +function create_and_merge() { + fixture originrepo destinationrepo + + create_pull_request originrepo wait_success ${options[url]}/api/v1/repos/user1/originrepo $(cat $TMPDIR/originrepo.sha) + has_cascade_pull_request merge_pull_request wait_success ${options[url]}/api/v1/repos/user1/originrepo $(cat $TMPDIR/originrepo.sha) } +function integration() { + no_change_no_cascade_pr + create_and_close + create_and_merge +} + function unit() { unit_retry }