1
0
Fork 0
mirror of https://code.forgejo.org/actions/cascading-pr synced 2025-03-15 06:46:59 +01:00

use sha for origin instead of head

because the head ref may be either a branch or a reference, depending
on wether the PR was merged and the branch deleted. The SHA (either
from the head or the merged commit) always represents the most up to
date state of the PR.
This commit is contained in:
Earl Warren 2023-10-24 18:19:30 +02:00
parent 68cd0972e4
commit 5c0fc66c82
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00

View file

@ -100,7 +100,7 @@ EOF
} }
function close_pr() { function close_pr() {
local direction=$1 local direction=destination
if test "$(pr_state ${direction})" = "open"; then if test "$(pr_state ${direction})" = "open"; then
log_info "closing $(pr_url ${direction})" log_info "closing $(pr_url ${direction})"
@ -147,9 +147,14 @@ function pr_number() {
pr $1 | jq --raw-output .number pr $1 | jq --raw-output .number
} }
function pr_head() { function pr_sha() {
pr_get $1 pr_get $1
pr $1 | jq --raw-output .head.ref 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
} }
function pr_merged() { function pr_merged() {
@ -158,19 +163,20 @@ function pr_merged() {
} }
function upsert_clone() { function upsert_clone() {
local direction=$1 branch=$2 clone=$3 local direction=$1 ref="$2" sha="$3" clone=$4
local fetch=true
if ! test -d $TMPDIR/$direction; then if ! test -d $TMPDIR/$direction; then
git -c credential.helper="store --file=$TMPDIR/$direction.git-credentials" clone $clone -b $branch $TMPDIR/$direction git -c credential.helper="store --file=$TMPDIR/$direction.git-credentials" clone $clone $TMPDIR/$direction
fetch=false
fi fi
( (
cd $TMPDIR/$direction cd $TMPDIR/$direction
git config credential.helper "store --file=$TMPDIR/$direction.git-credentials" if test "$ref"; then
if $fetch; then git switch -c $direction origin/$ref
git fetch origin
fi fi
if test "$sha"; then
git switch -c $direction $sha
fi
git config credential.helper "store --file=$TMPDIR/$direction.git-credentials"
git config user.email cascading-pr@example.com git config user.email cascading-pr@example.com
git config user.name cascading-pr git config user.name cascading-pr
) )
@ -190,7 +196,7 @@ function push() {
cd $TMPDIR/$direction cd $TMPDIR/$direction
git add . git add .
if git commit -m 'cascading-pr update'; then if git commit -m 'cascading-pr update'; then
git push --force origin $branch git push --force origin $direction:$branch
git rev-parse HEAD > ../$direction.sha git rev-parse HEAD > ../$direction.sha
log_info "pushed" log_info "pushed"
else else
@ -206,8 +212,8 @@ function wait_destination_ci() {
} }
function update() { function update() {
upsert_clone origin ${options[origin_head]} ${options[origin_clone]} upsert_clone origin "${options[origin_head]}" "${options[origin_sha]}" ${options[origin_clone]}
upsert_clone destination ${options[destination_head]} ${options[destination_clone]} upsert_clone destination "${options[destination_head]}" "${options[destination_sha]}" ${options[destination_clone]}
( (
cd $TMPDIR/origin cd $TMPDIR/origin
${options[update]} $TMPDIR/destination $TMPDIR/destination-pr.json $TMPDIR/origin-pr.json ${options[update]} $TMPDIR/destination $TMPDIR/destination-pr.json $TMPDIR/origin-pr.json
@ -237,7 +243,8 @@ function finalize_options() {
options[origin_scheme]=$(scheme ${options[origin_url]}) options[origin_scheme]=$(scheme ${options[origin_url]})
options[origin_host_port]=$(host_port ${options[origin_url]}) options[origin_host_port]=$(host_port ${options[origin_url]})
set_clone origin set_clone origin
options[origin_head]=$(pr_head origin) options[origin_head]=
options[origin_sha]=$(pr_sha origin)
options[destination_api]=${options[destination_url]}/api/v1/repos/${options[destination_repo]} options[destination_api]=${options[destination_url]}/api/v1/repos/${options[destination_repo]}
options[destination_scheme]=$(scheme ${options[destination_url]}) options[destination_scheme]=$(scheme ${options[destination_url]})
options[destination_host_port]=$(host_port ${options[destination_url]}) options[destination_host_port]=$(host_port ${options[destination_url]})
@ -245,6 +252,7 @@ function finalize_options() {
options[destination_base]=${options[destination_branch]} options[destination_base]=${options[destination_branch]}
: ${options[prefix]:=${options[origin_repo]}} : ${options[prefix]:=${options[origin_repo]}}
options[destination_head]=${options[prefix]}-${options[origin_pr]} options[destination_head]=${options[prefix]}-${options[origin_pr]}
options[destination_sha]=
} }
function run() { function run() {
@ -274,7 +282,7 @@ function run() {
else else
log_info "PR is closed, close the cascade PR and remove the branch" log_info "PR is closed, close the cascade PR and remove the branch"
repo_login ${options[destination_repo]} repo_login ${options[destination_repo]}
close_pr destination close_pr
fi fi
;; ;;
*) *)