Skip to content

Instantly share code, notes, and snippets.

@WorldDownTown
Last active June 28, 2024 02:01
Show Gist options
  • Save WorldDownTown/abebdc7f75b258aac763eee5aa271ca0 to your computer and use it in GitHub Desktop.
Save WorldDownTown/abebdc7f75b258aac763eee5aa271ca0 to your computer and use it in GitHub Desktop.
Pull Request の差分に対して swift-format でフォーマットして commit & push する GitHub Actions の設定
# Pull Request の差分に対して swift-format でフォーマットして commit & push
name: swift-format
on:
pull_request:
branches:
- main
paths:
- "**/*.swift"
defaults:
run:
shell: bash -euo pipefail {0}
concurrency: # 自動キャンセル
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
format:
runs-on: macos-14
steps:
- name: Install swift-format
run: |
brew install swift-format
- name: PR commits + 1
id: pre_fetch_depth
run: echo "depth=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> "${GITHUB_OUTPUT}"
- name: Checkout PR branch and all PR commits
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}
fetch-depth: ${{ steps.pre_fetch_depth.outputs.depth }} # (PR の commits + 1) 個 checkout する
- name: Find swift files to format
run: |
SWIFT_FILES=()
while IFS="" read -r file; do SWIFT_FILES+=("${file}"); done < <(git diff --name-only --diff-filter=AMRC ${{ github.event.pull_request.base.sha }} -- "*.swift")
if [ "${#SWIFT_FILES[@]}" -eq 0 ]; then
echo "No changes to format"
else
swift-format format --in-place --configuration .swift-format.json --parallel "${SWIFT_FILES[@]}" || true
fi
- name: Commit and Push changes
env:
HEAD_REF: ${{ github.head_ref }}
run: |
git config --local user.name "github-actions[bot]"
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add .
if git commit -m "[GitHub Actions] swift-format"; then
echo "Changes committed, pushing to repository..."
git push origin HEAD:"${HEAD_REF}"
else
echo "No changes to commit"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment