[llvm] Reland pr code format updates (PR #71131)

Aiden Grossman via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 3 23:29:24 PDT 2023


https://github.com/boomanaiden154 updated https://github.com/llvm/llvm-project/pull/71131

>From f56c8c2fd1592d65aff7554c241beecafd42171c Mon Sep 17 00:00:00 2001
From: Aiden Grossman <agrossman154 at yahoo.com>
Date: Mon, 30 Oct 2023 12:23:51 -0700
Subject: [PATCH 1/7] Reland "[Github] Fetch all commits in PR for code
 formatting checks (#69766)"

This commit relands 4aa12afb967bd7c5f051f3b72271f787f1a7538b. This was
originally reverted as it caused fetch failures due to the usage of the
wrong ref. I didn't catch this in testing as it was attempting to check
the branch out from origin, where it didn't exist, but did on my fork as
I never tested any cross-repo PRs.
---
 .github/workflows/pr-code-format.yml | 46 ++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 13 deletions(-)

diff --git a/.github/workflows/pr-code-format.yml b/.github/workflows/pr-code-format.yml
index fa0e69211b14788..4e20bc5d9374788 100644
--- a/.github/workflows/pr-code-format.yml
+++ b/.github/workflows/pr-code-format.yml
@@ -7,17 +7,37 @@ jobs:
   code_formatter:
     runs-on: ubuntu-latest
     steps:
-      - name: Fetch LLVM sources
-        uses: actions/checkout at v4
-        with:
-          fetch-depth: 2 # Fetches only the last 2 commits
-
+      # Get changed files before checking out the repository to force the action
+      # to analyze the diff from the Github API rather than looking at the
+      # shallow clone and erroring out, which is significantly more prone to
+      # failure.
       - name: Get changed files
         id: changed-files
         uses: tj-actions/changed-files at v39
         with:
           separator: ","
-          fetch_depth: 2000 # Fetches only the last 2000 commits
+
+      - name: Calculate number of commits to fetch
+        run: echo "PR_FETCH_DEPTH=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> "${GITHUB_ENV}"
+
+      - name: Fetch PR sources
+        uses: actions/checkout at v4
+        with:
+          ref: ${{ github.event.pull_request.head.sha }}
+          fetch-depth: ${{ env.PR_FETCH_DEPTH }}
+          path: pr-sources
+
+      # We need to make sure that we aren't executing/using any code from the
+      # PR for security reasons as we're using pull_request_target. Checkout
+      # the target branch with the necessary files.
+      - name: Fetch LLVM Sources
+        uses: actions/checkout at v4
+        with:
+          sparse-checkout: |
+            llvm/utils/git/requirements_formatting.txt
+            llvm/utils/git/code-format-helper.py
+          sparse-checkout-cone-mode: false
+          path: llvm-sources
 
       - name: "Listed files"
         run: |
@@ -34,21 +54,21 @@ jobs:
         with:
           python-version: '3.11'
           cache: 'pip'
-          cache-dependency-path: 'llvm/utils/git/requirements_formatting.txt'
+          cache-dependency-path: 'llvm-sources/llvm/utils/git/requirements_formatting.txt'
 
       - name: Install python dependencies
-        run: pip install -r llvm/utils/git/requirements_formatting.txt
+        run: pip install -r llvm-sources/llvm/utils/git/requirements_formatting.txt
 
       - name: Run code formatter
         env:
           GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }}
-          START_REV: ${{ github.event.pull_request.base.sha }}
-          END_REV: ${{ github.event.pull_request.head.sha }}
+          PR_DEPTH: ${{ github.event.pull_request.commits }}
           CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
+        working-directory: ./pr-sources
         run: |
-          python llvm/utils/git/code-format-helper.py \
+          python ../llvm-sources/llvm/utils/git/code-format-helper.py \
             --token ${{ secrets.GITHUB_TOKEN }} \
             --issue-number $GITHUB_PR_NUMBER \
-            --start-rev $START_REV \
-            --end-rev $END_REV \
+            --start-rev HEAD~$PR_DEPTH \
+            --end-rev HEAD \
             --changed-files "$CHANGED_FILES"

>From dbcd677d4057d4048c17aabf2f369705a7a66c2f Mon Sep 17 00:00:00 2001
From: Aiden Grossman <agrossman154 at yahoo.com>
Date: Thu, 2 Nov 2023 00:52:12 -0700
Subject: [PATCH 2/7] Add git log step for debugging

---
 .github/workflows/pr-code-format.yml | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/.github/workflows/pr-code-format.yml b/.github/workflows/pr-code-format.yml
index 4e20bc5d9374788..555081471a7ef7b 100644
--- a/.github/workflows/pr-code-format.yml
+++ b/.github/workflows/pr-code-format.yml
@@ -27,6 +27,13 @@ jobs:
           fetch-depth: ${{ env.PR_FETCH_DEPTH }}
           path: pr-sources
 
+      - name: Print commits being evaluated
+        working-directory: ./pr-sources
+        env:
+          PR_DEPTH: ${{ github.event.pull_request.commits }}
+        run: |
+          git log HEAD~$PR_DEPTH...HEAD
+
       # We need to make sure that we aren't executing/using any code from the
       # PR for security reasons as we're using pull_request_target. Checkout
       # the target branch with the necessary files.

>From 324fcd6d540e95d7a4c1d26126ba13f719fdabde Mon Sep 17 00:00:00 2001
From: Aiden Grossman <agrossman154 at yahoo.com>
Date: Fri, 3 Nov 2023 16:05:58 -0700
Subject: [PATCH 3/7] Check merge commits, only run if commit is mergeable

---
 .github/workflows/pr-code-format.yml | 17 +++--------------
 1 file changed, 3 insertions(+), 14 deletions(-)

diff --git a/.github/workflows/pr-code-format.yml b/.github/workflows/pr-code-format.yml
index 555081471a7ef7b..acc70c0b5131839 100644
--- a/.github/workflows/pr-code-format.yml
+++ b/.github/workflows/pr-code-format.yml
@@ -17,23 +17,13 @@ jobs:
         with:
           separator: ","
 
-      - name: Calculate number of commits to fetch
-        run: echo "PR_FETCH_DEPTH=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> "${GITHUB_ENV}"
-
       - name: Fetch PR sources
         uses: actions/checkout at v4
         with:
-          ref: ${{ github.event.pull_request.head.sha }}
-          fetch-depth: ${{ env.PR_FETCH_DEPTH }}
+          ref: "refs/pull/${{ github.event.number }}/merge"
+          fetch-depth: 2
           path: pr-sources
 
-      - name: Print commits being evaluated
-        working-directory: ./pr-sources
-        env:
-          PR_DEPTH: ${{ github.event.pull_request.commits }}
-        run: |
-          git log HEAD~$PR_DEPTH...HEAD
-
       # We need to make sure that we aren't executing/using any code from the
       # PR for security reasons as we're using pull_request_target. Checkout
       # the target branch with the necessary files.
@@ -69,13 +59,12 @@ jobs:
       - name: Run code formatter
         env:
           GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }}
-          PR_DEPTH: ${{ github.event.pull_request.commits }}
           CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
         working-directory: ./pr-sources
         run: |
           python ../llvm-sources/llvm/utils/git/code-format-helper.py \
             --token ${{ secrets.GITHUB_TOKEN }} \
             --issue-number $GITHUB_PR_NUMBER \
-            --start-rev HEAD~$PR_DEPTH \
+            --start-rev HEAD^ \
             --end-rev HEAD \
             --changed-files "$CHANGED_FILES"

>From 229078fd2713f5e6f28ec5efbe75906a4fedc03c Mon Sep 17 00:00:00 2001
From: Aiden Grossman <agrossman154 at yahoo.com>
Date: Fri, 3 Nov 2023 23:21:56 -0700
Subject: [PATCH 4/7] Add check for mergeability

---
 .github/workflows/pr-code-format.yml | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/.github/workflows/pr-code-format.yml b/.github/workflows/pr-code-format.yml
index acc70c0b5131839..70078b94eb31fef 100644
--- a/.github/workflows/pr-code-format.yml
+++ b/.github/workflows/pr-code-format.yml
@@ -7,15 +7,9 @@ jobs:
   code_formatter:
     runs-on: ubuntu-latest
     steps:
-      # Get changed files before checking out the repository to force the action
-      # to analyze the diff from the Github API rather than looking at the
-      # shallow clone and erroring out, which is significantly more prone to
-      # failure.
-      - name: Get changed files
-        id: changed-files
-        uses: tj-actions/changed-files at v39
-        with:
-          separator: ","
+      - name: Check if PR is mergeable
+        run: |
+          gh api https://api.github.com/repos/${{ github.event.repository }}/pulls/${{ github.event.number }} | jq -e 'if (.mergeable == true) then true else false end'
 
       - name: Fetch PR sources
         uses: actions/checkout at v4
@@ -24,6 +18,14 @@ jobs:
           fetch-depth: 2
           path: pr-sources
 
+      - name: Get changed files
+        id: changed-files
+        uses: tj-actions/changed-files at v39
+        with:
+          separator: ","
+          base_sha: HEAD^
+          sha: HEAD
+
       # We need to make sure that we aren't executing/using any code from the
       # PR for security reasons as we're using pull_request_target. Checkout
       # the target branch with the necessary files.

>From 16194858f9c3cfdb12771e75df2c844d5ce91c33 Mon Sep 17 00:00:00 2001
From: Aiden Grossman <agrossman154 at yahoo.com>
Date: Fri, 3 Nov 2023 23:23:42 -0700
Subject: [PATCH 5/7] setup gh token

---
 .github/workflows/pr-code-format.yml | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/.github/workflows/pr-code-format.yml b/.github/workflows/pr-code-format.yml
index 70078b94eb31fef..daca6ad58fff63b 100644
--- a/.github/workflows/pr-code-format.yml
+++ b/.github/workflows/pr-code-format.yml
@@ -8,6 +8,8 @@ jobs:
     runs-on: ubuntu-latest
     steps:
       - name: Check if PR is mergeable
+        env:
+          GH_TOKEN: ${{ github.token }}
         run: |
           gh api https://api.github.com/repos/${{ github.event.repository }}/pulls/${{ github.event.number }} | jq -e 'if (.mergeable == true) then true else false end'
 

>From b2fb75613b5233e02cc30a2deefc5303a2268b94 Mon Sep 17 00:00:00 2001
From: Aiden Grossman <agrossman154 at yahoo.com>
Date: Fri, 3 Nov 2023 23:25:13 -0700
Subject: [PATCH 6/7] Set path for files changed

---
 .github/workflows/pr-code-format.yml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/.github/workflows/pr-code-format.yml b/.github/workflows/pr-code-format.yml
index daca6ad58fff63b..f06c8ed40f6ba35 100644
--- a/.github/workflows/pr-code-format.yml
+++ b/.github/workflows/pr-code-format.yml
@@ -27,6 +27,7 @@ jobs:
           separator: ","
           base_sha: HEAD^
           sha: HEAD
+          path: ./pr-sources
 
       # We need to make sure that we aren't executing/using any code from the
       # PR for security reasons as we're using pull_request_target. Checkout

>From 0476d45d85a9e2f869722b8fdb745b6d5790eba7 Mon Sep 17 00:00:00 2001
From: Aiden Grossman <agrossman154 at yahoo.com>
Date: Fri, 3 Nov 2023 23:29:10 -0700
Subject: [PATCH 7/7] Fix repository reference

---
 .github/workflows/pr-code-format.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/pr-code-format.yml b/.github/workflows/pr-code-format.yml
index f06c8ed40f6ba35..7e66268eb49d211 100644
--- a/.github/workflows/pr-code-format.yml
+++ b/.github/workflows/pr-code-format.yml
@@ -11,7 +11,7 @@ jobs:
         env:
           GH_TOKEN: ${{ github.token }}
         run: |
-          gh api https://api.github.com/repos/${{ github.event.repository }}/pulls/${{ github.event.number }} | jq -e 'if (.mergeable == true) then true else false end'
+          gh api https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.number }} | jq -e 'if (.mergeable == true) then true else false end'
 
       - name: Fetch PR sources
         uses: actions/checkout at v4



More information about the llvm-commits mailing list