Skip to content

Commit

Permalink
fix(notifications/cdEvents): Fixed CDEvents notification for ManualJu…
Browse files Browse the repository at this point in the history
…dgment (#1451)

Co-authored-by: GARCIA, JOSE <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 12, 2024
1 parent 4fcd274 commit d935dae
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ public abstract class AbstractEventNotificationAgent implements EventListener {
.put(STAGE, ImmutableMap.of("type", STAGE, "link", "stage"))
.build();

private static final Map<String, String> MANUAL_JUDGMENT_CONDITIONS =
ImmutableMap.<String, String>builder()
.put(ManualJudgmentCondition.MANUAL_JUDGMENT.getName(), StageStatus.STARTING.getName())
.put(ManualJudgmentCondition.CONTINUE.getName(), StageStatus.COMPLETE.getName())
.put(ManualJudgmentCondition.STOP.getName(), StageStatus.FAILED.getName())
.build();

private final Logger log = LoggerFactory.getLogger(getClass());

protected ObjectMapper mapper = EchoObjectMapper.getInstance();
Expand Down Expand Up @@ -182,9 +189,11 @@ private boolean shouldSendRequestForNotification(
if (when != null) {
String requiredWhen = format("%s.%s", configType, status);
if (when instanceof String) {
return ((String) when).contains(requiredWhen);
return isManualJudgmentMatchStringCase((String) when, status)
|| ((String) when).contains(requiredWhen);
} else if (when instanceof Collection) {
return ((Collection<String>) when).contains(requiredWhen);
return isManualJudgmentMatchCollectionCase((Collection<String>) when, status)
|| ((Collection<String>) when).contains(requiredWhen);
}
}
}
Expand All @@ -199,6 +208,52 @@ private boolean contentKeyAsBoolean(Event event, String key) {
return Boolean.parseBoolean(value.toString());
}

enum StageStatus {
COMPLETE("complete"),
STARTING("starting"),
FAILED("failed");

final String name;

StageStatus(String name) {
this.name = name;
}

String getName() {
return this.name;
}
}

enum ManualJudgmentCondition {
CONTINUE("manualJudgmentContinue"),
STOP("manualJudgmentStop"),
MANUAL_JUDGMENT("manualJudgment");

final String name;

ManualJudgmentCondition(String name) {
this.name = name;
}

String getName() {
return this.name;
}
}

private static boolean isManualJudgmentMatchStringCase(String when, String status) {
return status.equals(MANUAL_JUDGMENT_CONDITIONS.get(when));
}

private static boolean isManualJudgmentMatchCollectionCase(
Collection<String> when, String status) {
for (String condition : when) {
if (status.equals(MANUAL_JUDGMENT_CONDITIONS.get(condition))) {
return true;
}
}
return false;
}

private static boolean isExecution(String type) {
return "pipeline".equals(type) || "orchestration".equals(type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ class AbstractEventNotificationAgentSpec extends Specification {
fakeStageEvent("orca:stage:complete", "stage.complete", false, true) || 0
}

@Unroll
def "sends notifications for ManualJudgment stage based on status and configuration"() {
given:
subclassMock.sendNotifications(*_) >> { notification, application, event_local, config, status -> }

when:
agent.processEvent(event)

then:
expectedNotifications * subclassMock.sendNotifications(*_)

where:
event || expectedNotifications
fakeStageEvent("orca:stage:complete", "manualJudgmentContinue") || 1
fakeStageEvent("orca:stage:starting", "manualJudgment") || 1
fakeStageEvent("orca:stage:failed", "manualJudgmentStop") || 1
}

private def fakePipelineEvent(String type, String status, String notifyWhen, Map extraExecutionProps = [:]) {
def eventProps = [
details: [type: type],
Expand Down

0 comments on commit d935dae

Please sign in to comment.