From ec542726c8c4c0483a1491f201fadc982868dcbe Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Thu, 12 Oct 2023 19:13:07 +0200 Subject: [PATCH] upsert_branch && upsert_pr --- .forgejo/workflows/integration.yml | 2 +- README.md | 2 +- action.yml | 4 ++ cascading-pr-lib.sh | 20 +++++++- cascading-pr.sh | 49 ++++++++++++++++++-- tests/originrepo/.forgejo/workflows/test.yml | 1 + 6 files changed, 72 insertions(+), 6 deletions(-) diff --git a/.forgejo/workflows/integration.yml b/.forgejo/workflows/integration.yml index 291cf55..0e82d9e 100644 --- a/.forgejo/workflows/integration.yml +++ b/.forgejo/workflows/integration.yml @@ -19,5 +19,5 @@ jobs: - name: tests run: | - .forgejo/run-test.sh --debug --host_port ${{ steps.forgejo.outputs.host-port }} --url ${{ steps.forgejo.outputs.url }} --token ${{ steps.forgejo.outputs.token }} + test/run.sh --debug --host_port ${{ steps.forgejo.outputs.host-port }} --url ${{ steps.forgejo.outputs.url }} --token ${{ steps.forgejo.outputs.token }} diff --git a/README.md b/README.md index c095724..4bb341e 100644 --- a/README.md +++ b/README.md @@ -16,5 +16,5 @@ url=http://$(cat forgejo-ip):3000 firefox $url tests/run.sh --debug tests/run.sh --debug create_pull_request -cascading-pr.sh --debug --origin-url "$url" --origin-repo "user1/originrepo" --origin-token "$(cat /tmp/cascading-pr/user1/token)" --destination-url "$url" --destination-repo "user2/destinationrepo" --destination-token "$(cat /tmp/cascading-pr/user2/token)" --destination-branch "main" --update "upgraded" +cascading-pr.sh --debug --origin-url "$url" --origin-repo "user1/originrepo" --origin-token "$(cat /tmp/cascading-pr/user1/token)" --origin-pr 1 --destination-url "$url" --destination-repo "user2/destinationrepo" --destination-token "$(cat /tmp/cascading-pr/user2/token)" --destination-branch "main" --update "upgraded" ``` diff --git a/action.yml b/action.yml index b3c17a0..7a4777f 100644 --- a/action.yml +++ b/action.yml @@ -14,6 +14,9 @@ inputs: origin-token: description: 'a token with write permission on origin-repo' required: true + origin-pr: + description: 'number of the PR in {orign-repo}' + required: true destination-url: description: 'URL of the Forgejo instance where the cascading PR is created or updated (e.g. https://code.forgejo.org)' required: true @@ -56,6 +59,7 @@ runs: --origin-url "${{ inputs.origin-url }}" \ --origin-repo "${{ inputs.origin-repo }}" \ --origin-token "${{ inputs.origin-token }}" \ + --origin-pr "${{ inputs.origin-pr }}" \ --destination-url "${{ inputs.destination-url }}" \ --destination-repo "${{ inputs.destination-repo }}" \ --destination-token "${{ inputs.destination-token }}" \ diff --git a/cascading-pr-lib.sh b/cascading-pr-lib.sh index ccc1890..8febf6c 100644 --- a/cascading-pr-lib.sh +++ b/cascading-pr-lib.sh @@ -2,6 +2,8 @@ declare -A options +PREFIX=============== + VERBOSE=false DEBUG=false @@ -35,7 +37,7 @@ function log_verbose() { } function log_info() { - log "$@" + echo "$PREFIX $@" } function fatal_error() { @@ -47,6 +49,22 @@ function fatal_error() { fi } +function stash_debug() { + echo start $SELF + mkdir -p $TMPDIR + > $TMPDIR/run.out + tail --follow $TMPDIR/run.out | sed --unbuffered -n -e "/^$PREFIX/s/^$PREFIX //p" & + pid=$! + if ! $SELF --debug "$@" >& $TMPDIR/run.out ; then + kill $pid + cat $TMPDIR/run.out + echo fail $SELF + return 1 + fi + kill $pid + echo success $SELF +} + function host_port() { local url="$1" diff --git a/cascading-pr.sh b/cascading-pr.sh index 8f771da..0dd00b8 100755 --- a/cascading-pr.sh +++ b/cascading-pr.sh @@ -1,7 +1,10 @@ #!/bin/bash # SPDX-License-Identifier: MIT -SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +set -e + +SELF=${BASH_SOURCE[0]} +SELF_DIR="$( cd "$( dirname "$SELF" )" && pwd )" source $SELF_DIR/cascading-pr-lib.sh trap "rm -fr $TMPDIR" EXIT @@ -21,12 +24,42 @@ function repo_curl() { DOT=$TMPDIR/$repo forgejo-curl.sh "$@" } -function run() { +function upsert_branch() { + local repo_api=${options[destination_url]}/api/v1/repos/${options[destination_repo]} + if forgejo-curl.sh api_json $repo_api/branches/${options[destination_head]} >& /dev/null ; then + log_info "branch ${options[destination_head]} already exists" + return + fi + forgejo-curl.sh api_json --data-raw '{"new_branch_name":"'${options[destination_head]}'","old_branch_name":"'${options[destination_base]}'"}' $repo_api/branches + log_info "branch ${options[destination_head]} created" +} + +function upsert_pr() { + local repo_api=${options[destination_url]}/api/v1/repos/${options[destination_repo]} + local title="cascading-pr from ${options[origin_url]}/${options[origin_repo]}/pulls/${options[origin_pr]}" + forgejo-curl.sh api --get --data state=open --data type=pulls --data-urlencode q="$title" $repo_api/issues | jq --raw-output .[0] > $TMPDIR/pr.json + url=$(jq --raw-output .url < $TMPDIR/pr.json) + if test "$url" != "null"; then + log_info "PR already exists $url" + return + fi + forgejo-curl.sh api_json --data-raw '{"title":"'"$title"'","base":"'${options[destination_base]}'","head":"'${options[destination_head]}'"}' $repo_api/pulls > $TMPDIR/pr.json + url=$(jq --raw-output .url < $TMPDIR/pr.json) + log_info "PR created $url" +} + +function finalize_options() { options[origin_host_port]=$(host_port ${options[origin_url]}) options[destination_host_port]=$(host_port ${options[destination_url]}) + options[destination_base]=${options[destination_branch]} : ${options[prefix]:=${options[origin_repo]}} + options[destination_head]=${options[prefix]}-${options[origin_pr]} +} +function run() { repo_login ${options[destination_repo]} + upsert_branch + upsert_pr repo_login ${options[origin_repo]} # open a PR on destination @@ -62,6 +95,11 @@ function main() { options[origin_token]=$1 shift ;; + --origin-pr) + shift + options[origin_pr]=$1 + shift + ;; --destination-url) shift options[destination_url]=$1 @@ -93,6 +131,7 @@ function main() { shift ;; *) + finalize_options "${1:-run}" return 0 ;; @@ -100,4 +139,8 @@ function main() { done } -${MAIN:-main} "${@}" +if echo "${@}" | grep --quiet -e '--debug' ; then + main "${@}" +else + stash_debug "${@}" +fi diff --git a/tests/originrepo/.forgejo/workflows/test.yml b/tests/originrepo/.forgejo/workflows/test.yml index 40ae608..96865fd 100644 --- a/tests/originrepo/.forgejo/workflows/test.yml +++ b/tests/originrepo/.forgejo/workflows/test.yml @@ -11,6 +11,7 @@ jobs: origin-url: ${{ env.GITHUB_SERVER_URL }} origin-repo: user1/originrepo origin-token: ${{ secrets.ORIGIN_TOKEN }} + origin-pr: ${{ github.event.pull_request.number }} destination-url: ${{ env.GITHUB_SERVER_URL }} destination-repo: user2/destinationrepo destination-token: ${{ secrets.DESTINATION_TOKEN }}