mirror of
https://code.forgejo.org/actions/cascading-pr
synced 2025-03-15 14:54:40 +01:00
upsert_branch && upsert_pr
This commit is contained in:
parent
71281446ea
commit
ec542726c8
6 changed files with 72 additions and 6 deletions
|
@ -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 }}
|
||||
|
||||
|
|
|
@ -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"
|
||||
```
|
||||
|
|
|
@ -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 }}" \
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 }}
|
||||
|
|
Loading…
Add table
Reference in a new issue