Skip to content

Instantly share code, notes, and snippets.

@WorldDownTown
Last active April 18, 2024 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 する GitHub Actions の設定
name: swift-format
on:
pull_request:
branches:
- main
paths:
- '**/*.swift'
jobs:
format:
runs-on: macos-14
steps:
- name: Install swift-format
run: |
brew install swift-format
- name: PR commits + 1
run: echo "PR_FETCH_DEPTH=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> "${GITHUB_ENV}"
- name: Checkout PR branch and all PR commits
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}
fetch-depth: ${{ env.PR_FETCH_DEPTH }} # (PR の commits + 1) 個 checkout する
- name: Find swift files to format
run: |
DELTA_FILES=$(git diff --name-only --diff-filter=ACMR ${{ github.event.pull_request.base.sha }}...HEAD -- '*.swift')
echo "SWIFT_FILES=$(echo ${DELTA_FILES})" >> "${GITHUB_ENV}"
- name: Run swift-format on swift files
if: ${{ env.SWIFT_FILES != '' }}
run: |
echo 'Formatting Swift files...'
swift-format format --in-place --configuration .swift-format --parallel ${SWIFT_FILES[@]} || true
- name: Commit and Push changes
run: |
git config --local user.name "${GITHUB_ACTOR}"
git config --local user.email "${GITHUB_ACTOR}@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:${{ github.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