| # Licensed to the Apache Software Foundation (ASF) under one | |
| # or more contributor license agreements. See the NOTICE file | |
| # distributed with this work for additional information | |
| # regarding copyright ownership. The ASF licenses this file | |
| # to you under the Apache License, Version 2.0 (the | |
| # "License"); you may not use this file except in compliance | |
| # with the License. You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, | |
| # software distributed under the License is distributed on an | |
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| # KIND, either express or implied. See the License for the | |
| # specific language governing permissions and limitations | |
| # under the License. | |
| # | |
| --- | |
| name: Code Style | |
| on: | |
| pull_request: | |
| branches: [master] | |
| permissions: | |
| contents: read | |
| pull-requests: read | |
| jobs: | |
| style: | |
| name: Code Style | |
| runs-on: ubuntu-24.04 | |
| concurrency: | |
| group: code-style-${{ github.event.pull_request.number }}-${{ github.sha }} | |
| cancel-in-progress: true | |
| steps: | |
| - name: Verify current PR head | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| PR_NUMBER: ${{ github.event.pull_request.number }} | |
| HEAD_SHA: ${{ github.event.pull_request.head.sha }} | |
| run: | | |
| current_head=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" --jq '.head.sha') | |
| if [[ "$current_head" != "$HEAD_SHA" ]]; then | |
| echo "::error::PR #$PR_NUMBER moved from $HEAD_SHA to $current_head; refusing stale validation" | |
| exit 1 | |
| fi | |
| - name: Select checks for the PR snapshot | |
| id: paths | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| BASE_SHA: ${{ github.event.pull_request.base.sha }} | |
| HEAD_SHA: ${{ github.event.pull_request.head.sha }} | |
| shell: bash | |
| run: | | |
| # The first compare page lists at most 300 files. At the limit, run both checks. | |
| files="$RUNNER_TEMP/code-style-files.json" | |
| if ! gh api "repos/$GITHUB_REPOSITORY/compare/$BASE_SHA...$HEAD_SHA?per_page=1" \ | |
| --jq '{merge_base: .merge_base_commit.sha, files: (.files | map({filename, previous_filename, status, sha}))}' > "$files" || | |
| ! selection=$(jq -er ' | |
| .files | select(type == "array" and length < 300) | |
| | [.[] | .filename, (.previous_filename // empty)] | |
| | any(.[]; . == ".github/workflows/code-style.yml") as $workflow | |
| | ($workflow or any(.[]; test("^(fe/|gensrc/(proto|thrift)/)"))) as $java | |
| | ($workflow or any(.[]; test("^(be|cloud)/(src|test)/"))) as $cpp | |
| | "java=\($java)\ncpp=\($cpp)"' "$files"); then | |
| echo "::warning::Changed paths unavailable or incomplete; running both style checks" | |
| selection=$'java=true\ncpp=true' | |
| fi | |
| printf '%s\n' "$selection" >> "$GITHUB_OUTPUT" | |
| - name: Warn about large added files | |
| id: sizes | |
| continue-on-error: true | |
| timeout-minutes: 2 | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| shell: python3 {0} | |
| run: | | |
| import html | |
| import json | |
| import os | |
| from pathlib import Path | |
| import subprocess | |
| def api(*args): | |
| return json.loads(subprocess.check_output(["gh", "api", *args])) | |
| pr = json.loads(Path(os.environ["GITHUB_EVENT_PATH"]).read_text())["pull_request"] | |
| repo = os.environ["GITHUB_REPOSITORY"] | |
| temp = Path(os.environ["RUNNER_TEMP"]) | |
| try: | |
| comparison = json.loads((temp / "code-style-files.json").read_text()) | |
| except (OSError, ValueError): | |
| comparison = {} | |
| files = comparison.get("files") | |
| if files is not None and len(files) < 300: | |
| added = [(f["filename"], f["sha"]) for f in files if f["status"] == "added"] | |
| else: | |
| # GitHub caps compare at 300 and PR files at 3,000. Diff exact commits without a checkout. | |
| objects_dir = temp / "code-style-objects.git" | |
| subprocess.run(["git", "init", "--bare", "-q", objects_dir], check=True) | |
| def git(*args): | |
| return subprocess.check_output(["git", "-C", objects_dir, *args]) | |
| git("remote", "add", "origin", f"{os.environ['GITHUB_SERVER_URL']}/{repo}.git") | |
| head = pr["head"]["sha"] | |
| base = comparison.get("merge_base") | |
| if base: | |
| git("fetch", "--no-tags", "--depth=1", "--filter=blob:none", "origin", base, head) | |
| else: | |
| git("fetch", "--no-tags", "--filter=tree:0", "origin", pr["base"]["sha"], head) | |
| base = git("merge-base", pr["base"]["sha"], head).decode().strip() | |
| entries = git("diff", "--raw", "--no-abbrev", "-z", "--find-renames", "--diff-filter=A", | |
| base, head, "--").split(b"\0") | |
| added = [(os.fsdecode(path), header.split()[3].decode()) | |
| for header, path in zip(entries[::2], entries[1::2]) if header.split()[1] != b"160000"] | |
| owner, name = repo.split("/") | |
| summary = Path(os.environ["GITHUB_STEP_SUMMARY"]) | |
| missing_size = False | |
| oversized = 0 | |
| for start in range(0, len(added), 100): | |
| batch = added[start:start + 100] | |
| fields = " ".join(f"f{i}:object(oid:{json.dumps(oid)}){{__typename ... on Blob {{byteSize}}}}" | |
| for i, (_, oid) in enumerate(batch)) | |
| query = "query($owner:String!,$name:String!){repository(owner:$owner,name:$name){" + fields + "}}" | |
| objects = api("graphql", "-f", f"query={query}", "-f", f"owner={owner}", "-f", f"name={name}")["data"]["repository"] | |
| for i, (filename, _) in enumerate(batch): | |
| obj = objects[f"f{i}"] | |
| if obj is None: | |
| missing_size = True | |
| continue | |
| if obj["__typename"] != "Blob" or obj["byteSize"] <= 1024 * 1024: | |
| continue | |
| size = obj["byteSize"] | |
| path = html.escape(json.dumps(filename, ensure_ascii=False)[1:-1]) | |
| oversized += 1 | |
| with summary.open("a", encoding="utf-8", errors="backslashreplace") as out: | |
| print(f"<p>Large added file: <code>{path}</code> ({size:,} bytes, limit 1 MiB).</p>", file=out) | |
| if oversized: | |
| print(f"::warning::{oversized} added file(s) exceed 1 MiB. See the Code Style summary.") | |
| if missing_size: | |
| raise RuntimeError("GitHub did not return sizes for all added objects") | |
| - name: Report incomplete large-file check | |
| if: steps.sizes.outcome == 'failure' | |
| run: | | |
| echo "::warning::The large-file check was incomplete. See the Code Style summary." | |
| echo "The large-file check could not verify all added files." >> "$GITHUB_STEP_SUMMARY" | |
| - name: Checkout source | |
| id: source | |
| uses: actions/checkout@v7 | |
| with: | |
| persist-credentials: false | |
| - name: Check for secrets | |
| if: "!cancelled() && steps.source.outcome == 'success'" | |
| uses: ./.github/actions/pr-gitleaks | |
| - name: Check license headers | |
| if: "!cancelled() && steps.source.outcome == 'success'" | |
| uses: ./.github/actions/pr-license | |
| - name: Checkout clang-format action | |
| id: clang-action | |
| if: "!cancelled() && steps.source.outcome == 'success' && steps.paths.outputs.cpp == 'true'" | |
| uses: actions/checkout@v7 | |
| with: | |
| repository: DoozyX/clang-format-lint-action | |
| ref: c71d0bf4e21876ebec3e5647491186f8797fde31 # v0.18.2 | |
| path: .github/actions/clang-format-lint-action | |
| persist-credentials: false | |
| - name: Set up Python for clang-format | |
| id: clang-python | |
| if: "!cancelled() && steps.clang-action.outcome == 'success'" | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.10' | |
| - name: Run Clang Formatter | |
| if: "!cancelled() && steps.clang-python.outcome == 'success'" | |
| uses: ./.github/actions/clang-format-lint-action | |
| with: | |
| source: 'be/src be/test cloud/src cloud/test' | |
| clangFormatVersion: 16 | |
| inplace: false | |
| - name: Set up JDK with Maven | |
| id: jdk | |
| # Report Java violations even if the C++ check failed on the same PR. | |
| if: "!cancelled() && steps.source.outcome == 'success' && steps.paths.outputs.java == 'true'" | |
| uses: actions/setup-java@v4 | |
| with: | |
| distribution: temurin | |
| java-version: '17' | |
| cache: maven | |
| - name: Run Java CheckStyle | |
| if: "!cancelled() && steps.jdk.outcome == 'success'" | |
| working-directory: fe | |
| run: mvn clean checkstyle:check |