Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre-commit config with pre-commit-hooks #3283

Merged
merged 20 commits into from
Jun 11, 2024
Merged

Add pre-commit config with pre-commit-hooks #3283

merged 20 commits into from
Jun 11, 2024

Conversation

weiji14
Copy link
Member

@weiji14 weiji14 commented Jun 4, 2024

Description of proposed changes

Add pre-commit hooks to enforce certain rules on all files in the repo.

The following rules are applied:

  • check-added-large-files
  • check-yaml
  • end-of-file-fixer
  • trailing-whitespace
  • forbid-crlf
  • remove-crlf
  • chmod

Note that pre-commit run --all-files has been added to the Makefile, so running make format locally or /format on a GitHub PR will apply the formatting rules.

TODO:

  • Add a .pre-commit-config.yaml file for standard pre-commit hooks
  • Configure pre-commit-ci GitHub App not using, doing pre-commit manually ourselves

References:

Fixes #1593

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If wrapping a new module, open a 'Wrap new GMT module' issue and submit reasonably-sized PRs.
  • If adding new functionality, add an example to docstrings or tutorials.
  • Use underscores (not hyphens) in names of Python files and directories.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash command is:

  • /format: automatically format and lint the code

Adding a .pre-commit-config.yaml file with some pre-commit hooks (check-added-large-files, check-yaml, end-of-file-fixer, trailing-whitespace). Also added pre-commit.ci config with autofix_prs=false, and autoupdate_schedule=quarterly.
@weiji14 weiji14 self-assigned this Jun 4, 2024
@weiji14
Copy link
Member Author

weiji14 commented Jun 4, 2024

Results from first pre-commit.ci run at https://results.pre-commit.ci/run/github/85352251/1717479459.EAFPxChRS_aFLgAcskCvfQ:

check for added large files..............................................Passed
check yaml...............................................................Passed
fix end of files.........................................................Failed
- hook id: end-of-file-fixer
- exit code: 1
- files were modified by this hook

Fixing doc/_templates/autosummary/class.rst
Fixing doc/_templates/autosummary/exception.rst
Fixing pygmt/tests/data/contours.txt
Fixing doc/_templates/autosummary/function.rst
Fixing doc/team.md
Fixing CODE_OF_CONDUCT.md

trim trailing whitespace.................................................Failed
- hook id: trailing-whitespace
- exit code: 1
- files were modified by this hook

Fixing examples/projections/README.txt
Fixing doc/changes.md

Do we want to try applying the autofixes directly in this PR?

@seisman
Copy link
Member

seisman commented Jun 4, 2024

Yes to make sure that autofix works as expected.

@weiji14
Copy link
Member Author

weiji14 commented Jun 4, 2024

pre-commit.ci autofix

- id: check-added-large-files
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
Copy link
Member Author

@weiji14 weiji14 Jun 4, 2024

Choose a reason for hiding this comment

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

Can also add these hooks from https://github.com/Lucas-C/pre-commit-hooks that handle LF file endings and 644 permissions? (Then we can remove these lines from style_checks.yml).

Suggested change
- id: trailing-whitespace
- id: trailing-whitespace
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.5.5
hooks:
- id: forbid-crlf
- id: remove-crlf
- id: chmod
args: ['644']

Copy link
Member

Choose a reason for hiding this comment

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

Looks good.

Copy link
Member Author

@weiji14 weiji14 Jun 4, 2024

Choose a reason for hiding this comment

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

Hmm, not sure why, but pre-commit.ci seeems to think our files have 664 permission instead of 644? See logs at https://results.pre-commit.ci/run/github/85352251/1717481309.E-FyucnQQ_CvuFT06LGuHg

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added back the GitHub Action workflows for 644 permission checks/formatting for now at commit 5a01ad6.

Copy link
Member Author

Choose a reason for hiding this comment

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

Since we're not using pre-commit.ci anymore but just GitHub Actions, I've reinstated the 644 pre-commit hook in the YAML file. Seems to be working at https://github.com/GenericMappingTools/pygmt/actions/runs/9458943127/job/26055263366?pr=3283#step:5:31:

[INFO] Initializing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Initializing environment for https://github.com/Lucas-C/pre-commit-hooks.
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
[INFO] Installing environment for https://github.com/Lucas-C/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
check for added large files..............................................Passed
check yaml...............................................................Passed
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
CRLF end-lines checker...................................................Passed
CRLF end-lines remover...................................................Passed
Set file permissions.....................................................Failed
- hook id: chmod
- exit code: 1
- files were modified by this hook

Fixing file permissions on requirements.txt: 0o755 -> 0o644

Error: Process completed with exit code 1.

@seisman
Copy link
Member

seisman commented Jun 4, 2024

Don't forget to add the .pre-commit-config.yaml file to https://github.com/GenericMappingTools/pygmt/blob/main/MANIFEST.in.

Enforcing LF line endings and 644 permissions via pre-commit hooks!
Partially revert ce6d457 to keep the 644 permission checks and format commands.
Comment on lines 488 to 489
Even better, you can just write `/format` and/or `pre-commit.ci autofix` in the first
line of any comment in a pull request to lint the code automatically.
Copy link
Member Author

@weiji14 weiji14 Jun 4, 2024

Choose a reason for hiding this comment

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

Debating on whether to just add pre-commit run --all-files to the Makefile, or have it listed separately. Main consideration is that Windows users won't have make installed by default, so they would only be able to run pre-commit run --all-files.

Copy link
Member

Choose a reason for hiding this comment

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

make is added as a development dependency in environment.yml, so even Windows developers have make installed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh yes, forgot that it is possible to conda install make now! I'll add it to the Makefile then.

@seisman
Copy link
Member

seisman commented Jun 5, 2024

Also need to mention pre-commit.ci autofix in the PR template.

README.md Outdated Show resolved Hide resolved
Comment on lines 488 to 489
Even better, you can just write `/format` and/or `pre-commit.ci autofix` in the first
line of any comment in a pull request to lint the code automatically.
Copy link
Member

Choose a reason for hiding this comment

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

make is added as a development dependency in environment.yml, so even Windows developers have make installed.

@seisman seisman added this to the 0.13.0 milestone Jun 5, 2024
@seisman seisman added the maintenance Boring but important stuff for the core devs label Jun 5, 2024
Copy link
Member Author

@weiji14 weiji14 left a comment

Choose a reason for hiding this comment

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

Also need to mention pre-commit.ci autofix in the PR template.

Done at b3eee6f. But I'm also wondering if this is redundant, because I've added pre-commit run --all-files to the make format command, so typing /format will also run pre-commit now 🙂

README.md Outdated Show resolved Hide resolved
@seisman
Copy link
Member

seisman commented Jun 9, 2024

But I'm also wondering if this is redundant, because I've added pre-commit run --all-files to the make format command, so typing /format will also run pre-commit now

Adding pre-commit run --all-files to make format means that users/maintainers must set up pre-commit locally (although they just need to run pre-commit install once).

What about adding pre-commit run --all-files to https://github.com/GenericMappingTools/pygmt/blob/main/.github/workflows/format-command.yml, so that pre-commit is run when /format is executed?

@weiji14
Copy link
Member Author

weiji14 commented Jun 9, 2024

But I'm also wondering if this is redundant, because I've added pre-commit run --all-files to the make format command, so typing /format will also run pre-commit now

Adding pre-commit run --all-files to make format means that users/maintainers must set up pre-commit locally (although they just need to run pre-commit install once).

As long as they have pre-commit installed, they don't need to run pre-commit install because make format and pre-commit run --all-files will automatically set up pre-commit on the first run. E.g.:

$ pre-commit clean  # uninstall/clean out pre-commit files
$ make format
ruff check --fix --exit-zero pygmt doc/conf.py examples
All checks passed!
ruff format pygmt doc/conf.py examples
301 files left unchanged
pre-commit run --all-files
[INFO] Initializing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Initializing environment for https://github.com/Lucas-C/pre-commit-hooks.
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
[INFO] Installing environment for https://github.com/Lucas-C/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
check for added large files..............................................Passed
check yaml...............................................................Passed
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
CRLF end-lines checker...................................................Passed
CRLF end-lines remover...................................................Passed

What about adding pre-commit run --all-files to https://github.com/GenericMappingTools/pygmt/blob/main/.github/workflows/format-command.yml, so that pre-commit is run when /format is executed?

Depends on whether we want pre-commit in the Makefile or not. I'm actually thinking if we want to install ruff as a pre-commit hook (in a separate PR), and then make format would just be an alias for pre-commit run --all-files.

@seisman
Copy link
Member

seisman commented Jun 10, 2024

As long as they have pre-commit installed, they don't need to run pre-commit install because make format and pre-commit run --all-files will automatically set up pre-commit on the first run.

Good to know. Then we don't need the pre-commit.ci service anymore, right?

@weiji14
Copy link
Member Author

weiji14 commented Jun 10, 2024

As long as they have pre-commit installed, they don't need to run pre-commit install because make format and pre-commit run --all-files will automatically set up pre-commit on the first run.

Good to know. Then we don't need the pre-commit.ci service anymore, right?

Yes, pre-commit.ci isn't absolutely necessary since we can run it via style_checks.yml. Only other reason to use pre-commit.ci is to have the quarterly updates for the pre-commit hook versions. It doesn't seem like dependabot supports updating .pre-commit-config.yaml yet according to dependabot/dependabot-core#1524.

@seisman
Copy link
Member

seisman commented Jun 10, 2024

I'm OK with either keeping pre-commit.ci or not.

.github/PULL_REQUEST_TEMPLATE.md Outdated Show resolved Hide resolved
.github/workflows/style_checks.yaml Outdated Show resolved Hide resolved
.github/workflows/style_checks.yaml Outdated Show resolved Hide resolved
.github/workflows/format-command.yml Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
doc/contributing.md Outdated Show resolved Hide resolved
@weiji14
Copy link
Member Author

weiji14 commented Jun 11, 2024

/format

@weiji14 weiji14 marked this pull request as ready for review June 11, 2024 03:00
@weiji14
Copy link
Member Author

weiji14 commented Jun 11, 2024

Ok, I've removed the pre-commit.ci GitHub App installation from the repo. Let me try the 644 permissions check again to see if it works on GitHub Actions.

Edit: Yep, 644 permission check seems to be ok, see note at #3283 (comment)

Copy link
Member

@seisman seisman left a comment

Choose a reason for hiding this comment

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

Looks great.

@weiji14 weiji14 merged commit b5242be into main Jun 11, 2024
18 checks passed
@weiji14 weiji14 deleted the ci/pre-commit branch June 11, 2024 04:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Boring but important stuff for the core devs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add pre-commit configuration file
2 participants