Skip to content

Clean up your code with GitHub Action. Save your time from having to do a code review and make corrections and also from having to enter a commit message each time. The commit with the changes is created automatically.

License

Notifications You must be signed in to change notification settings

ArturWincenciak/ReSharper_CleanupCode

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ReSharper CLI CleanupCode

Visit the blog post to read more about the context of use cases

Zaoszczędź swój czas, niech kod wyczyści się sam – ReSharper CLI CleanupCode GitHub Action

GitHub Action automatically cleans up your code and creates a commit with the changes in your remote repository.

Save your time from having to do a code review and make corrections and also from having to enter a commit message each time. The commit with the changes is created automatically.

Automatically clean up your code

That is a GitHub Action that allows you to run ReSharper's CleanupCode Command-Line Tool in order to automatically apply code style rules and fix code issues in your project.

CleanupCode will read the following preferences from DotSettings files:

Inputs

The following input parameters can be passed to the action:

solution

Solution or project file to be cleaned up.

  • Required: true

fail_on_reformat_needed

Determines whether the action should fail if the code needs to be reformatted.

  • Required: false
  • Default: no
  • Accepted values: yes, no

jb_cleanup_code_arg

Additional arguments to pass to the ReSharper's CleanupCode Command-Line Tool. Configure the tool with command-line parameters, e.g.: --verbosity=INFO --profile=Built-in: Full Cleanup --exclude=**UnitTests/**.*.

  • See more here in that clear and concise specification
  • Notice: Never use quotation marks " even if the value contains spaces. The command separator is -- (two hyphens in a row) and this is enough to split arguments. If you use quotation marks, the behavior is undefined.
  • Required: false
  • Default: --verbosity=WARN

In that demo project, you will find all the knowledge you need to effectively start using this action.

Try your self demo project

  1. Go to the demo project
  2. Fork this repo
  3. Create a Pull Request
  4. Go to Actions and observe the action in action
  5. Check out history of your repo and see newly created commit

The simplest way to usage

steps:
  - name: Cleanup Code
    uses: ArturWincenciak/ReSharper_CleanupCode@v2.0
    with:
      solution: 'ReSharperCleanupCodeDemo.sln'

Interrupt your CI/CD pipeline if Cleanup detected the code needs to be cleaned up

steps:
  - name: Cleanup Code
    uses: ArturWincenciak/ReSharper_CleanupCode@v2.0
    with:
      solution: 'ReSharperCleanupCodeDemo.sln'
      fail_on_reformat_needed: 'yes'

If this setting is enabled, the process will stop and return an error code if it finds that the code needs to be cleaned up. This can be helpful for stopping the pipeline from continuing if there are problems with the code.

No interrupt your CI/CD pipeline if Cleanup detected the code needs to be cleaned up

steps:
  - name: Cleanup Code
    uses: ArturWincenciak/ReSharper_CleanupCode@v2.0
    with:
      solution: 'ReSharperCleanupCodeDemo.sln'
      fail_on_reformat_needed: 'no'

At times, you may want to disable the interruption and continue with the execution of your CI/CD pipeline, for instance, when you need to debug subsequent steps without performing clean up.

Perform clean up your code locally with a fully automated commit and save your time

For such a case, I have prepared a ready-made script that you can run locally. This script will perform clean up code and create a commit with the changes in your local repository. This will save your time from having to enter a commit message each time. The commit with the changes will be created automatically.

This script can be attached to the git hooks, however, attaching this script to the pre-commit git hook is not advisable as it may slow down our work considerably due to the lengthy clean up code process. It may be more beneficial to add this script to the pre-push git hook instead.

Fully configured and ready to use

name: ReSharper CLI CleanupCode

on: [ push ]

jobs:
  cleanup:
    runs-on: ubuntu-latest
    name: Cleanup Code
    
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup .NET
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: 7.0.x

      - name: Restore Dependencies
        run: dotnet restore ReSharperCleanupCodeDemo.sln
          
      - name: Cleanup Code
        id: cleanup
        uses: ArturWincenciak/ReSharper_CleanupCode@v4.14
        with:
          solution: 'ReSharperCleanupCodeDemo.sln'
          fail_on_reformat_needed: 'no'
          jb_cleanup_code_arg: '--verbosity=INFO --profile=Built-in: Full Cleanup --exclude=**UnitTests/**.*'

Sequence of actions performed by the prepared GitHub Action

  • Checkout: download the source code from the current repository where the Action was initiated
  • Setup .NET: install the specified version of .NET on the virtual machine where the Action is run
  • Restore Dependencies: restore all project dependencies, such as NuGet libraries
  • Cleanup Code clean up the code

Cleanup Code works perfectly with Inspect Code

There are situations where Cleanup Code does not do the entire job for us, but we can still greatly help ourselves and speed up ours Code Review process by adding an additional automatic step that performs an inspection of the code and, adds a comments to the submitted Pull Request on our behalf.

Here in that demo project ReSharper CLI CleanupCode GitHub Action Demo, I show you how to combine ReSharper CLI CleanupCode and ReSharper CLI InspectCode using GitHub Action Definition that contains two jobs: cleanup and inspection.

Refer to the demo project and gain all the knowledge on how to speed up your daily development process.

name: ReSharper CLI CleanupCode

on: [ push ]

jobs:
  cleanup:
    runs-on: ubuntu-latest
    name: Cleanup Code
    
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup .NET
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: 7.0.x

      - name: Restore Dependencies
        run: dotnet restore ReSharperCleanupCodeDemo.sln
          
      - name: Cleanup Code
        id: cleanup
        uses: ArturWincenciak/ReSharper_CleanupCode@v3.0
        with:
          solution: 'ReSharperCleanupCodeDemo.sln'
          fail_on_reformat_needed: 'no'
          jb_cleanup_code_arg: '--verbosity=INFO --profile=Almost Full Cleanup --exclude=**UnitTests/**.*'
  
  inspection:
    runs-on: ubuntu-latest
    name: Inspect Code
    needs: cleanup

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup .NET
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: 7.0.x

      - name: Restore Dependencies
        run: dotnet restore ReSharperCleanupCodeDemo.sln

      - name: Inspect code
        uses: muno92/resharper_inspectcode@1.6.5
        with:
          solutionPath: ./ReSharperCleanupCodeDemo.sln
          failOnIssue: 1
          minimumSeverity: notice
          solutionWideAnalysis: true

ReSharper CLI InspectCode - that one I've found in the Marketplace and used in the demo project, it was an inspiration for me to create my own ReSharper CLI CleanupCode the second tool in this toolkit. These two tools complement each other well and produce nice results.