Auto Format #206
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@249c01ba271e19fa76eede7f766161cc95ace489 # v2.1.10 | |
- 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@c1a285145b9d317df6ced56c09f525b5c2b6f755 # v1.11.1 | |
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" | |
}); |