Skip to content
Merged
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
41 changes: 31 additions & 10 deletions .github/workflows/validate_modified_targets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,57 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v5
with:
# Checkout the base branch but fetch all history to avoid a second fetch call
ref: ${{ github.base_ref }}
fetch-depth: 1
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.13'
python-version: "3.13"

- name: Install Poetry
uses: abatilo/actions-poetry@v4
with:
poetry-version: 'latest'
poetry-version: "latest"

- name: Install dependencies
run: |
poetry install --no-interaction --with dev

- name: Drop in place updated manifest from base
- name: Prepare JSON versions for comparison
run: |
cp sherlock_project/resources/data.json data.json.base
git fetch origin pull/${{ github.event.pull_request.number }}/head:pr --depth=1
git show pr:sherlock_project/resources/data.json > sherlock_project/resources/data.json
cp sherlock_project/resources/data.json data.json.head
# Fetch only the PR's branch head (single network call in this step)
git fetch origin pull/${{ github.event.pull_request.number }}/head:pr

# Find the merge-base commit between the target branch and the PR branch
MERGE_BASE=$(git merge-base origin/${{ github.base_ref }} pr)
echo "Comparing PR head against merge-base commit: $MERGE_BASE"

# Safely extract the file from the PR's head and the merge-base commit
git show pr:sherlock_project/resources/data.json > data.json.head
git show $MERGE_BASE:sherlock_project/resources/data.json > data.json.base

# CRITICAL FIX: Overwrite the checked-out data.json with the one from the PR
# This ensures that pytest runs against the new, updated file.
cp data.json.head sherlock_project/resources/data.json

- name: Discover modified targets
id: discover-modified
run: |
CHANGED=$(
python - <<'EOF'
import json
with open("data.json.base") as f: base = json.load(f)
with open("data.json.head") as f: head = json.load(f)
import sys
try:
with open("data.json.base") as f: base = json.load(f)
with open("data.json.head") as f: head = json.load(f)
except FileNotFoundError as e:
print(f"Error: Could not find {e.filename}", file=sys.stderr)
sys.exit(1)
except json.JSONDecodeError as e:
print(f"Error: Could not decode JSON from a file - {e}", file=sys.stderr)
sys.exit(1)

changed = []
for k, v in head.items():
Expand All @@ -63,6 +82,8 @@ jobs:
echo -e ">>> Changed targets: \n$(echo $CHANGED | tr ',' '\n')"
echo "changed_targets=$CHANGED" >> "$GITHUB_OUTPUT"

# --- The rest of the steps below are unchanged ---
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick retrospective tip -

These lines are unnecessary as the diff itself shows what was changed and what wasn't --- so really all it does it add clutter to the code.

Not important, just a friendly note as you continue your development work!

Cheers! And thanks again! Your changes saved me quite a bit of time here. And it resolved what was quite an annoying issue on PRs with stale merge bases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tip and for all the help, @ppfeister! I'm glad I could contribute.


- name: Validate modified targets
if: steps.discover-modified.outputs.changed_targets != ''
continue-on-error: true
Expand Down