From 46dfc5eb1dd0d1ee0ebfe62d89dc664f5e558fab Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@arkins.net> Date: Thu, 18 Oct 2018 15:58:15 +0200 Subject: [PATCH] =?UTF-8?q?feat(schedule):=20massage=20=E2=80=9Cevery=20mo?= =?UTF-8?q?nth=E2=80=9D=20and=20=E2=80=9Cmonthly=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/workers/branch/schedule.js | 12 ++++++++++-- test/workers/branch/schedule.spec.js | 8 ++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/workers/branch/schedule.js b/lib/workers/branch/schedule.js index 9eba89007b..a03a69deff 100644 --- a/lib/workers/branch/schedule.js +++ b/lib/workers/branch/schedule.js @@ -8,6 +8,11 @@ module.exports = { isScheduledNow, }; +const scheduleMappings = { + 'every month': 'before 3am on the first day of the month', + monthly: 'before 3am on the first day of the month', +}; + function fixShortHours(input) { return input.replace(/( \d?\d)((a|p)m)/g, '$1:00$2'); } @@ -30,7 +35,9 @@ function hasValidSchedule(schedule) { } // check if any of the schedules fail to parse const hasFailedSchedules = schedule.some(scheduleText => { - const massagedText = fixShortHours(scheduleText); + const massagedText = fixShortHours( + scheduleMappings[scheduleText] || scheduleText + ); const parsedSchedule = later.parse.text(massagedText); if (parsedSchedule.error !== -1) { message = `Failed to parse schedule "${scheduleText}"`; @@ -108,7 +115,8 @@ function isScheduledNow(config) { logger.debug(`Checking ${configSchedule.length} schedule(s)`); // We run if any schedule matches const isWithinSchedule = configSchedule.some(scheduleText => { - const parsedSchedule = later.parse.text(fixShortHours(scheduleText)); + const massagedText = scheduleMappings[scheduleText] || scheduleText; + const parsedSchedule = later.parse.text(fixShortHours(massagedText)); logger.debug({ parsedSchedule }, `Checking schedule "${scheduleText}"`); // Later library returns array of schedules return parsedSchedule.schedules.some(schedule => { diff --git a/test/workers/branch/schedule.spec.js b/test/workers/branch/schedule.spec.js index 9c510f9dfb..602c29fbb7 100644 --- a/test/workers/branch/schedule.spec.js +++ b/test/workers/branch/schedule.spec.js @@ -75,6 +75,14 @@ describe('workers/branch/schedule', () => { ])[0] ).toBe(true); }); + it('massages schedules', () => { + expect( + schedule.hasValidSchedule([ + 'before 3am on the first day of the month', + ])[0] + ).toBe(true); + expect(schedule.hasValidSchedule(['every month'])[0]).toBe(true); + }); it('supports hours shorthand', () => { const [res] = schedule.hasValidSchedule([ 'after 11pm and before 6am every weekend', -- GitLab