From df5e8cf0d77b0b9b9baa28f9133ad8f3f127beda Mon Sep 17 00:00:00 2001
From: Andrea Frittoli <andrea.frittoli@uk.ibm.com>
Date: Thu, 5 Dec 2024 18:30:16 +0000
Subject: [PATCH] Fix getting the commit sha from comment event

The comment event does not include the commit sha directly.
Extract the pull_url, query that and get the head sha.

This is for the "/retest" chatops command.

Signed-off-by: Andrea Frittoli <andrea.frittoli@uk.ibm.com>
---
 .github/workflows/chatops_retest.yaml | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/.github/workflows/chatops_retest.yaml b/.github/workflows/chatops_retest.yaml
index 01f3388c6b4..1a16ed0f60e 100644
--- a/.github/workflows/chatops_retest.yaml
+++ b/.github/workflows/chatops_retest.yaml
@@ -29,11 +29,20 @@ jobs:
       run: 'cat $GITHUB_EVENT_PATH || true'
     - name: Rerun Failed Actions
       run: |
+        echo '::group:: Get the PT commit sha'
+        # Get the sha of the HEAD commit in the PR
+        GITHUB_COMMIT_SHA=$(gh api $(echo ${GITHUB_PULL_URL#https://api.github.com/}) | \
+          | jq -r .head.sha)
+        echo '::endgroup::'
+
+        echo '::group:: Get the list of run IDs'
         # Get a list of run IDs
         RUN_IDS=$(gh api repos/${GITHUB_REPO}/commits/${GITHUB_COMMIT_SHA}/check-runs | \
             jq -r '.check_runs[] | select(.name != "Rerun Failed Actions") | .html_url | capture("/runs/(?<number>[0-9]+)/job") | .number' | \
             sort -u)
+        echo '::endgroup::'
 
+        echo '::group:: Rerun failed runs'
         # For each run, retrigger faild jobs
         for runid in ${RUN_IDS}; do
             gh run \
@@ -41,10 +50,11 @@ jobs:
                 rerun ${runid} \
                 --failed
         done
+        echo '::endgroup::'
       env:
         GITHUB_TOKEN: ${{ secrets.CHATOPS_TOKEN }}
         GITHUB_REPO: ${{ github.event.client_payload.github.payload.repository.full_name }}
-        GITHUB_COMMIT_SHA: ${{ github.event.client_payload.github.payload.commit.sha }}
+        GITHUB_PULL_URL: ${{ github.event.client_payload.github.payload.issue.pull_request.url }}
 
     - name: Create comment
       if: ${{ failure() && steps.landStack.outcome == 'failure' }}