Validate publishing target when submitting a package#3900
Validate publishing target when submitting a package#3900sumerjabri merged 1 commit intocraftercms:developfrom
Conversation
WalkthroughThe PR adds publish target validation to prevent invalid targets from being submitted. It introduces a new exception class, exception handler, validation logic that checks targets against configured environments, supporting API response code, and comprehensive test coverage. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/test/java/org/craftercms/studio/impl/v2/service/publish/internal/PublishServiceInternalImplTest.java (1)
50-57: Consider verifying exception details.The test verifies that
InvalidTargetExceptionis thrown, but doesn't assert that the exception contains the correct valid targets. Verifying the exception'sgetValidTargets()would strengthen the test.💡 Enhanced assertion example
-@Test(expected = InvalidTargetException.class) -public void testStagingFailsIfNotEnabled() throws AuthenticationException, ServiceLayerException { +@Test +public void testStagingFailsIfNotEnabled() throws AuthenticationException, ServiceLayerException { String siteId = "site1"; when(servicesConfig.getLiveEnvironment(siteId)).thenReturn("live"); - service.routePackageSubmission(siteId, "staging", emptyList(), emptyList(), - null, "Publish test", "Testing with invalid target", false, false); + InvalidTargetException exception = assertThrows(InvalidTargetException.class, () -> + service.routePackageSubmission(siteId, "staging", emptyList(), emptyList(), + null, "Publish test", "Testing with invalid target", false, false)); + + assertArrayEquals(new String[]{"live"}, exception.getValidTargets()); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/test/java/org/craftercms/studio/impl/v2/service/publish/internal/PublishServiceInternalImplTest.java` around lines 50 - 57, Update the testStagingFailsIfNotEnabled to capture the thrown InvalidTargetException and assert its valid targets: call service.routePackageSubmission inside a try-catch (or use ExpectedException rule) to catch InvalidTargetException from routePackageSubmission, then assert on ex.getValidTargets() contains the expected list (e.g., ["live"]) and any other expected details; reference the test method name testStagingFailsIfNotEnabled, the service.routePackageSubmission invocation, and the exception type InvalidTargetException/getValidTargets when adding the assertion.src/main/java/org/craftercms/studio/api/v2/exception/publish/InvalidTargetException.java (1)
30-32: Consider returning a defensive copy of the array.The getter exposes the internal array, allowing callers to modify it. While low-risk for an exception class, a defensive copy would be more robust.
🔧 Optional: Defensive copy
public String[] getValidTargets() { - return validTargets; + return validTargets != null ? validTargets.clone() : null; }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/main/java/org/craftercms/studio/api/v2/exception/publish/InvalidTargetException.java` around lines 30 - 32, The getter getValidTargets() in InvalidTargetException currently returns the internal array validTargets directly, exposing mutable state; change it to return a defensive copy (e.g., return validTargets == null ? null : validTargets.clone() or Arrays.copyOf(validTargets, validTargets.length)) so callers cannot modify the exception's internal array while preserving null handling.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In
`@src/main/java/org/craftercms/studio/api/v2/exception/publish/InvalidTargetException.java`:
- Around line 30-32: The getter getValidTargets() in InvalidTargetException
currently returns the internal array validTargets directly, exposing mutable
state; change it to return a defensive copy (e.g., return validTargets == null ?
null : validTargets.clone() or Arrays.copyOf(validTargets, validTargets.length))
so callers cannot modify the exception's internal array while preserving null
handling.
In
`@src/test/java/org/craftercms/studio/impl/v2/service/publish/internal/PublishServiceInternalImplTest.java`:
- Around line 50-57: Update the testStagingFailsIfNotEnabled to capture the
thrown InvalidTargetException and assert its valid targets: call
service.routePackageSubmission inside a try-catch (or use ExpectedException
rule) to catch InvalidTargetException from routePackageSubmission, then assert
on ex.getValidTargets() contains the expected list (e.g., ["live"]) and any
other expected details; reference the test method name
testStagingFailsIfNotEnabled, the service.routePackageSubmission invocation, and
the exception type InvalidTargetException/getValidTargets when adding the
assertion.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: a8524d0d-c1c4-4017-8160-1e4c498686db
📒 Files selected for processing (6)
src/main/java/org/craftercms/studio/api/v2/exception/publish/InvalidTargetException.javasrc/main/java/org/craftercms/studio/controller/rest/v2/ExceptionHandlers.javasrc/main/java/org/craftercms/studio/controller/rest/v2/ResultConstants.javasrc/main/java/org/craftercms/studio/impl/v2/service/publish/internal/PublishServiceInternalImpl.javasrc/main/java/org/craftercms/studio/model/rest/ApiResponse.javasrc/test/java/org/craftercms/studio/impl/v2/service/publish/internal/PublishServiceInternalImplTest.java
https://github.com/craftersoftware/craftercms/issues/1187
Validate publishing target when submitting a package
Summary by CodeRabbit
Release Notes