From 71281446eaa977559e43536a17bab68f15fbc304 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Thu, 12 Oct 2023 15:12:52 +0200 Subject: [PATCH] cascading-pr.sh login both repos --- .gitignore | 1 + README.md | 4 ++- action.yml | 2 +- cascading-pr-lib.sh | 4 +++ cascading-pr.sh | 37 ++++++++++++++++++-- tests/originrepo/.forgejo/workflows/test.yml | 5 +-- tests/run.sh | 15 +++----- 7 files changed, 52 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 7cd9130..dd96ab5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ forgejo-runner-pid forgejo-runner-token forgejo-runner.clientpid forgejo-runner.log +forgejo-runner-home forgejo-token forgejo-api forgejo-header diff --git a/README.md b/README.md index af9b18b..c095724 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,9 @@ forgejo-runner.sh teardown forgejo.sh teardown forgejo.sh setup root admin1234 codeberg.org/forgejo/forgejo 1.21 forgejo-runner.sh setup -firefox http://$(cat forgejo-ip):3000 +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" ``` diff --git a/action.yml b/action.yml index 709abc1..b3c17a0 100644 --- a/action.yml +++ b/action.yml @@ -60,5 +60,5 @@ runs: --destination-repo "${{ inputs.destination-repo }}" \ --destination-token "${{ inputs.destination-token }}" \ --destination-branch "${{ inputs.destination-branch }}" \ - --update "${{ inputs.update }}" + --update "${{ inputs.update }}" \ --prefix "${{ inputs.prefix }}" diff --git a/cascading-pr-lib.sh b/cascading-pr-lib.sh index ff33f9e..ccc1890 100644 --- a/cascading-pr-lib.sh +++ b/cascading-pr-lib.sh @@ -3,9 +3,13 @@ declare -A options VERBOSE=false + DEBUG=false + : ${EXIT_ON_ERROR:=true} +: ${TMPDIR:=$(mktemp -d)} + function debug() { DEBUG=true set -x diff --git a/cascading-pr.sh b/cascading-pr.sh index 10c7b59..8f771da 100755 --- a/cascading-pr.sh +++ b/cascading-pr.sh @@ -4,12 +4,31 @@ SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source $SELF_DIR/cascading-pr-lib.sh +trap "rm -fr $TMPDIR" EXIT + +function repo_login() { + local repo="$1" + ( + export DOT=$TMPDIR/$repo + forgejo-curl.sh logout + forgejo-curl.sh --token "${options[destination_token]}" login "${options[destination_url]}" + ) +} + +function repo_curl() { + local repo=$1 + shift + DOT=$TMPDIR/$repo forgejo-curl.sh "$@" +} + function run() { options[origin_host_port]=$(host_port ${options[origin_url]}) options[destination_host_port]=$(host_port ${options[destination_url]}) : ${options[prefix]:=${options[origin_repo]}} - - # login destination + + repo_login ${options[destination_repo]} + repo_login ${options[origin_repo]} + # open a PR on destination # checkout the head of the PR # update the PR @@ -31,34 +50,48 @@ function main() { --origin-url) shift options[origin_url]=$1 + shift ;; --origin-repo) shift options[origin_repo]=$1 + shift ;; --origin-token) shift options[origin_token]=$1 + shift ;; --destination-url) shift options[destination_url]=$1 + shift ;; --destination-repo) shift options[destination_repo]=$1 + shift ;; --destination-token) shift options[destination_token]=$1 + shift + ;; + --destination-branch) + shift + options[destination_branch]=$1 + shift ;; --update) shift options[update]=$1 + shift ;; --prefix) shift options[prefix]=$1 + shift + ;; *) "${1:-run}" return 0 diff --git a/tests/originrepo/.forgejo/workflows/test.yml b/tests/originrepo/.forgejo/workflows/test.yml index 6222bc3..40ae608 100644 --- a/tests/originrepo/.forgejo/workflows/test.yml +++ b/tests/originrepo/.forgejo/workflows/test.yml @@ -8,11 +8,12 @@ jobs: - uses: actions/checkout@v4 - uses: SELF@vTest with: - origin-url: ${{ env.$GITHUB_SERVER_URL }} + origin-url: ${{ env.GITHUB_SERVER_URL }} origin-repo: user1/originrepo origin-token: ${{ secrets.ORIGIN_TOKEN }} - destination-url: ${{ env.$GITHUB_SERVER_URL }} + destination-url: ${{ env.GITHUB_SERVER_URL }} destination-repo: user2/destinationrepo destination-token: ${{ secrets.DESTINATION_TOKEN }} + destination-branch: main update: upgraded debug: true diff --git a/tests/run.sh b/tests/run.sh index 8b8ce4e..f8060e6 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -4,13 +4,11 @@ set -e SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +TMPDIR=/tmp/cascading-pr-test source $SELF_DIR/../cascading-pr-lib.sh -TMPDIR=/tmp/cascading-pr function push_self() { - local host_port=$1 - - forgejo-test-helper.sh push_self_action http://user1:admin1234@$host_port user1 cascading-pr vTest + forgejo-test-helper.sh push_self_action http://user1:admin1234@${options[host_port]} user1 cascading-pr vTest } function user_login() { @@ -25,16 +23,13 @@ function user_login() { function user_curl() { local username=$1 shift - ( - export DOT=$TMPDIR/$username - forgejo-curl.sh "$@" - ) + DOT=$TMPDIR/$username forgejo-curl.sh "$@" } function user_token() { local username=$1 name=$2 - curl -sS -f -H Content-Type:application/json --user "$username:${options[password]}" --data-raw '{"name":"'$name'","scopes":["write:repository","write:issue"]}' ${options[url]}/api/v1/users/$username/tokens | jq --raw-output .sha1 + curl -sS -f -H Content-Type:application/json --user "$username:${options[password]}" --data-raw '{"name":"'$name'","scopes":["write:repository","write:issue"]}' ${options[url]}/api/v1/users/$username/tokens | jq --raw-output .sha1 | tee $TMPDIR/$username/token } function user_secret() { @@ -83,7 +78,7 @@ function run() { create_pull_request - push_self ${options[host_port]} + push_self echo do something }