Skip to content

Commit

Permalink
fix(#529): 🐛 edge case with branch cleanup and force push (#534)
Browse files Browse the repository at this point in the history
* fix(#529): 🐛 edge case with branch cleanup and force push

---------

Signed-off-by: Andy Augustin <[email protected]>
  • Loading branch information
AndreasAugustin committed Jun 16, 2024
1 parent 78f7bb7 commit ab9498f
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/sync_template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,15 @@ 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"

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"
Expand All @@ -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
Expand Down Expand Up @@ -321,19 +331,20 @@ function create_pr() {
# Arguments:
# title
# body
# branch
# upstream_branch
# labels
# reviewers
###################################
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}" \
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit ab9498f

Please sign in to comment.