Skip to content

Commit 0f64f22

Browse files
[7.17] Adjust date conversion for execution context in watcher compare condition (#88467)
* Add test of date math with ctx.execution_time Add one test to confirm the below watcher compare condition is always met. ``` "condition": { "compare": { "ctx.execution_time": { "gte": "<{now-5m}>" } } } ``` v1 can be an instance of JodaCompatibleZonedDateTime in versions prior to #78417, which has to be converted to ZonedDateTime for appropriate comparison. Closes #88408
1 parent c0fac94 commit 0f64f22

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

docs/changelog/88467.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 88467
2+
summary: Adjust date conversion for execution context in watcher compare condition
3+
area: Infra/Scripting
4+
type: bug
5+
issues:
6+
- 88408

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/LenientCompare.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
package org.elasticsearch.xpack.watcher.condition;
88

9+
import org.elasticsearch.script.JodaCompatibleZonedDateTime;
910
import org.elasticsearch.xpack.core.watcher.support.WatcherDateTimeUtils;
1011

1112
import java.time.Instant;
@@ -69,6 +70,9 @@ public static Integer compare(Object v1, Object v2) {
6970
}
7071
} else if (v1 instanceof Number) {
7172
v1 = Instant.ofEpochMilli(((Number) v1).longValue()).atZone(ZoneOffset.UTC);
73+
} else if (v1 instanceof JodaCompatibleZonedDateTime) {
74+
// this can only occur in versions prior to #78417
75+
v1 = ((JodaCompatibleZonedDateTime) v1).getZonedDateTime();
7276
} else {
7377
// cannot convert to date...
7478
return null;

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/CompareConditionTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ public void testExecuteDateMath() throws Exception {
188188
assertThat(condition.execute(ctx).met(), is(met));
189189
}
190190

191+
public void testExecuteDateMathExecutionContext() throws Exception {
192+
ClockMock clock = ClockMock.frozen();
193+
boolean met = true;
194+
Op op = CompareCondition.Op.GTE;
195+
String value = "<{now-5m}>";
196+
197+
CompareCondition condition = new CompareCondition("ctx.execution_time", op, value, clock);
198+
WatchExecutionContext ctx = mockExecutionContext("_name", null);
199+
assertThat(condition.execute(ctx).met(), is(met));
200+
}
201+
191202
public void testExecutePath() throws Exception {
192203
ClockMock clock = ClockMock.frozen();
193204
boolean met = randomBoolean();

0 commit comments

Comments
 (0)