|
| 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