-
Notifications
You must be signed in to change notification settings - Fork 1
109 lines (92 loc) · 3.5 KB
/
auto-format.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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-22.04
timeout-minutes: 15
steps:
# https://github.com/marketplace/actions/checkout
- name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
ref: main
# https://github.com/marketplace/actions/mise-action
- name: mise action
uses: jdx/mise-action@d6e32c1796099e0f1f3ac741c220a8b7eae9e5dd # v2.0.6
- 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"
});