Skip to content

Commit

Permalink
refactor: replace valibot schemas with parser functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mogyuchi committed Sep 22, 2024
1 parent 354c9db commit 8dc6edb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
9 changes: 9 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { nullable, optional, parser, string } from 'valibot'

const StringSchema = string()
const OptionalStringSchema = optional(string())
const NullableStringSchema = nullable(string())

export const stringParser = parser(StringSchema)
export const optionalStringParser = parser(OptionalStringSchema)
export const nullableStringParser = parser(NullableStringSchema)
46 changes: 19 additions & 27 deletions src/ref.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as v from 'valibot'

Check failure on line 1 in src/ref.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

'v' is defined but never used
import { WebhookPayload } from '@actions/github/lib/interfaces'
import {
OptionalStringSchema,
NullableStringSchema,
StringSchema
} from './schema'
optionalStringParser,
nullableStringParser,
stringParser
} from './parser'
import { convertRef } from './internal/utils'

export const getRef = ({
Expand All @@ -17,38 +17,31 @@ export const getRef = ({
switch (eventName) {
case 'check_run':
return convertRef(
v.parse(
NullableStringSchema,
payload.check_run.check_suite.head_branch
),
nullableStringParser(payload.check_run.check_suite.head_branch),
{ refType: 'branch' }
)
case 'check_suite':
return convertRef(
v.parse(NullableStringSchema, payload.check_suite.head_branch),
{ refType: 'branch' }
)
return convertRef(nullableStringParser(payload.check_suite.head_branch), {
refType: 'branch'
})
case 'create':
case 'delete':
return convertRef(v.parse(NullableStringSchema, payload.ref), {
return convertRef(nullableStringParser(payload.ref), {
refType: payload.ref_type
})
case 'deployment_status':
return convertRef(
v.parse(OptionalStringSchema, payload.workflow_run?.head_branch),
optionalStringParser(payload.workflow_run?.head_branch),
{
refType: 'branch'
}
)
case 'issue_comment':
return convertRef(
v.parse(StringSchema, payload.issue?.number.toString()),
{
refType: 'pull'
}
)
return convertRef(stringParser(payload.issue?.number.toString()), {
refType: 'pull'
})
case 'merge_group':
return v.parse(StringSchema, payload.merge_group.head_ref)
return stringParser(payload.merge_group.head_ref)
case 'pull_request':
case 'pull_request_review':
case 'pull_request_review_comment':
Expand All @@ -57,26 +50,25 @@ export const getRef = ({
refType: 'pull'
})
case 'push':
return v.parse(StringSchema, payload.ref)
return stringParser(payload.ref)
case 'registry_package':
return convertRef(
v.parse(
OptionalStringSchema,
optionalStringParser(
payload.registry_package?.package_version?.release?.tag_name
),
{
refType: 'tag'
}
)
case 'release':
return convertRef(v.parse(StringSchema, payload.release.tag_name), {
return convertRef(stringParser(payload.release.tag_name), {
refType: 'tag'
})
case 'workflow_dispatch':
return v.parse(StringSchema, payload.ref)
return stringParser(payload.ref)
case 'workflow_run':
return convertRef(
v.parse(NullableStringSchema, payload.workflow_run.head_branch),
nullableStringParser(payload.workflow_run.head_branch),
{
refType: 'branch'
}
Expand Down
5 changes: 0 additions & 5 deletions src/schema.ts

This file was deleted.

0 comments on commit 8dc6edb

Please sign in to comment.