2023-10-11 18:05:11 +02:00
|
|
|
#!/bin/bash
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2023-10-12 19:13:07 +02:00
|
|
|
set -e
|
2023-10-22 22:26:03 +02:00
|
|
|
set -o posix
|
2023-10-12 19:13:07 +02:00
|
|
|
|
|
|
|
SELF=${BASH_SOURCE[0]}
|
|
|
|
SELF_DIR="$( cd "$( dirname "$SELF" )" && pwd )"
|
2023-10-11 18:05:11 +02:00
|
|
|
source $SELF_DIR/cascading-pr-lib.sh
|
|
|
|
|
2023-10-12 15:12:52 +02:00
|
|
|
trap "rm -fr $TMPDIR" EXIT
|
|
|
|
|
|
|
|
function repo_login() {
|
2023-10-27 13:50:52 +02:00
|
|
|
local direction="$1"
|
|
|
|
local repo=${options[${direction}_repo]}
|
2023-10-12 15:12:52 +02:00
|
|
|
(
|
|
|
|
export DOT=$TMPDIR/$repo
|
|
|
|
forgejo-curl.sh logout
|
2023-10-27 13:50:52 +02:00
|
|
|
forgejo-curl.sh --token "${options[${direction}_token]}" login "${options[${direction}_url]}"
|
2023-10-12 15:12:52 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function repo_curl() {
|
|
|
|
local repo=$1
|
|
|
|
shift
|
|
|
|
DOT=$TMPDIR/$repo forgejo-curl.sh "$@"
|
|
|
|
}
|
|
|
|
|
2023-10-28 17:40:09 +02:00
|
|
|
function default_branch() {
|
|
|
|
local direction=$1
|
|
|
|
|
|
|
|
repo_curl ${options[${direction}_repo]} api_json ${options[${direction}_api]} > $TMPDIR/$direction.json
|
|
|
|
jq --raw-output .default_branch < $TMPDIR/$direction.json
|
|
|
|
}
|
|
|
|
|
2023-12-23 21:09:43 +01:00
|
|
|
function destination_updated_at() {
|
|
|
|
local api
|
2023-12-23 18:32:49 +01:00
|
|
|
|
2023-12-23 21:09:43 +01:00
|
|
|
if ${options[destination_is_fork]} ; then
|
|
|
|
repo_curl ${options[destination_repo]} api_json ${options[destination_fork_api]} > $TMPDIR/updated_at.json
|
|
|
|
else
|
|
|
|
repo_curl ${options[destination_repo]} api_json ${options[destination_api]} > $TMPDIR/updated_at.json
|
|
|
|
fi
|
|
|
|
jq --raw-output .updated_at < $TMPDIR/updated_at.json
|
2023-12-23 18:32:49 +01:00
|
|
|
}
|
|
|
|
|
2024-01-09 19:48:08 +01:00
|
|
|
function delete_branch_destination() {
|
|
|
|
local branch=${options[destination_head]}
|
|
|
|
local repo=${options[destination_repo]}
|
|
|
|
local api=${options[destination_api]}
|
2023-10-13 19:10:50 +02:00
|
|
|
|
2024-01-09 19:48:08 +01:00
|
|
|
if ${options[destination_is_fork]} ; then
|
|
|
|
repo=${options[destination_fork_repo]}
|
|
|
|
api=${options[destination_fork_api]}
|
|
|
|
fi
|
2023-10-22 17:45:18 +02:00
|
|
|
|
2024-01-09 19:48:08 +01:00
|
|
|
if ! repo_curl ${options[destination_repo]} api_json $api/branches/$branch >& /dev/null ; then
|
|
|
|
log_info "branch $branch does not exists in $repo"
|
2023-10-13 19:10:50 +02:00
|
|
|
return
|
|
|
|
fi
|
2024-01-09 19:48:08 +01:00
|
|
|
repo_curl ${options[destination_repo]} api_json -X DELETE $api/branches/$branch
|
|
|
|
log_info "branch $branch deleted in $repo"
|
2023-10-13 19:10:50 +02:00
|
|
|
}
|
|
|
|
|
2023-10-22 17:55:58 +02:00
|
|
|
function pr_origin_comment_body() {
|
2023-10-22 18:26:56 +02:00
|
|
|
echo "cascading-pr updated at ${options[destination_url]}/${options[destination_repo]}/pulls/$(pr_number destination)"
|
2023-10-22 17:55:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function comment_origin_pr() {
|
|
|
|
cat > $TMPDIR/data <<EOF
|
|
|
|
{
|
|
|
|
"body":"$(pr_origin_comment_body)"
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
repo_curl ${options[origin_repo]} api_json --data @$TMPDIR/data ${options[origin_api]}/issues/${options[origin_pr]}/comments
|
|
|
|
log_info "comment added to $(pr_url origin)"
|
|
|
|
}
|
|
|
|
|
2023-10-13 19:10:50 +02:00
|
|
|
function pr_destination_title() {
|
2024-01-03 19:20:39 +01:00
|
|
|
echo "cascading-pr from ${options[origin_url]}/${options[origin_repo]} ${options[origin_head]}"
|
2023-10-13 19:10:50 +02:00
|
|
|
}
|
|
|
|
|
2023-10-22 17:31:07 +02:00
|
|
|
function pr_destination_body() {
|
2024-12-07 15:16:48 +01:00
|
|
|
if [[ -n "${options[origin_pr]}" ]]; then
|
|
|
|
echo "cascading-pr from ${options[origin_url]}/${options[origin_repo]}/pulls/${options[origin_pr]}"
|
|
|
|
else
|
|
|
|
echo "cascading-pr from ${options[origin_url]}/${options[origin_repo]} ${options[origin_head]}"
|
|
|
|
fi
|
2023-10-22 17:31:07 +02:00
|
|
|
}
|
|
|
|
|
2023-10-22 17:45:18 +02:00
|
|
|
function upsert_destination_pr() {
|
2023-10-13 19:10:50 +02:00
|
|
|
url=$(pr_url destination)
|
|
|
|
state=$(pr_state destination)
|
|
|
|
if test "$url" != "null" -a "$state" = "open"; then
|
|
|
|
log_info "an open PR already exists $url"
|
2023-10-12 19:13:07 +02:00
|
|
|
return
|
|
|
|
fi
|
2023-10-31 22:25:43 +01:00
|
|
|
if ${options[destination_is_fork]} ; then
|
|
|
|
head="$(owner ${options[destination_fork_repo]}):${options[destination_head]}"
|
|
|
|
else
|
|
|
|
head=${options[destination_head]}
|
|
|
|
fi
|
2023-10-13 19:10:50 +02:00
|
|
|
local title=$(pr_destination_title)
|
2023-10-22 17:15:02 +02:00
|
|
|
cat > $TMPDIR/data <<EOF
|
|
|
|
{
|
2023-10-22 17:31:07 +02:00
|
|
|
"title":"$(pr_destination_title)",
|
|
|
|
"body":"$(pr_destination_body)",
|
2023-10-22 17:15:02 +02:00
|
|
|
"base":"${options[destination_base]}",
|
2023-10-31 22:25:43 +01:00
|
|
|
"head":"$head"
|
2023-10-22 17:15:02 +02:00
|
|
|
}
|
|
|
|
EOF
|
|
|
|
retry repo_curl ${options[destination_repo]} api_json --data @$TMPDIR/data ${options[destination_api]}/pulls > $TMPDIR/destination-pr.json
|
2023-10-13 19:10:50 +02:00
|
|
|
log_info "PR created $(pr_url destination)"
|
|
|
|
}
|
|
|
|
|
|
|
|
function close_pr() {
|
2023-10-24 18:19:30 +02:00
|
|
|
local direction=destination
|
2023-10-13 19:10:50 +02:00
|
|
|
|
|
|
|
if test "$(pr_state ${direction})" = "open"; then
|
|
|
|
log_info "closing $(pr_url ${direction})"
|
|
|
|
local number=$(pr_number $direction)
|
2023-10-22 17:15:02 +02:00
|
|
|
repo_curl ${options[${direction}_repo]} api_json -X PATCH --data '{"state":"closed"}' ${options[${direction}_api]}/issues/$number
|
2024-01-09 19:48:08 +01:00
|
|
|
delete_branch_destination
|
2023-10-13 19:10:50 +02:00
|
|
|
else
|
|
|
|
log_info "no open PR found"
|
|
|
|
fi
|
2023-10-12 19:13:07 +02:00
|
|
|
}
|
|
|
|
|
2023-10-13 19:10:50 +02:00
|
|
|
function pr_get_origin() {
|
|
|
|
repo_curl ${options[origin_repo]} api_json ${options[origin_api]}/pulls/${options[origin_pr]} > $TMPDIR/origin-pr.json
|
|
|
|
}
|
|
|
|
|
|
|
|
function pr_get_destination() {
|
|
|
|
local title=$(pr_destination_title)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-22 18:26:56 +02:00
|
|
|
function pr_get() {
|
2023-10-13 19:10:50 +02:00
|
|
|
local direction=$1
|
|
|
|
if ! test -f $TMPDIR/${direction}-pr.json; then
|
|
|
|
pr_get_$direction
|
|
|
|
fi
|
2023-10-22 18:26:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function pr() {
|
|
|
|
cat $TMPDIR/$1-pr.json
|
2023-10-13 19:10:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function pr_state() {
|
2023-10-22 18:26:56 +02:00
|
|
|
pr_get $1
|
2023-10-13 19:10:50 +02:00
|
|
|
pr $1 | jq --raw-output .state
|
|
|
|
}
|
|
|
|
|
|
|
|
function pr_url() {
|
2023-10-22 18:26:56 +02:00
|
|
|
pr_get $1
|
2023-10-13 19:10:50 +02:00
|
|
|
pr $1 | jq --raw-output .url
|
|
|
|
}
|
|
|
|
|
|
|
|
function pr_number() {
|
2023-10-22 18:26:56 +02:00
|
|
|
pr_get $1
|
2023-10-13 19:10:50 +02:00
|
|
|
pr $1 | jq --raw-output .number
|
|
|
|
}
|
|
|
|
|
2023-10-13 21:08:52 +02:00
|
|
|
function pr_merged() {
|
2023-10-22 18:26:56 +02:00
|
|
|
pr_get $1
|
2023-10-13 21:08:52 +02:00
|
|
|
pr $1 | jq --raw-output .merged
|
|
|
|
}
|
|
|
|
|
2023-10-28 17:40:09 +02:00
|
|
|
function pr_from_fork() {
|
|
|
|
pr_get $1
|
|
|
|
pr $1 | jq --raw-output .head.repo.fork
|
|
|
|
}
|
|
|
|
|
2023-10-31 22:25:43 +01:00
|
|
|
function git_clone() {
|
|
|
|
local direction=$1 url=$2
|
2023-10-12 21:49:24 +02:00
|
|
|
|
|
|
|
if ! test -d $TMPDIR/$direction; then
|
2023-10-31 22:25:43 +01:00
|
|
|
git -c credential.helper="store --file=$TMPDIR/$direction.git-credentials" clone $url $TMPDIR/$direction
|
2023-10-12 21:49:24 +02:00
|
|
|
fi
|
|
|
|
(
|
|
|
|
cd $TMPDIR/$direction
|
2023-10-24 18:19:30 +02:00
|
|
|
git config credential.helper "store --file=$TMPDIR/$direction.git-credentials"
|
2023-10-12 21:49:24 +02:00
|
|
|
git config user.email cascading-pr@example.com
|
|
|
|
git config user.name cascading-pr
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-10-31 22:25:43 +01:00
|
|
|
function git_checkout() {
|
2023-10-31 23:23:30 +01:00
|
|
|
local direction=$1 ref="$2"
|
2023-10-31 22:25:43 +01:00
|
|
|
local remote=origin
|
|
|
|
|
|
|
|
(
|
|
|
|
cd $TMPDIR/$direction
|
|
|
|
if [[ "$ref" =~ ^refs/ ]] ; then
|
2024-01-02 18:39:58 +01:00
|
|
|
git fetch --update-head-ok ${remote} +$ref:$ref
|
2023-10-31 22:25:43 +01:00
|
|
|
else
|
|
|
|
ref=${remote}/$ref
|
|
|
|
fi
|
|
|
|
git checkout -b prbranch $ref
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function git_remote() {
|
|
|
|
local direction=$1 remote=$2 url=$3
|
|
|
|
|
|
|
|
(
|
|
|
|
cd $TMPDIR/$direction
|
|
|
|
git remote add $remote $url
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function git_reset_branch() {
|
|
|
|
local direction=$1 remote=$2 branch=$3
|
|
|
|
(
|
|
|
|
cd $TMPDIR/$direction
|
|
|
|
if git ls-remote --exit-code --heads ${remote} $branch ; then
|
|
|
|
git fetch --quiet ${remote} $branch
|
|
|
|
git reset --hard ${remote}/$branch
|
|
|
|
fi
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-10-22 18:26:56 +02:00
|
|
|
function sha_pushed() {
|
|
|
|
local direction=$1
|
|
|
|
if test -f $TMPDIR/$direction.sha ; then
|
|
|
|
cat $TMPDIR/$direction.sha
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-12-23 21:09:43 +01:00
|
|
|
function destination_updated_at_changed() {
|
|
|
|
local before="$1"
|
|
|
|
local after="$(destination_updated_at)"
|
|
|
|
test "$before" != "$after"
|
|
|
|
}
|
|
|
|
|
2023-10-12 21:49:24 +02:00
|
|
|
function push() {
|
2023-12-23 21:09:43 +01:00
|
|
|
local remote=$1 branch=$2
|
2023-10-12 21:49:24 +02:00
|
|
|
|
|
|
|
(
|
2023-12-23 21:09:43 +01:00
|
|
|
cd $TMPDIR/destination
|
2023-10-12 23:24:11 +02:00
|
|
|
git add .
|
|
|
|
if git commit -m 'cascading-pr update'; then
|
2023-12-23 21:09:43 +01:00
|
|
|
local before=$(destination_updated_at)
|
|
|
|
sleep 1 # the resolution of the update time is one second
|
2023-10-31 22:25:43 +01:00
|
|
|
git push --force ${remote} prbranch:$branch
|
2023-12-23 21:09:43 +01:00
|
|
|
git rev-parse HEAD > ../destination.sha
|
|
|
|
retry destination_updated_at_changed "$before"
|
|
|
|
local after=$(destination_updated_at)
|
2023-10-12 21:49:24 +02:00
|
|
|
log_info "pushed"
|
2023-10-12 23:24:11 +02:00
|
|
|
else
|
|
|
|
log_info "nothing to push"
|
2023-10-12 21:49:24 +02:00
|
|
|
fi
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function wait_destination_ci() {
|
|
|
|
local sha="$1"
|
|
|
|
local repo_api=${options[destination_url]}/api/v1/repos/${options[destination_repo]}
|
|
|
|
wait_success $repo_api $sha
|
|
|
|
}
|
|
|
|
|
2023-10-31 22:25:43 +01:00
|
|
|
function upsert_fork() {
|
|
|
|
if repo_curl ${options[destination_repo]} api_json ${options[destination_fork_api]} > $TMPDIR/fork.json 2> /dev/null ; then
|
2023-11-01 16:32:39 +01:00
|
|
|
if test "$(jq --raw-output .fork < $TMPDIR/fork.json)" != true ; then
|
2023-10-31 22:25:43 +01:00
|
|
|
log_error "the destination fork already exists but is not a fork ${options[destination_fork]}"
|
|
|
|
return 1
|
|
|
|
fi
|
2023-11-01 16:32:39 +01:00
|
|
|
local forked_from_repo=$(jq --raw-output .parent.full_name < $TMPDIR/fork.json)
|
2023-10-31 22:25:43 +01:00
|
|
|
if test "$forked_from_repo" != "${options[destination_repo]}" ; then
|
|
|
|
log_error "${options[destination_fork]} must be a fork of ${options[destination_repo]} but is a fork of $forked_from_repo instead"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
else
|
2023-10-31 23:23:30 +01:00
|
|
|
local fork_owner=$(owner ${options[destination_fork_repo]})
|
2023-10-31 22:25:43 +01:00
|
|
|
local data="{}"
|
|
|
|
if repo_curl ${options[destination_repo]} api_json ${options[destination_url]}/api/v1/orgs/${fork_owner} >& /dev/null ; then
|
|
|
|
data='{"organization":"'$fork_owner'"}'
|
|
|
|
fi
|
2023-10-31 23:23:30 +01:00
|
|
|
repo_curl ${options[destination_repo]} api_json --data "$data" ${options[destination_url]}/api/v1/repos/${options[destination_repo]}/forks
|
2023-10-31 22:25:43 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkout() {
|
|
|
|
#
|
|
|
|
# origin
|
|
|
|
#
|
|
|
|
git_clone origin ${options[origin_clone]}
|
|
|
|
git_checkout origin "${options[origin_head]}"
|
|
|
|
|
|
|
|
#
|
|
|
|
# destination
|
|
|
|
#
|
|
|
|
git_clone destination ${options[destination_clone]}
|
|
|
|
git_checkout destination "${options[destination_base]}"
|
|
|
|
|
|
|
|
#
|
|
|
|
# fork
|
|
|
|
#
|
|
|
|
local head_remote=origin
|
|
|
|
if ${options[destination_is_fork]} ; then
|
|
|
|
upsert_fork
|
|
|
|
git_remote destination fork ${options[destination_fetch_fork]}
|
|
|
|
head_remote=fork
|
|
|
|
fi
|
|
|
|
git_reset_branch destination $head_remote "${options[destination_head]}"
|
|
|
|
}
|
|
|
|
|
2023-10-12 21:49:24 +02:00
|
|
|
function update() {
|
|
|
|
(
|
2023-10-28 17:40:09 +02:00
|
|
|
local update=${options[update]}
|
|
|
|
if ! [[ "$update" =~ ^/ ]] ; then
|
|
|
|
local d
|
2024-01-02 18:39:58 +01:00
|
|
|
if $(origin_has_pr) && $(pr_from_fork origin); then
|
2023-10-28 17:40:09 +02:00
|
|
|
local default_branch=$(default_branch origin)
|
|
|
|
log_info "PR is from a forked repository, using the default branch $default_branch to obtain the update script"
|
|
|
|
d=$TMPDIR/update
|
|
|
|
git -C $TMPDIR/origin worktree add $d $default_branch
|
|
|
|
else
|
|
|
|
d=$TMPDIR/origin
|
|
|
|
fi
|
|
|
|
update=$d/$update
|
|
|
|
fi
|
|
|
|
cd $TMPDIR
|
2024-01-02 18:39:58 +01:00
|
|
|
local origin_info
|
|
|
|
if $(origin_has_pr); then
|
|
|
|
origin_info=$TMPDIR/origin-pr.json
|
|
|
|
else
|
|
|
|
origin_info="${options[origin_ref]}"
|
|
|
|
fi
|
|
|
|
$update $TMPDIR/destination $TMPDIR/destination-pr.json $TMPDIR/origin $origin_info
|
2023-10-12 21:49:24 +02:00
|
|
|
)
|
2023-10-31 22:25:43 +01:00
|
|
|
local remote_head=origin
|
|
|
|
if ${options[destination_is_fork]} ; then
|
|
|
|
remote_head=fork
|
|
|
|
fi
|
2023-12-23 21:09:43 +01:00
|
|
|
push $remote_head ${options[destination_head]}
|
2023-10-12 21:49:24 +02:00
|
|
|
}
|
|
|
|
|
2023-10-31 23:23:30 +01:00
|
|
|
function set_git_url() {
|
|
|
|
local direction=$1 name=$2 repo=$3
|
2023-10-14 18:00:02 +02:00
|
|
|
local token=${options[${direction}_token]}
|
|
|
|
|
|
|
|
if [[ "$token" =~ ^@ ]] ; then
|
|
|
|
local file=${token##@}
|
|
|
|
(
|
|
|
|
echo -n ${options[${direction}_scheme]}://any:
|
|
|
|
cat $file
|
2023-10-31 23:23:30 +01:00
|
|
|
echo @${options[${direction}_host_port]}/$repo
|
2023-10-14 18:00:02 +02:00
|
|
|
) > $TMPDIR/$direction.git-credentials
|
|
|
|
else
|
2023-10-31 23:23:30 +01:00
|
|
|
echo ${options[${direction}_scheme]}://any:${options[${direction}_token]}@${options[${direction}_host_port]}/$repo > $TMPDIR/$direction.git-credentials
|
2023-10-14 18:00:02 +02:00
|
|
|
fi
|
2023-10-31 23:23:30 +01:00
|
|
|
options[$name]=${options[${direction}_scheme]}://${options[${direction}_host_port]}/$repo
|
2023-10-14 18:00:02 +02:00
|
|
|
}
|
|
|
|
|
2023-10-31 22:25:43 +01:00
|
|
|
function fork_sanity_check() {
|
|
|
|
local fork_repo=${options[destination_fork_repo]}
|
|
|
|
local repo=${options[destination_repo]}
|
|
|
|
if test "$(repository $fork_repo)" != "$(repository $repo)"; then
|
|
|
|
echo "$repo and its fork $fork_repo must have the same repository name (see https://codeberg.org/forgejo/forgejo/issues/1707)"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-11-01 16:18:41 +01:00
|
|
|
function origin_sanity_check() {
|
|
|
|
pr_get_origin
|
|
|
|
}
|
|
|
|
|
2023-10-12 19:13:07 +02:00
|
|
|
function finalize_options() {
|
2023-10-13 19:10:50 +02:00
|
|
|
options[origin_api]=${options[origin_url]}/api/v1/repos/${options[origin_repo]}
|
2023-10-12 21:49:24 +02:00
|
|
|
options[origin_scheme]=$(scheme ${options[origin_url]})
|
2023-10-11 18:05:11 +02:00
|
|
|
options[origin_host_port]=$(host_port ${options[origin_url]})
|
2023-10-31 23:23:30 +01:00
|
|
|
set_git_url origin origin_clone ${options[origin_repo]}
|
2024-01-02 18:39:58 +01:00
|
|
|
set_origin_head
|
2023-10-31 22:25:43 +01:00
|
|
|
|
2023-10-13 19:10:50 +02:00
|
|
|
options[destination_api]=${options[destination_url]}/api/v1/repos/${options[destination_repo]}
|
2023-10-12 21:49:24 +02:00
|
|
|
options[destination_scheme]=$(scheme ${options[destination_url]})
|
2023-10-11 18:05:11 +02:00
|
|
|
options[destination_host_port]=$(host_port ${options[destination_url]})
|
2023-10-31 23:23:30 +01:00
|
|
|
set_git_url destination destination_clone ${options[destination_repo]}
|
2023-10-12 19:13:07 +02:00
|
|
|
options[destination_base]=${options[destination_branch]}
|
2023-10-12 14:57:38 +02:00
|
|
|
: ${options[prefix]:=${options[origin_repo]}}
|
2024-01-02 18:39:58 +01:00
|
|
|
set_destination_head
|
2023-10-31 22:25:43 +01:00
|
|
|
|
|
|
|
if test "${options[destination_fork_repo]}"; then
|
|
|
|
fork_sanity_check
|
|
|
|
options[destination_is_fork]=true
|
2023-10-31 23:23:30 +01:00
|
|
|
set_git_url destination destination_fetch_fork ${options[destination_fork_repo]}
|
2023-10-31 22:25:43 +01:00
|
|
|
options[destination_fork_api]=${options[destination_url]}/api/v1/repos/${options[destination_fork_repo]}
|
2023-10-31 23:23:30 +01:00
|
|
|
else
|
2023-10-31 22:25:43 +01:00
|
|
|
options[destination_is_fork]=false
|
|
|
|
fi
|
|
|
|
|
2024-01-09 19:47:48 +01:00
|
|
|
: ${options[close]:=false}
|
2023-10-12 19:13:07 +02:00
|
|
|
}
|
2023-10-12 15:12:52 +02:00
|
|
|
|
2023-10-12 19:13:07 +02:00
|
|
|
function run() {
|
2023-10-27 15:56:06 +02:00
|
|
|
repo_login origin
|
|
|
|
repo_login destination
|
|
|
|
|
2024-01-02 18:39:58 +01:00
|
|
|
if $(origin_has_pr); then
|
|
|
|
run_origin_pr
|
|
|
|
else
|
|
|
|
run_origin_ref
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function run_origin_ref() {
|
|
|
|
log_info "update or create the cascade branch and PR"
|
|
|
|
checkout
|
|
|
|
update
|
|
|
|
local sha=$(sha_pushed destination)
|
|
|
|
if test "$sha" ; then
|
|
|
|
upsert_destination_pr
|
|
|
|
local status
|
|
|
|
if wait_destination_ci "$sha" ; then
|
|
|
|
log_info "cascade PR status successful"
|
|
|
|
status=0
|
|
|
|
else
|
|
|
|
log_info "cascade PR status failed"
|
|
|
|
status=1
|
|
|
|
fi
|
2024-01-09 19:47:48 +01:00
|
|
|
if "${options[close]}" ; then
|
|
|
|
log_info "close the cascade PR and remove the branch"
|
|
|
|
close_pr
|
|
|
|
fi
|
2024-01-02 18:39:58 +01:00
|
|
|
return $status
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function run_origin_pr() {
|
|
|
|
local state=$(pr_state origin)
|
|
|
|
|
2023-10-13 19:10:50 +02:00
|
|
|
case "$state" in
|
|
|
|
open)
|
|
|
|
log_info "PR is open, update or create the cascade branch and PR"
|
2023-10-31 22:25:43 +01:00
|
|
|
checkout
|
2023-10-13 19:10:50 +02:00
|
|
|
update
|
2023-10-22 18:26:56 +02:00
|
|
|
local sha=$(sha_pushed destination)
|
|
|
|
if test "$sha" ; then
|
|
|
|
upsert_destination_pr
|
|
|
|
comment_origin_pr
|
|
|
|
wait_destination_ci "$sha"
|
|
|
|
fi
|
2023-10-13 19:10:50 +02:00
|
|
|
;;
|
|
|
|
closed)
|
2023-10-13 21:08:52 +02:00
|
|
|
if "$(pr_merged origin)"; then
|
2024-01-09 19:47:48 +01:00
|
|
|
if "${options[close]}" ; then
|
2023-10-24 22:51:30 +02:00
|
|
|
log_info "PR is merged, close the cascade PR and remove the branch"
|
|
|
|
close_pr
|
|
|
|
else
|
|
|
|
log_info "PR was merged, update the cascade PR"
|
|
|
|
pr_get origin
|
|
|
|
pr_get destination
|
2023-10-31 22:25:43 +01:00
|
|
|
checkout
|
2023-10-24 22:51:30 +02:00
|
|
|
update
|
|
|
|
fi
|
2023-10-13 21:08:52 +02:00
|
|
|
else
|
|
|
|
log_info "PR is closed, close the cascade PR and remove the branch"
|
2023-10-24 18:19:30 +02:00
|
|
|
close_pr
|
2023-10-13 21:08:52 +02:00
|
|
|
fi
|
2023-10-13 19:10:50 +02:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
log_info "state '$state', do nothing"
|
|
|
|
;;
|
|
|
|
esac
|
2023-10-11 18:05:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function main() {
|
|
|
|
while true; do
|
|
|
|
case "$1" in
|
|
|
|
--verbose)
|
|
|
|
shift
|
|
|
|
verbose
|
|
|
|
;;
|
|
|
|
--debug)
|
|
|
|
shift
|
|
|
|
debug
|
|
|
|
;;
|
|
|
|
--origin-url)
|
|
|
|
shift
|
|
|
|
options[origin_url]=$1
|
2023-10-12 15:12:52 +02:00
|
|
|
shift
|
2023-10-11 18:05:11 +02:00
|
|
|
;;
|
|
|
|
--origin-repo)
|
|
|
|
shift
|
|
|
|
options[origin_repo]=$1
|
2023-10-12 15:12:52 +02:00
|
|
|
shift
|
2023-10-11 18:05:11 +02:00
|
|
|
;;
|
|
|
|
--origin-token)
|
|
|
|
shift
|
|
|
|
options[origin_token]=$1
|
2023-10-12 15:12:52 +02:00
|
|
|
shift
|
2023-10-11 18:05:11 +02:00
|
|
|
;;
|
2023-10-12 19:13:07 +02:00
|
|
|
--origin-pr)
|
|
|
|
shift
|
|
|
|
options[origin_pr]=$1
|
|
|
|
shift
|
|
|
|
;;
|
2024-01-02 18:39:58 +01:00
|
|
|
--origin-ref)
|
|
|
|
shift
|
|
|
|
options[origin_ref]=$1
|
|
|
|
shift
|
|
|
|
;;
|
2023-10-11 18:05:11 +02:00
|
|
|
--destination-url)
|
|
|
|
shift
|
|
|
|
options[destination_url]=$1
|
2023-10-12 15:12:52 +02:00
|
|
|
shift
|
2023-10-11 18:05:11 +02:00
|
|
|
;;
|
|
|
|
--destination-repo)
|
|
|
|
shift
|
|
|
|
options[destination_repo]=$1
|
2023-10-12 15:12:52 +02:00
|
|
|
shift
|
2023-10-11 18:05:11 +02:00
|
|
|
;;
|
2023-10-31 22:25:43 +01:00
|
|
|
--destination-fork-repo)
|
|
|
|
shift
|
|
|
|
options[destination_fork_repo]=$1
|
|
|
|
shift
|
|
|
|
;;
|
2023-10-11 18:05:11 +02:00
|
|
|
--destination-token)
|
|
|
|
shift
|
|
|
|
options[destination_token]=$1
|
2023-10-12 15:12:52 +02:00
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--destination-branch)
|
|
|
|
shift
|
|
|
|
options[destination_branch]=$1
|
|
|
|
shift
|
2023-10-11 18:05:11 +02:00
|
|
|
;;
|
|
|
|
--update)
|
|
|
|
shift
|
|
|
|
options[update]=$1
|
2023-10-12 15:12:52 +02:00
|
|
|
shift
|
2023-10-11 18:05:11 +02:00
|
|
|
;;
|
2023-10-12 14:57:38 +02:00
|
|
|
--prefix)
|
|
|
|
shift
|
|
|
|
options[prefix]=$1
|
2023-10-12 15:12:52 +02:00
|
|
|
shift
|
|
|
|
;;
|
2024-01-09 19:47:48 +01:00
|
|
|
--close)
|
2023-10-24 22:51:30 +02:00
|
|
|
shift
|
2024-01-09 19:47:48 +01:00
|
|
|
options[close]=$1
|
2023-10-24 22:51:30 +02:00
|
|
|
shift
|
|
|
|
;;
|
2023-10-11 18:05:11 +02:00
|
|
|
*)
|
2023-10-12 19:13:07 +02:00
|
|
|
finalize_options
|
2023-10-11 18:05:11 +02:00
|
|
|
"${1:-run}"
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2023-10-13 14:54:56 +02:00
|
|
|
dependencies
|
|
|
|
|
2023-10-12 19:13:07 +02:00
|
|
|
if echo "${@}" | grep --quiet -e '--debug' ; then
|
|
|
|
main "${@}"
|
|
|
|
else
|
|
|
|
stash_debug "${@}"
|
|
|
|
fi
|