diff --git a/.github/workflows/pre-commit.yaml b/.github/workflows/pre-commit.yaml index 201d3aa..5602edc 100644 --- a/.github/workflows/pre-commit.yaml +++ b/.github/workflows/pre-commit.yaml @@ -18,9 +18,13 @@ on: jobs: pre-commit: runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write steps: - - name: Checkout repository - uses: actions/checkout@v4 + - uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref || github.ref_name }} - name: Set up Python id: setup-python @@ -45,15 +49,30 @@ jobs: pre-commit install - name: Run pre-commit hooks + continue-on-error: true working-directory: ${{ inputs.working-directory }} run: | - git ls-files | xargs pre-commit run \ - --show-diff-on-failure \ - --color=always \ - --files + git fetch --no-tags --depth=1 origin "${{ github.base_ref }}:refs/remotes/origin/${{ github.base_ref }}" + if [ "${{ github.event_name }}" = "pull_request" ]; then + pre-commit run \ + --from-ref "origin/${{ github.base_ref }}" \ + --to-ref "HEAD" \ + --show-diff-on-failure \ + --color=always + else + pre-commit run \ + --from-ref "HEAD~1" \ + --to-ref "HEAD" \ + --show-diff-on-failure \ + --color=always + fi - name: Commit if changes - if: github.event_name == 'pull_request' + # always() bypasses the error code from pre-commit. + # don't run again if this review has been triggered by github-actions[bot], + # since this would create an action loop. + if: github.event_name == 'pull_request' && github.actor != 'github-actions[bot]' + # Normally github message would include [skip ci] to prevent loops, but since this is the pre-commit fixer, it should be stable. run: | git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" @@ -62,3 +81,21 @@ jobs: git commit -m "chore: auto-format code [skip ci]" git push fi + + - name: Re-run pre-commit hooks + working-directory: ${{ inputs.working-directory }} + run: | + git fetch --no-tags --depth=1 origin "${{ github.base_ref }}:refs/remotes/origin/${{ github.base_ref }}" + if [ "${{ github.event_name }}" = "pull_request" ]; then + pre-commit run \ + --from-ref "origin/${{ github.base_ref }}" \ + --to-ref "HEAD" \ + --show-diff-on-failure \ + --color=always + else + pre-commit run \ + --from-ref "HEAD~1" \ + --to-ref "HEAD" \ + --show-diff-on-failure \ + --color=always + fi diff --git a/README.md b/README.md index 81ebdcf..b0b992f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # test_github_workflows + Test github workflows diff --git a/src/main.c b/src/main.c index 77bf4a6..f2399e7 100644 --- a/src/main.c +++ b/src/main.c @@ -2,8 +2,7 @@ #include "mylib.h" int main(void) { - int x = 3 - ; + int x = 3; printf("%s\n", greet("World")); return 0; }