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

CI(clang-format): Run faster and post fixes as code suggestions if possible when running on a PR #3483

Merged
merged 1 commit into from
Mar 13, 2024

Conversation

echoix
Copy link
Member

@echoix echoix commented Mar 10, 2024

Replaces #3284
Closes #3284

This PR replaces #3284.
It is an adaptation of OSGeo/grass-addons#1038.

This integrates the ideas of #3284, which is to run clang-format way faster, in the 30-second range. It also sets the infrastructure needed to post code suggestions to the PRs of linter fixes. Clang-format is now the first here to have it. It is designed, and planned, to have some of our other tools add code suggestions as fixes to PRs to facilitate completing them and applying small fixes without a full round of feedback from the author.

Here is what I wrote for OSGeo/grass-addons#1038:

After a hundred iterations or so, I've got a solution that I'm satisfied with. This allows linter formatting in a PR from a forked pull request to do so without any special secret access, and also be able to add PR review comments, that need pull-request: write permissions. These permissions cannot be granted for PRs coming from forks. Since all the PRs come from forks, we could never use any write permissions from our forks. It also avoids using pull_request_target with a checkout of the external code, which is something that should never be done.

Instead, it runs another workflow triggered by the completion of another workflow, through the workflow_run trigger. That second workflow takes 3 seconds, max 10 including startup. It won't appear as a check for a PR, but still manages to use the event's payload information available to apply to the PR. The fixes that need to be added as code suggestion comments are fed through the upload of a diff artifact. It is then easily downloaded in the second workflow.

Note that PR review comments can only be added to lines in the diff context, that is 3 lines above or below the changed lines of a PR, just like the web interface: the API isn't different. The code suggestion comments are added on a best-effort basis to facilitate contributors to finish a PR. So only upgrading the clang-format version in a PR won't be able to post suggestions on all changes in the repo, since the affected lines aren't changed in that PR.

I designed this solution to be able to accept and post code suggestions for other tools in the near future. Other workflows could upload diffs from multiple tools in the same artifact, and only the tool name and the file name needs to be handled by conditional expressions (if: ...) or a bash loop (careful to not inject the file names from the artifact, as it is untrusted inputs; prefer whitelisting the expected file names to match to). For now, it is implemented for clang-format.

This time, I created a new organization, so I could develop with the forked-repo situation from the start. Once this PR is merged, the "callback" workflow that post code suggestions will be enabled, and will work right away.

Examples (when intentionally creating formatting errors):
image
They can be batch committed as always by going to the files changed tab and adding the suggestions to a batch, and creating one commit.

image
image

@github-actions github-actions bot added the CI Continuous integration label Mar 10, 2024
@echoix
Copy link
Member Author

echoix commented Mar 11, 2024

@wenzeslaus can you take a look at this on your next time on the repo? I'd love to see this functionality finally integrated and used by all ;)

@neteler neteler added this to the 8.4.0 milestone Mar 12, 2024
@echoix echoix mentioned this pull request Mar 12, 2024
Copy link
Member

@wenzeslaus wenzeslaus left a comment

Choose a reason for hiding this comment

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

Well, this looks like a great accomplishment. I'm curious about balance of clear usefulness for small changes and of how this will work for places where change triggers a large reformatting. That needs testing in practice, though. However, the suggestion-based workflow with reviewdog seems to address all my previous concerns (and bad experience) with automation for fixes. This may save us some review time in the PRs to come, thanks!

@echoix
Copy link
Member Author

echoix commented Mar 13, 2024

If ever we happen to have a really large code change, that on top of that is really badly formatted, and that there was no other limits preventing from having that much of comments added (like the 10 annotations per step limit: annotations is when a log output uses the special formatting codes or is matched by a problem matcher, making yellow or red boxes throughout the code or in the summary), then I would look into it at a later time. If it happens, the PR would already be big itself and in bad shape.

@echoix echoix merged commit b1d7a4f into OSGeo:main Mar 13, 2024
26 checks passed
@echoix echoix deleted the clang-format-pr-code-suggestions branch March 13, 2024 01:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI Continuous integration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants