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: refactor #26

Merged
merged 1 commit into from
Jan 23, 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
24 changes: 6 additions & 18 deletions dist/index.js

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

12 changes: 5 additions & 7 deletions src/internal/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export const convertRef = ((str, { refType }) => {
export const convertRef = (
str: string | undefined | null,
{ refType }: { refType: 'branch' | 'tag' | 'pull' }
): string | null => {
if (str === null || str === undefined) return null
if (str.startsWith('refs/')) return str
switch (refType) {
Expand All @@ -8,10 +11,5 @@ export const convertRef = ((str, { refType }) => {
return `refs/tags/${str}`
case 'pull':
return `refs/pull/${str}/merge`
default:
return null
}
}) satisfies (
str: string | undefined | null,
{ refType }: { refType: 'branch' | 'tag' | 'pull' }
) => string | null
}
28 changes: 8 additions & 20 deletions src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import {
} from './schema'
import { convertRef } from './internal/utils'

export const getRef = (({ eventName, payload }) => {
export const getRef = ({
eventName,
payload
}: {
eventName: string
payload: WebhookPayload
}): string | null => {
switch (eventName) {
case 'check_run':
return convertRef(
Expand All @@ -23,9 +29,6 @@ export const getRef = (({ eventName, payload }) => {
{ refType: 'branch' }
)
case 'create':
return convertRef(v.parse(NullableStringSchema, payload.ref), {
refType: payload.ref_type
})
case 'delete':
return convertRef(v.parse(NullableStringSchema, payload.ref), {
refType: payload.ref_type
Expand All @@ -45,17 +48,8 @@ export const getRef = (({ eventName, payload }) => {
}
)
case 'pull_request':
return convertRef(payload.pull_request?.number.toString(), {
refType: 'pull'
})
case 'pull_request_review':
return convertRef(payload.pull_request?.number.toString(), {
refType: 'pull'
})
case 'pull_request_review_comment':
return convertRef(payload.pull_request?.number.toString(), {
refType: 'pull'
})
case 'pull_request_target':
return convertRef(payload.pull_request?.number.toString(), {
refType: 'pull'
Expand Down Expand Up @@ -88,10 +82,4 @@ export const getRef = (({ eventName, payload }) => {
default:
throw new Error(`${eventName} event is not supported.`)
}
}) satisfies ({
eventName,
payload
}: {
eventName: string
payload: WebhookPayload
}) => string | null
}
Loading