Skip to content

Commit

Permalink
refactor: refactor (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mogyuchi authored Jan 23, 2024
1 parent cdef7f5 commit fc61df0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 45 deletions.
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
}

0 comments on commit fc61df0

Please sign in to comment.