cascading-pr/cascading-pr.sh

371 lines
9.1 KiB
Bash
Raw Normal View History

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() {
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 exists_branch() {
2023-10-22 17:45:18 +02:00
local direction=$1
repo_curl ${options[${direction}_repo]} api_json ${options[${direction}_api]}/branches/${options[${direction}_head]} >& /dev/null
}
function delete_branch() {
2023-10-22 17:45:18 +02:00
local direction=$1
if ! $(exists_branch $direction) ; then
log_info "branch ${options[${direction}_head]} does not exists"
return
fi
2023-10-22 17:45:18 +02:00
repo_curl ${options[${direction}_repo]} api_json -X DELETE ${options[${direction}_api]}/branches/${options[${direction}_head]}
log_info "branch ${options[${direction}_head]} deleted"
}
function pr_origin_comment_body() {
echo "cascading-pr updated at ${options[destination_url]}/${options[destination_repo]}/pulls/$(pr_number destination)"
}
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-22 17:45:18 +02:00
function upsert_destination_branch() {
if $(exists_branch destination) ; then
2023-10-12 19:13:07 +02:00
log_info "branch ${options[destination_head]} already exists"
return
fi
2023-10-22 17:15:02 +02:00
cat > $TMPDIR/data <<EOF
{
"new_branch_name":"${options[destination_head]}",
"old_branch_name":"${options[destination_base]}"
}
EOF
repo_curl ${options[destination_repo]} api_json --data @$TMPDIR/data ${options[destination_api]}/branches
2023-10-12 19:13:07 +02:00
log_info "branch ${options[destination_head]} created"
}
function pr_destination_title() {
echo "cascading-pr from ${options[origin_url]}/${options[origin_repo]}/pulls/${options[origin_pr]}"
}
2023-10-22 17:31:07 +02:00
function pr_destination_body() {
echo "cascading-pr from ${options[origin_url]}/${options[origin_repo]}/pulls/${options[origin_pr]}"
}
2023-10-22 17:45:18 +02:00
function upsert_destination_pr() {
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
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]}",
"head":"${options[destination_head]}"
}
EOF
retry repo_curl ${options[destination_repo]} api_json --data @$TMPDIR/data ${options[destination_api]}/pulls > $TMPDIR/destination-pr.json
log_info "PR created $(pr_url destination)"
}
function close_pr() {
local direction=destination
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
2023-10-22 17:45:18 +02:00
delete_branch ${direction}
else
log_info "no open PR found"
fi
2023-10-12 19:13:07 +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
}
function pr_get() {
local direction=$1
if ! test -f $TMPDIR/${direction}-pr.json; then
pr_get_$direction
fi
}
function pr() {
cat $TMPDIR/$1-pr.json
}
function pr_state() {
pr_get $1
pr $1 | jq --raw-output .state
}
function pr_url() {
pr_get $1
pr $1 | jq --raw-output .url
}
function pr_number() {
pr_get $1
pr $1 | jq --raw-output .number
}
function pr_sha() {
pr_get $1
merged=$(pr $1 | jq --raw-output .merged)
if "$merged"; then
pr $1 | jq --raw-output .merge_commit_sha
else
pr $1 | jq --raw-output .head.sha
fi
2023-10-12 23:02:27 +02:00
}
2023-10-13 21:08:52 +02:00
function pr_merged() {
pr_get $1
2023-10-13 21:08:52 +02:00
pr $1 | jq --raw-output .merged
}
2023-10-12 21:49:24 +02:00
function upsert_clone() {
local direction=$1 ref="$2" sha="$3" clone=$4
2023-10-12 21:49:24 +02:00
if ! test -d $TMPDIR/$direction; then
git -c credential.helper="store --file=$TMPDIR/$direction.git-credentials" clone $clone $TMPDIR/$direction
2023-10-12 21:49:24 +02:00
fi
(
cd $TMPDIR/$direction
if test "$ref"; then
git switch -c $direction origin/$ref
fi
if test "$sha"; then
git switch -c $direction $sha
2023-10-12 21:49:24 +02:00
fi
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
)
}
function sha_pushed() {
local direction=$1
if test -f $TMPDIR/$direction.sha ; then
cat $TMPDIR/$direction.sha
fi
}
2023-10-12 21:49:24 +02:00
function push() {
local direction=$1 branch=$2 clone=$3
(
cd $TMPDIR/$direction
2023-10-12 23:24:11 +02:00
git add .
if git commit -m 'cascading-pr update'; then
git push --force origin $direction:$branch
git rev-parse HEAD > ../$direction.sha
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
}
function update() {
upsert_clone origin "${options[origin_head]}" "${options[origin_sha]}" ${options[origin_clone]}
upsert_clone destination "${options[destination_head]}" "${options[destination_sha]}" ${options[destination_clone]}
2023-10-12 21:49:24 +02:00
(
cd $TMPDIR/origin
2023-10-13 21:08:52 +02:00
${options[update]} $TMPDIR/destination $TMPDIR/destination-pr.json $TMPDIR/origin-pr.json
2023-10-12 21:49:24 +02:00
)
2023-10-12 23:24:11 +02:00
push destination ${options[destination_head]} ${options[destination_clone]}
2023-10-12 21:49:24 +02:00
}
function set_clone() {
local direction=$1
local token=${options[${direction}_token]}
if [[ "$token" =~ ^@ ]] ; then
local file=${token##@}
(
echo -n ${options[${direction}_scheme]}://any:
cat $file
echo @${options[${direction}_host_port]}/${options[${direction}_repo]}
) > $TMPDIR/$direction.git-credentials
else
echo ${options[${direction}_scheme]}://any:${options[${direction}_token]}@${options[${direction}_host_port]}/${options[${direction}_repo]} > $TMPDIR/$direction.git-credentials
fi
options[${direction}_clone]=${options[${direction}_scheme]}://${options[${direction}_host_port]}/${options[${direction}_repo]}
}
2023-10-12 19:13:07 +02:00
function finalize_options() {
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]})
set_clone origin
options[origin_head]=
options[origin_sha]=$(pr_sha origin)
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]})
set_clone destination
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]}}
2023-10-12 19:13:07 +02:00
options[destination_head]=${options[prefix]}-${options[origin_pr]}
options[destination_sha]=
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() {
local state=$(pr_state origin)
case "$state" in
open)
log_info "PR is open, update or create the cascade branch and PR"
repo_login ${options[destination_repo]}
2023-10-22 17:45:18 +02:00
upsert_destination_branch
update
local sha=$(sha_pushed destination)
if test "$sha" ; then
upsert_destination_pr
repo_login ${options[origin_repo]}
comment_origin_pr
wait_destination_ci "$sha"
fi
;;
closed)
2023-10-13 21:08:52 +02:00
if "$(pr_merged origin)"; then
log_info "PR was merged, update the cascade PR"
repo_login ${options[destination_repo]}
pr_get origin
pr_get destination
2023-10-13 21:08:52 +02:00
update
else
log_info "PR is closed, close the cascade PR and remove the branch"
repo_login ${options[destination_repo]}
close_pr
2023-10-13 21:08:52 +02:00
fi
;;
*)
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
;;
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
;;
--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
;;
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