about summary refs log tree commit diff
path: root/.github/workflows
diff options
context:
space:
mode:
authorSilvan Mosberger <silvan.mosberger@tweag.io>2023-10-18 01:12:06 +0200
committerSilvan Mosberger <silvan.mosberger@tweag.io>2023-10-18 01:59:47 +0200
commit785b8ca2e73c02cd085991dfdebc3a404288417b (patch)
tree1b9c75d985a7ebac25509005ed8b9043236c4dec /.github/workflows
parenta8840a945f9ea405928f67dcc52bfb3ff94c2b11 (diff)
workflows/check-by-name: Improved mergeability check
Diffstat (limited to '.github/workflows')
-rw-r--r--.github/workflows/check-by-name.yml32
1 files changed, 26 insertions, 6 deletions
diff --git a/.github/workflows/check-by-name.yml b/.github/workflows/check-by-name.yml
index 9c46ecea6916b..c6cd142bfa619 100644
--- a/.github/workflows/check-by-name.yml
+++ b/.github/workflows/check-by-name.yml
@@ -18,14 +18,34 @@ jobs:
     runs-on: ubuntu-latest
     steps:
       - name: Resolving the merge commit
+        env:
+          GH_TOKEN: ${{ github.token }}
         run: |
-          if result=$(git ls-remote --exit-code ${{ github.event.pull_request.base.repo.clone_url }} refs/pull/${{ github.event.pull_request.number }}/merge 2>&1); then
-            mergedSha=$(cut -f1 <<< "$result")
-            echo "The PR appears to not have any conflicts, checking the merge commit $mergedSha"
+          # This checks for mergeability of a pull request as recommended in
+          # https://docs.github.com/en/rest/guides/using-the-rest-api-to-interact-with-your-git-database?apiVersion=2022-11-28#checking-mergeability-of-pull-requests
+          while true; do
+            echo "Checking whether the pull request can be merged"
+            prInfo=$(gh api \
+              -H "Accept: application/vnd.github+json" \
+              -H "X-GitHub-Api-Version: 2022-11-28" \
+              /repos/"$GITHUB_REPOSITORY"/pulls/${{ github.event.pull_request.number }})
+            mergeable=$(jq -r .mergeable <<< "$prInfo")
+            mergedSha=$(jq -r .merge_commit_sha <<< "$prInfo")
+
+            if [[ "$mergeable" == "null" ]]; then
+              # null indicates that GitHub is still computing whether it's mergeable
+              # Wait a couple seconds before trying again
+              echo "GitHub is still computing whether this PR can be merged, waiting 5 seconds before trying again"
+              sleep 5
+            else
+              break
+            fi
+          done
+
+          if [[ "$mergeable" == "true" ]]; then
+            echo "The PR can be merged, checking the merge commit $mergedSha"
           else
-            echo "The PR may have a merge conflict"
-            echo "'git ls-remote' output was:"
-            echo "$result"
+            echo "The PR cannot be merged, it has a merge conflict"
             exit 1
           fi
           echo "mergedSha=$mergedSha" >> "$GITHUB_ENV"