Skip to content

Commit df3cd38

Browse files
authored
Merge pull request Expensify#62993 from MacroLens/fix_62656_lint_changed
[NO QA] Fix 62656 lint changed
2 parents 4db7a1f + de2156f commit df3cd38

3 files changed

Lines changed: 75 additions & 5 deletions

File tree

.github/workflows/lint-changed.yml

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,47 @@ jobs:
1717
if: ${{ github.actor != 'OSBotify' || github.event_name == 'workflow_call' }}
1818
runs-on: ubuntu-latest
1919
steps:
20+
- name: Count commits between merge base and HEAD
21+
id: count
22+
run: |
23+
# Compare the base commit and HEAD to get the count of commits after base
24+
# up to and including HEAD. Add 2 to this total when fetching history.
25+
#
26+
# When opening a PR, GitHub will create a merge commit that squashes
27+
# your changes for use in the workflow. So your history locally
28+
# may look like
29+
# o---o---o---B - development
30+
# /
31+
# ---o---1---C---o---o---A - main
32+
# Note that the `1` is the merge-base of development and main. The
33+
# number of commits after `1` upto and including `A` is 4.
34+
# In the workflow, GitHub squashs development, so it becomes:
35+
# ---o---1---C---o---o---A - main
36+
# \--M - PR branch
37+
# Where `M`` is the new squashed commit. To fetch enough history to
38+
# include the merge base, we need to fetch `M`, `A` through `C` and
39+
# `1` for total of 4+2=6 commits. The +2 commits accounts for the
40+
# base `1` and merge commit `M`.
41+
RAW_COUNT="$(gh api "repos/$REPO/compare/${BASE}...main" | jq -r '.total_commits')"
42+
ADJUSTED_COUNT=$((RAW_COUNT + 2))
43+
echo "count=$ADJUSTED_COUNT" >> "$GITHUB_OUTPUT"
44+
env:
45+
GH_TOKEN: ${{ github.token }}
46+
BASE: ${{ github.event.pull_request.base.sha }}
47+
REPO: ${{ github.repository }}
48+
2049
- name: Checkout
2150
# v4
2251
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608
52+
with:
53+
fetch-depth: ${{ fromJSON(steps.count.outputs.count) }}
2354

2455
- name: Setup Node
2556
uses: ./.github/actions/composite/setupNode
2657
with:
2758
IS_DESKTOP_BUILD: true
28-
59+
2960
- name: Run ESLint to check for deprecation warnings
3061
run: |
31-
# This will just fetch the latest commit from main
32-
git fetch origin main --no-tags --depth=1
33-
3462
# shellcheck disable=SC2046
3563
npm run lint-changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"perf-test": "NODE_OPTIONS=--experimental-vm-modules npx reassure",
4747
"typecheck": "NODE_OPTIONS=--max_old_space_size=8192 tsc",
4848
"lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=0 --cache --cache-location=node_modules/.cache/eslint",
49-
"lint-changed": "NODE_OPTIONS=--max_old_space_size=8192 eslint --max-warnings=0 --config ./.eslintrc.changed.js $(git diff --diff-filter=AMR --name-only origin/main HEAD -- \"*.ts\" \"*.tsx\")",
49+
"lint-changed": "NODE_OPTIONS=--max_old_space_size=8192 ./scripts/lintChanged.sh",
5050
"lint-watch": "npx eslint-watch --watch --changed",
5151
"shellcheck": "./scripts/shellCheck.sh",
5252
"prettier": "prettier --write .",

scripts/lintChanged.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
3+
# Lints .ts and .tsx files that have changed in this branch
4+
5+
set -eu
6+
7+
TOP="$(realpath "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)/..")"
8+
readonly TOP
9+
source "${TOP}/scripts/shellUtils.sh"
10+
11+
# Ensure this script is running inside an npm run context
12+
if [[ -z "${npm_lifecycle_event:-}" ]]; then
13+
info "Re-executing this script from an npm context"
14+
exec npx -c "NODE_OPTIONS=--max_old_space_size=8192 ${BASH_SOURCE[0]} $*"
15+
fi
16+
17+
# Fetch the commit history to include the merge-base commit
18+
info "Fetching origin/main"
19+
git fetch origin main --no-tags
20+
21+
MERGE_BASE_SHA_HASH="$(git merge-base origin/main HEAD)"
22+
readonly MERGE_BASE_SHA_HASH
23+
24+
# Check if output is empty or malformed
25+
if [[ -z "$MERGE_BASE_SHA_HASH" ]] || ! [[ "$MERGE_BASE_SHA_HASH" =~ ^[a-fA-F0-9]{40}$ ]]; then
26+
error "git merge-base returned unexpected output: $MERGE_BASE_SHA_HASH"
27+
exit 1
28+
fi
29+
30+
# Get the diff output and check status
31+
if ! GIT_DIFF_OUTPUT="$(git diff --diff-filter=AMR --name-only "$MERGE_BASE_SHA_HASH" HEAD -- '*.ts' '*.tsx')"; then
32+
error "git diff failed - output: $GIT_DIFF_OUTPUT"
33+
exit 1
34+
fi
35+
36+
# Run eslint on the changed files
37+
if [[ -n "$GIT_DIFF_OUTPUT" ]] ; then
38+
# shellcheck disable=SC2086 # For multiple files in variable
39+
eslint --max-warnings=0 --config ./.eslintrc.changed.js $GIT_DIFF_OUTPUT
40+
else
41+
info "No TypeScript files changed"
42+
fi

0 commit comments

Comments
 (0)