Skip to content

Commit 3145571

Browse files
feat: workflow to automatically update java format + create PR
1 parent 3fc0d3b commit 3145571

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: Code Quality Checks
22

33
on:
4+
workflow_dispatch:
45
pull_request:
56
branches:
67
- "**"
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
name: Update google-java-format
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: "0 6 1 * *"
7+
8+
permissions:
9+
contents: write
10+
pull-requests: write
11+
12+
concurrency:
13+
group: ${{ github.workflow }}
14+
cancel-in-progress: true
15+
16+
jobs:
17+
update:
18+
name: Check for upstream formatter update
19+
runs-on: ubuntu-latest
20+
timeout-minutes: 20
21+
22+
steps:
23+
- uses: actions/checkout@v6
24+
with:
25+
fetch-depth: 0
26+
27+
- uses: actions/setup-node@v6
28+
with:
29+
node-version: 24
30+
31+
- name: Configure JDK
32+
uses: actions/setup-java@v5
33+
with:
34+
distribution: "temurin"
35+
java-version: "25"
36+
37+
- uses: actions/cache/restore@v5
38+
name: Yarn Cache Restore
39+
id: yarn-cache
40+
with:
41+
path: .yarn/cache
42+
key: ${{ runner.os }}-yarn-v1-${{ hashFiles('yarn.lock') }}
43+
restore-keys: ${{ runner.os }}-yarn-v1
44+
45+
- name: Yarn Install
46+
uses: nick-fields/retry@v3
47+
with:
48+
timeout_minutes: 15
49+
retry_wait_seconds: 30
50+
max_attempts: 3
51+
command: yarn
52+
53+
- name: Check latest upstream version
54+
id: check
55+
shell: bash
56+
run: |
57+
set -euo pipefail
58+
59+
current_jar="$(ls lib/google-java-format-*-all-deps.jar)"
60+
current_version="${current_jar##*/google-java-format-}"
61+
current_version="${current_version%-all-deps.jar}"
62+
63+
release_json="$(curl -fsSL -H "Accept: application/vnd.github+json" "https://api.github.com/repos/google/google-java-format/releases/latest")"
64+
65+
latest_version="$(
66+
printf '%s' "$release_json" | node -e '
67+
const fs = require("fs");
68+
const release = JSON.parse(fs.readFileSync(0, "utf8"));
69+
const asset = (release.assets || []).find((entry) =>
70+
/google-java-format-\d+\.\d+\.\d+-all-deps\.jar$/.test(entry.name)
71+
);
72+
if (!asset) {
73+
throw new Error("Unable to find all-deps jar asset in latest release");
74+
}
75+
process.stdout.write(asset.name.replace(/^google-java-format-/, "").replace(/-all-deps\.jar$/, ""));
76+
'
77+
)"
78+
download_url="$(
79+
printf '%s' "$release_json" | node -e '
80+
const fs = require("fs");
81+
const release = JSON.parse(fs.readFileSync(0, "utf8"));
82+
const asset = (release.assets || []).find((entry) =>
83+
/google-java-format-\d+\.\d+\.\d+-all-deps\.jar$/.test(entry.name)
84+
);
85+
if (!asset) {
86+
throw new Error("Unable to find all-deps jar asset in latest release");
87+
}
88+
process.stdout.write(asset.browser_download_url);
89+
'
90+
)"
91+
92+
echo "current_version=$current_version" >> "$GITHUB_OUTPUT"
93+
echo "latest_version=$latest_version" >> "$GITHUB_OUTPUT"
94+
echo "download_url=$download_url" >> "$GITHUB_OUTPUT"
95+
96+
if [[ "$current_version" == "$latest_version" ]]; then
97+
echo "update_available=false" >> "$GITHUB_OUTPUT"
98+
exit 0
99+
fi
100+
101+
echo "update_available=true" >> "$GITHUB_OUTPUT"
102+
103+
- name: Download new formatter jar
104+
if: steps.check.outputs.update_available == 'true'
105+
shell: bash
106+
run: |
107+
set -euo pipefail
108+
109+
rm -f lib/google-java-format-*-all-deps.jar
110+
curl -fsSL \
111+
"${{ steps.check.outputs.download_url }}" \
112+
-o "lib/google-java-format-${{ steps.check.outputs.latest_version }}-all-deps.jar"
113+
114+
- name: Update hardcoded jar path
115+
if: steps.check.outputs.update_available == 'true'
116+
shell: bash
117+
run: |
118+
set -euo pipefail
119+
120+
node - <<'EOF'
121+
const fs = require("fs");
122+
const indexPath = "index.js";
123+
const latestVersion = process.env.LATEST_VERSION;
124+
const oldSource = fs.readFileSync(indexPath, "utf8");
125+
const newSource = oldSource.replace(
126+
/google-java-format-\d+\.\d+\.\d+-all-deps\.jar/g,
127+
`google-java-format-${latestVersion}-all-deps.jar`
128+
);
129+
130+
if (oldSource === newSource) {
131+
throw new Error("Failed to update google-java-format jar path in index.js");
132+
}
133+
134+
fs.writeFileSync(indexPath, newSource);
135+
EOF
136+
env:
137+
LATEST_VERSION: ${{ steps.check.outputs.latest_version }}
138+
139+
- name: Run tests
140+
if: steps.check.outputs.update_available == 'true'
141+
run: ./test.sh
142+
143+
- uses: actions/cache/save@v5
144+
name: Yarn Cache Save
145+
if: ${{ steps.check.outputs.update_available == 'true' && github.ref == 'refs/heads/main' }}
146+
with:
147+
path: .yarn/cache
148+
key: ${{ runner.os }}-yarn-v1-${{ hashFiles('yarn.lock') }}
149+
150+
- name: Create pull request
151+
if: steps.check.outputs.update_available == 'true'
152+
uses: peter-evans/create-pull-request@v7
153+
with:
154+
token: ${{ github.token }}
155+
commit-message: "feat: adopt upstream google-java-format [${{ steps.check.outputs.latest_version }}]"
156+
branch: ci/google-java-format-update-${{ steps.check.outputs.latest_version }}
157+
delete-branch: true
158+
title: "feat: adopt upstream google-java-format [${{ steps.check.outputs.latest_version }}]"
159+
body: |
160+
Updates bundled google-java-format from `${{ steps.check.outputs.current_version }}` to `${{ steps.check.outputs.latest_version }}`.
161+
162+
- Replaces the jar in `lib/`
163+
- Updates the hardcoded jar path in `index.js`
164+
- Runs `./test.sh`

0 commit comments

Comments
 (0)