summaryrefslogtreecommitdiffstats
path: root/.github/workflows/lint.yml
blob: 9862ff502c53ba269614bf8167b9ebd1eb2dadb7 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
name: PR Lint keyboards

permissions:
  contents: read

on:
  pull_request:
    paths:
    - 'keyboards/**'

jobs:
  lint:
    runs-on: ubuntu-latest

    container: ghcr.io/qmk/qmk_cli

    steps:
    - name: Disable safe.directory check
      run : git config --global --add safe.directory '*'

    - uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - name: Install dependencies
      run: pip3 install -r requirements-dev.txt

    - name: Get changed files
      id: file_changes
      uses: tj-actions/changed-files@v39

    - name: Print info
      run: |
        git rev-parse --short HEAD
        echo ${{ github.event.pull_request.base.sha }}
        echo '${{ steps.file_changes.outputs.all_changed_files}}'

    - name: Run qmk lint
      if: always()
      shell: 'bash {0}'
      run: |
        QMK_CHANGES=$(echo -e '${{ steps.file_changes.outputs.all_changed_files}}' | sed 's/ /\n/g')
        QMK_KEYBOARDS=$(qmk list-keyboards)

        exit_code=0

        for KB in $QMK_KEYBOARDS; do
          KEYBOARD_CHANGES=$(echo "$QMK_CHANGES" | grep -E '^(keyboards/'${KB}'/)')
          if [[ -z "$KEYBOARD_CHANGES" ]]; then
            # skip as no changes for this keyboard
            continue
          fi

          KEYMAP_ONLY=$(echo "$KEYBOARD_CHANGES" | grep -cv /keymaps/)
          if [[ $KEYMAP_ONLY -gt 0 ]]; then
            echo "linting ${KB}"

            qmk lint --keyboard ${KB} && qmk info -l --keyboard ${KB}
            exit_code=$(($exit_code + $?))
          fi
        done

        qmk format-text ${{ steps.file_changes.outputs.all_changed_files}} || true
        for file in ${{ steps.file_changes.outputs.all_changed_files}}; do
          if ! git diff --quiet $file; then
            echo "File '${file}' Requires Formatting"
            echo "::error file=${file}::Requires Formatting"
            exit_code=$(($exit_code + 1))
          fi
        done

        if [[ $exit_code -gt 255 ]]; then
            exit 255
        fi
        exit $exit_code

    - name: Verify at most one added keyboard
      if: always()
      shell: 'bash {0}'
      run: |
        git reset --hard
        git clean -xfd

        # Get the keyboard list and count for the target branch
        git checkout -f ${{ github.base_ref }}
        git pull --ff-only
        QMK_KEYBOARDS_BASE=$(qmk list-keyboards)
        QMK_KEYBOARDS_BASE_COUNT=$(qmk list-keyboards | wc -l)

        # Get the keyboard list and count for the PR
        git checkout -f ${{ github.head_ref }}
        git merge --no-commit --squash ${{ github.base_ref }}
        QMK_KEYBOARDS_PR=$(qmk list-keyboards)
        QMK_KEYBOARDS_PR_COUNT=$(qmk list-keyboards | wc -l)

        echo "::group::Keyboards changes in this PR"
        diff -d -U 0 <(echo "$QMK_KEYBOARDS_BASE") <(echo "$QMK_KEYBOARDS_PR") | grep -vE '^(---|\+\+\+|@@)' | sed -e 's@^-@Removed: @g' -e 's@^+@  Added: @g'
        echo "::endgroup::"

        if [[ $QMK_KEYBOARDS_PR_COUNT -gt $(($QMK_KEYBOARDS_BASE_COUNT + 1)) ]]; then
            echo "More than one keyboard added in this PR -- see the PR Checklist."
            echo "::error::More than one keyboard added in this PR -- see the PR Checklist."
            exit 1
        fi