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: use @octokit/webhooks-types for stronger typing #253

Merged
merged 1 commit into from
Sep 28, 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
5,662 changes: 17 additions & 5,645 deletions dist/index.js

Large diffs are not rendered by default.

13 changes: 0 additions & 13 deletions dist/licenses.txt

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@
},
"dependencies": {
"@actions/core": "1.10.1",
"@actions/github": "6.0.0",
"valibot": "0.42.1"
"@actions/github": "6.0.0"
},
"devDependencies": {
"@eslint/js": "9.11.1",
"@octokit/openapi-types": "22.2.0",
"@octokit/webhooks-types": "7.5.1",
"@types/eslint-config-prettier": "6.11.3",
"@types/eslint__js": "8.42.3",
"@types/jest": "29.5.13",
Expand Down
23 changes: 8 additions & 15 deletions pnpm-lock.yaml

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

9 changes: 4 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getRef } from './ref.js'
import type * as types from '@octokit/openapi-types'
import { getInputs } from './get-inputs.js'
import { convertRef } from './internal/utils.js'
import type { StrictContext } from './types.js'

type Cache =
types.components['schemas']['actions-cache-list']['actions_caches'][number]
Expand Down Expand Up @@ -56,7 +57,7 @@ export async function run(): Promise<void> {
const octokit = github.getOctokit(token)

// get repostiory information
const { repo, eventName, payload } = github.context
const context = github.context as StrictContext

const infoNull = (name: string): never[] => {
core.info(`🤔 Could not determine deletion target: ${name}`)
Expand All @@ -68,15 +69,13 @@ export async function run(): Promise<void> {

const refs: string[] =
branchNames.length === 0
? [getRef({ eventName, payload })].flatMap(x =>
x ? x : infoNull(eventName)
)
? [getRef(context)].flatMap(x => (x ? x : infoNull(context.eventName)))
: branchNames
.map(branchName => convertRef(branchName, { refType: 'branch' }))
.filter(ref => ref !== null)

for (const ref of refs) {
await deleteRefActionsCaches(octokit, repo, ref, isDryRun)
await deleteRefActionsCaches(octokit, context.repo, ref, isDryRun)
}

core.info(`${prefix({ isDryRun })}✅ Done`)
Expand Down
9 changes: 0 additions & 9 deletions src/parser.ts

This file was deleted.

57 changes: 20 additions & 37 deletions src/ref.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,60 @@
import { WebhookPayload } from '@actions/github/lib/interfaces.js'
import {
optionalStringParser,
nullableStringParser,
stringParser
} from './parser.js'
import { convertRef } from './internal/utils.js'
import type { StrictContext } from './types.js'

export const getRef = ({
eventName,
payload
}: {
eventName: string
payload: WebhookPayload
}): string | null => {
}: StrictContext): string | null => {
switch (eventName) {
case 'check_run':
return convertRef(
nullableStringParser(payload.check_run.check_suite.head_branch),
{ refType: 'branch' }
)
return convertRef(payload.check_run.check_suite.head_branch, {
refType: 'branch'
})
case 'check_suite':
return convertRef(nullableStringParser(payload.check_suite.head_branch), {
return convertRef(payload.check_suite.head_branch, {
refType: 'branch'
})
case 'create':
case 'delete':
return convertRef(nullableStringParser(payload.ref), {
return convertRef(payload.ref, {
refType: payload.ref_type
})
case 'deployment_status':
return convertRef(
optionalStringParser(payload.workflow_run?.head_branch),
{
refType: 'branch'
}
)
return convertRef(payload.workflow_run?.head_branch, {
refType: 'branch'
})
case 'issue_comment':
return convertRef(stringParser(payload.issue?.number.toString()), {
return convertRef(payload.issue.number.toString(), {
refType: 'pull'
})
case 'merge_group':
return stringParser(payload.merge_group.head_ref)
return payload.merge_group.head_ref
case 'pull_request':
case 'pull_request_review':
case 'pull_request_review_comment':
case 'pull_request_target':
return convertRef(payload.pull_request?.number.toString(), {
return convertRef(payload.pull_request.number.toString(), {
refType: 'pull'
})
case 'push':
return stringParser(payload.ref)
return payload.ref
case 'registry_package':
return convertRef(
optionalStringParser(
payload.registry_package?.package_version?.release?.tag_name
),
payload.registry_package.package_version?.release?.tag_name,
{
refType: 'tag'
}
)
case 'release':
return convertRef(stringParser(payload.release.tag_name), {
return convertRef(payload.release.tag_name, {
refType: 'tag'
})
case 'workflow_dispatch':
return stringParser(payload.ref)
return payload.ref
case 'workflow_run':
return convertRef(
nullableStringParser(payload.workflow_run.head_branch),
{
refType: 'branch'
}
)
return convertRef(payload.workflow_run.head_branch, {
refType: 'branch'
})
default:
throw new Error(`${eventName} event is not supported.`)
}
Expand Down
33 changes: 33 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { Context } from '@actions/github/lib/context.js'
import type { EventPayloadMap } from '@octokit/webhooks-types'

type WorkflowEventPayloadMap = EventPayloadMap & {
// https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#pull_request_target
pull_request_target: EventPayloadMap['pull_request']
}

type PayloadAndEventName<TName extends keyof WorkflowEventPayloadMap> = Omit<
Context,
'payload' | 'eventName'
> & {
payload: WorkflowEventPayloadMap[TName]
eventName: TName
}

export type StrictContext =
| PayloadAndEventName<'check_run'>
| PayloadAndEventName<'check_suite'>
| PayloadAndEventName<'create'>
| PayloadAndEventName<'delete'>
| PayloadAndEventName<'deployment_status'>
| PayloadAndEventName<'issue_comment'>
| PayloadAndEventName<'merge_group'>
| PayloadAndEventName<'pull_request'>
| PayloadAndEventName<'pull_request_review'>
| PayloadAndEventName<'pull_request_review_comment'>
| PayloadAndEventName<'pull_request_target'>
| PayloadAndEventName<'push'>
| PayloadAndEventName<'registry_package'>
| PayloadAndEventName<'release'>
| PayloadAndEventName<'workflow_dispatch'>
| PayloadAndEventName<'workflow_run'>
Loading