Skip to content

PREQ-5794 Fix check-sca Gradle Kotlin DSL projectKey parsing#254

Merged
bwalsh434 merged 1 commit into
masterfrom
feat/smarini/PREQ-5794-check-sca-kts-projectkey-parsing
May 15, 2026
Merged

PREQ-5794 Fix check-sca Gradle Kotlin DSL projectKey parsing#254
bwalsh434 merged 1 commit into
masterfrom
feat/smarini/PREQ-5794-check-sca-kts-projectkey-parsing

Conversation

@SamirM-BE
Copy link
Copy Markdown
Contributor

@SamirM-BE SamirM-BE commented May 13, 2026

Context

Surfaced via PREQ-5794 — Nils, while preparing the SCA check workflow on SonarSource/sonar-php (nw/sca branch), reported that check-sca ignores sonar.projectKey in build.gradle.kts and falls back to a derived key that doesn't match the actual Sonar project key.

Originating action: introduced in BUILD-11091.

Root cause

The Gradle key-extraction regex in check-sca/check-sca.sh:

sonar\.projectKey[^"]*"([^"]+)"

is too loose for Kotlin DSL. On the typical KTS form:

property(\"sonar.projectKey\", \"org.sonarsource.php:php\")

it matches but captures , (the literal \", \" between the two arguments), not the project key. The action then queries the Sonar API with garbage and falls back to SonarSource_<repo> — which does not exist for analyzers using Maven-style keys.

Reproduction:

$ echo '    property(\"sonar.projectKey\", \"org.sonarsource.php:php\")' \
    | perl -ne 'print \"[\$1]\n\" if /sonar\.projectKey[^\"]*\"([^\"]+)\"/'
[, ]

Fix

Tighten the regex to two well-defined shapes:

  1. sonar.projectKey = \"value\" / sonar.projectKey: \"value\" (Groovy assignment / KTS map literal).
  2. (\"sonar.projectKey\", \"value\") (KTS property(...) / set(...) calls, including single-quoted Groovy form).

Both single (') and double (\") quotes are accepted.

Validation

Manual matrix run on the new pattern:

Input Captured
property(\"sonar.projectKey\", \"org.sonarsource.php:php\") org.sonarsource.php:php
set(\"sonar.projectKey\", \"k1\") k1
sonar.projectKey = \"k3\" k3
sonar.projectKey: \"k4\" k4
property('sonar.projectKey', 'k5') k5
properties[\"sonar.projectKey\"] = \"k2\" no match (known gap)

Regression test added in spec/check-sca_spec.sh for the property(\"sonar.projectKey\", ...) KTS form.

Known limitations / non-goals

  • properties[\"sonar.projectKey\"] = \"...\" indexed-property form still not detected (uncommon in our analyzers, can be added if needed).
  • Comments containing sonar.projectKey = \"...\" would still be matched — same behaviour as before, no regression.
  • Did not try to parse settings.gradle.kts or composite-build root projects.

Workaround already used downstream

sonar-php nw/sca branch sets the project-key: input explicitly, which is the supported override and unblocks the requester:

- uses: SonarSource/ci-github-actions/check-sca@v1
  with:
    project-key: \"org.sonarsource.php:php\"

So this PR is not urgent — it removes a foot-gun before broad onboarding.

Test plan

  • Owner (@sonarsource/platform-eng-xp-squad, ideally Brian as the original author of check-sca) confirms regex shape covers all KTS variants you care about for v1.
  • CI check-sca.yml self-test still green.
  • Optional: dry-run on sonar-php nw/sca after removing the explicit project-key: input.

@hashicorp-vault-sonar-prod
Copy link
Copy Markdown

hashicorp-vault-sonar-prod Bot commented May 13, 2026

PREQ-5794

@SamirM-BE SamirM-BE force-pushed the feat/smarini/PREQ-5794-check-sca-kts-projectkey-parsing branch from 086aef8 to 4e3d881 Compare May 13, 2026 13:14
The previous regex `sonar\.projectKey[^"]*"([^"]+)"` captured the wrong
token on `property("sonar.projectKey", "<value>")` (typical KTS form),
returning `, ` instead of the actual project key. The action then queried
the Sonar measures API with garbage and fell back to the derived
`SonarSource_<repo>` key, which does not match the real project key for
analyzers like sonar-php (`org.sonarsource.php:php`).

Tighten the regex to match either:
  * `sonar.projectKey = "<value>"` / `sonar.projectKey: "<value>"`
  * `("sonar.projectKey", "<value>")` (KTS `property(...)` / `set(...)`)

and add a regression test covering the KTS form.

Refs: BUILD-11091
@bwalsh434 bwalsh434 force-pushed the feat/smarini/PREQ-5794-check-sca-kts-projectkey-parsing branch from 4e3d881 to c545e10 Compare May 15, 2026 19:13
@sonarqubecloud
Copy link
Copy Markdown

@bwalsh434 bwalsh434 marked this pull request as ready for review May 15, 2026 19:20
@bwalsh434 bwalsh434 requested a review from a team as a code owner May 15, 2026 19:20
Copilot AI review requested due to automatic review settings May 15, 2026 19:20
@sonar-review-alpha
Copy link
Copy Markdown
Contributor

sonar-review-alpha Bot commented May 15, 2026

Summary

Fixes check-sca's inability to parse sonar.projectKey from Gradle Kotlin DSL (build.gradle.kts) files. The original regex was too loose and captured the separator between arguments instead of the key value. The fix tightens the pattern to explicitly match two common syntaxes: assignment (sonar.projectKey = "value") and function calls (property("sonar.projectKey", "value")), supporting both single and double quotes. Includes a regression test for the KTS property syntax case.

What reviewers should know

Code changes:

  • check-sca/check-sca.sh (lines 119–126): The core regex replacement. Note it now uses an inline Perl script with two alternative patterns and early exit for efficiency.
  • spec/check-sca_spec.sh: One new test case for the previously failing KTS property syntax.

Key details for review:

  • Both quoted-string patterns use ["\x27] (double and single quotes). The ["\x27]+ means "one or more non-quote characters", anchored correctly at both ends.
  • The second pattern matches sonar.projectKey" followed by a comma and separator — this is the specific form that was failing before (it captured , instead of the key).
  • Early exit (exit; on first match) prevents overwriting the captured value if there are multiple matches in the file.
  • Still does not detect properties["sonar.projectKey"] = ... (indexed form), but this is acknowledged as intentional and rare in the analyzers covered.

Questions for reviewers:

  • Does this regex shape cover all KTS/Groovy variants you need for v1?
  • The test matrix in the PR description is helpful — if you know of edge cases not listed there, they should be confirmed or added as tests.

  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes check-sca project-key discovery for Gradle Kotlin DSL by tightening the sonar.projectKey extraction so that build.gradle.kts property("sonar.projectKey", "...") (and similar shapes) is parsed correctly instead of capturing the comma/spacing and falling back to a derived key.

Changes:

  • Update Gradle projectKey extraction in check-sca.sh to match well-defined assignment and property(...)/set(...) argument forms (supporting both single and double quotes).
  • Add a ShellSpec regression test covering build.gradle.kts property("sonar.projectKey", "...") syntax.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
check-sca/check-sca.sh Tightens Perl regex logic for Gradle/GKTS sonar.projectKey extraction to correctly capture Kotlin DSL property/set forms.
spec/check-sca_spec.sh Adds regression coverage for discovering sonar.projectKey from build.gradle.kts via property("sonar.projectKey", "...").

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

sonar-review-alpha[bot]

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor

@sonar-review-alpha sonar-review-alpha Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! ✅

🗣️ Give feedback

@bwalsh434 bwalsh434 merged commit 6252996 into master May 15, 2026
24 checks passed
@bwalsh434 bwalsh434 deleted the feat/smarini/PREQ-5794-check-sca-kts-projectkey-parsing branch May 15, 2026 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants