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: add workflow to format whitespaces #236

Merged
merged 19 commits into from
Oct 20, 2023
Merged

ci: add workflow to format whitespaces #236

merged 19 commits into from
Oct 20, 2023

Conversation

winstxnhdw
Copy link
Contributor

@winstxnhdw winstxnhdw commented Aug 31, 2023

Summary

This PR adds a GitHub Action workflow that will, on every change to a .cs file, trigger a workflow which will fix the whitespace of such files, and commit the produced changes. If no whitespace offenses are found, nothing is committed. The workflow was written to complete as quickly as possible to provide immediate user feedback.

Reason

Even with EditorConfig configured, I often find that the whitespaces in my commits somehow fail the existing check-format workflow. This is inconvenient as I have to commit the result from dotnet tool run dotnet-format multiple times while working on a PR. If you check the git diff of the offending commits, you will notice that they appear identical. Furthermore, we are only fixing the whitespace in this workflow which shouldn't introduce any logical bugs if the tool were to detect a false positive.

TL;DR Free formatting at zero cost???

Copy link
Contributor

@marcin-krystianc marcin-krystianc left a comment

Choose a reason for hiding this comment

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

I'm still not 100% convinced whether it is actually good idea to update user's branches on push, because it can cause unexpected merge-conflicts (user pushes a change, then does more code changes, and later before another push merge is necessary).

There is only one way to find out - lets give it a try 😄

.github/workflows/formatter.yml Outdated Show resolved Hide resolved
.github/workflows/formatter.yml Outdated Show resolved Hide resolved
.github/workflows/formatter.yml Outdated Show resolved Hide resolved
@winstxnhdw
Copy link
Contributor Author

because it can cause unexpected merge-conflicts (user pushes a change, then does more code changes, and later before another push merge is necessary).

I understand that this might be a concern but usually whitespace conflicts are handled well by git and a simple git pull --rebase will solve it without further intervention in my own experience.

winstxnhdw and others added 2 commits September 5, 2023 00:02
Co-authored-by: Marcin Krystianc <marcin.krystianc@gmail.com>
@winstxnhdw
Copy link
Contributor Author

winstxnhdw commented Sep 4, 2023

Welp @marcin-krystianc, it seems that GitHub doesn't allow workflows to write to forks even with "Allow edits and access to secrets by maintainers " enabled. This means that this approach won't work.

I was testing this on a branch of the same repository, and assumed it would work on a fork. I apologise for wasting your time.

I'll look into it a bit more before closing this PR.

@marcin-krystianc
Copy link
Contributor

it seems that GitHub doesn't allow workflows to write to forks

I don't think that everything is lost. Maybe the workflow for the fork can run directly in the forked repository, that would solve the problem with permissions?

@winstxnhdw
Copy link
Contributor Author

winstxnhdw commented Sep 4, 2023

Maybe the workflow for the fork can run directly in the forked repository, that would solve the problem with permissions?

I think it's already doing that.. I'll have to play around with it more.

@winstxnhdw
Copy link
Contributor Author

winstxnhdw commented Sep 14, 2023

Ok. Good news. I just tested this. It works.

I created a GitHub organisation, setup the repository with the workflow, forked the repository, created a PR, and the workflow ran fine on my fork.

There's still some issues left to smooth out.

winstxnhdw and others added 7 commits September 15, 2023 00:46
Bumps [actions/checkout](https://github.com/actions/checkout) from 3 to 4.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](actions/checkout@v3...v4)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
@winstxnhdw
Copy link
Contributor Author

Funnily enough, what always worked was running the workflow on a push trigger and just ignoring the master branch. Not sure if you guys will be okay with this.

@winstxnhdw
Copy link
Contributor Author

It's not super ideal but I made it skip all non-fork repositories.

@marcin-krystianc
Copy link
Contributor

I'll have a look, for the time being I just found this link which mentions that this automatic formatting solution will not work in all cases.

@winstxnhdw
Copy link
Contributor Author

winstxnhdw commented Sep 15, 2023

Important caveat 1: Due to token restrictions on public repository forks these workflows do not work for pull requests raised from forks. Private repositories can be configured to enable workflows from forks to run without restriction.

This was the problem I faced but it seems to work for now in the current implementation. We aren't using on: pull_request anymore but on: push.

As you can see here, it runs fine.

@winstxnhdw
Copy link
Contributor Author

winstxnhdw commented Sep 20, 2023

Hi @marcin-krystianc, with some advice from @AdnaneKhan, I managed to get it to work with pull_request_target. It's pretty ideal now.

It will only work once this PR is merged, so we can't test it on this repository but you can see my tests on my own organisation/repository PR here.

Copy link
Contributor

@marcin-krystianc marcin-krystianc left a comment

Choose a reason for hiding this comment

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

OK, let's give it a try.

@marcin-krystianc marcin-krystianc merged commit d0d1327 into G-Research:master Oct 20, 2023
147 checks passed
@winstxnhdw winstxnhdw deleted the formatter branch October 20, 2023 11:08
@marcin-krystianc
Copy link
Contributor

All right, it seems it works in bot cases, for PR from the original repository and from the forked repository as well. Nice job!!

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.

3 participants