From 34d1b7120fff7ee074f341c2189d30d632298a43 Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Thu, 21 Dec 2023 19:15:04 +0600 Subject: [PATCH] Fix typos CI job for PRs that don't edit any files or do edit binary files (#52600) Co-authored-by: Dilum Aluthge --- .github/workflows/Typos.yml | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/.github/workflows/Typos.yml b/.github/workflows/Typos.yml index f9fa20fff5d12..da5a6a550abe8 100644 --- a/.github/workflows/Typos.yml +++ b/.github/workflows/Typos.yml @@ -23,6 +23,13 @@ jobs: OLD_FILES=$(git diff-index --name-only --diff-filter=ad FETCH_HEAD) NEW_FILES=$(git diff-index --name-only --diff-filter=d FETCH_HEAD) + # This is necessary because the typos command interprets the + # empty string as "check all files" rather than "check no files". + if [ -z "$NEW_FILES" ]; then + echo "All edited files were deleted. Skipping typos check." + exit 0 + fi + mkdir -p "${{ runner.temp }}/typos" RELEASE_ASSET_URL="$( gh api /repos/crate-ci/typos/releases/latest \ @@ -35,19 +42,26 @@ jobs: echo -n $NEW_FILES | xargs "${{ runner.temp }}/typos/typos" --format json >> ${{ runner.temp }}/new_typos.jsonl || true git checkout FETCH_HEAD -- $OLD_FILES - echo -n $OLD_FILES | xargs "${{ runner.temp }}/typos/typos" --format json >> ${{ runner.temp }}/old_typos.jsonl || true + if [ -z "$OLD_FILES" ]; then + touch "${{ runner.temp }}/old_typos.jsonl" # No old files, so no old typos. + else + echo -n $OLD_FILES | xargs "${{ runner.temp }}/typos/typos" --format json >> ${{ runner.temp }}/old_typos.jsonl || true + fi + python -c ' import sys, json old = set() with open(sys.argv[1]) as old_file: for line in old_file: - old.add(json.loads(line)["typo"]) + j = json.loads(line) + if j["type"] == "typo": + old.add(j["typo"]) clean = True with open(sys.argv[2]) as new_file: for line in new_file: new = json.loads(line) - if new["typo"] not in old: + if new["type"] == "typo" and new["typo"] not in old: if len(new["typo"]) > 6: # Short typos might be false positives. Long are probably real. clean = False print("::warning file={},line={},col={}::perhaps \"{}\" should be \"{}\".".format(