|
1 | 1 | package io.kestra.plugin.git; |
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.core.type.TypeReference; |
| 4 | +import io.kestra.core.exceptions.FlowProcessingException; |
4 | 5 | import io.kestra.core.models.flows.Flow; |
5 | 6 | import io.kestra.core.models.flows.FlowId; |
6 | 7 | import io.kestra.core.models.flows.FlowWithSource; |
|
16 | 17 | import io.kestra.core.junit.annotations.KestraTest; |
17 | 18 | import jakarta.inject.Inject; |
18 | 19 | import org.apache.commons.io.IOUtils; |
| 20 | +import org.eclipse.jgit.api.Git; |
19 | 21 | import org.junit.jupiter.api.BeforeEach; |
20 | 22 | import org.junit.jupiter.api.Test; |
21 | 23 |
|
@@ -442,6 +444,104 @@ void shouldFailWhenGitDirectoryDoesNotExist() throws Exception { |
442 | 444 | assertThat(exception.getMessage(), containsString("The directory 'nonexistent/path' was not found")); |
443 | 445 | } |
444 | 446 |
|
| 447 | + @Test |
| 448 | + void shouldIgnoreInvalidFlowFromGit() throws Exception { |
| 449 | + Path repoDir = Files.createTempDirectory("git-test"); |
| 450 | + Git git = Git.init().setDirectory(repoDir.toFile()).call(); |
| 451 | + |
| 452 | + Path flowsDir = repoDir.resolve("_flows"); |
| 453 | + Files.createDirectories(flowsDir); |
| 454 | + |
| 455 | + Files.writeString( |
| 456 | + flowsDir.resolve("invalid-flow.yaml"), |
| 457 | + """ |
| 458 | + id: invalid-flow |
| 459 | + namespace: my.namespace |
| 460 | +
|
| 461 | + tasks: |
| 462 | + - id: bad |
| 463 | + type: unknown.type |
| 464 | + """ |
| 465 | + ); |
| 466 | + |
| 467 | + git.add().addFilepattern(".").call(); |
| 468 | + git.commit().setMessage("Add invalid flow").call(); |
| 469 | + |
| 470 | + RunContext runContext = runContextFactory.of(Map.of( |
| 471 | + "flow", Map.of( |
| 472 | + "tenantId", TENANT_ID, |
| 473 | + "namespace", NAMESPACE, |
| 474 | + "id", FLOW_ID |
| 475 | + ), |
| 476 | + "url", repoDir.toUri().toString(), |
| 477 | + "branch", "master", |
| 478 | + "gitDirectory", "_flows", |
| 479 | + "namespace", NAMESPACE |
| 480 | + )); |
| 481 | + |
| 482 | + SyncFlows task = SyncFlows.builder() |
| 483 | + .url(new Property<>("{{url}}")) |
| 484 | + .branch(new Property<>("{{branch}}")) |
| 485 | + .gitDirectory(new Property<>("{{gitDirectory}}")) |
| 486 | + .targetNamespace(new Property<>("{{namespace}}")) |
| 487 | + .ignoreInvalidFlows(Property.ofValue(true)) |
| 488 | + .build(); |
| 489 | + |
| 490 | + SyncFlows.Output output = task.run(runContext); |
| 491 | + |
| 492 | + List<Flow> flows = flowRepositoryInterface.findByNamespace(TENANT_ID, NAMESPACE); |
| 493 | + assertThat(flows.stream().map(Flow::getId).toList(), not(hasItem("invalid-flow"))); |
| 494 | + |
| 495 | + assertThat(runContext.storage().getFile(output.diffFileUri()).toString(), not(containsString("\"flowId\":\"invalid-flow\""))); |
| 496 | + } |
| 497 | + |
| 498 | + @Test |
| 499 | + void shouldThrowOnInvalidFlowFromGit() throws Exception { |
| 500 | + Path repoDir = Files.createTempDirectory("git-test"); |
| 501 | + Git git = Git.init().setDirectory(repoDir.toFile()).call(); |
| 502 | + |
| 503 | + Path flowsDir = repoDir.resolve("_flows"); |
| 504 | + Files.createDirectories(flowsDir); |
| 505 | + |
| 506 | + Files.writeString( |
| 507 | + flowsDir.resolve("demo-invalid-flow.yaml"), |
| 508 | + """ |
| 509 | + id: invalid-flow-demo |
| 510 | + namespace: my.namespace |
| 511 | +
|
| 512 | + tasks: |
| 513 | + - id: bad |
| 514 | + type: unknown.type |
| 515 | + """ |
| 516 | + ); |
| 517 | + |
| 518 | + git.add().addFilepattern(".").call(); |
| 519 | + git.commit().setMessage("Add invalid flow").call(); |
| 520 | + |
| 521 | + RunContext runContext = runContextFactory.of(Map.of( |
| 522 | + "flow", Map.of( |
| 523 | + "tenantId", TENANT_ID, |
| 524 | + "namespace", NAMESPACE, |
| 525 | + "id", FLOW_ID |
| 526 | + ), |
| 527 | + "url", repoDir.toUri().toString(), |
| 528 | + "branch", "master", |
| 529 | + "gitDirectory", "_flows", |
| 530 | + "namespace", NAMESPACE |
| 531 | + )); |
| 532 | + |
| 533 | + SyncFlows task = SyncFlows.builder() |
| 534 | + .url(new Property<>("{{url}}")) |
| 535 | + .branch(new Property<>("{{branch}}")) |
| 536 | + .gitDirectory(new Property<>("{{gitDirectory}}")) |
| 537 | + .targetNamespace(new Property<>("{{namespace}}")) |
| 538 | + .build(); |
| 539 | + |
| 540 | + FlowProcessingException ex = assertThrows(FlowProcessingException.class, () -> task.run(runContext)); |
| 541 | + |
| 542 | + assertThat(ex.getMessage(), containsString("demo-invalid-flow")); |
| 543 | + } |
| 544 | + |
445 | 545 | private List<Map<String, Object>> defaultCaseDiffs(boolean includeSubNamespaces, Map<String, Object>... additionalDiffs) { |
446 | 546 | List<Map<String, Object>> diffs = new ArrayList<>(List.of( |
447 | 547 | Map.of("gitPath", "to_clone/_flows/unchanged-flow.yaml", "syncState", "UNCHANGED", "flowId", "unchanged-flow", "namespace", NAMESPACE, "revision", previousRevisionByUid.getOrDefault(FlowId.uidWithoutRevision(TENANT_ID, NAMESPACE, "unchanged-flow"), 1)), |
|
0 commit comments