|
| 1 | +name: Typos |
| 2 | + |
| 3 | +permissions: {} |
| 4 | + |
| 5 | +on: [pull_request] |
| 6 | + |
| 7 | +jobs: |
| 8 | + typos-check: |
| 9 | + name: Check for new typos |
| 10 | + runs-on: ubuntu-latest |
| 11 | + timeout-minutes: 5 |
| 12 | + steps: |
| 13 | + - name: Checkout the JuliaLang/julia repository |
| 14 | + uses: actions/checkout@v4 |
| 15 | + with: |
| 16 | + persist-credentials: false |
| 17 | + - name: Check spelling with typos |
| 18 | + #uses: crate-ci/typos@master |
| 19 | + env: |
| 20 | + GH_TOKEN: "${{ github.token }}" |
| 21 | + run: | |
| 22 | + git fetch --depth=1 origin ${{ github.base_ref }} |
| 23 | + OLD_FILES=$(git diff-index --name-only --diff-filter=ad FETCH_HEAD) |
| 24 | + NEW_FILES=$(git diff-index --name-only --diff-filter=d FETCH_HEAD) |
| 25 | +
|
| 26 | + # This is necessary because the typos command interprets the |
| 27 | + # empty string as "check all files" rather than "check no files". |
| 28 | + if [ -z "$NEW_FILES" ]; then |
| 29 | + echo "All edited files were deleted. Skipping typos check." |
| 30 | + exit 0 |
| 31 | + fi |
| 32 | +
|
| 33 | + mkdir -p "${{ runner.temp }}/typos" |
| 34 | + RELEASE_ASSET_URL="$( |
| 35 | + gh api /repos/crate-ci/typos/releases/latest \ |
| 36 | + --jq '."assets"[] | select(."name" | test("^typos-.+-x86_64-unknown-linux-musl\\.tar\\.gz$")) | ."browser_download_url"' |
| 37 | + )" |
| 38 | + wget --secure-protocol=TLSv1_3 --max-redirect=1 --retry-on-host-error --retry-connrefused --tries=3 \ |
| 39 | + --quiet --output-document=- "${RELEASE_ASSET_URL}" \ |
| 40 | + | tar -xz -C "${{ runner.temp }}/typos" ./typos |
| 41 | + "${{ runner.temp }}/typos/typos" --version |
| 42 | +
|
| 43 | + echo -n $NEW_FILES | xargs "${{ runner.temp }}/typos/typos" --format json >> ${{ runner.temp }}/new_typos.jsonl || true |
| 44 | + git checkout FETCH_HEAD -- $OLD_FILES |
| 45 | + if [ -z "$OLD_FILES" ]; then |
| 46 | + touch "${{ runner.temp }}/old_typos.jsonl" # No old files, so no old typos. |
| 47 | + else |
| 48 | + echo -n $OLD_FILES | xargs "${{ runner.temp }}/typos/typos" --format json >> ${{ runner.temp }}/old_typos.jsonl || true |
| 49 | + fi |
| 50 | +
|
| 51 | +
|
| 52 | + python -c ' |
| 53 | + import sys, json |
| 54 | + old = set() |
| 55 | + with open(sys.argv[1]) as old_file: |
| 56 | + for line in old_file: |
| 57 | + j = json.loads(line) |
| 58 | + if j["type"] == "typo": |
| 59 | + old.add(j["typo"]) |
| 60 | + clean = True |
| 61 | + with open(sys.argv[2]) as new_file: |
| 62 | + for line in new_file: |
| 63 | + new = json.loads(line) |
| 64 | + if new["type"] == "typo" and new["typo"] not in old: |
| 65 | + if len(new["typo"]) > 6: # Short typos might be false positives. Long are probably real. |
| 66 | + clean = False |
| 67 | + print("::warning file={},line={},col={}::perhaps \"{}\" should be \"{}\".".format( |
| 68 | + new["path"], new["line_num"], new["byte_offset"], |
| 69 | + new["typo"], " or ".join(new["corrections"]))) |
| 70 | + sys.exit(1 if not clean else 0)' "${{ runner.temp }}/old_typos.jsonl" "${{ runner.temp }}/new_typos.jsonl" |
0 commit comments