diff --git a/.github/scripts/check-sonar-pr-diff.py b/.github/scripts/check-sonar-pr-diff.py
new file mode 100755
index 000000000000..1caa45aec156
--- /dev/null
+++ b/.github/scripts/check-sonar-pr-diff.py
@@ -0,0 +1,237 @@
+#!/usr/bin/env python3
+#
+# 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.
+#
+
+"""Check whether a PR diff touches any Sonar-analyzable main Java source.
+
+SonarCloud for apache_iotdb analyzes main Java sources (`src/main/java/**` and
+`src/main/i18n/en/**`) in the default Maven reactor excluding `distribution`,
+any module (and its subtree) marked `true` (such as
+`example`), and profile-only modules (`integration-test`, `code-coverage`,
+`iotdb-client/client-cpp`, `iotdb-core/ainode`).
+
+Given a base Git ref and head ref, this script inspects the three-dot PR diff
+(`...
`) and emits `run_sonar=true|false` for GitHub Actions so PRs
+without analyzable Java changes can skip the SonarCloud build and scan.
+"""
+
+from __future__ import annotations
+
+import argparse
+from pathlib import Path, PurePosixPath
+import subprocess
+import sys
+import xml.etree.ElementTree as ET
+
+EXCLUDED_MODULES = {"distribution"}
+ANALYZABLE_SOURCE_PREFIXES = ("src/main/java/", "src/main/i18n/en/")
+MAVEN_NS = "{http://maven.apache.org/POM/4.0.0}"
+
+
+def discover_reactor_modules(repo_root: Path) -> set[str]:
+ """Walk default `` from the root pom.xml, excluding non-Sonar modules."""
+ modules: set[str] = set()
+
+ def walk(rel_dir: PurePosixPath) -> None:
+ rel_str = "." if rel_dir == PurePosixPath(".") else rel_dir.as_posix()
+ if rel_str in EXCLUDED_MODULES:
+ return
+ pom_path = repo_root / rel_str / "pom.xml" if rel_str != "." else repo_root / "pom.xml"
+ if not pom_path.is_file():
+ return
+
+ tree = ET.parse(pom_path)
+ root = tree.getroot()
+ ns = MAVEN_NS if root.tag.startswith(MAVEN_NS) else ""
+ props_elem = root.find(f"{ns}properties")
+ if props_elem is not None:
+ sonar_skip = props_elem.findtext(f"{ns}sonar.skip", "").strip().lower()
+ if sonar_skip == "true":
+ return
+ modules.add(rel_str)
+
+ modules_elem = root.find(f"{ns}modules")
+ if modules_elem is None:
+ return
+ for child in modules_elem.findall(f"{ns}module"):
+ if not child.text:
+ continue
+ child_name = child.text.strip()
+ if not child_name:
+ continue
+ child_rel = (
+ PurePosixPath(child_name)
+ if rel_str == "."
+ else rel_dir / PurePosixPath(child_name)
+ )
+ walk(child_rel)
+
+ walk(PurePosixPath("."))
+ modules.discard(".")
+ return modules
+
+
+def find_enclosing_module(rel_path: str, eligible_modules: set[str]) -> str | None:
+ """Return the nearest enclosing eligible Maven module for a repo-relative path."""
+ parent = PurePosixPath(rel_path).parent
+ while str(parent) not in ("", "."):
+ candidate = parent.as_posix()
+ if candidate in eligible_modules:
+ return candidate
+ parent = parent.parent
+ return None
+
+
+def is_analyzable_source(rel_path: str, module_dir: str) -> bool:
+ """Check whether `rel_path` is a main Java source indexed by SonarCloud."""
+ if not rel_path.endswith(".java"):
+ return False
+ prefix = f"{module_dir}/"
+ if not rel_path.startswith(prefix):
+ return False
+ module_rel = rel_path[len(prefix) :]
+ return module_rel.startswith(ANALYZABLE_SOURCE_PREFIXES)
+
+
+def find_analyzable_files(
+ changed_files: list[str],
+ repo_root: Path,
+ eligible_modules: set[str],
+ *,
+ require_existing: bool = True,
+) -> list[str]:
+ """Filter changed repo-relative paths to sorted Sonar-analyzable Java files."""
+ analyzable_files: list[str] = []
+
+ for raw_path in changed_files:
+ rel_path = raw_path.replace("\\", "/")
+ while rel_path.startswith("./"):
+ rel_path = rel_path[2:]
+ if not rel_path:
+ continue
+ if require_existing and not (repo_root / rel_path).is_file():
+ continue
+ module_dir = find_enclosing_module(rel_path, eligible_modules)
+ if module_dir is None:
+ continue
+ if is_analyzable_source(rel_path, module_dir):
+ analyzable_files.append(rel_path)
+
+ return sorted(analyzable_files)
+
+
+def git_changed_files(repo_root: Path, base_ref: str, head_ref: str) -> tuple[str, list[str]]:
+ """Verify merge-base and return (merge_base_sha, changed_files) for `...`."""
+ merge_base = subprocess.check_output(
+ ["git", "merge-base", base_ref, head_ref],
+ cwd=repo_root,
+ text=True,
+ ).strip()
+
+ diff_output = subprocess.check_output(
+ [
+ "git",
+ "-c",
+ "core.quotepath=false",
+ "diff",
+ "-z",
+ "--name-only",
+ "--diff-filter=ACMR",
+ f"{base_ref}...{head_ref}",
+ ],
+ cwd=repo_root,
+ )
+ files = [
+ entry.decode("utf-8", errors="surrogateescape")
+ for entry in diff_output.split(b"\0")
+ if entry
+ ]
+ return merge_base, files
+
+
+def write_github_outputs(output_path: Path, run_sonar: bool) -> None:
+ with output_path.open("a", encoding="utf-8") as fh:
+ fh.write(f"run_sonar={'true' if run_sonar else 'false'}\n")
+
+
+def parse_args(argv: list[str]) -> argparse.Namespace:
+ parser = argparse.ArgumentParser(
+ description="Determine whether a PR diff touches Sonar-analyzable Java sources."
+ )
+ parser.add_argument(
+ "--repo-root",
+ type=Path,
+ default=Path(__file__).resolve().parents[2],
+ help="Path to the repository root.",
+ )
+ parser.add_argument(
+ "--base-ref",
+ required=True,
+ help="Git base ref to diff against (e.g. refs/remotes/origin/master).",
+ )
+ parser.add_argument(
+ "--head-ref",
+ default="HEAD",
+ help="Git head ref to diff (default: HEAD).",
+ )
+ parser.add_argument(
+ "--github-output",
+ type=Path,
+ default=None,
+ help="Optional path to $GITHUB_OUTPUT file.",
+ )
+ return parser.parse_args(argv)
+
+
+def main(argv: list[str] | None = None) -> int:
+ args = parse_args(sys.argv[1:] if argv is None else argv)
+ repo_root = args.repo_root.resolve()
+
+ eligible_modules = discover_reactor_modules(repo_root)
+ merge_base, changed_files = git_changed_files(repo_root, args.base_ref, args.head_ref)
+ analyzable_files = find_analyzable_files(
+ changed_files,
+ repo_root,
+ eligible_modules,
+ require_existing=(args.head_ref == "HEAD"),
+ )
+
+ print(f"Base ref: {args.base_ref}")
+ print(f"Merge base: {merge_base}")
+ print(f"Changed files in PR diff (ACMR): {len(changed_files)}")
+ print(f"Sonar-analyzable changed Java files: {len(analyzable_files)}")
+ for path in analyzable_files[:50]:
+ print(f" - {path}")
+ if len(analyzable_files) > 50:
+ print(f" ... and {len(analyzable_files) - 50} more")
+
+ run_sonar = bool(analyzable_files)
+ if run_sonar:
+ print("Analyzable Java changes detected; running full-reactor SonarCloud PR analysis.")
+ else:
+ print("No Sonar-analyzable main Java sources changed in this PR; skipping SonarCloud scan.")
+
+ if args.github_output is not None:
+ write_github_outputs(args.github_output, run_sonar)
+
+ return 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())
diff --git a/.github/workflows/sonar-codecov.yml b/.github/workflows/sonar-codecov.yml
index 88f69e5938d4..92bf0d61102f 100644
--- a/.github/workflows/sonar-codecov.yml
+++ b/.github/workflows/sonar-codecov.yml
@@ -68,10 +68,36 @@ jobs:
sonar:
runs-on: ubuntu-latest
- if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == 'apache/iotdb' || github.event_name == 'push'
+ if: (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == 'apache/iotdb') || github.event_name == 'push'
+ env:
+ MAVEN_OPTS: -Xms2g -Xmx8g -Dhttp.keepAlive=false -Dmaven.wagon.http.pool=false -Dmaven.wagon.http.retryHandler.class=standard -Dmaven.wagon.http.retryHandler.count=3
steps:
- uses: actions/checkout@v5
+ with:
+ ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
+ fetch-depth: 0
+ - name: Check SonarCloud scan scope
+ id: sonar-scope
+ shell: bash
+ env:
+ EVENT_NAME: ${{ github.event_name }}
+ BASE_REF: ${{ github.base_ref }}
+ run: |
+ set -euo pipefail
+ if [ "${EVENT_NAME}" = "push" ]; then
+ echo "Push event on ${GITHUB_REF}: running full SonarCloud scan (excluding distribution)."
+ echo 'run_sonar=true' >> "${GITHUB_OUTPUT}"
+ exit 0
+ fi
+ if ! git rev-parse --verify --quiet "refs/remotes/origin/${BASE_REF}" >/dev/null; then
+ git fetch --no-tags origin "+refs/heads/${BASE_REF}:refs/remotes/origin/${BASE_REF}"
+ fi
+ python3 .github/scripts/check-sonar-pr-diff.py \
+ --base-ref "refs/remotes/origin/${BASE_REF}" \
+ --head-ref HEAD \
+ --github-output "${GITHUB_OUTPUT}"
- name: Set up JDK 21
+ if: steps.sonar-scope.outputs.run_sonar == 'true'
uses: actions/setup-java@v5
with:
distribution: corretto
@@ -79,19 +105,29 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Cache Maven packages
+ if: steps.sonar-scope.outputs.run_sonar == 'true'
uses: actions/cache@v5
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2-
+ - name: Cache SonarCloud packages
+ if: steps.sonar-scope.outputs.run_sonar == 'true'
+ uses: actions/cache@v5
+ with:
+ path: ~/.sonar/cache
+ key: ${{ runner.os }}-sonar
+ restore-keys: ${{ runner.os }}-sonar
- name: SonarCloud Report
+ if: steps.sonar-scope.outputs.run_sonar == 'true'
+ shell: bash
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONARCLOUD_TOKEN }}
run: |
- mvn -B -P with-integration-tests,with-code-coverage verify sonar:sonar \
+ mvn -B verify sonar:sonar \
-Dsonar.organization=apache \
-Dsonar.projectKey=apache_iotdb \
-Dsonar.host.url=https://sonarcloud.io \
- -Dsonar.token=${{ secrets.SONARCLOUD_TOKEN }} \
- -DskipTests -pl '!distribution,!integration-test' -am
+ -Dsonar.token="${SONAR_TOKEN}" \
+ -DskipTests -pl '!distribution' -am
diff --git a/example/pom.xml b/example/pom.xml
index 2e27f19cc5c5..81d31a00ffb9 100644
--- a/example/pom.xml
+++ b/example/pom.xml
@@ -29,6 +29,9 @@
iotdb-examples
pom
IoTDB: Examples
+
+ true
+
jdbc
mqtt
diff --git a/integration-test/pom.xml b/integration-test/pom.xml
index f9045c99508e..87040d53ab1a 100644
--- a/integration-test/pom.xml
+++ b/integration-test/pom.xml
@@ -90,6 +90,7 @@
2
Ratis
3
+ true