|
1 | | -name: SwiftLint |
2 | | - |
| 1 | +name: Accessibility Checks |
3 | 2 | on: |
4 | 3 | pull_request: |
5 | | - push: |
6 | | - branches: [main, develop] |
| 4 | + types: [opened, synchronize, reopened] |
7 | 5 |
|
8 | 6 | jobs: |
9 | | - lint: |
| 7 | + swiftlint: |
| 8 | + name: SwiftLint A11y Rules |
10 | 9 | runs-on: macos-latest |
11 | 10 | steps: |
12 | 11 | - name: Checkout |
13 | 12 | uses: actions/checkout@v4 |
| 13 | + with: |
| 14 | + fetch-depth: 0 # Fetch full history for comparison |
14 | 15 |
|
15 | 16 | - name: Cache SwiftLint |
16 | 17 | uses: actions/cache@v3 |
17 | 18 | with: |
18 | 19 | path: /usr/local/bin/swiftlint |
19 | 20 | key: swiftlint-${{ runner.os }} |
20 | | - |
| 21 | + |
21 | 22 | - name: Install SwiftLint |
22 | 23 | run: brew install swiftlint |
| 24 | + |
| 25 | + - name: Get changed Swift files |
| 26 | + id: changed-files |
| 27 | + uses: tj-actions/changed-files@v41 |
| 28 | + with: |
| 29 | + files: | |
| 30 | + **/*.swift |
| 31 | + |
| 32 | + - name: Run SwiftLint on changed files |
| 33 | + if: steps.changed-files.outputs.any_changed == 'true' |
| 34 | + run: | |
| 35 | + echo "Running SwiftLint on changed files..." |
| 36 | + for file in ${{ steps.changed-files.outputs.all_changed_files }}; do |
| 37 | + echo "Linting: $file" |
| 38 | + swiftlint lint --strict --reporter github-actions-logging "$file" |
| 39 | + done |
| 40 | + |
| 41 | + - name: No files to check |
| 42 | + if: steps.changed-files.outputs.any_changed != 'true' |
| 43 | + run: echo "No Swift files changed in this PR" |
23 | 44 |
|
24 | | - - name: Get Changed Swift Files |
| 45 | + accessibility-audit: |
| 46 | + name: Custom A11y Pattern Check |
| 47 | + runs-on: ubuntu-latest |
| 48 | + steps: |
| 49 | + - name: Checkout |
| 50 | + uses: actions/checkout@v4 |
| 51 | + with: |
| 52 | + fetch-depth: 0 |
| 53 | + |
| 54 | + - name: Get changed Swift files |
25 | 55 | id: changed-files |
| 56 | + uses: tj-actions/changed-files@v41 |
| 57 | + with: |
| 58 | + files: | |
| 59 | + **/*.swift |
| 60 | + |
| 61 | + - name: Check for accessibility issues |
| 62 | + if: steps.changed-files.outputs.any_changed == 'true' |
26 | 63 | run: | |
27 | | - if [ "${{ github.event_name }}" = "pull_request" ]; then |
28 | | - git fetch origin ${{ github.base_ref }} |
29 | | - CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }} | grep '\.swift$' || true) |
30 | | - echo "changed_files=$CHANGED_FILES" >> $GITHUB_OUTPUT |
| 64 | + echo "Checking accessibility patterns in changed files..." |
| 65 | + |
| 66 | + EXIT_CODE=0 |
| 67 | + |
| 68 | + # Check each changed Swift file |
| 69 | + for file in ${{ steps.changed-files.outputs.all_changed_files }}; do |
| 70 | + echo "Checking: $file" |
| 71 | + |
| 72 | + # Check for Images without labels (excluding known valid patterns) |
| 73 | + if grep -n "Image(" "$file" | grep -v "accessibilityLabel" | grep -v "accessibilityHidden" | grep -v "combine" | grep -v "swiftlint:disable"; then |
| 74 | + echo "::error file=$file::Found Image without accessibility label" |
| 75 | + EXIT_CODE=1 |
| 76 | + fi |
| 77 | + |
| 78 | + # Check for Buttons without text or labels |
| 79 | + if grep -n "Button {" "$file" | grep -v "accessibilityLabel" | grep -v '"' | grep -v "Text(" | grep -v "swiftlint:disable"; then |
| 80 | + echo "::warning file=$file::Found Button that may be missing accessibility label" |
| 81 | + fi |
| 82 | + |
| 83 | + # Check for empty Text views |
| 84 | + if grep -n 'Text("")' "$file"; then |
| 85 | + echo "::error file=$file::Found empty Text() view - bad for accessibility" |
| 86 | + EXIT_CODE=1 |
| 87 | + fi |
| 88 | + |
| 89 | + # Check for tap gestures without labels |
| 90 | + if grep -n "onTapGesture" "$file" | grep -v "accessibilityLabel" | grep -v "accessibilityAction" | grep -v "swiftlint:disable"; then |
| 91 | + echo "::warning file=$file::Found onTapGesture without accessibility label or action" |
| 92 | + fi |
| 93 | + |
| 94 | + # Check for generic labels |
| 95 | + if grep -n 'accessibilityLabel("Button")\|accessibilityLabel("Image")\|accessibilityLabel("Icon")' "$file"; then |
| 96 | + echo "::warning file=$file::Found generic accessibility label - be more descriptive" |
| 97 | + fi |
| 98 | + done |
| 99 | + |
| 100 | + if [ $EXIT_CODE -ne 0 ]; then |
| 101 | + echo "❌ Accessibility issues found!" |
| 102 | + exit $EXIT_CODE |
31 | 103 | else |
32 | | - echo "changed_files=" >> $GITHUB_OUTPUT |
| 104 | + echo "✅ No accessibility issues found" |
33 | 105 | fi |
34 | 106 |
|
35 | | - - name: Run SwiftLint on Changed Files |
36 | | - if: steps.changed-files.outputs.changed_files != '' |
| 107 | + accessibility-report: |
| 108 | + name: Generate A11y Report |
| 109 | + runs-on: ubuntu-latest |
| 110 | + if: always() |
| 111 | + needs: [swiftlint, accessibility-audit] |
| 112 | + steps: |
| 113 | + - name: Checkout |
| 114 | + uses: actions/checkout@v4 |
| 115 | + |
| 116 | + - name: Generate Accessibility Summary |
37 | 117 | run: | |
38 | | - for file in ${{ steps.changed-files.outputs.changed_files }}; do |
39 | | - swiftlint lint --strict --reporter github-actions-logging --path "$file" |
40 | | - done |
| 118 | + echo "# Accessibility Check Summary 🔍" >> $GITHUB_STEP_SUMMARY |
| 119 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 120 | + echo "## Checks Performed:" >> $GITHUB_STEP_SUMMARY |
| 121 | + echo "- ✅ SwiftLint accessibility rules" >> $GITHUB_STEP_SUMMARY |
| 122 | + echo "- ✅ Image accessibility labels" >> $GITHUB_STEP_SUMMARY |
| 123 | + echo "- ✅ Button accessibility labels" >> $GITHUB_STEP_SUMMARY |
| 124 | + echo "- ✅ Empty text views" >> $GITHUB_STEP_SUMMARY |
| 125 | + echo "- ✅ Tap gesture accessibility" >> $GITHUB_STEP_SUMMARY |
| 126 | + echo "- ✅ Generic label detection" >> $GITHUB_STEP_SUMMARY |
| 127 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 128 | + echo "## Resources:" >> $GITHUB_STEP_SUMMARY |
| 129 | + echo "- [Apple Accessibility Guidelines](https://developer.apple.com/accessibility/)" >> $GITHUB_STEP_SUMMARY |
| 130 | + echo "- [SwiftUI Accessibility](https://developer.apple.com/documentation/swiftui/view-accessibility)" >> $GITHUB_STEP_SUMMARY |
| 131 | +
|
| 132 | + block-merge: |
| 133 | + name: Block Merge on Failures |
| 134 | + runs-on: ubuntu-latest |
| 135 | + needs: [swiftlint, accessibility-audit] |
| 136 | + if: failure() |
| 137 | + steps: |
| 138 | + - name: Comment on PR |
| 139 | + uses: actions/github-script@v7 |
| 140 | + with: |
| 141 | + script: | |
| 142 | + github.rest.issues.createComment({ |
| 143 | + issue_number: context.issue.number, |
| 144 | + owner: context.repo.owner, |
| 145 | + repo: context.repo.repo, |
| 146 | + body: '❌ **Accessibility checks failed**\n\nPlease review the accessibility issues found in the checks above before merging.\n\n**Common fixes:**\n- Add `.accessibilityLabel("description")` to images\n- Add `.accessibilityHidden(true)` for decorative images\n- Ensure buttons have descriptive labels\n- Use `.accessibilityElement(children: .combine)` for complex views\n\n**Need to suppress a warning?**\n```swift\n// swiftlint:disable:next a11y_swiftui_image_missing_label\nImage(systemName: "icon")\n```' |
| 147 | + }) |
0 commit comments