Skip to content

Commit

Permalink
[WTH Core] Adding a spell checker to all pull requests (microsoft#611)
Browse files Browse the repository at this point in the history
* Create create-new-hack.yml

* Update create-new-hack.yml

* spell check init

* Spell check (#2)

* Create create-new-hack.yml

* Update create-new-hack.yml

* spell check init

* path to cspell.json

* spell check

* rename

* Spell check (#3)

* Create create-new-hack.yml

* Update create-new-hack.yml

* spell check init

* path to cspell.json

* spell check

* rename

* version

* spellcheck

* spelling

* ignore case

* more words

* more words

* more words

* more words

* version

* naming

* spell

* extend

* 3.7

* 8

* yq

* 11

* 12

* 13

* print

* print

* 15

* 47

* 4307

* download

* quotes

* move spellcheck action to repo

* changed files

* 20

* paths

* test

* 21

* extra files

* extra files

* debug

* 23

* 24

* 25

* 26

* 27

* 28

* 29

* 30

* spelling

* action

* fix

* spell

* spelling

* spelling

* spelling

* sync

* label

* asdf

* sha

* spelling
  • Loading branch information
jordanbean-msft authored May 21, 2023
1 parent eef403b commit 5a562f1
Show file tree
Hide file tree
Showing 13 changed files with 860 additions and 5 deletions.
22 changes: 22 additions & 0 deletions .github/actions/spell-check/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM python:3.9-slim-buster

LABEL "com.github.actions.name"="WTH Spell Check Action"
LABEL "com.github.actions.description"="Check spelling of Markdown files in the WhatTheHack repo"
LABEL "com.github.actions.icon"="clipboard"
LABEL "com.github.actions.color"="green"
LABEL "repository"="http://github.com/whatthehack"
LABEL "homepage"="http://github.com/actions"

RUN apt-get update \
&& apt-get install -y aspell hunspell wget

RUN wget https://github.com/mikefarah/yq/releases/download/v4.30.7/yq_linux_amd64 -O /usr/bin/yq && \
chmod +x /usr/bin/yq

RUN pip3 install pyspelling pyyaml

COPY generate-spellcheck.py /generate-spellcheck.py
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
1 change: 1 addition & 0 deletions .github/actions/spell-check/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# wth-spell-check-action
26 changes: 26 additions & 0 deletions .github/actions/spell-check/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: "WTH Spell Check"
description: "A Github Action that spell checks the Markdown files in the WTH repository"
author: Jordan Bean
inputs:
spell_check_yaml_path:
description: "Path to the spell check yaml file"
required: true
default: ".github/workflows/spellcheck.yml"
markdown_base_path:
description: "Path to the markdown files"
required: true
default: "."
changed_files:
description: "Files changed in the PR (space separated)"
required: true
default: ""
branding:
color: green
icon: type
runs:
using: docker
image: "Dockerfile"
args:
- ${{ inputs.spell_check_yaml_path }}
- ${{ inputs.markdown_base_path }}
- ${{ inputs.changed_files }}
31 changes: 31 additions & 0 deletions .github/actions/spell-check/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash -l

echo "Starting..."
configFile="$1"; shift
pathToMarkdownFiles="$1"; shift
changedFiles=("$@")

echo "Config file: $configFile"
echo "Path to markdown files: $pathToMarkdownFiles"
echo "Changed files: ${changedFiles[@]}"

echo "Setup languages and spelling tool..."

python /generate-spellcheck.py "$configFile" "$pathToMarkdownFiles" "${changedFiles[@]}"

# convert from JSON to YAML
yq -P "$configFile".tmp > "$configFile"

rm -rf /var/lib/apt/lists/*

echo "Using PySpelling according to configuration from $configFile"

pyspelling --config "$configFile"

EXITCODE=$?

test $EXITCODE -gt 1 && echo "Spelling check action failed, please check logs.";

test $EXITCODE -eq 1 && echo "Files in repository contain spelling errors. Please fix these errors. Alternatively, follow the instructions at the following link to add your own words to the dictionary: https://microsoft.github.io/WhatTheHack/CONTRIBUTING.html#spell-check";

exit $EXITCODE
40 changes: 40 additions & 0 deletions .github/actions/spell-check/generate-spellcheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sys
import os
import yaml
import json

CUSTOM_WORD_LIST_FILENAME = '.wordlist.txt'

def find_wordlist_files(path):
wordlist_paths = []
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith(CUSTOM_WORD_LIST_FILENAME):
wordlist_paths.append(os.path.join(root, file))
return wordlist_paths

if __name__ == '__main__':
spell_check_yaml_path = sys.argv[1]
markdown_base_path = sys.argv[2]
changed_files_tmp = sys.argv[3:]
# the changed files come in as a list with a single element, each of which is space-separated in the first element
# therefore, we need to split them
changed_files = changed_files_tmp[0].split(' ')

spell_check_yaml = None

with open(spell_check_yaml_path, 'r') as read_file:
spell_check_yaml = yaml.load(read_file, Loader=yaml.FullLoader)

wordlist_paths = find_wordlist_files(markdown_base_path)

# Add any custom wordlists defined to the spellcheck config
spell_check_yaml['matrix'][0]['dictionary']['wordlists'].extend(wordlist_paths)

# Set the list of files to check
spell_check_yaml['matrix'][0]['sources'] = changed_files

with open(spell_check_yaml_path + ".tmp", 'w') as write_file:
#yaml.dump doesn't work in Python >3, so we dump to JSON instead & convert using yq in the outer script
#yaml.dump(write_file, spell_check_yaml, Dumper=yaml.Dumper)
json.dump(spell_check_yaml, write_file, indent=4)
28 changes: 28 additions & 0 deletions .github/workflows/spell-check-markdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Spell Check

on:
pull_request:
types:
- opened
- synchronize
branches: [master]
paths:
- "**.md"

jobs:
spell-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@ce810b29b28abf274afebdcd8fe47b8fba0f28bd
with:
files: |
**/*.md
- uses: ./.github/actions/spell-check
name: WTH Spell Check
with:
spell_check_yaml_path: .github/workflows/spell-check/spellcheck.yml
markdown_base_path: .
changed_files: ${{ steps.changed-files.outputs.all_changed_files }}
Loading

0 comments on commit 5a562f1

Please sign in to comment.