From ab9498f63d0cf03e677228c6a2f6828adf8b4662 Mon Sep 17 00:00:00 2001 From: andy Augustin Date: Sun, 16 Jun 2024 13:38:18 +0200 Subject: [PATCH] fix(#529): :bug: edge case with branch cleanup and force push (#534) * fix(#529): :bug: edge case with branch cleanup and force push --------- Signed-off-by: Andy Augustin --- src/sync_template.sh | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/sync_template.sh b/src/sync_template.sh index e4fb768..c7b1faa 100755 --- a/src/sync_template.sh +++ b/src/sync_template.sh @@ -178,6 +178,7 @@ function force_delete_files() { # upstream_branch # pr_labels # is_keep_branch_on_pr_cleanup +# local_branch_name ####################################### function cleanup_older_prs () { info "cleanup older prs" @@ -185,6 +186,7 @@ function cleanup_older_prs () { local upstream_branch=$1 local pr_labels=$2 local is_keep_branch_on_pr_cleanup=$3 + local local_branch_name=$4 if [[ -z "${pr_labels}" ]]; then warn "env var 'PR_LABELS' is empty. Skipping older prs cleanup" @@ -195,16 +197,24 @@ function cleanup_older_prs () { --base "${upstream_branch}" \ --state open \ --label "${pr_labels}" \ - --json number \ - --template '{{range .}}{{printf "%v" .number}}{{"\n"}}{{end}}') + --json number,headRefName \ + --jq '.[]') for older_pr in $older_prs do + branch_name=$(echo "$older_pr" | jq -r .headRefName) + pr_number=$(echo "$older_pr" | jq -r .number) + + if [ "$branch_name" == "$local_branch_name" ] ; then + warn "local branch name equals remote pr branch name ${local_branch_name}. Skipping pr cleanup for this branch" + continue + fi + if [ "$is_keep_branch_on_pr_cleanup" == true ] ; then - gh pr close -c "[actions-template-sync] :construction_worker: automatically closed because there is a new open PR. Branch is kept alive" "$older_pr" + gh pr close -c "[actions-template-sync] :construction_worker: automatically closed because there is a new open PR. Branch is kept alive" "$pr_number" debug "Closed PR #${older_pr} but kept the branch" else - gh pr close -c "[actions-template-sync] :construction_worker: automatically closed because there is a new open PR" -d "$older_pr" + gh pr close -c "[actions-template-sync] :construction_worker: automatically closed because there is a new open PR" -d "$pr_number" debug "Closed PR #${older_pr}" fi done @@ -321,7 +331,7 @@ function create_pr() { # Arguments: # title # body -# branch +# upstream_branch # labels # reviewers ################################### @@ -329,11 +339,12 @@ function create_or_edit_pr() { info "create pr or edit the pr" local title=$1 local body=$2 - local branch=$3 + local upstream_branch=$3 local labels=$4 local reviewers=$5 + local pr_branch=$6 - create_pr "${title}" "${body}" "${branch}" "${labels}" "${reviewers}" || gh pr edit \ + create_pr "${title}" "${body}" "${upstream_branch}" "${labels}" "${reviewers}" || gh pr edit \ --title "${title}" \ --body "${body}" \ --add-label "${labels}" \ @@ -467,7 +478,7 @@ function arr_prepare_pr_create_pr() { warn "env var 'PR_LABELS' is empty. Skipping older prs cleanup" else cmd_from_yml "precleanup" - cleanup_older_prs "${UPSTREAM_BRANCH}" "${PR_LABELS}" "${IS_KEEP_BRANCH_ON_PR_CLEANUP}" + cleanup_older_prs "${UPSTREAM_BRANCH}" "${PR_LABELS}" "${IS_KEEP_BRANCH_ON_PR_CLEANUP}" "${PR_BRANCH}" fi else warn "is_pr_cleanup option is set to off. Skipping older prs cleanup"