Skip to content

Commit

Permalink
feat(ci): Add codespell to Makefile and GitHub Actions (#3996)
Browse files Browse the repository at this point in the history
* feat(ci): Add codespell to Makefile and GitHub Actions

This is a followup from
#3980 to add the
automation for checking for code spelling errors to a different PR.

[Codespell][] makes use of the `.codespellrc` file automatically as a config
file for convenience. The configuration file specifies directories and
flags to pass to `codespell`. Codespell also makes use of the
`.codespellignore` file to ignore certain word spellings.

There is a new section in `Makefile` for installing python tooling.
It is similar to how the go tools are installed. It sets up a virtualenv
in `venv `and installs python tooling there if it hasn't been installed
yet.

The new [codespell workflow][] will run in pull requests to annotate
misspelled words. It is set up to only warn the user with annotations in
the pull request if there are typos, but we can fail the pull request as
well if we want to by deleting the [warn_only][] key.

[codespell]: https://github.com/codespell-project/codespell
[codespell workflow]: https://github.com/codespell-project/actions-codespell
[warn_only]: https://github.com/codespell-project/actions-codespell#parameter-only_warn

* Use docker to run python and codespell

Since this is a Go project, having a dependency on python seemed
disconcerting. So, docker is used to create a virtual environment
with a python image and also run the tools that were installed.

* Remove wording on requirement for python

* Apply suggestions from code review

Co-authored-by: Damien Mathieu <[email protected]>

* Pin versions into requirements.txt

* Pin version in requirements.txt

* Fix typo in license

* Revert typo fix on deleted file

* Update .github/workflows/codespell.yaml

Co-authored-by: Robert Pająk <[email protected]>

* Update codespell.yaml

* Update codespell.yaml

---------

Co-authored-by: Chester Cheung <[email protected]>
Co-authored-by: Damien Mathieu <[email protected]>
Co-authored-by: Robert Pająk <[email protected]>
Co-authored-by: Tyler Yahn <[email protected]>
  • Loading branch information
5 people authored May 18, 2023
1 parent c3b48e2 commit 8dcabc3
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .codespellignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ot
fo
te
collison
consequentially
10 changes: 10 additions & 0 deletions .codespellrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# https://github.com/codespell-project/codespell
[codespell]
builtin = clear,rare,informal
check-filenames =
check-hidden =
ignore-words = .codespellignore
interactive = 1
skip = .git,go.mod,go.sum,semconv,venv,.tools
uri-ignore-words-list = *
write =
14 changes: 14 additions & 0 deletions .github/workflows/codespell.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: codespell
on:
push:
branches:
- main
pull_request:
jobs:
codespell:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3
- name: Codespell
run: make codespell
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Thumbs.db

.tools/
venv/
.idea/
.vscode/
*.iml
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ linters-settings:
- name: constant-logical-expr
disabled: false
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#context-as-argument
# TODO (#3372) reenable linter when it is compatible. https://github.com/golangci/golangci-lint/issues/3280
# TODO (#3372) re-enable linter when it is compatible. https://github.com/golangci/golangci-lint/issues/3280
- name: context-as-argument
disabled: true
arguments:
Expand Down
5 changes: 5 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ precommit` - the `precommit` target is the default).
The `precommit` target also fixes the formatting of the code and
checks the status of the go module files.

Additionally, there is a `codespell` target that checks for common
typos in the code. It is not run by default, but you can run it
manually with `make codespell`. It will set up a virtual environment
in `venv` and install `codespell` there.

If after running `make precommit` the output of `git status` contains
`nothing to commit, working tree clean` then it means that everything
is up-to-date and properly formatted.
Expand Down
37 changes: 37 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,39 @@ $(TOOLS)/gojq: PACKAGE=github.com/itchyny/gojq/cmd/gojq
.PHONY: tools
tools: $(CROSSLINK) $(DBOTCONF) $(GOLANGCI_LINT) $(MISSPELL) $(GOCOVMERGE) $(STRINGER) $(PORTO) $(GOJQ) $(SEMCONVGEN) $(MULTIMOD) $(SEMCONVKIT)

# Virtualized python tools via docker

# The directory where the virtual environment is created.
VENVDIR := venv

# The directory where the python tools are installed.
PYTOOLS := $(VENVDIR)/bin

# The pip executable in the virtual environment.
PIP := $(PYTOOLS)/pip

# The directory in the docker image where the current directory is mounted.
WORKDIR := /workdir

# The python image to use for the virtual environment.
PYTHONIMAGE := python:3.11.3-slim-bullseye

# Run the python image with the current directory mounted.
DOCKERPY := docker run --rm -v "$(CURDIR):$(WORKDIR)" -w $(WORKDIR) $(PYTHONIMAGE)

# Create a virtual environment for Python tools.
$(PYTOOLS):
# The `--upgrade` flag is needed to ensure that the virtual environment is
# created with the latest pip version.
@$(DOCKERPY) bash -c "python3 -m venv $(VENVDIR) && $(PIP) install --upgrade pip"

# Install python packages into the virtual environment.
$(PYTOOLS)/%: | $(PYTOOLS)
@$(DOCKERPY) $(PIP) install -r requirements.txt

CODESPELL = $(PYTOOLS)/codespell
$(CODESPELL): PACKAGE=codespell

# Generate

.PHONY: generate
Expand Down Expand Up @@ -180,6 +213,10 @@ vanity-import-fix: | $(PORTO)
misspell: | $(MISSPELL)
@$(MISSPELL) -w $(ALL_DOCS)

.PHONY: codespell
codespell: | $(CODESPELL)
@$(DOCKERPY) $(CODESPELL)

.PHONY: license-check
license-check:
@licRes=$$(for f in $$(find . -type f \( -iname '*.go' -o -iname '*.sh' \) ! -path '**/third_party/*' ! -path './.git/*' ) ; do \
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
codespell==2.2.4

0 comments on commit 8dcabc3

Please sign in to comment.