PREQ-5794 Fix check-sca Gradle Kotlin DSL projectKey parsing#254
Conversation
086aef8 to
4e3d881
Compare
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
4e3d881 to
c545e10
Compare
|
SummaryFixes What reviewers should knowCode changes:
Key details for review:
Questions for reviewers:
|
There was a problem hiding this comment.
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.shto match well-defined assignment andproperty(...)/set(...)argument forms (supporting both single and double quotes). - Add a ShellSpec regression test covering
build.gradle.ktsproperty("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.



Context
Surfaced via PREQ-5794 — Nils, while preparing the SCA check workflow on
SonarSource/sonar-php(nw/scabranch), reported thatcheck-scaignoressonar.projectKeyinbuild.gradle.ktsand 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: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 toSonarSource_<repo>— which does not exist for analyzers using Maven-style keys.Reproduction:
Fix
Tighten the regex to two well-defined shapes:
sonar.projectKey = \"value\"/sonar.projectKey: \"value\"(Groovy assignment / KTS map literal).(\"sonar.projectKey\", \"value\")(KTSproperty(...)/set(...)calls, including single-quoted Groovy form).Both single (
') and double (\") quotes are accepted.Validation
Manual matrix run on the new pattern:
property(\"sonar.projectKey\", \"org.sonarsource.php:php\")org.sonarsource.php:phpset(\"sonar.projectKey\", \"k1\")k1sonar.projectKey = \"k3\"k3sonar.projectKey: \"k4\"k4property('sonar.projectKey', 'k5')k5properties[\"sonar.projectKey\"] = \"k2\"Regression test added in
spec/check-sca_spec.shfor theproperty(\"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).sonar.projectKey = \"...\"would still be matched — same behaviour as before, no regression.settings.gradle.ktsor composite-build root projects.Workaround already used downstream
sonar-phpnw/scabranch sets theproject-key:input explicitly, which is the supported override and unblocks the requester:So this PR is not urgent — it removes a foot-gun before broad onboarding.
Test plan
@sonarsource/platform-eng-xp-squad, ideally Brian as the original author ofcheck-sca) confirms regex shape covers all KTS variants you care about for v1.check-sca.ymlself-test still green.sonar-phpnw/scaafter removing the explicitproject-key:input.