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

Enforce pre-commit in ArduPilot #24683

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

Ryanf55
Copy link
Collaborator

@Ryanf55 Ryanf55 commented Aug 17, 2023

Related issues

Fixes #24555.
Depends on #24727.

Purpose

Pre-commit provides basic guardrails for the repos, but it's not enforced. This PR adds it as a CI job.

How to test

CI_BUILD_TARGET=pre-commit-cleanliness ./Tools/scripts/build_ci.sh

Future ideas

We could autofix and re-commit the changes in CI to save developer time, but one could see it as risky.

Actions from Tridge's testing:

  • We should add --allow-missing-config, this will support ArduPilot devs working on branches before 4.3.
  • Stop pre-commit from running when using subsystem split
  • Need dev documentation on pre-commit

@Ryanf55
Copy link
Collaborator Author

Ryanf55 commented Aug 18, 2023

Need to install pre-commit as part of the normal set of python packages.
https://github.com/ArduPilot/ardupilot/actions/runs/5896623868/job/15994803894#step:4:142

.pre-commit-config.yaml Outdated Show resolved Hide resolved
Copy link
Contributor

@khancyr khancyr left a comment

Choose a reason for hiding this comment

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

we should fix the files instead of excluding them.

.pre-commit-config.yaml Outdated Show resolved Hide resolved
@tridge
Copy link
Contributor

tridge commented Aug 22, 2023

problem on older branches, this is on Copter-4.3:
image

@Ryanf55
Copy link
Collaborator Author

Ryanf55 commented Aug 22, 2023

problem on older branches, this is on Copter-4.3: image

Looks like we might be able to force an upgrade of isort to fix this: home-assistant/core#86892
I'll give it a whirl.

@Ryanf55 Ryanf55 force-pushed the enforce-pre-commit branch 4 times, most recently from 9f2f02a to 48cc7b1 Compare September 11, 2023 14:31
@Ryanf55 Ryanf55 marked this pull request as ready for review September 11, 2023 14:32
@Ryanf55 Ryanf55 force-pushed the enforce-pre-commit branch 2 times, most recently from 173500a to 7a89854 Compare October 31, 2023 00:06
@Ryanf55
Copy link
Collaborator Author

Ryanf55 commented Oct 31, 2023

Rebased and fixed the regression. Dropped changes of installing pre-commit automatically.

ryan@B650-970:~/Dev/ros2_ws/src/ardupilot$ pre-commit run --all-files
Check line ending character (LF).........................................Failed
- hook id: mixed-line-ending
- exit code: 1
- files were modified by this hook

libraries/SITL/SITL_Airspeed.cpp: fixed mixed line endings

@Ryanf55
Copy link
Collaborator Author

Ryanf55 commented Oct 31, 2023

Here's a demo for the CI script with a good PR:

ryan@B650-970:~/Dev/ros2_ws/src/ardupilot$ CI_BUILD_TARGET=pre-commit-cleanliness  ./Tools/scripts/build_ci.sh
...
+ echo 'Checking pre-commit code cleanliness'
Checking pre-commit code cleanliness
+ pre-commit install
pre-commit installed at .git/hooks/pre-commit
+ pre-commit run --all-files --verbose --show-diff-on-failure
Check line ending character (LF).........................................Passed
- hook id: mixed-line-ending
- duration: 0.06s
check for added large files..............................................Passed
- hook id: check-added-large-files
- duration: 0.06s
check that executables have shebangs.....................................Passed
- hook id: check-executables-have-shebangs
- duration: 0.04s
check that scripts with shebangs are executable..........................Passed
- hook id: check-shebang-scripts-are-executable
- duration: 0.06s
check for merge conflicts................................................Passed
- hook id: check-merge-conflict
- duration: 0.05s
check xml................................................................Passed
- hook id: check-xml
- duration: 0.04s
check yaml...............................................................Passed
- hook id: check-yaml
- duration: 0.04s
Formats XML files........................................................Passed
- hook id: format-xmllint
- duration: 0.02s

XML check for libraries/AP_DDS/dds_xrce_profile.xml: OK

black....................................................................Passed
- hook id: black
- duration: 0.07s

All done! ✨ 🍰 ✨
2 files left unchanged.

+ continue
+ echo build OK
build OK
+ exit 0

Now, if we accidentally make a cpp file executable, you get this in the CI logs:

+ pre-commit run --all-files --verbose --show-diff-on-failure
Check line ending character (LF).........................................Passed
- hook id: mixed-line-ending
- duration: 0.06s
check for added large files..............................................Passed
- hook id: check-added-large-files
- duration: 0.06s
check that executables have shebangs.....................................Failed
- hook id: check-executables-have-shebangs
- duration: 0.04s
- exit code: 1

ArduCopter/AP_ExternalControl_Copter.cpp: marked executable but has no (or invalid) shebang!
  If it isn't supposed to be executable, try: `chmod -x ArduCopter/AP_ExternalControl_Copter.cpp`
  If on Windows, you may also need to: `git add --chmod=-x ArduCopter/AP_ExternalControl_Copter.cpp`
  If it is supposed to be executable, double-check its shebang.

check that scripts with shebangs are executable..........................Passed
- hook id: check-shebang-scripts-are-executable
- duration: 0.06s
check for merge conflicts................................................Passed
- hook id: check-merge-conflict
- duration: 0.05s
check xml................................................................Passed
- hook id: check-xml
- duration: 0.03s
check yaml...............................................................Passed
- hook id: check-yaml
- duration: 0.04s
Formats XML files........................................................Passed
- hook id: format-xmllint
- duration: 0.02s

XML check for libraries/AP_DDS/dds_xrce_profile.xml: OK

black....................................................................Passed
- hook id: black
- duration: 0.07s

All done! ✨ 🍰 ✨
2 files left unchanged.

Ryanf55 added a commit to Ryanf55/ardupilot_dev_docker that referenced this pull request Nov 10, 2023
khancyr pushed a commit to ArduPilot/ardupilot_dev_docker that referenced this pull request Nov 11, 2023
@Ryanf55
Copy link
Collaborator Author

Ryanf55 commented Nov 28, 2023

@khancyr Can you create a new tag/release for the dev dockerfiles? v0.1.3 ,which is being used above, results in the failed build because it's missing xmllint.

@khancyr
Copy link
Contributor

khancyr commented Nov 28, 2023

my bad, I haven't seen it wasn't in the release. I have force push a rebuild with the latest version

@Ryanf55 Ryanf55 added CI and removed NeedsTesting labels Nov 29, 2023
@MichelleRos
Copy link
Contributor

Is there an option to disable this pre-vcommit though? I really wouldn't want to be forced to obey specific rules when I'm just doing test development in my own fork.

That would really slow down development.

@Ryanf55
Copy link
Collaborator Author

Ryanf55 commented Nov 29, 2023

Is there an option to disable this pre-vcommit though? I really wouldn't want to be forced to obey specific rules when I'm just doing test development in my own fork.

That would really slow down development.

Yep! git commit --no-verify disables it when you commit.

That said, do you have examples of this slowing down development and specifics on which hooks or workflows have affected you? We only run a bare minimum set of hooks right now.

You can also disable specific hooks like so: https://stackoverflow.com/questions/7230820/skip-git-commit-hooks

@tridge tridge added WikiNeeded needs wiki update MergeOnCIPass and removed DevCallEU labels Nov 29, 2023
@@ -424,6 +424,13 @@ for t in $CI_BUILD_TARGET; do
continue
fi

if [ "$t" == "pre-commit-cleanliness" ]; then
echo "Checking pre-commit code cleanliness"
pre-commit install
Copy link
Contributor

Choose a reason for hiding this comment

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

@Ryanf55 that means that after a first run precommit will be enable on git commit. Probably not the best move. Just doing the pre-commit run is enough and don't mess the git config for now (I still want pre-commit)

Copy link
Collaborator Author

@Ryanf55 Ryanf55 Nov 29, 2023

Choose a reason for hiding this comment

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

Are you saying that local developers run build_ci with specific arguments to hit this code block?

Without the above line, the job would fail in CI because pre-commit wasn't enabled for the repo.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Ryanf55 I think Pierre is saying we shouldn't be making this permanent change to their setup in here.

We can do it as part of the machine setup in the github workflows instead, yes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, but I thought it would be ok because this is a CI script. I'm happy to move it if you think we need to worry about devs running this check and then being surprised pre-commit is now enabled.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, please move it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done! Let's see how it goes...

Copy link
Contributor

Choose a reason for hiding this comment

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

@Ryanf55 normally there is not issue to use pre-commit run --all-files on CI, install isn't need for CI

@magicrub
Copy link
Contributor

@Ryanf55 This is tagged MergeOnCiPass, and it's passing, but it has a merge conflict

@Ryanf55
Copy link
Collaborator Author

Ryanf55 commented Jan 13, 2024

@Ryanf55 This is tagged MergeOnCiPass, and it's passing, but it has a merge conflict

I never got an answer on Pierre's comment. I don't have a path forward on this without feedback on the raised concern.

@magicrub
Copy link
Contributor

@khancyr ping!

Signed-off-by: Ryan Friedman <[email protected]>
* These are not scripts and should not be executable

Signed-off-by: Ryan Friedman <[email protected]>
* It should be moved to the workflow

Signed-off-by: Ryan Friedman <[email protected]>
- name: install pre-commit
shell: bash
run: |
pre-commit install
Copy link
Contributor

Choose a reason for hiding this comment

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

why need this ? we don't run git command on CI, so the install isn't need normally, or I miss something ?

Copy link
Contributor

Choose a reason for hiding this comment

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

are you sure that shouldn't be pip install pre-commit instead ?

Copy link
Contributor

Choose a reason for hiding this comment

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

OK i have checked and we got pre-commit into the docker image already so we are fine. the pre-commit install is just to install the git hook so that isn't need on CI

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You are conflating installation of the pre-commit tool in the environment and installation of the git hooks into the repo.
https://pre-commit.com/#3-install-the-git-hook-scripts

We need both at some point before calling pre-commit run in ArduPilot.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nope you don't need to install the git hook to use precommit.

Copy link
Contributor

@khancyr khancyr left a comment

Choose a reason for hiding this comment

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

something is wrong with this commit as it moves the submodules ! ce6e3fb (the xmllint one's )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Run pre-commit for ArduPilot source automatically
7 participants