From 89c8f08d578bf7c00e3d447ed16fcee4346f561f Mon Sep 17 00:00:00 2001 From: Andy Augustin Date: Sun, 9 Jun 2024 21:43:12 +0200 Subject: [PATCH 1/3] fix(#529): :bug: edge case with branch cleanup and force push Signed-off-by: Andy Augustin --- src/sync_template.sh | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/sync_template.sh b/src/sync_template.sh index e4fb7680..2402ef9a 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 remot 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 @@ -467,7 +477,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" From 104d1497fce9aa8d73b4410bdcac2106d6a0e6b3 Mon Sep 17 00:00:00 2001 From: Andy Augustin Date: Sun, 9 Jun 2024 21:43:12 +0200 Subject: [PATCH 2/3] fix(#529): :bug: edge case with branch cleanup and force push Signed-off-by: Andy Augustin --- src/sync_template.sh | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/sync_template.sh b/src/sync_template.sh index e4fb7680..9d2bbf06 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 @@ -467,7 +477,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" From 70d9429905980f2098e06a79c1674d765c2ef1df Mon Sep 17 00:00:00 2001 From: Andy Augustin Date: Sun, 16 Jun 2024 13:31:23 +0200 Subject: [PATCH 3/3] fix: :bug: issue with gh pr edit Signed-off-by: Andy Augustin --- src/sync_template.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/sync_template.sh b/src/sync_template.sh index 9d2bbf06..c7b1faae 100755 --- a/src/sync_template.sh +++ b/src/sync_template.sh @@ -331,7 +331,7 @@ function create_pr() { # Arguments: # title # body -# branch +# upstream_branch # labels # reviewers ################################### @@ -339,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}" \