-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
132 lines (115 loc) · 4.48 KB
/
action.yml
File metadata and controls
132 lines (115 loc) · 4.48 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
name: 'Palantir Java Format Check'
description: 'Checks Java code formatting using palantir-java-formatter native binary'
author: 'abashev'
branding:
icon: 'check-circle'
color: 'green'
inputs:
version:
description: 'Version of palantir-java-formatter to use'
required: true
default: '2.89.0'
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" ;;
*)
echo "::error::No palantir-java-format native binary for ${OS}-${ARCH}. Supported: Linux x86_64, Linux aarch64, macOS aarch64."
exit 1
;;
esac
echo "suffix=$SUFFIX" >> "$GITHUB_OUTPUT"
- name: Cache palantir-java-format binary
id: cache
uses: actions/cache@v5
with:
path: ${{ github.action_path }}/palantir-java-format
key: palantir-java-format-${{ inputs.version }}-${{ steps.platform.outputs.suffix }}
- name: Download palantir-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/com/palantir/javaformat/palantir-java-format-native/${VERSION}"
ARTIFACT="palantir-java-format-native-${VERSION}-nativeImage-${SUFFIX}.bin"
DOWNLOAD_URL="${BASE_URL}/${ARTIFACT}"
BINARY="${{ github.action_path }}/palantir-java-format"
echo "Downloading palantir-java-format ${VERSION} (${SUFFIX})..."
if ! curl -sSfL -o "$BINARY" "$DOWNLOAD_URL"; then
rm -f "$BINARY"
echo "::error::Failed to download palantir-java-format from: $DOWNLOAD_URL"
exit 1
fi
chmod +x "$BINARY"
- name: Set binary path
shell: bash
run: echo "PJF_BINARY=${{ github.action_path }}/palantir-java-format" >> "$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/*' | 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$' | tr '\n' ' ')
elif [ -n "${{ github.event.before }}" ]; 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/*' | tr '\n' ' ')
fi
if [ -z "$FILES" ]; then
echo "No Java files to check."
echo "PJF_FILES=" >> "$GITHUB_ENV"
else
echo "Files to check:"
echo "$FILES" | tr ' ' '\n' | grep -v '^$'
echo "PJF_FILES=$FILES" >> "$GITHUB_ENV"
fi
env:
GH_TOKEN: ${{ github.token }}
- name: Run format check
shell: bash
run: |
if [ -z "$PJF_FILES" ]; then
echo "Nothing to check, skipping."
exit 0
fi
UNFORMATTED=()
for FILE in $PJF_FILES; do
if [ -f "$FILE" ]; then
if ! "$PJF_BINARY" --palantir --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 palantir-java-formatter locally to fix:"
echo " palantir-java-format --palantir --replace ${UNFORMATTED[*]}"
exit 1
fi
echo "All files are correctly formatted."