xt0rted/dotnet-format

fix upon pull request is not working

cilerler opened this issue · 7 comments

As you may see here, I tried to make pull requests as fixer action.

Two issues occurred,

  1. It triggered a new action upon commit
  2. steps.format.outputs.has-changes returns true even there is no file to commit, which causes an additional error you may find it in here

I don't want to use a comment; if the formatting is wrong, it should be fixed it right at the commit into the pull request in my case.

Hi @cilerler, the link to your workflow file is giving me a 404 error. Could you include a copy of it here in the issue for me to see?

Hi, @xt0rted,

my bad, I just switched it to a public repository, please let me know if you will still have any issue to access it.

There's a few things going on here.

  1. You don't need to use xt0rted/pull-request-comment-branch with the pull_request event. Instead you can use ${{ github.head_ref }} or ${{ github.event.pull_request.head.ref }} during checkout and push.

  2. Since you don't want the push from this to trigger another build you should use GITHUB_TOKEN instead of a Personal Access Token. When using this token persist-credentials isn't needed on the checkout step.

  3. The extra blank line in the commit message is needed for co-authored-by to be picked up by GitHub. This part of the commit message is optional, I found it useful to know who caused the commit to be created.

Give this workflow a try. It's working in one of my test repos. I'll add this to the readme as long as it works for you.

name: Format check on pull request
on: pull_request
jobs:
  dotnet-format:
    runs-on: ubuntu-latest
    steps:
      - name: Install dotnet-format
        run: |
          echo "::set-env name=PATH::${PATH}:${HOME}/.dotnet/tools"
          dotnet tool install -g dotnet-format

      - name: Checkout repo
        uses: actions/checkout@v2
        with:
          ref: ${{ github.head_ref }}

      - name: Run dotnet format
        id: format
        uses: xt0rted/dotnet-format@v1
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          action: "fix"
          only-changed-files: true

      - name: Commit files
        if: steps.format.outputs.has-changes == 'true'
        run: |
          git config --local user.name "github-actions[bot]"
          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git commit -a -m 'Automated dotnet-format update

          Co-authored-by: ${{ github.event.pull_request.user.login }} <${{ github.event.pull_request.user.id }}+${{ github.event.pull_request.user.login }}@users.noreply.github.com>'

      - name: Push changes
        if: steps.format.outputs.has-changes == 'true'
        uses: ad-m/github-push-action@v0.5.0
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.head_ref }}

Thank you for a very detailed explanation.

It works in an initial pull-request scenario. However, if I update a none *.cs file like pull_request.yml it returns an error.

Reproduce steps

  1. Create a simple pull request, it will trigger a workflow, and it will work fine
  2. Update a *.cs file in that branch, it will trigger a workflow, and it will work fine
  3. Update a none *.cs file, it will trigger a workflow, and it will fail as nothing to commit, working tree clean

So merely steps.format.outputs.has-changes should return true only if it fixes any file. If no file fixed, it should return false.

image

Hi @cilerler, the issue is because in your .editorconfig line endings are set to crlf but this is running on a Ubuntu VM so lf is being used on checkout.

dotnet format converts the lf tocrlf so there are changed files, but Git converts those back to lf and doesn't think there's anything to commit. This is why you're getting that error.

If you update the workflow to use a Windows VM this won't be an issue. Or you can use a Ubuntu VM and run this at the start of your workflow:

- run: git config --global core.autocrlf true

If you do switch to a Windows VM the installation step can be simplified to just dotnet tool install -g dotnet-format.

Here's a copy of your PR with each of the changes so you can see exactly what I tested https://github.com/xt0rted/orange/pull/2.

You pinpointed the issue, thank you. 🤗

I can confirm that runs-on: ubuntu-latest is now working as expected.

image

The next step is adding action/caching to ad-m/github-push-action@v0.5.0 since it consumes so much time to build a docker image, every single time.

Thanks again

I'll add this to the readme as long as it works for you.

Hi, can you update the readme with this? As far as I am concerned, the primary scenario for this tool is to automatically format files in pull requests and the readme does not show this case. Does it require a "Personal Access Token" like the other example? it's not clear, and not approachable to the beginner.