Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace valibot schemas with parser functions #238

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 33 additions & 52 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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)
47 changes: 19 additions & 28 deletions src/ref.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as v from 'valibot'
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 +16,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 +49,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.

Loading