Skip to content

Instantly share code, notes, and snippets.

@yanaokahiroki
Last active October 22, 2022 09:25
Show Gist options
  • Save yanaokahiroki/bc35a39b7ee571e900667936bda88df5 to your computer and use it in GitHub Desktop.
Save yanaokahiroki/bc35a39b7ee571e900667936bda88df5 to your computer and use it in GitHub Desktop.
Javaファイルを自動フォーマット
name: Auto Format
on: pull_request
jobs:
format:
runs-on: ubuntu-latest
steps:
- name: Set up Workspace
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
fetch-depth: 0
- name: Set up JDK
uses: actions/setup-java@v3
with:
distribution: adopt
java-version: 17
- name: Format Java file
run: |
wget -q -O google-java-format.jar \
https://github.com/google/google-java-format/releases/download/v1.15.0/google-java-format-1.15.0-all-deps.jar
java -jar google-java-format.jar -replace $(git diff origin/${GITHUB_BASE_REF} HEAD --name-only)
- name: Check for modified files
id: git-check
run: echo ::set-output name=modified::$(if git diff-index --quiet HEAD --; then echo "false"; else echo "true"; fi)
- name: Push auto-formatted files
if: steps.git-check.outputs.modified == 'true'
# commitするユーザーはgithub-actionsのボットにする
run: |
git remote set-url origin https://github-actions:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}
git config --global user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
// 新規追加されたgoogle-java-format.jarを除くファイルをインデックスに追加
git add -u
git commit -m "Auto formatted by github-actions"
git push origin HEAD:${GITHUB_HEAD_REF}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment