-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
193 lines (169 loc) · 7.42 KB
/
Copy pathaction.yml
File metadata and controls
193 lines (169 loc) · 7.42 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
name: 'Open Java Format Check'
description: 'Checks Java code formatting with the open-java-format native binary'
author: 'abashev'
branding:
icon: 'check-circle'
color: 'green'
inputs:
version:
description: 'Version of open-java-format to use'
required: true
default: '2.98.0.4'
mode:
description: 'Which files to check: "changed" for files from PR/push, "all" for every .java file'
required: false
default: 'changed'
runs:
using: 'composite'
steps:
- name: Detect platform
id: platform
shell: bash
run: |
OS=$(uname -s)
ARCH=$(uname -m)
case "${OS}-${ARCH}" in
Linux-x86_64) SUFFIX="linux-glibc_x86-64" ;;
Linux-aarch64) SUFFIX="linux-glibc_aarch64" ;;
Darwin-arm64) SUFFIX="macos_aarch64" ;;
Darwin-x86_64) SUFFIX="macos_x86-64" ;;
MINGW*-x86_64 | MSYS*-x86_64) SUFFIX="windows_x86-64" ;;
*)
echo "::error::No open-java-format native binary for ${OS}-${ARCH}. Supported: Linux x86_64, Linux aarch64, macOS aarch64, macOS x86_64, Windows x86_64."
exit 1
;;
esac
# The Windows binary is an .exe, on Maven Central and on disk. It exists from 2.98.0.2 on.
if [ "$SUFFIX" = "windows_x86-64" ]; then
EXTENSION="exe"
BINARY="open-java-format.exe"
else
EXTENSION="bin"
BINARY="open-java-format"
fi
echo "suffix=$SUFFIX" >> "$GITHUB_OUTPUT"
echo "extension=$EXTENSION" >> "$GITHUB_OUTPUT"
echo "binary=$BINARY" >> "$GITHUB_OUTPUT"
- name: Check for newer open-java-format version
shell: bash
env:
GH_TOKEN: ${{ github.token }}
run: |
VERSION="${{ inputs.version }}"
# Best-effort version check — must never fail the build. The GitHub API
# returns 403 for unauthenticated CI requests (rate limit), so authenticate
# with the job token when available and tolerate any failure.
AUTH=()
if [ -n "${GH_TOKEN:-}" ]; then
AUTH=(-H "Authorization: Bearer $GH_TOKEN")
fi
LATEST=$(curl -sSL "${AUTH[@]}" -H "Accept: application/vnd.github+json" \
https://api.github.com/repos/openjavaformat/open-java-format/releases/latest 2>/dev/null \
| jq -r '.tag_name // empty' 2>/dev/null || true)
if [ -z "$LATEST" ]; then
echo "Could not check the latest open-java-format version — skipping this check."
elif [ "$LATEST" != "$VERSION" ]; then
echo "::warning::A newer open-java-format version is available: $LATEST (you are using $VERSION)"
fi
- name: Cache open-java-format binary
id: cache
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ${{ github.action_path }}/${{ steps.platform.outputs.binary }}
key: open-java-format-${{ inputs.version }}-${{ steps.platform.outputs.suffix }}
- name: Download open-java-format native binary
if: steps.cache.outputs.cache-hit != 'true'
shell: bash
run: |
VERSION="${{ inputs.version }}"
SUFFIX="${{ steps.platform.outputs.suffix }}"
BASE_URL="https://repo1.maven.org/maven2/dev/openjavaformat/open-java-format-native/${VERSION}"
ARTIFACT="open-java-format-native-${VERSION}-nativeImage-${SUFFIX}.${{ steps.platform.outputs.extension }}"
DOWNLOAD_URL="${BASE_URL}/${ARTIFACT}"
BINARY="${{ github.action_path }}/${{ steps.platform.outputs.binary }}"
echo "Downloading open-java-format ${VERSION} (${SUFFIX})..."
if ! curl -sSfL -o "$BINARY" "$DOWNLOAD_URL"; then
rm -f "$BINARY"
echo "::error::Failed to download open-java-format from: $DOWNLOAD_URL"
exit 1
fi
chmod +x "$BINARY"
- name: Set binary path
shell: bash
run: echo "OJF_BINARY=${{ github.action_path }}/${{ steps.platform.outputs.binary }}" >> "$GITHUB_ENV"
- name: Collect files to check
shell: bash
run: |
MODE="${{ inputs.mode }}"
if [ "$MODE" = "all" ]; then
echo "Mode: all — checking every .java file"
FILES=$(find . -name '*.java' -not -path './.git/*' | sed 's|^\./||' | tr '\n' ' ')
elif [ -n "${{ github.event.pull_request.number }}" ]; then
echo "Mode: changed (PR #${{ github.event.pull_request.number }})"
FILES=$(gh pr diff ${{ github.event.pull_request.number }} --name-only | { grep '\.java$' || true; } | tr '\n' ' ')
elif [ -n "${{ github.event.before }}" ] && [ "${{ github.event.before }}" != "0000000000000000000000000000000000000000" ] && git cat-file -e "${{ github.event.before }}" 2>/dev/null; then
echo "Mode: changed (push ${{ github.event.before }}...${{ github.sha }})"
FILES=$(git diff --name-only --diff-filter=ACMR ${{ github.event.before }}...${{ github.sha }} -- '*.java' | tr '\n' ' ')
else
echo "Mode: changed — unknown event, falling back to all .java files"
FILES=$(find . -name '*.java' -not -path './.git/*' | sed 's|^\./||' | tr '\n' ' ')
fi
# Drop files matching patterns in .open-java-format-exclude (repo root), if present.
# Each non-empty, non-comment line is a git pathspec (relative to the repo root).
# This file is the single source of exclusions, shared with the pre-commit hook.
EXCLUDE_FILE=".open-java-format-exclude"
if [ -n "$FILES" ] && [ -f "$EXCLUDE_FILE" ]; then
PATTERNS=()
while IFS= read -r line || [ -n "$line" ]; do
line="${line#"${line%%[![:space:]]*}"}"
line="${line%"${line##*[![:space:]]}"}"
case "$line" in ''|\#*) continue ;; esac
PATTERNS+=("$line")
done < "$EXCLUDE_FILE"
if [ ${#PATTERNS[@]} -gt 0 ]; then
EXCLUDED=$(git ls-files --cached --others --exclude-standard -- "${PATTERNS[@]}" 2>/dev/null || true)
if [ -n "$EXCLUDED" ]; then
echo "Excluding files matching $EXCLUDE_FILE:"
printf '%s\n' "$EXCLUDED" | sed 's/^/ /'
FILES=$(printf '%s\n' $FILES | grep -vxFf <(printf '%s\n' "$EXCLUDED") | tr '\n' ' ' || true)
fi
fi
fi
if [ -z "$FILES" ]; then
echo "No Java files to check."
echo "OJF_FILES=" >> "$GITHUB_ENV"
else
echo "Files to check:"
echo "$FILES" | tr ' ' '\n' | grep -v '^$'
echo "OJF_FILES=$FILES" >> "$GITHUB_ENV"
fi
env:
GH_TOKEN: ${{ github.token }}
- name: Run format check
shell: bash
run: |
if [ -z "$OJF_FILES" ]; then
echo "Nothing to check, skipping."
exit 0
fi
UNFORMATTED=()
for FILE in $OJF_FILES; do
if [ -f "$FILE" ]; then
if ! "$OJF_BINARY" --dry-run --set-exit-if-changed "$FILE" > /dev/null 2>&1; then
UNFORMATTED+=("$FILE")
fi
fi
done
if [ ${#UNFORMATTED[@]} -gt 0 ]; then
echo "::error::Java formatting issues found!"
echo ""
echo "The following files are not formatted correctly:"
for FILE in "${UNFORMATTED[@]}"; do
echo " $FILE"
done
echo ""
echo "Run open-java-format locally to fix:"
echo " open-java-format --replace ${UNFORMATTED[*]}"
exit 1
fi
echo "All files are correctly formatted."