From b27237808d967aef3b9b35fc8df455b9fe07a9d5 Mon Sep 17 00:00:00 2001 From: Kevin Brian Bader Date: Thu, 31 Jul 2025 13:01:41 -0700 Subject: [PATCH 1/2] checkCoverageChanges update --- .github/scripts/checkCoverageChanges.sh | 70 ++++++++++++++++++------- .github/workflows/testCoverage.yml | 5 -- 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/.github/scripts/checkCoverageChanges.sh b/.github/scripts/checkCoverageChanges.sh index 3519ae409667..a9e6c9869db2 100755 --- a/.github/scripts/checkCoverageChanges.sh +++ b/.github/scripts/checkCoverageChanges.sh @@ -1,42 +1,76 @@ #!/bin/bash +set -euo pipefail -# Ensure upstream/main exists -git fetch --depth=50 upstream main:upstream/main +# ----------------------------- +# 1. Ensure upstream remote exists +# ----------------------------- +# The PR branch comes from either the base repo or a fork. +# We need the base repository's main branch for comparison. +git remote add upstream https://github.com/${GITHUB_REPOSITORY}.git 2>/dev/null || true -# Determine diff range safely -if git merge-base upstream/main HEAD >/dev/null 2>&1; then - DIFF_RANGE="upstream/main...HEAD" -else - echo "No merge base with upstream/main; falling back to comparing HEAD against upstream/main" - DIFF_RANGE="upstream/main HEAD" +# ----------------------------- +# 2. Attempt shallow fetch first +# ----------------------------- +# Start with a depth of 50 commits, which covers most PRs. +DEPTH=50 +echo "Fetching upstream/main with depth=$DEPTH..." +git fetch upstream main --depth=$DEPTH + +# ----------------------------- +# 3. Check if a merge-base exists +# ----------------------------- +# merge-base finds the common ancestor between the PR branch and main. +# If this fails, it means our shallow history didn't go back far enough. +if ! git merge-base upstream/main HEAD >/dev/null 2>&1; then + echo "No merge base found with depth=$DEPTH, fetching full history..." + + # Try unshallowing the entire repo (pulls full commit history). + # If already full, this will be a no-op. + git fetch --unshallow upstream || git fetch upstream main fi -# Get changed files in src directory -readarray -t ALL_CHANGED_FILES < <(git diff --name-only "$DIFF_RANGE" | grep '^src/' | grep -E '\.(ts|tsx|js|jsx)$' || true) +# ----------------------------- +# 4. Define the diff range +# ----------------------------- +# Using three-dot notation (A...B) shows only the commits in HEAD +# that aren't in upstream/main (i.e. just the PR changes). +DIFF_RANGE="upstream/main...HEAD" + +# ----------------------------- +# 5. Collect changed src/ files +# ----------------------------- +readarray -t ALL_CHANGED_FILES < <( + git diff --name-only "$DIFF_RANGE" \ + | grep '^src/' \ + | grep -E '\.(ts|tsx|js|jsx)$' || true +) -# Filter out excluded directories and files +# ----------------------------- +# 6. Filter excluded files/dirs +# ----------------------------- CHANGED_FILES=() for file in "${ALL_CHANGED_FILES[@]}"; do - # Skip excluded directories + # Exclude directories if [[ "$file" =~ ^src/(CONST|languages|setup|stories|styles|types)/ ]]; then echo "Skipping excluded directory: $file" continue fi - - # Skip excluded files in src root + + # Exclude specific files in src root filename=$(basename "$file") if [[ "$filename" =~ ^(App\.tsx|CONFIG\.ts|Expensify\.tsx|HybridAppHandler\.tsx|NAICS\.ts|NAVIGATORS\.ts|ONYXKEYS\.ts|ROUTES\.ts|SCREENS\.ts|SplashScreenStateContext\.tsx|TIMEZONES\.ts)$ ]]; then echo "Skipping excluded file: $file" continue fi - # Add to coverage collection CHANGED_FILES+=("$file") done -# Check if any files remain for coverage +# ----------------------------- +# 7. Output results +# ----------------------------- if [ ${#CHANGED_FILES[@]} -eq 0 ]; then - echo "No relevant src files changed (all changes were in excluded directories/files), skipping coverage" + echo "No relevant src files changed, skipping coverage" echo "run_coverage=false" >> "$GITHUB_OUTPUT" exit 0 fi @@ -45,5 +79,5 @@ echo "Changed src files for coverage:" printf '%s\n' "${CHANGED_FILES[@]}" echo "run_coverage=true" >> "$GITHUB_OUTPUT" -# Save changed files for coverage collection +# Save changed files for later coverage steps printf '%s\n' "${CHANGED_FILES[@]}" > changed_files.txt diff --git a/.github/workflows/testCoverage.yml b/.github/workflows/testCoverage.yml index 48570e289339..6a6d5a2fcb53 100644 --- a/.github/workflows/testCoverage.yml +++ b/.github/workflows/testCoverage.yml @@ -45,11 +45,6 @@ jobs: repository: ${{ steps.get_pr.outputs.repo }} ref: ${{ steps.get_pr.outputs.ref }} - - name: Fetch Upstream Remote for Base Repository - run: | - git remote add upstream https://github.com/${{ github.repository }}.git - git fetch upstream main - - name: Setup Git for OSBotify uses: Expensify/GitHub-Actions/setupGitForOSBotify@main id: setupGitForOSBotify From 75852fab0c0f274a60686af57c2a10a050947b6b Mon Sep 17 00:00:00 2001 From: Kevin Brian Bader Date: Thu, 31 Jul 2025 13:20:44 -0700 Subject: [PATCH 2/2] shell lint --- .github/scripts/checkCoverageChanges.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/scripts/checkCoverageChanges.sh b/.github/scripts/checkCoverageChanges.sh index a9e6c9869db2..1bdc4a86bc28 100755 --- a/.github/scripts/checkCoverageChanges.sh +++ b/.github/scripts/checkCoverageChanges.sh @@ -6,7 +6,7 @@ set -euo pipefail # ----------------------------- # The PR branch comes from either the base repo or a fork. # We need the base repository's main branch for comparison. -git remote add upstream https://github.com/${GITHUB_REPOSITORY}.git 2>/dev/null || true +git remote add upstream "https://github.com/${GITHUB_REPOSITORY}.git" 2>/dev/null || true # ----------------------------- # 2. Attempt shallow fetch first @@ -52,14 +52,14 @@ CHANGED_FILES=() for file in "${ALL_CHANGED_FILES[@]}"; do # Exclude directories if [[ "$file" =~ ^src/(CONST|languages|setup|stories|styles|types)/ ]]; then - echo "Skipping excluded directory: $file" + echo "Skipping excluded directory: \"$file\"" continue fi # Exclude specific files in src root - filename=$(basename "$file") + filename="$(basename "$file")" if [[ "$filename" =~ ^(App\.tsx|CONFIG\.ts|Expensify\.tsx|HybridAppHandler\.tsx|NAICS\.ts|NAVIGATORS\.ts|ONYXKEYS\.ts|ROUTES\.ts|SCREENS\.ts|SplashScreenStateContext\.tsx|TIMEZONES\.ts)$ ]]; then - echo "Skipping excluded file: $file" + echo "Skipping excluded file: \"$file\"" continue fi