From e66e1df86e3188f3473472a3298aa1b24e27ae63 Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Sat, 8 Jun 2024 01:23:58 +0900 Subject: [PATCH] Make strict typing --- dist/index.js | 44 +++++++++++++++++++++++++++----------------- src/report.test.ts | 10 +++++----- src/schema.test.ts | 10 +++++----- src/schema.ts | 42 +++++++++++++++++++++++++++--------------- 4 files changed, 64 insertions(+), 42 deletions(-) diff --git a/dist/index.js b/dist/index.js index dc4b2c33..30d67294 100644 --- a/dist/index.js +++ b/dist/index.js @@ -31063,23 +31063,33 @@ function getDuration(durationable) { } throw new Error("unexpected value is specified in durations"); } -var FilterCondition = z2.object({ - workflowFile: z2.string().endsWith(".yml"), - jobName: z2.string().min(1).optional(), +var workflowFile = z2.string().endsWith(".yml"); +var matchAllJobs = z2.object({ + workflowFile, + jobName: z2.undefined(), + // Preferring undefined over null for backward compatibility + jobMatchMode: z2.literal("all").default("all") +}).strict(); +var matchPartialJobs = z2.object({ + workflowFile, + jobName: z2.string().min(1), jobMatchMode: z2.enum(["exact", "prefix"]).default("exact") -}); +}).strict(); +var FilterCondition = z2.union([matchAllJobs, matchPartialJobs]); var SkipFilterCondition = FilterCondition.readonly(); -var WaitFilterCondition = FilterCondition.extend( - { - optional: z2.boolean().default(false).readonly(), - // - Intentionally avoided to use enum for now. Only GitHub knows whole eventNames and the adding plans - // - Intentionally omitted in skip-list, let me know if you have the use-case - eventName: z2.string().min(1).optional(), - // Do not raise validation errors for the reasonability of max value. - // Even in equal_intervals mode, we can't enforce the possibility of the whole running time - startupGracePeriod: Durationable.default(defaultGrace) - } -).readonly(); +var waitOptions = { + optional: z2.boolean().default(false).readonly(), + // - Intentionally avoided to use enum for now. Only GitHub knows whole eventNames and the adding plans + // - Intentionally omitted in skip-list, let me know if you have the use-case + eventName: z2.string().min(1).optional(), + // Do not raise validation errors for the reasonability of max value. + // Even in equal_intervals mode, we can't enforce the possibility of the whole running time + startupGracePeriod: Durationable.default(defaultGrace) +}; +var WaitFilterCondition = z2.union([ + matchAllJobs.extend(waitOptions).strict(), + matchPartialJobs.extend(waitOptions).strict() +]).readonly(); var WaitList = z2.array(WaitFilterCondition).readonly(); var SkipList = z2.array(SkipFilterCondition).readonly(); var retryMethods = z2.enum(["exponential_backoff", "equal_intervals"]); @@ -32409,8 +32419,8 @@ function getSummaries(checks, trigger) { (a2, b2) => join2(a2.workflowBasename, a2.jobName).localeCompare(join2(b2.workflowBasename, b2.jobName)) ); } -function matchPath({ workflowFile, jobName, jobMatchMode }, summary) { - if (workflowFile !== summary.workflowBasename) { +function matchPath({ workflowFile: workflowFile2, jobName, jobMatchMode }, summary) { + if (workflowFile2 !== summary.workflowBasename) { return false; } if (!jobName) { diff --git a/src/report.test.ts b/src/report.test.ts index c99b8f4e..98eb787b 100644 --- a/src/report.test.ts +++ b/src/report.test.ts @@ -54,7 +54,7 @@ test('wait-list', async (t) => { waitList: [ { 'workflowFile': 'lint.yml', - jobMatchMode: 'exact', + jobMatchMode: 'all', 'optional': false, 'eventName': 'pull_request', startupGracePeriod: Temporal.Duration.from({ seconds: 10 }), @@ -68,7 +68,7 @@ test('wait-list', async (t) => { }, { 'workflowFile': 'THERE_ARE_NO_FILES_AS_THIS.yml', - jobMatchMode: 'exact', + jobMatchMode: 'all', 'optional': true, startupGracePeriod: Temporal.Duration.from({ seconds: 10 }), }, @@ -513,15 +513,15 @@ test('skip-list', async (t) => { skipList: [ { 'workflowFile': 'itself.yml', - jobMatchMode: 'exact', + jobMatchMode: 'all', }, { 'workflowFile': 'ci.yml', - jobMatchMode: 'exact', + jobMatchMode: 'all', }, { 'workflowFile': 'ci-nix.yml', - jobMatchMode: 'exact', + jobMatchMode: 'all', }, { 'workflowFile': 'merge-bot-pr.yml', diff --git a/src/schema.test.ts b/src/schema.test.ts index aee0ebea..8c709641 100644 --- a/src/schema.test.ts +++ b/src/schema.test.ts @@ -37,7 +37,7 @@ test('Options set some default values it cannot be defined in action.yml', () => ...defaultOptions, waitList: [{ workflowFile: 'ci.yml', - jobMatchMode: 'exact', + jobMatchMode: 'all', optional: false, startupGracePeriod: Temporal.Duration.from({ seconds: 10 }), }], @@ -123,7 +123,7 @@ test('wait-list have startupGracePeriod', async (t) => { ...defaultOptions, waitList: [{ workflowFile: 'ci.yml', - jobMatchMode: 'exact', + jobMatchMode: 'all', optional: false, startupGracePeriod: Temporal.Duration.from({ minutes: 5 }), }], @@ -172,7 +172,7 @@ test('wait-list have startupGracePeriod', async (t) => { ...defaultOptions, waitList: [{ workflowFile: 'ci.yml', - jobMatchMode: 'exact', + jobMatchMode: 'all', optional: false, startupGracePeriod: Temporal.Duration.from({ minutes: 1, seconds: 42 }), }], @@ -207,7 +207,7 @@ test('wait-list have startupGracePeriod', async (t) => { initialDuration: Temporal.Duration.from({ seconds: 42 }), waitList: [{ workflowFile: 'ci.yml', - jobMatchMode: 'exact', + jobMatchMode: 'all', optional: false, startupGracePeriod: Temporal.Duration.from({ seconds: 10 }), }], @@ -225,7 +225,7 @@ test('wait-list have startupGracePeriod', async (t) => { initialDuration: Temporal.Duration.from({ seconds: 42 }), waitList: [{ workflowFile: 'ci.yml', - jobMatchMode: 'exact', + jobMatchMode: 'all', optional: false, startupGracePeriod: Temporal.Duration.from({ seconds: 10 }), }], diff --git a/src/schema.ts b/src/schema.ts index 6da52a6c..09bc2f33 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -66,25 +66,37 @@ export function getDuration(durationable: string | MyDurationLike): Duration { throw new Error('unexpected value is specified in durations'); } -const FilterCondition = z.object({ - workflowFile: z.string().endsWith('.yml'), - jobName: (z.string().min(1)).optional(), +const workflowFile = z.string().endsWith('.yml'); +const matchAllJobs = z.object({ + workflowFile: workflowFile, + jobName: z.undefined(), // Preferring undefined over null for backward compatibility + jobMatchMode: z.literal('all').default('all'), +}).strict(); +const matchPartialJobs = z.object({ + workflowFile: workflowFile, + jobName: z.string().min(1), jobMatchMode: z.enum(['exact', 'prefix']).default('exact'), -}); +}).strict(); + +const FilterCondition = z.union([matchAllJobs, matchPartialJobs]); const SkipFilterCondition = FilterCondition.readonly(); -const WaitFilterCondition = FilterCondition.extend( - { - optional: z.boolean().default(false).readonly(), - // - Intentionally avoided to use enum for now. Only GitHub knows whole eventNames and the adding plans - // - Intentionally omitted in skip-list, let me know if you have the use-case - eventName: z.string().min(1).optional(), +const waitOptions = { + optional: z.boolean().default(false).readonly(), - // Do not raise validation errors for the reasonability of max value. - // Even in equal_intervals mode, we can't enforce the possibility of the whole running time - startupGracePeriod: Durationable.default(defaultGrace), - }, -).readonly(); + // - Intentionally avoided to use enum for now. Only GitHub knows whole eventNames and the adding plans + // - Intentionally omitted in skip-list, let me know if you have the use-case + eventName: z.string().min(1).optional(), + + // Do not raise validation errors for the reasonability of max value. + // Even in equal_intervals mode, we can't enforce the possibility of the whole running time + startupGracePeriod: Durationable.default(defaultGrace), +}; + +const WaitFilterCondition = z.union([ + matchAllJobs.extend(waitOptions).strict(), + matchPartialJobs.extend(waitOptions).strict(), +]).readonly(); const WaitList = z.array(WaitFilterCondition).readonly(); const SkipList = z.array(SkipFilterCondition).readonly(); export type FilterCondition = z.infer;