From a9ac74047e8f13107b70eb9d24829d3ed24d10f6 Mon Sep 17 00:00:00 2001
From: Uladzimir Havenchyk <125459446+uladzimirdev@users.noreply.github.com>
Date: Thu, 15 Aug 2024 00:29:23 +0300
Subject: [PATCH] ci(manual backport): improve logic for searching commit that
 wasn't merged correctly (#46729)

* ci(manual backport): improve logic for searching commit that wasn't merged correctly

* fixup
---
 .github/workflows/backport.yml | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/.github/workflows/backport.yml b/.github/workflows/backport.yml
index b914ff3acc1..9e5f241c1a9 100644
--- a/.github/workflows/backport.yml
+++ b/.github/workflows/backport.yml
@@ -107,8 +107,23 @@ jobs:
             echo "PR has been merged, searching for a squashed commit in the base branch"
             echo "searching for a commit in a ${ORIGINAL_BASE_REF} that contains pull request number ${ORIGINAL_PULL_REQUEST_NUMBER}"
             SQUASHED_COMMIT=$(env -i git log ${ORIGINAL_BASE_REF} --grep="(#${ORIGINAL_PULL_REQUEST_NUMBER})" --format="%H")
-            echo "found commit ${SQUASHED_COMMIT}"
-            git cherry-pick ${SQUASHED_COMMIT}
+
+            if [ -n "${SQUASHED_COMMIT}" ]; then
+              echo "found commit ${SQUASHED_COMMIT}"
+              git cherry-pick ${SQUASHED_COMMIT}
+            else
+              echo "probably a PR wasn't merged with Squash And Merge button, searching again"
+              SQUASHED_COMMIT=$(env -i git log ${ORIGINAL_BASE_REF} --grep="#${ORIGINAL_PULL_REQUEST_NUMBER}" --format="%H")
+
+              if [ -n "${SQUASHED_COMMIT}" ]; then
+                echo "found commit ${SQUASHED_COMMIT}"
+                git cherry-pick ${SQUASHED_COMMIT}
+              else
+                echo "No squashed commit found for PR #${ORIGINAL_PULL_REQUEST_NUMBER}"
+                exit 1
+              fi
+            fi
+
           else
             echo "PR has not been merged, copying all commits"
             git cherry-pick ${ORIGINAL_BASE_SHA}..${ORIGINAL_HEAD_SHA}
-- 
GitLab