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 only_changed input to run rubocop only against changed files #103

Merged

Conversation

toy
Copy link
Contributor

@toy toy commented May 2, 2024

Add an input to limit running rubocop only against files changed in the PR.

Had to switch to bash for process substitution and working with arrays.

Ignore if there are more than 100 changed files. Main reason is to protect from possibly hitting the command line length limit. In reality the command line length limit most probably (depends on OS) allows much higher number of files and if needed can be calculated or provided as one more input.

Fix ci workflow to fetch all commits for pr branch plus head commit of base branch to be able to find changed files.

Will conflict with #102, I will update whichever is not merged first

@toy toy force-pushed the option-to-test-only-changed-files-reviewdog branch 4 times, most recently from 2aac704 to cd3cfda Compare May 8, 2024 16:10
@toy toy mentioned this pull request May 13, 2024
@toy toy force-pushed the option-to-test-only-changed-files-reviewdog branch from cd3cfda to 998b37a Compare May 15, 2024 17:37
@toy
Copy link
Contributor Author

toy commented May 23, 2024

@javierjulio May I ping you for input?

@toy toy force-pushed the option-to-test-only-changed-files-reviewdog branch 3 times, most recently from 02f0ae4 to c83f927 Compare May 31, 2024 11:56
@toy toy force-pushed the option-to-test-only-changed-files-reviewdog branch from c83f927 to 2719576 Compare June 25, 2024 12:10
@haya14busa
Copy link
Member

Can you add a test? #103

@toy
Copy link
Contributor Author

toy commented Jun 25, 2024

@haya14busa Can you suggest how/what to test? I can think of either adding test branches and test workflows to check complete thing, or just unit testing script.sh similar to how it is done in test/rdjson_formatter/test.sh

@@ -70,6 +74,8 @@ runs:
INPUT_TOOL_NAME: ${{ inputs.tool_name }}
INPUT_USE_BUNDLER: ${{ inputs.use_bundler }}
INPUT_WORKDIR: ${{ inputs.workdir }}
BASE_REF: ${{ github.event.pull_request.base.sha }}
HEAD_REF: ${{ github.sha }}
Copy link

@thiemonipro thiemonipro Jun 26, 2024

Choose a reason for hiding this comment

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

First off all thanks for this PR, it seems like a really useful addition that could speed up our github action by a lot as using rubocop takes about 5 minutes currently to run over the whole repo.

Should this look at github.event.pull_request.base.sha instead?

I tried out your branch in a github action workflow but the HEAD_REF commit seemed wrong and gave me the following error: fatal: Invalid revision range

After creating my own branch based on this but with the change to the HEAD_REF sha the action succeeded

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean using github.event.pull_request.head.sha for HEAD_REF?

The idea of this change is to give to rubocop only files introduced in the PR branch, but we don't want to also give to rubocop files that changed on the base branch especially if it diverged a lot.

This could be resolved in different ways, but in current implementation the idea is to also fetch bare minimum to not slowdown by fetching, so usage of actions/checkout with default depth: 1 is assumed.

Also github.sha is pointing at automatically created merge commit of base into head (docs), while github.event.pull_request.head.sha points at the PR branch head.

If we would have all commits available it would be simpler to do diff base...head (note three dots) where head can be both merge commit or the tip of the branch, but if we fetch only two tips of the branches, diff base...head is not possible, as not all needed commits are available.
Otherwise diff base..merge-head should give much smaller diff then diff base..head.

So most probable cause of fatal: Invalid revision range is because your workflow either doesn't fetch the tip of the base branch or fetches not the automatically created merge head (default using actions/checkout).

@haya14busa
Copy link
Member

I think we can add a job (or workflow) with only_changed=true and add new test files under test dir.

We can at least check the behavior with this pr.
As you mentioned it, it would be cool if we can prepare a test branch too, but it's optional.

@@ -85,9 +88,41 @@ else
BUNDLE_EXEC="bundle exec "
fi

if [ "${INPUT_ONLY_CHANGED}" = "true" ]; then
echo '::group:: Getting changed files list'

Copy link
Member

Choose a reason for hiding this comment

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

How about running `git fetch --depth 1 origin "${BASE_REF}" here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems like a good idea, compared to adding a step to do it manually. Only concern is if remote is named differently or if checkout is very custom, so the command breaks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doing it after checking commit availability should be the good way to make it simple by default and solvable if default doesn't work: eb0eda0

@haya14busa
Copy link
Member

Ideally, it would be great if we can use the existing solution like https://github.com/tj-actions/changed-files for getting changed files so that we can reuse the similar solution for other reviewdog actions.

But we use robocop specific command (rubocop --list-target-files ), so it's optional.

@toy toy force-pushed the option-to-test-only-changed-files-reviewdog branch from eb0eda0 to 5ea6c6e Compare June 27, 2024 17:40
toy and others added 3 commits June 28, 2024 14:07
Switch to bash for process substitution and working with arrays.

Ignore if there are more than 100. Protecting from possibly hitting the
command line length limit, it can be much higher and can be calculated
or also extracted as one more input if needed. The effect from limiting
number of files processed by rubocop is also smaller.

Fix ci workflow to fetch all commits for pr branch plus head commit of
base branch to be able to find changed files.

Co-authored-by: Oliver Günther <mail@oliverguenther.de>
@v-kumar
Copy link

v-kumar commented Jul 12, 2024

Any update on this? Would love to see this merged.

@toy
Copy link
Contributor Author

toy commented Jul 12, 2024

@v-kumar I did an unsuccessful attempt and afterwards came up with a working way to test this, but didn't yet have time. The idea is to test by running the script while mocking binaries by manipulating PATH

@toy toy force-pushed the option-to-test-only-changed-files-reviewdog branch from 5ea6c6e to 705dbf7 Compare July 12, 2024 19:55
@toy toy requested a review from haya14busa July 12, 2024 19:56
@toy toy force-pushed the option-to-test-only-changed-files-reviewdog branch from 13a7ab4 to 4e840d4 Compare July 12, 2024 20:21
@toy
Copy link
Contributor Author

toy commented Jul 12, 2024

@haya14busa I added tests of script.sh without using any frameworks

- name: Check when there are too many relevant files
run: |
git checkout ${{ github.sha }}
touch a{00..100}.rb
Copy link
Member

Choose a reason for hiding this comment

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

Should we create 101 (more than 100) files here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The range is inclusive, so 101 files are generated, I can change to {01..101} if it will be clearer

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 accidentally tested that with exactly 100 it passes file names to rubocop: https://github.com/reviewdog/action-rubocop/actions/runs/9913233268/job/27389778004

Copy link
Member

Choose a reason for hiding this comment

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

ok.

I'm a bit confused, but why does the test result shows " No relevant files for rubocop, skipping"?
image
https://github.com/reviewdog/action-rubocop/actions/runs/9913690348/job/27391225472

Copy link
Contributor Author

@toy toy Jul 14, 2024

Choose a reason for hiding this comment

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

Great that you spotted it, I didn't notice that change from ruby to bash script for rubocop mock was producing files prefixed with ./. I reverted to ruby script for simplicity and hardened the test by explicitly validating that reviewdog mock was or was not called.

@toy toy force-pushed the option-to-test-only-changed-files-reviewdog branch from 4e840d4 to 2f1682e Compare July 14, 2024 10:52
@toy toy force-pushed the option-to-test-only-changed-files-reviewdog branch from 2f1682e to baffa02 Compare July 14, 2024 10:58
Copy link
Member

@haya14busa haya14busa left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution! 💯

@haya14busa haya14busa merged commit e44fb2c into reviewdog:master Jul 14, 2024
8 checks passed
@review-dog
Copy link
Member

Hi, @toy! We merged your PR to reviewdog! 🐶
Thank you for your contribution! ✨

We just invited you to join the @reviewdog organization on GitHub.
Accept the invite by visiting https://github.com/orgs/reviewdog/invitation.
By joining the team, you'll be a part of reviewdog community and can help the maintenance of reviewdog.

Thanks again!

Copy link
Contributor

🚀 [bumpr] Bumped! New version:v2.17.0 Changes:v2.16.0...v2.17.0

@toy toy deleted the option-to-test-only-changed-files-reviewdog branch July 14, 2024 20:23
${INPUT_RUBOCOP_FLAGS} \
--require ${GITHUB_ACTION_PATH}/rdjson_formatter/rdjson_formatter.rb \
--format RdjsonFormatter \
--fail-level error \
Copy link

@kehra kehra Aug 21, 2024

Choose a reason for hiding this comment

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

@toy
Did you want to intentionally change(set) fail-level?
I think it seems a bug.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kehra It was intentional, due to turning on pipefail, which in turn is needed for pipes in getting the list of changed files.
Before this commit the exit code of rubocop … | reviewdog … was the exit code of reviewdog. But with pipefail and without changing fail-level, any rubocop violation would make the script have an error exit code, which will be confusing, as both violations will be reported and the action will fail.

Copy link

Choose a reason for hiding this comment

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

@toy
Thank you for your response.
I think this change caused our CI to miss non-error level(e.g. warning) violations.
However, my colleague has made it possible to specify the fail-level in #120, so it is not a problem 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kehra #120 looks good, it allows to "shoot in the foot" by changing formatter, but I think it is better to restrict less.

I'm still wondering about your CI - any violations of any level will create either annotation or comment to PR, so the rubocop outcome will be failing, but not the rubocop action itself, am I missing something?

Copy link

Choose a reason for hiding this comment

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

@toy
Hmm.
I might be wrong, so I'll investigate this again 💦
Sorry for bothering you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No worries, I'm just trying to understand if there is some case that allows action to not report violations. Please reply with your findings

moznion added a commit to moznion/action-rubocop that referenced this pull request Aug 21, 2024
Since PR reviewdog#103 introduced the default --fail-level flag,
the severity level has become unchangeable due to this
modification.
Ideally, this flag (and others) should be overridable,
but the RuboCop command prioritizes the flags that are
specified last. This commit moves the user-specified
flags to the end of the hard-coded flags, allowing them
to be overridden.

Signed-off-by: moznion <moznion@mail.moznion.net>
ohbarye pushed a commit that referenced this pull request Aug 21, 2024
Since PR #103 introduced the default --fail-level flag,
the severity level has become unchangeable due to this
modification.
Ideally, this flag (and others) should be overridable,
but the RuboCop command prioritizes the flags that are
specified last. This commit moves the user-specified
flags to the end of the hard-coded flags, allowing them
to be overridden.

Signed-off-by: moznion <moznion@mail.moznion.net>
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.

6 participants