Skip to content

Commit f1eb504

Browse files
committed
Refactor doNotRunCodeAfter into isDate
1 parent 81c0894 commit f1eb504

3 files changed

Lines changed: 84 additions & 25 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ full change log see the commit log.
2121
then they can enhance the filtering available to users calling `isBuilding`
2222
step with any metadata which falls outside of triggering and context built
2323
into the Jervis library.
24+
- `isDate` step available.
2425

2526
# jervis 2.3 - Jan 16th, 2026
2627

vars/doNotRunCodeAfter.groovy

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,12 @@
3838
}
3939
*/
4040

41-
import java.time.Instant
42-
43-
@NonCPS
44-
Boolean dateIsValid(String date) {
45-
if(!(date =~ /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/)) {
46-
return false
47-
}
48-
date.tokenize('-').collect { Integer.parseInt(it) }.with { List dateItems ->
49-
if(dateItems.any { it < 1 }) {
50-
return false
51-
}
52-
// month and day
53-
if(dateItems[1] > 12 || dateItems[2] > 31) {
54-
return false
55-
}
56-
// date is valid
57-
true
58-
}
59-
}
60-
6141
@NonCPS
6242
def date(String date, Closure body = null) {
63-
if(!dateIsValid(date)) {
43+
if(!isDate.valid(date)) {
6444
throw new Exception('doNotRunCodeAfter date format must be "YYYY-MM-DD"')
6545
}
66-
// ISO 8601 date-time format is used for comparison
67-
String iso8601suffix = 'T00:00:00.0Z'
68-
Boolean shouldRun = Instant.now().isBefore(Instant.parse(date + iso8601suffix))
46+
Boolean shouldRun = isDate(before: date)
6947
if(body) {
7048
if(shouldRun) {
7149
return body()
@@ -81,7 +59,7 @@ def call(String date, Closure body) {
8159
}
8260

8361
def call(Map settings, Closure body) {
84-
if(!dateIsValid(settings.date ?: '')) {
62+
if(!isDate.valid(settings.date ?: '')) {
8563
error 'doNotRunCodeAfter date format must be "YYYY-MM-DD"'
8664
}
8765
if(settings.afterDate && !(settings.afterDate in Closure)) {

vars/isDate.groovy

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
Copyright 2014-2026 Sam Gleske - https://github.com/samrocketman/jervis
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
import java.time.Instant
17+
18+
/**
19+
This step provides some date-based comparisions to pipeline conditionals.
20+
21+
Example usage:
22+
if(isDate(before: '2026-01-01')) {
23+
// code runs if the current date is before the given date
24+
// never runs after the given date.
25+
}
26+
if(isDate(after: '2026-01-01')) {
27+
// code runs if the current date is after the given date
28+
// never runs before the given date.
29+
}
30+
if(isDate(date: '2025-01-01', before: '2026-01-01')) {
31+
// code always runs because specific date is before a given date.
32+
}
33+
if(isDate(date: '2025-01-01', after: '2026-01-01')) {
34+
// code never runs because specific date is before a given date.
35+
}
36+
37+
Date validation examples:
38+
// true because the given date is a valid YYYY-MM-DD.
39+
Boolean isDateValid = isDate.valid('2026-01-01')
40+
41+
// false because the given date is a not formatted as YYYY-MM-DD.
42+
Boolean isDateValid = isDate.valid('not a number')
43+
*/
44+
45+
@NonCPS
46+
String now() {
47+
Instant.now()
48+
}
49+
50+
@NonCPS
51+
Boolean valid(String date) {
52+
if(!(date =~ /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/)) {
53+
return false
54+
}
55+
date.tokenize('-').collect { Integer.parseInt(it) }.with { List dateItems ->
56+
if(dateItems.any { it < 1 }) {
57+
return false
58+
}
59+
// month and day
60+
if(dateItems[1] > 12 || dateItems[2] > 31) {
61+
return false
62+
}
63+
// date is valid
64+
true
65+
}
66+
}
67+
68+
@NonCPS
69+
Boolean call(Map settings) {
70+
// ISO 8601 date-time format is used for comparison
71+
String iso8601suffix = 'T00:00:00.0Z'
72+
Instant date = (settings.date) ? Instant.parse(settings.date + iso8601suffix) : now()
73+
if(settings.before) {
74+
date.isBefore(Instant.parse(date + iso8601suffix))
75+
} else if(settings.after) {
76+
date.isAfter(Instant.parse(date + iso8601suffix))
77+
} else {
78+
error 'isDate must be called with "before" or "after" date option.'
79+
}
80+
}

0 commit comments

Comments
 (0)