aboutsummaryrefslogtreecommitdiff
path: root/.github/workflows/zig-fmt.yml
blob: 4d65a935cda7a08834a8b26f9779a56fceb2d533 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
name: zig-fmt

env:
  ZIG_VERSION: 0.11.0-dev.2571+31738de28

on:
  pull_request:
    branches:
      - main
      - jarred/test-actions
    paths:
      - "src/**/*.zig"
      - "src/*.zig"
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  zig-fmt:
    name: zig fmt
    runs-on: ubuntu-latest
    outputs:
      zig_fmt_errs: ${{ steps.fmt.outputs.zig_fmt_errs }}
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: recursive
      - name: Install zig
        run: |
          curl https://ziglang.org/builds/zig-linux-x86_64-${{env.ZIG_VERSION}}.tar.xz -L -o zig.tar.xz
          tar -xf zig.tar.xz
          echo "$(pwd)/zig-linux-x86_64-${{env.ZIG_VERSION}}" >> $GITHUB_PATH
      - name: Run zig fmt
        id: fmt
        run: |
          zig fmt --check src/*.zig src/**/*.zig 2> zig-fmt.err > zig-fmt.err2 || echo "Failed"
          delimiter="$(openssl rand -hex 8)"
          echo "zig_fmt_errs<<${delimiter}" >> "${GITHUB_OUTPUT}"

          if [ -s zig-fmt.err ]; then
            echo "// The following errors occurred:" >> "${GITHUB_OUTPUT}"
            cat zig-fmt.err >> "${GITHUB_OUTPUT}"
          fi

          if [ -s zig-fmt.err2 ]; then
            echo "// The following files were not formatted:" >> "${GITHUB_OUTPUT}"
            cat zig-fmt.err2 >> "${GITHUB_OUTPUT}"
          fi

          echo "${delimiter}" >> "${GITHUB_OUTPUT}"
      - name: Comment on PR
        if: steps.fmt.outputs.zig_fmt_errs != ''
        uses: thollander/actions-comment-pull-request@v2
        with:
          comment_tag: zig-fmt
          message: |
            ❌ @${{ github.actor }} `zig fmt` reported errors. Consider configuring your text editor to [auto-format on save](https://github.com/ziglang/vscode-zig)

            ```zig
            // # zig fmt --check src/*.zig src/**/*.zig
            ${{ steps.fmt.outputs.zig_fmt_errs }}
            ```

            To one-off fix this manually, run:

            ```sh
            zig fmt src/*.zig src/**/*.zig
            ```

            <sup>[#${{github.sha}}](https://github.com/oven-sh/bun/commits/${{github.sha}})</sup>
            <sup>zig v${{env.ZIG_VERSION}}</sup>

      - name: Uncomment on PR
        if: steps.fmt.outputs.zig_fmt_errs == ''
        uses: thollander/actions-comment-pull-request@v2
        with:
          comment_tag: zig-fmt
          mode: upsert
          create_if_not_exists: false
          message: |
            ✅ `zig fmt` errors have been resolved. Thank you.

            <sup>[#${{github.sha}}](https://github.com/oven-sh/bun/commits/${{github.sha}})</sup>
            <sup>zig v${{env.ZIG_VERSION}}</sup>

      - name: Fail the job
        if: steps.fmt.outputs.zig_fmt_errs != ''
        run: exit 1