|
| 1 | +name: Update Linter Versions |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: '0 10 * * 1' # Monday 10:00 UTC |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + pull-requests: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + check-updates: |
| 14 | + runs-on: ${{ vars.MACOS_RUNNER }} |
| 15 | + env: |
| 16 | + MINT_PATH: ${{ github.workspace }}/.mint/lib |
| 17 | + MINT_LINK_PATH: ${{ github.workspace }}/.mint/bin |
| 18 | + steps: |
| 19 | + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 20 | + |
| 21 | + - name: Read current versions from Mintfile |
| 22 | + id: current |
| 23 | + run: | |
| 24 | + SWIFTLINT=$(grep 'realm/SwiftLint@' Mintfile | sed 's/.*@//') |
| 25 | + SWIFTFORMAT=$(grep 'nicklockwood/SwiftFormat@' Mintfile | sed 's/.*@//') |
| 26 | + echo "swiftlint=$SWIFTLINT" >> "$GITHUB_OUTPUT" |
| 27 | + echo "swiftformat=$SWIFTFORMAT" >> "$GITHUB_OUTPUT" |
| 28 | +
|
| 29 | + - name: Check latest versions |
| 30 | + id: latest |
| 31 | + env: |
| 32 | + GH_TOKEN: ${{ github.token }} |
| 33 | + run: | |
| 34 | + SWIFTLINT=$(gh api repos/realm/SwiftLint/releases/latest --jq '.tag_name') |
| 35 | + SWIFTFORMAT=$(gh api repos/nicklockwood/SwiftFormat/releases/latest --jq '.tag_name') |
| 36 | + echo "swiftlint=$SWIFTLINT" >> "$GITHUB_OUTPUT" |
| 37 | + echo "swiftformat=$SWIFTFORMAT" >> "$GITHUB_OUTPUT" |
| 38 | +
|
| 39 | + - name: Determine updates needed |
| 40 | + id: check |
| 41 | + run: | |
| 42 | + HAS_UPDATES=false |
| 43 | + if [ "${{ steps.current.outputs.swiftlint }}" != "${{ steps.latest.outputs.swiftlint }}" ]; then |
| 44 | + echo "swiftlint_updated=true" >> "$GITHUB_OUTPUT" |
| 45 | + HAS_UPDATES=true |
| 46 | + fi |
| 47 | + if [ "${{ steps.current.outputs.swiftformat }}" != "${{ steps.latest.outputs.swiftformat }}" ]; then |
| 48 | + echo "swiftformat_updated=true" >> "$GITHUB_OUTPUT" |
| 49 | + HAS_UPDATES=true |
| 50 | + fi |
| 51 | + echo "has_updates=$HAS_UPDATES" >> "$GITHUB_OUTPUT" |
| 52 | +
|
| 53 | + - name: Check for existing PR |
| 54 | + if: steps.check.outputs.has_updates == 'true' |
| 55 | + id: existing_pr |
| 56 | + env: |
| 57 | + GH_TOKEN: ${{ github.token }} |
| 58 | + run: | |
| 59 | + EXISTING=$(gh pr list --head "auto/update-linters" --state open --json number --jq '.[0].number // empty') |
| 60 | + echo "exists=$( [ -n "$EXISTING" ] && echo true || echo false )" >> "$GITHUB_OUTPUT" |
| 61 | +
|
| 62 | + - name: Create branch and update Mintfile |
| 63 | + if: steps.check.outputs.has_updates == 'true' && steps.existing_pr.outputs.exists != 'true' |
| 64 | + run: | |
| 65 | + git checkout -b auto/update-linters |
| 66 | + printf '%s\n' \ |
| 67 | + "realm/SwiftLint@${{ steps.latest.outputs.swiftlint }}" \ |
| 68 | + "nicklockwood/SwiftFormat@${{ steps.latest.outputs.swiftformat }}" \ |
| 69 | + > Mintfile |
| 70 | +
|
| 71 | + - name: Install Mint and bootstrap new versions |
| 72 | + if: steps.check.outputs.has_updates == 'true' && steps.existing_pr.outputs.exists != 'true' |
| 73 | + run: | |
| 74 | + brew install mint |
| 75 | + echo "${{ github.workspace }}/.mint/bin" >> "$GITHUB_PATH" |
| 76 | + mint bootstrap --link |
| 77 | +
|
| 78 | + - name: Run lint fix |
| 79 | + if: steps.check.outputs.has_updates == 'true' && steps.existing_pr.outputs.exists != 'true' |
| 80 | + run: ./scripts/lint_swift fix --verbose |
| 81 | + |
| 82 | + - name: Commit and push |
| 83 | + if: steps.check.outputs.has_updates == 'true' && steps.existing_pr.outputs.exists != 'true' |
| 84 | + run: | |
| 85 | + git config user.name "github-actions[bot]" |
| 86 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 87 | + git add -A |
| 88 | + git commit -m "chore: update linter versions" \ |
| 89 | + -m "SwiftLint: ${{ steps.current.outputs.swiftlint }} -> ${{ steps.latest.outputs.swiftlint }}" \ |
| 90 | + -m "SwiftFormat: ${{ steps.current.outputs.swiftformat }} -> ${{ steps.latest.outputs.swiftformat }}" |
| 91 | + git push origin auto/update-linters |
| 92 | +
|
| 93 | + - name: Create Pull Request |
| 94 | + if: steps.check.outputs.has_updates == 'true' && steps.existing_pr.outputs.exists != 'true' |
| 95 | + env: |
| 96 | + GH_TOKEN: ${{ github.token }} |
| 97 | + run: | |
| 98 | + BODY="## Linter Version Updates\n\n" |
| 99 | + if [ "${{ steps.check.outputs.swiftlint_updated }}" == "true" ]; then |
| 100 | + BODY+="- **SwiftLint**: \`${{ steps.current.outputs.swiftlint }}\` → \`${{ steps.latest.outputs.swiftlint }}\`\n" |
| 101 | + BODY+=" [Release notes](https://github.com/realm/SwiftLint/releases/tag/${{ steps.latest.outputs.swiftlint }})\n" |
| 102 | + fi |
| 103 | + if [ "${{ steps.check.outputs.swiftformat_updated }}" == "true" ]; then |
| 104 | + BODY+="- **SwiftFormat**: \`${{ steps.current.outputs.swiftformat }}\` → \`${{ steps.latest.outputs.swiftformat }}\`\n" |
| 105 | + BODY+=" [Release notes](https://github.com/nicklockwood/SwiftFormat/releases/tag/${{ steps.latest.outputs.swiftformat }})\n" |
| 106 | + fi |
| 107 | + BODY+="\n---\n\n" |
| 108 | + BODY+="\`./scripts/lint_swift fix\` has been run to apply formatting changes.\n\n" |
| 109 | + BODY+="### ⚠️ CI checks need to be triggered manually\n\n" |
| 110 | + BODY+="This PR was created by GitHub Actions using \`GITHUB_TOKEN\`, which " |
| 111 | + BODY+="[does not trigger other workflows](https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication#using-the-github_token-in-a-workflow). " |
| 112 | + BODY+="To run CI checks, either:\n\n" |
| 113 | + BODY+="1. **Close and reopen this PR** — this fires a new \`pull_request\` event from your user, which triggers all checks.\n" |
| 114 | + BODY+="2. **Trigger workflows via CLI:**\n" |
| 115 | + BODY+="\`\`\`bash\n" |
| 116 | + BODY+="gh workflow run ci.yml --ref auto/update-linters\n" |
| 117 | + BODY+="\`\`\`\n" |
| 118 | +
|
| 119 | + gh pr create \ |
| 120 | + --title "chore: bump linter versions" \ |
| 121 | + --body "$(echo -e "$BODY")" \ |
| 122 | + --label "dependencies" \ |
| 123 | + --head "auto/update-linters" \ |
| 124 | + --base "main" |
0 commit comments