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

Create workflow to automate code formatting #11911

Merged
merged 6 commits into from
Sep 27, 2023
Merged

Conversation

jeskew
Copy link
Contributor

@jeskew jeskew commented Sep 21, 2023

This PR adds a workflow to automate running dotnet format per the discussion in #11691. Since we can't create a PR from a GitHub action, the workflow is a bit of a Rube Goldberg machine:

  1. There is a new GitHub action added that will run dotnet format and then either commit and push the results directly to the source branch (if the branch is not protected) or commit and push the results to a branch named formatted-refs/heads/<branch name> otherwise. This action can be run on demand for any branch and will be run weekly for the main branch.
  2. If the push went to a formatted-... branch, the action will also create a new issue with the name of the branch in the body and a code-formatting-automation label.
  3. There is a new policy that will scan for issues with the code-formatting-automation label and formatting-refs/heads/main in the body and create a PR to merge formatting-refs/heads/main into main with the code-formatting-automation label.
  4. There is another policy that will approve and enable automerge for PRs with the code-formatting-automation label that were opened by the MS policy bot.

I was able to test steps 1 and 2 with both protected and unprotected branches. The policy yml (for steps 3 & 4) is untested since policies must be committed to the repository's default branch before they will run.

@jeskew jeskew requested review from asilverman and a team September 21, 2023 21:09
@github-actions
Copy link
Contributor

github-actions bot commented Sep 21, 2023

Test this change out locally with the following install scripts (Action run 6328626962)

VSCode
  • Mac/Linux
    bash <(curl -Ls https://aka.ms/bicep/nightly-vsix.sh) --run-id 6328626962
  • Windows
    iex "& { $(irm https://aka.ms/bicep/nightly-vsix.ps1) } -RunId 6328626962"
Azure CLI
  • Mac/Linux
    bash <(curl -Ls https://aka.ms/bicep/nightly-cli.sh) --run-id 6328626962
  • Windows
    iex "& { $(irm https://aka.ms/bicep/nightly-cli.ps1) } -RunId 6328626962"

@github-actions
Copy link
Contributor

Test Results

     132 files  ±0       132 suites  ±0   3h 24m 27s ⏱️ - 5m 41s
10 552 tests ±0  10 552 ✔️ ±0  0 💤 ±0  0 ±0 
50 759 runs  ±0  50 759 ✔️ ±0  0 💤 ±0  0 ±0 

Results for commit 940fbfa. ± Comparison against base commit e964970.

@@ -0,0 +1,49 @@
id: prAutoApproval.formatter
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any documentation you can point to in a comment here? I'm having trouble finding anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

So I guess https://microsoft.github.io/GitOps/policies/resource-management.html?tabs=event-conditions%2Csearch-frequency was the documentation I was looking for. Does it make sense to add this link as a comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe we should have a readme.md file in .github/policies? This is the repo's third policy file, but I did need to search emails and Teams chats to find documentation on the structure of these files.

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 Policy Service doesn't seem to like .md files in the .github/policies folder. I'll add that note to the repo's CONTRIBUTING.md file instead.

@@ -0,0 +1,75 @@
name: Run formatter
# This action will be run weekly against main and can be run on-demand against main.
# It will run an automated code formatter and commit and push the changes (if any). If the source branch is protected, then the workflow will create a new to commit and push the changes.
Copy link
Contributor

Choose a reason for hiding this comment

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

will create a new (missing word) to commit

CL, branch, PR?

@StephenWeatherford
Copy link
Contributor

Thanks!

@@ -7,6 +7,7 @@ indent_size = 4
charset = utf-8
Copy link
Contributor

Choose a reason for hiding this comment

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

So is it possible to test out policies in a forked branch? (I hate that actions and build scripts are difficult to verify pre-merge.)

Copy link
Contributor Author

@jeskew jeskew Sep 26, 2023

Choose a reason for hiding this comment

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

Unfortunately not. I was able to test out the GH action declared in .github/workflows/run-formatter.yml before opening the PR (#11910 was opened automatically when I enabled branch protection on the jeskew/automated-formatting branch; a prior run pushed directly to jeskew/automated-formatting before branch protection was enabled).

Github policies, however, will by design only run once they are committed to the repo's default branch. The GitOps.YmlValidation workflow checks the syntax of policies, but it can't verify that a policy will do what we expect it to.

@majastrz
Copy link
Member

@jeskew Are the formatting settings consistent between VS, VS code, and dotnet format?

@jeskew
Copy link
Contributor Author

jeskew commented Sep 26, 2023

Are the formatting settings consistent between VS, VS code, and dotnet format?

@majastrz Yes, they should all be following the directives in the .editorconfig file in the repository root. You can override those directives in both VS Code and VS with IDE settings, so some workspaces will be inconsistent. Anthony's suggestion was to autocut a PR weekly that runs dotnet format to keep the style consistent without, say, forcing all PRs to pass dotnet format --verify-no-changes prior to merging.

@majastrz
Copy link
Member

Are the formatting settings consistent between VS, VS code, and dotnet format?

@majastrz Yes, they should all be following the directives in the .editorconfig file in the repository root. You can override those directives in both VS Code and VS with IDE settings, so some workspaces will be inconsistent. Anthony's suggestion was to autocut a PR weekly that runs dotnet format to keep the style consistent without, say, forcing all PRs to pass dotnet format --verify-no-changes prior to merging.

Makes sense. If we do have VS code or VS specific directives in use, we should remove them, so all methods of editing converge on the same formatting. (I wouldn't block the PR on this, though.)

Copy link
Member

@anthony-c-martin anthony-c-martin left a comment

Choose a reason for hiding this comment

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

🚢🚢🚢

@jeskew jeskew enabled auto-merge (squash) September 27, 2023 15:53
@jeskew jeskew merged commit 0bdd85b into main Sep 27, 2023
48 checks passed
@jeskew jeskew deleted the jeskew/automated-formatting branch October 2, 2023 17:40
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.

None yet

4 participants