Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 55 additions & 21 deletions .github/scripts/checkCoverageChanges.sh
Original file line number Diff line number Diff line change
@@ -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"
echo "Skipping excluded directory: \"$file\""
continue
fi
# Skip excluded files in src root
filename=$(basename "$file")

# 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"
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
Expand All @@ -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
5 changes: 0 additions & 5 deletions .github/workflows/testCoverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down