Skip to content

Commit

Permalink
refactor(migrations): schedule (#14737)
Browse files Browse the repository at this point in the history
  • Loading branch information
pret-a-porter committed Mar 28, 2022
1 parent 18b884d commit 25d793f
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 66 deletions.
66 changes: 0 additions & 66 deletions lib/config/migration.ts
@@ -1,4 +1,3 @@
import later from '@breejs/later';
import is from '@sindresorhus/is';
import { dequal } from 'dequal';
import { logger } from '../logger';
Expand Down Expand Up @@ -182,71 +181,6 @@ export function migrateConfig(
} else if (key === 'separateMajorReleases') {
delete migratedConfig.separateMultipleMajor;
migratedConfig.separateMajorMinor = val;
} else if (key === 'schedule' && val) {
// massage to array first
const schedules = is.string(val) ? [val] : [...(val as string[])];
// split 'and'
const schedulesLength = schedules.length;
const afterBeforeRe = regEx(
/^(.*?)(after|before) (.*?) and (after|before) (.*?)( |$)(.*)/
);
for (let i = 0; i < schedulesLength; i += 1) {
if (
schedules[i].includes(' and ') &&
schedules[i].includes('before ') &&
schedules[i].includes('after ')
) {
const parsedSchedule = later.parse.text(
// We need to massage short hours first before we can parse it
fixShortHours(schedules[i])
).schedules[0];
// Only migrate if the after time is greater than before, e.g. "after 10pm and before 5am"
if (parsedSchedule?.t_a?.[0] > parsedSchedule?.t_b?.[0]) {
const toSplit = schedules[i];
schedules[i] = toSplit
.replace(afterBeforeRe, '$1$2 $3 $7')
.trim();
schedules.push(
toSplit.replace(afterBeforeRe, '$1$4 $5 $7').trim()
);
}
}
}
for (let i = 0; i < schedules.length; i += 1) {
if (schedules[i].includes('on the last day of the month')) {
schedules[i] = schedules[i].replace(
'on the last day of the month',
'on the first day of the month'
);
}
if (schedules[i].includes('on every weekday')) {
schedules[i] = schedules[i].replace(
'on every weekday',
'every weekday'
);
}
if (schedules[i].endsWith(' every day')) {
schedules[i] = schedules[i].replace(' every day', '');
}
if (
regEx(/every (mon|tues|wednes|thurs|fri|satur|sun)day$/).test(
schedules[i]
)
) {
schedules[i] = schedules[i].replace(
regEx(/every ([a-z]*day)$/),
'on $1'
);
}
if (schedules[i].endsWith('days')) {
schedules[i] = schedules[i].replace('days', 'day');
}
}
if (is.string(val) && schedules.length === 1) {
[migratedConfig.schedule] = schedules as any; // TODO: fixme
} else {
migratedConfig.schedule = schedules;
}
} else if (is.string(val) && val.startsWith('{{semanticPrefix}}')) {
migratedConfig[key] = val.replace(
'{{semanticPrefix}}',
Expand Down
62 changes: 62 additions & 0 deletions lib/config/migrations/custom/schedule-migration.spec.ts
@@ -0,0 +1,62 @@
import { ScheduleMigration } from './schedule-migration';

describe('config/migrations/custom/schedule-migration', () => {
it('migrates every friday', () => {
expect(ScheduleMigration).toMigrate(
{
schedule: 'every friday',
} as any,
{
schedule: 'on friday',
} as any
);
});

it('does not migrate every weekday', () => {
expect(ScheduleMigration).toMigrate(
{
schedule: 'every weekday',
} as any,
{
schedule: 'every weekday',
} as any,
false
);
});

it('does not migrate multi days', () => {
expect(ScheduleMigration).toMigrate(
{
schedule: 'after 5:00pm on wednesday and thursday',
} as any,
{
schedule: 'after 5:00pm on wednesday and thursday',
} as any,
false
);
});

it('does not migrate hour range', () => {
expect(ScheduleMigration).toMigrate(
{
schedule: 'after 1:00pm and before 5:00pm',
} as any,
{
schedule: 'after 1:00pm and before 5:00pm',
} as any,
false
);
});

it('does not migrate invalid range', () => {
expect(ScheduleMigration).toMigrate(
{
schedule: 'after and before 5:00',
} as any,
{
schedule: 'after and before 5:00',
} as any,
false
);
});
});
96 changes: 96 additions & 0 deletions lib/config/migrations/custom/schedule-migration.ts
@@ -0,0 +1,96 @@
import later from '@breejs/later';
import is from '@sindresorhus/is';
import { regEx } from '../../../util/regex';
import { AbstractMigration } from '../base/abstract-migration';

export class ScheduleMigration extends AbstractMigration {
override readonly propertyName = 'schedule';

override run(value: unknown): void {
if (value) {
// massage to array first
let schedules: string[] = [];
if (is.string(value)) {
schedules = [value];
}
if (Array.isArray(value)) {
schedules = [...value];
}
// split 'and'
const schedulesLength = schedules.length;
for (let i = 0; i < schedulesLength; i += 1) {
if (
schedules[i].includes(' and ') &&
schedules[i].includes('before ') &&
schedules[i].includes('after ')
) {
const parsedSchedule = later.parse.text(
// We need to massage short hours first before we can parse it
schedules[i].replace(regEx(/( \d?\d)((a|p)m)/g), '$1:00$2') // TODO #12071
).schedules[0];
// Only migrate if the after time is greater than before, e.g. "after 10pm and before 5am"
if (!parsedSchedule || !parsedSchedule.t_a || !parsedSchedule.t_b) {
continue;
}

if (parsedSchedule.t_a[0] > parsedSchedule.t_b[0]) {
const toSplit = schedules[i];
schedules[i] = toSplit
.replace(
regEx(
/^(.*?)(after|before) (.*?) and (after|before) (.*?)( |$)(.*)/
), // TODO #12071
'$1$2 $3 $7'
)
.trim();
schedules.push(
toSplit
.replace(
regEx(
/^(.*?)(after|before) (.*?) and (after|before) (.*?)( |$)(.*)/
), // TODO #12071
'$1$4 $5 $7'
)
.trim()
);
}
}
}
for (let i = 0; i < schedules.length; i += 1) {
if (schedules[i].includes('on the last day of the month')) {
schedules[i] = schedules[i].replace(
'on the last day of the month',
'on the first day of the month'
);
}
if (schedules[i].includes('on every weekday')) {
schedules[i] = schedules[i].replace(
'on every weekday',
'every weekday'
);
}
if (schedules[i].endsWith(' every day')) {
schedules[i] = schedules[i].replace(' every day', '');
}
if (
regEx(/every (mon|tues|wednes|thurs|fri|satur|sun)day$/).test(
schedules[i]
) // TODO #12071
) {
schedules[i] = schedules[i].replace(
regEx(/every ([a-z]*day)$/), // TODO #12071
'on $1'
);
}
if (schedules[i].endsWith('days')) {
schedules[i] = schedules[i].replace('days', 'day');
}
}
if (is.string(value) && schedules.length === 1) {
this.rewrite(schedules[0]);
} else {
this.rewrite(schedules);
}
}
}
}
2 changes: 2 additions & 0 deletions lib/config/migrations/migrations-service.ts
Expand Up @@ -27,6 +27,7 @@ import { RebaseConflictedPrs } from './custom/rebase-conflicted-prs-migration';
import { RebaseStalePrsMigration } from './custom/rebase-stale-prs-migration';
import { RenovateForkMigration } from './custom/renovate-fork-migration';
import { RequiredStatusChecksMigration } from './custom/required-status-checks-migration';
import { ScheduleMigration } from './custom/schedule-migration';
import { SemanticCommitsMigration } from './custom/semantic-commits-migration';
import { SuppressNotificationsMigration } from './custom/suppress-notifications-migration';
import { TrustLevelMigration } from './custom/trust-level-migration';
Expand Down Expand Up @@ -90,6 +91,7 @@ export class MigrationsService {
RebaseStalePrsMigration,
RenovateForkMigration,
RequiredStatusChecksMigration,
ScheduleMigration,
SemanticCommitsMigration,
SuppressNotificationsMigration,
TrustLevelMigration,
Expand Down

0 comments on commit 25d793f

Please sign in to comment.