mirror of
https://code.forgejo.org/actions/cascading-pr.git
synced 2025-07-17 21:08:25 +02:00
146 lines
3.3 KiB
Bash
Executable file
146 lines
3.3 KiB
Bash
Executable file
#!/bin/bash
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
set -e
|
|
|
|
SELF=${BASH_SOURCE[0]}
|
|
SELF_DIR="$( cd "$( dirname "$SELF" )" && 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 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
|
|
# checkout the head of the PR
|
|
# update the PR
|
|
# force-push the head
|
|
# wait on the status of the tip of the head
|
|
}
|
|
|
|
function main() {
|
|
while true; do
|
|
case "$1" in
|
|
--verbose)
|
|
shift
|
|
verbose
|
|
;;
|
|
--debug)
|
|
shift
|
|
debug
|
|
;;
|
|
--origin-url)
|
|
shift
|
|
options[origin_url]=$1
|
|
shift
|
|
;;
|
|
--origin-repo)
|
|
shift
|
|
options[origin_repo]=$1
|
|
shift
|
|
;;
|
|
--origin-token)
|
|
shift
|
|
options[origin_token]=$1
|
|
shift
|
|
;;
|
|
--origin-pr)
|
|
shift
|
|
options[origin_pr]=$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
|
|
;;
|
|
*)
|
|
finalize_options
|
|
"${1:-run}"
|
|
return 0
|
|
;;
|
|
esac
|
|
done
|
|
}
|
|
|
|
if echo "${@}" | grep --quiet -e '--debug' ; then
|
|
main "${@}"
|
|
else
|
|
stash_debug "${@}"
|
|
fi
|