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

Fix #2198: Introduce pre push hook for ktlint #2002

Merged

Conversation

anandwana001
Copy link
Contributor

@anandwana001 anandwana001 commented Oct 13, 2020

Explanation

Fix #2198

As we know that we have ktlint check in our actions and we also enforce for good formatting code. So, rather than sending a PR and then fixing the ktlint issue, we are introducing a pre-push hook which will run the ktlint before any push.
As per this PR, we want any contributor after opening the codebase we want them to run a script which will copy our hooks script into the .git/hooks folder.

Script to run -> bash scripts/setup.sh

Checklist

  • The PR title starts with "Fix #bugnum: ", followed by a short, clear summary of the changes. (If this PR fixes part of an issue, prefix the title with "Fix part of #bugnum: ...".)
  • The PR explanation includes the words "Fixes #bugnum: ..." (or "Fixes part of #bugnum" if the PR only partially fixes an issue).
  • The PR follows the style guide.
  • The PR does not contain any unnecessary auto-generated code from Android Studio.
  • The PR is made from a branch that's not called "develop".
  • The PR is made from a branch that is up-to-date with "develop".
  • The PR's branch is based on "develop" and not on any other branch.
  • The PR is assigned to an appropriate reviewer in both the Assignees and the Reviewers sections.

Copy link
Contributor

@rt4914 rt4914 left a comment

Choose a reason for hiding this comment

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

Screenshot 2020-10-13 at 12 06 30 PM

Tested this and it works correctly. Thanks @anandwana001

Will need to defer to @BenHenning and @vinitamurthi for code correctness.

@rt4914 rt4914 removed their assignment Oct 13, 2020
@BenHenning
Copy link
Member

I'll need to take a look at this tomorrow my time (it's getting a bit late). @vinitamurthi it might be nice if you could take a look at this first. One issue with the pre-commit hook is that we can't force people to actually install it in the Android workflow, and if you don't install it it's not as useful. :)

scripts/pre-commit Outdated Show resolved Hide resolved
scripts/setup.py Outdated
Comment on lines 26 to 27
if file_exists:
print('Pre-commit hook already exists')
Copy link
Contributor

Choose a reason for hiding this comment

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

The downside to this is that, if we make any changes to the pre-commit hook, it will not get updated to folks who already have the hook installed. Maybe we should not do this and just replace the file even if it exists (Since this script shouldn't be run very often anyway)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

now with bash, what we are doing is moving of file.

@vinitamurthi
Copy link
Contributor

One issue with the pre-commit hook is that we can't force people to actually install it in the Android workflow, and if you don't install it it's not as useful. :)

That's actually why I thought we should install the hook in a setup script (among other reasons). Today the setup script is not something we can force people to do, but in the future we can actually have people use the setup script to install everything they require to get oppia-android working (particularly, bazel :) )
Today since we only need Android Studio which has gradle along with it, the setup script is an 'extra' script that is not really important. But when we move to Bazel, we can actually have the setup script do the following:

  1. Install Bazel (which makes it essential)
  2. Install Ktlint
  3. Install Buf
  4. Install whatever XML/java linters we chose to use
  5. Install codecoverage libraries if need be
  6. Add the pre-commit hook

And in documentation we basically have the installation steps as: Clone the repo, download Android studio, run this setup script.

@vinitamurthi vinitamurthi removed their assignment Oct 13, 2020
Copy link
Member

@BenHenning BenHenning left a comment

Choose a reason for hiding this comment

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

Thanks @anandwana001! Left some high-level thoughts.

scripts/pre-commit Outdated Show resolved Hide resolved
scripts/setup.py Outdated Show resolved Hide resolved
scripts/pre-commit Outdated Show resolved Hide resolved
Comment on lines 3 to 5
echo "********************************"
echo "Checking code formatting"
echo "********************************"
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm being a bit nitpicky (sorry!) - can we have the entire lint check work within a function and then call that function in this script? The reason I ask for that is so that we can cleanly extend this hook if we want to add more checks into it later on

Copy link
Member

Choose a reason for hiding this comment

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

One step further: I think the lint check itself should be its own script that this hook calls (apologies if that's what you meant @vinitamurthi). The pre-commit hook ought to just be a set of commands to run, and having the command separate also lets people use it directly.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes that's what I meant sorry I didn't make it clear! We should keep them in separate scripts and the pre-commit hook should have commands to run those scripts one after the other

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, Thanks @vinitamurthi

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@vinitamurthi vinitamurthi removed their assignment Oct 20, 2020
Copy link
Member

@BenHenning BenHenning left a comment

Choose a reason for hiding this comment

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

Thanks @anandwana001! Left a few more thoughts.

Comment on lines 3 to 5
echo "********************************"
echo "Checking code formatting"
echo "********************************"
Copy link
Member

Choose a reason for hiding this comment

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

One step further: I think the lint check itself should be its own script that this hook calls (apologies if that's what you meant @vinitamurthi). The pre-commit hook ought to just be a set of commands to run, and having the command separate also lets people use it directly.

@rt4914
Copy link
Contributor

rt4914 commented Nov 10, 2020

@anandwana001 We discussed that if possible I can resolve ben's comment but even if I do that we will need approval from @BenHenning and therefore I am not resolving his comments. Thanks.

@rt4914 rt4914 removed their assignment Nov 10, 2020
@BenHenning
Copy link
Member

Thanks @anandwana001. Just had one follow-up regarding the executable bit of the pre-commit file, otherwise this looks good to me.

Copy link
Member

@BenHenning BenHenning left a comment

Choose a reason for hiding this comment

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

Thanks @anandwana001! I was about to approve then realized one last thing. :( We should make setup.sh the one-stop-solves-all solution for preparing the codebase. To start, it should set up ktlint and in the future we can add other tools (such as Bazel).

.idea/gradle.xml Outdated Show resolved Hide resolved
scripts/kotlin-lint-check.sh Outdated Show resolved Hide resolved
@BenHenning BenHenning removed their assignment Dec 2, 2020
Copy link
Contributor

@FareesHussain FareesHussain left a comment

Choose a reason for hiding this comment

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

Small doubt regarding how to run ktlint in pre push hook

scripts/kotlin-lint-check.sh Outdated Show resolved Hide resolved
@anandwana001 anandwana001 changed the title Introduce pre push hook for ktlint Fix #2198: Introduce pre push hook for ktlint Dec 13, 2020
scripts/setup.sh Outdated Show resolved Hide resolved
Copy link
Member

@BenHenning BenHenning left a comment

Choose a reason for hiding this comment

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

Thanks @anandwana001! LGTM!

scripts/setup.sh Outdated Show resolved Hide resolved
@BenHenning
Copy link
Member

@anandwana001 just 2 comments to address from me before merging. PTAL.

scripts/setup.sh Outdated Show resolved Hide resolved
@anandwana001 anandwana001 merged commit 53b4a65 into oppia:develop Dec 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce pre push hook for Kotlin
6 participants