Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Add just and pre-commit #201

Closed
wants to merge 24 commits into from
Closed

Add just and pre-commit #201

wants to merge 24 commits into from

Conversation

sarayourfriend
Copy link
Contributor

Fixes

Fixes #193 by @sarayourfriend
Fixes #187 by @sarayourfriend

Description

Adds pre-commit using the same configuration as the openverse-catalog with some modifications to ignore rules in certain files that were problematic.

Also adds a justfile to make docker stuff easier.

Technical details

I had to add some noqa comments for long strings (I wish flake8 would ignore strings...)

I also added .flake8 that mirrors the pre-commit configuration for flake8 so that editor integration will work

Tests

Checkout the branch and run just install then pipenv run pre-commit run --all-files and ensure that there are no errors reported.

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the default branch of the repository (main or master).
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@sarayourfriend sarayourfriend requested a review from a team as a code owner September 7, 2021 17:10
@dhruvkb
Copy link
Member

dhruvkb commented Sep 8, 2021

@sarayourfriend we should remove the 'Style' job from the 'Automated tests' workflow as the new 'Linting' job supersedes it (the Flake8 check in 'Linting' already includes the pycodestyle check from 'Style'). Also that's the only failing check in a PR that otherwise LGTM.

@sarayourfriend
Copy link
Contributor Author

Oops, I meant to remove it when I added the linting job 😅 It's gone now, thanks!

Copy link
Contributor

@AetherUnbound AetherUnbound 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! I only have some additional thoughts on the justfile

justfile Outdated Show resolved Hide resolved
justfile Outdated Show resolved Hide resolved
Copy link
Member

@krysal krysal left a comment

Choose a reason for hiding this comment

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

It looks pretty good! I just leave some suggestions and questions to evaluate.

.github/workflows/integration-tests.yml Outdated Show resolved Hide resolved
.pre-commit-config.yaml Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
ingestion_server/ingestion_server/es_mapping.py Outdated Show resolved Hide resolved
2. Run the tests in a Pipenv subshell.
```
pipenv run bash ./test/run_test.sh
just testlocal
Copy link
Member

Choose a reason for hiding this comment

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

I got this by trying to run this command:

error: Justfile does not contain recipe `testlocal`

Copy link
Contributor Author

@sarayourfriend sarayourfriend Sep 13, 2021

Choose a reason for hiding this comment

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

Oh hmm I removed it for some reason. Let me see.

Do you know why these tests must run in a "pipenv subshell"?

Edit: Oh never mind, I see that the script calls to pytest.

This is going to throw a wrench in the root Pipfile idea 😞

Copy link
Member

Choose a reason for hiding this comment

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

Personally, I run the tests inside the container, we can leave only those instructions for tests.

docker-compose exec web bash ./test/run_test.sh

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wonder if it's ever necessary to run it outside of the container 🤔

Copy link
Member

@dhruvkb dhruvkb Sep 14, 2021

Choose a reason for hiding this comment

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

Sometimes, I run the code outside the container when debugging it line by line (I like PyCharm's debugger GUI). Running the tests outside the containers helps in that scenario. I'm open to a different process though if I'm the only one using it like that.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, Pycharm can take the interpreter of a container, it's quite flexible in this aspect. However, I can see the value in the local approach in not being tied up to this particular IDE, neither to the use of just if that is what is blocking here.

Copy link
Member

Choose a reason for hiding this comment

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

@krysal from what I had read in the docs, PyCharm can work with a Docker environment only if it created the container. I wasn't able to get it to connect with the existing web container orchestrated by Docker Compose 😢.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I don't think that's true Dhruv, the docs might be incorrect. At my previous job we were able to get it to connect to the docker container just fine and it was running using docker compose... but maybe things have changed since then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In any case, after making the changes in the latest commits we're able to add back the testlocal script 🎉

Copy link
Member

Choose a reason for hiding this comment

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

The testlocal script works perfectly. So does the regular test one.

@zackkrida zackkrida added 🟩 priority: low Low priority and doesn't need to be rushed 🤖 aspect: dx Concerns developers' experience with the codebase 🧰 goal: internal improvement Improvement that benefits maintainers, not users labels Sep 14, 2021
@dhruvkb
Copy link
Member

dhruvkb commented Sep 15, 2021

@sarayourfriend this might be a silly suggestion but I was playing around with pre-commit in the current setup and came across something that might be a good compromise.

Let's say we installed pre-commit in both the existing Pipfiles while keeping the .pre-commit-config.yaml in the repo root. To set up the hooks we go into either of the repos and invoke pipenv run pre-commit install.

The hook created records the virtualenv that was active when install was invoked so there's no need to always activate the virtualenv when committing. All pre-commit hooks manage their own environment so there's no need for installing black or flake8 on any project Pipfile.

Apart from having to list pre-commit in two Pipfiles, what do you think are the cons of this approach?

@sarayourfriend
Copy link
Contributor Author

sarayourfriend commented Sep 15, 2021

Apart from having to list pre-commit in two Pipfiles, what do you think are the cons of this approach?

The real cons are for editor integration, but we're already sacrificing the best of editor integration (dependency knowledge) in the current set up, so I think what you propose is better.

We can add it to both, but just install should only install one right?

@dhruvkb
Copy link
Member

dhruvkb commented Sep 15, 2021

Let's use just install to install from the openverse-api directory. That's the main project in the repo (it shouldn't matter but still)

@sarayourfriend
Copy link
Contributor Author

@dhruvkb I did as you suggested and it works great 🙂

Copy link
Member

@dhruvkb dhruvkb left a comment

Choose a reason for hiding this comment

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

To finalise the changes, it'll be good to squash the commits in such a way that

  • all formatting changes are squashed into one commit to avoid ruining Git blame
  • all config changes are squashed into one commit (to make reviewing the PR easier)

- --ensure-newline-before-comments
- --line-length=88

- repo: https://gitlab.com/pycqa/flake8
Copy link
Member

@dhruvkb dhruvkb Sep 16, 2021

Choose a reason for hiding this comment

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

Just to match the official way their org is named.

Copy link
Member

Choose a reason for hiding this comment

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

Their docs show a GitHub URL. Let's use that.

Suggested change
- repo: https://gitlab.com/pycqa/flake8
- repo: https://github.com/PyCQA/flake8

@dhruvkb
Copy link
Member

dhruvkb commented Sep 16, 2021

I wonder why the tests are still failing. I'll pull this locally to review so will also look into that then.

@sarayourfriend
Copy link
Contributor Author

@dhruvkb Sure, I can do that. Alternatively I could also merge using the squash and merge strategy so it will all be under one commit. Do you have a strong preference either way?

@dhruvkb
Copy link
Member

dhruvkb commented Sep 16, 2021

I have no hard preferences, whatever is convenient for you is cool.

Copy link
Member

@dhruvkb dhruvkb left a comment

Choose a reason for hiding this comment

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

I'll review this tomorrow.

.github/workflows/integration-tests.yml Outdated Show resolved Hide resolved
Copy link
Member

@dhruvkb dhruvkb left a comment

Choose a reason for hiding this comment

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

A linter, formatter and Git hook trio is a much needed addition to the repo! Thanks for doing this.

And with passing tests, I have a lot of confidence in this PR. Apart from all the breaking changes this'll cause in all other PRs (especially the big one #194), LGTM.

.flake8 Show resolved Hide resolved
- --ensure-newline-before-comments
- --line-length=88

- repo: https://gitlab.com/pycqa/flake8
Copy link
Member

Choose a reason for hiding this comment

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

Their docs show a GitHub URL. Let's use that.

Suggested change
- repo: https://gitlab.com/pycqa/flake8
- repo: https://github.com/PyCQA/flake8

.pre-commit-config.yaml Outdated Show resolved Hide resolved
Comment on lines 47 to 49
- --per-file-ignores=*test*:E501,*__init__*:F401,*wsgi.py:E402
- --max-line-length=88
- --ignore=E203,W503
Copy link
Member

Choose a reason for hiding this comment

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

I think these args won't be necessary as the hook will automatically use the config from .flake8.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I won't, unfortunately. pre-commit runs hooks inside a virtualenv that doesn't include the root of your project, so it doesn't know any of your configuration files 😢

Copy link
Member

Choose a reason for hiding this comment

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

I tried this locally and it seemed to work fine. Are you sure about it not working?

Copy link
Member

Choose a reason for hiding this comment

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

@sarayourfriend for the hook, is it possible to provide a config path instead of defining the rules twice in two different locations? If not I think just leaving a note here should be enough that mentions the rules also being in the other file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess I'm not sure 😅 I'd tried it in the past and it didn't work and I found some SO answers about it to confirm the issue, but if you've got it working I'll try it again!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah it worked! I removed the duplicated config. Thanks for testing it out!

README.md Show resolved Hide resolved
2. Run the tests in a Pipenv subshell.
```
pipenv run bash ./test/run_test.sh
just testlocal
Copy link
Member

Choose a reason for hiding this comment

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

The testlocal script works perfectly. So does the regular test one.

justfile Outdated Show resolved Hide resolved
justfile Show resolved Hide resolved
@dhruvkb
Copy link
Member

dhruvkb commented Sep 17, 2021

@sarayourfriend is there a possibility of holding the merge for this PR and merging other open PRs first? It'll be very hard to update them with main after this is merged, while this one will be easier to update if we just took the configs and re-ran the hooks.

Copy link
Member

@dhruvkb dhruvkb left a comment

Choose a reason for hiding this comment

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

There are some indentation inconsistencies in the pre-commit config. Is it weird that pre-commit didn't catch this?

.pre-commit-config.yaml Outdated Show resolved Hide resolved
.pre-commit-config.yaml Outdated Show resolved Hide resolved
Co-authored-by: Dhruv Bhanushali <dhruv_b@live.com>
@sarayourfriend
Copy link
Contributor Author

is there a possibility of holding the merge for this PR and merging other open PRs first

Sure! I suppose it'd be easier to just let the repo settle down at some point and then redo this PR from scratch... the merge conflicts will be difficult to resolve either way (usually these types of PRs are best applied quickly before too much work is done in the meantime, but this one has taken longer 😞) so it'll be easy to just copy the configuration from this PR and then run the formatting.

I guess can you let me know when you think this PR should be merged? Otherwise we'll be waiting forever for work to settle 🙂 At some point we just have to accept the complications.

@sarayourfriend
Copy link
Contributor Author

Oh also, indentation is fixed! Thanks for pointing it out.

@zackkrida
Copy link
Member

How about we merge this after these are merged/closed?

CleanShot 2021-09-19 at 19 28 14@2x

@dhruvkb
Copy link
Member

dhruvkb commented Sep 20, 2021

#202 will need to be reworked anyway, we can merge that later. But #194 is too big to redo 😢.

@sarayourfriend
Copy link
Contributor Author

Closing in favor of #224

@sarayourfriend sarayourfriend deleted the add/pre-commit branch September 21, 2021 20:52
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🤖 aspect: dx Concerns developers' experience with the codebase 🧰 goal: internal improvement Improvement that benefits maintainers, not users 🟩 priority: low Low priority and doesn't need to be rushed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Quality] Use black and isort [Quality] Pre-commit hooks
5 participants