Auto Format #189
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: 'Auto Format' | |
on: | |
workflow_dispatch: | |
schedule: | |
# AM 6:00(JST) | |
- cron: '0 21 * * *' | |
concurrency: | |
group: auto-format | |
cancel-in-progress: true | |
jobs: | |
format: | |
runs-on: ubuntu-24.04 | |
timeout-minutes: 15 | |
steps: | |
# https://github.com/marketplace/actions/checkout | |
- name: Checkout | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
with: | |
ref: main | |
# https://github.com/marketplace/actions/mise-action | |
- name: mise action | |
uses: jdx/mise-action@63a673876c7072c1d6ecb2049c0d572976c83b22 # v2.1.7 | |
- name: setup gitignore target files | |
uses: ./.github/actions/setup-gitignore-target-files | |
- name: setup pub | |
uses: ./.github/actions/setup-pub | |
- name: Run Melos Format | |
run: melos format | |
- name: Check for file changes | |
id: diff | |
run: | | |
if git diff --quiet -- '*.dart'; then | |
echo "changes=false" >> "$GITHUB_OUTPUT" | |
else | |
echo "changes=true" >> "$GITHUB_OUTPUT" | |
fi | |
# https://github.com/marketplace/actions/create-github-app-token | |
- name: Create GitHub App Token | |
if: steps.diff.outputs.changes == 'true' | |
uses: actions/create-github-app-token@5d869da34e18e7287c1daad50e0b8ea0f506ce69 # v1.11.0 | |
id: app-token | |
with: | |
app-id: ${{ vars.BOT_APP_ID }} | |
private-key: ${{ secrets.BOT_PRIVATE_KEY }} | |
# https://github.com/marketplace/actions/github-script | |
- name: Commit and push changes | |
if: steps.diff.outputs.changes == 'true' | |
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 | |
with: | |
github-token: ${{ steps.app-token.outputs.token }} | |
script: | | |
const { readFileSync } = require('fs'); | |
const { resolve } = require('path'); | |
const { execSync } = require('child_process'); | |
const changedFiles = execSync('git diff --name-only') | |
.toString() | |
.split('\n') | |
.filter(Boolean); | |
const branchRef = `auto-format-${context.sha.slice(0, 7)}`; | |
const branch = await github.rest.git.createRef({ | |
...context.repo, | |
ref: `refs/heads/${branchRef}`, | |
sha: context.sha | |
}); | |
const tree = await github.rest.git.createTree({ | |
...context.repo, | |
base_tree: context.sha, | |
tree: changedFiles.map(path => ({ | |
path, | |
mode: '100644', | |
content: readFileSync(resolve(process.cwd(), path), 'utf8') | |
})) | |
}); | |
const commit = await github.rest.git.createCommit({ | |
...context.repo, | |
message: "refactor: auto format", | |
tree: tree.data.sha, | |
parents: [context.sha] | |
}); | |
await github.rest.git.updateRef({ | |
...context.repo, | |
ref: `heads/${branchRef}`, | |
sha: commit.data.sha | |
}); | |
const pullRequest = await github.rest.pulls.create({ | |
...context.repo, | |
title: "refactor: auto format", | |
// Use the table as the description | |
body: `This was automatically generated by the [${context.workflow}](https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId})`, | |
head: branchRef, | |
base: "main" | |
}); |