From 2fb7aaf7f16e4a2c0b8c5f142f27198502f1d3d8 Mon Sep 17 00:00:00 2001
From: Antoine du Hamel <duhamelantoine1995@gmail.com>
Date: Thu, 16 Dec 2021 01:57:59 +0100
Subject: [PATCH] tools: fix GitHub Actions status when CQ is empty

Refs: https://github.com/nodejs/node/pull/40985#issuecomment-995252065
---
 .github/workflows/auto-start-ci.yml | 24 ++++++++++++++++++----
 .github/workflows/commit-queue.yml  | 32 ++++++++++++++++++++---------
 2 files changed, 42 insertions(+), 14 deletions(-)

diff --git a/.github/workflows/auto-start-ci.yml b/.github/workflows/auto-start-ci.yml
index 11ebd15d85ec5a..4d2cb12497cfd6 100644
--- a/.github/workflows/auto-start-ci.yml
+++ b/.github/workflows/auto-start-ci.yml
@@ -12,9 +12,27 @@ env:
   NODE_VERSION: lts/*
 
 jobs:
-  startCI:
+  get_prs_for_ci:
     if: github.repository == 'nodejs/node'
     runs-on: ubuntu-latest
+    outputs:
+      numbers: ${{ steps.get_prs_for_ci.outputs.numbers }}
+    steps:
+      - name: Get Pull Requests
+        id: get_prs_for_ci
+        run: >
+          gh pr list \
+                  --repo ${{ github.repository }} \
+                  --label 'request-ci' \
+                  --json 'number' \
+                  -t '::set-output name=numbers::{{ range . }}{{ .number }} {{ end }}' \
+                  --limit 100
+        env:
+          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+  startCI:
+    needs: get_prs_for_ci
+    if: needs.get_prs_for_ci.outputs.numbers != ''
+    runs-on: ubuntu-latest
     steps:
       - uses: actions/checkout@v2
         with:
@@ -37,8 +55,6 @@ jobs:
           ncu-config set repo "$(echo ${{ github.repository }} | cut -d/ -f2)"
 
       - name: Start the CI
-        run: |
-          PRs=$(gh pr list --label 'request-ci' --json number --jq 'map(.number) | .[]' --limit 100)
-          ./tools/actions/start-ci.sh "$PRs"
+        run: ./tools/actions/start-ci.sh "${{ needs.get_prs_for_ci.outputs.numbers }}"
         env:
           GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
diff --git a/.github/workflows/commit-queue.yml b/.github/workflows/commit-queue.yml
index dbaf55dafc34fa..4ee050a2786db8 100644
--- a/.github/workflows/commit-queue.yml
+++ b/.github/workflows/commit-queue.yml
@@ -17,9 +17,28 @@ env:
   NODE_VERSION: lts/*
 
 jobs:
-  commitQueue:
+  get_mergeable_prs:
     if: github.repository == 'nodejs/node'
     runs-on: ubuntu-latest
+    outputs:
+      numbers: ${{ steps.get_mergeable_prs.outputs.numbers }}
+    steps:
+      - name: Get Pull Requests
+        id: get_mergeable_prs
+        run: >
+          gh pr list \
+                  --repo ${{ github.repository }} \
+                  --base ${{ github.ref_name }} \
+                  --label 'commit-queue' \
+                  --json 'number' \
+                  -t '::set-output name=numbers::{{ range . }}{{ .number }} {{ end }}' \
+                  --limit 100
+        env:
+          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+  commitQueue:
+    needs: get_mergeable_prs
+    if: needs.get_mergeable_prs.outputs.numbers != ''
+    runs-on: ubuntu-latest
     steps:
       - uses: actions/checkout@v2
         with:
@@ -44,11 +63,10 @@ jobs:
         run: |
           echo "REPOSITORY=$(echo ${{ github.repository }} | cut -d/ -f2)" >> $GITHUB_ENV
           echo "OWNER=${{ github.repository_owner }}" >> $GITHUB_ENV
-          echo "DEFAULT_BRANCH=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV
 
       - name: Configure node-core-utils
         run: |
-          ncu-config set branch ${DEFAULT_BRANCH}
+          ncu-config set branch ${GITHUB_REF_NAME}
           ncu-config set upstream origin
           ncu-config set username "${{ secrets.GH_USER_NAME }}"
           ncu-config set token "${{ secrets.GH_USER_TOKEN }}"
@@ -57,12 +75,6 @@ jobs:
           ncu-config set owner "${OWNER}"
 
       - name: Start the Commit Queue
-        run: |
-          PRs=$(gh pr list \
-                  --base ${{ env.DEFAULT_BRANCH }} \
-                  --label 'commit-queue' \
-                  --json number --jq 'map(.number) | .[]' \
-                  --limit 100)
-          ./tools/actions/commit-queue.sh ${{ env.OWNER }} ${{ env.REPOSITORY }} "$PRs"
+        run: ./tools/actions/commit-queue.sh ${{ env.OWNER }} ${{ env.REPOSITORY }} "${{ needs.get_mergeable_prs.outputs.numbers }}"
         env:
           GITHUB_TOKEN: ${{ secrets.GH_USER_TOKEN }}