Skip to content

Commit

Permalink
feat: 様々なイベントに対応
Browse files Browse the repository at this point in the history
  • Loading branch information
Mogyuchi and hulk510 committed Jan 22, 2024
1 parent a00555c commit 5cd782e
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 35 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
[![Check dist/](https://github.com/actions/typescript-action/actions/workflows/check-dist.yml/badge.svg)](https://github.com/actions/typescript-action/actions/workflows/check-dist.yml)
[![CodeQL](https://github.com/actions/typescript-action/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/actions/typescript-action/actions/workflows/codeql-analysis.yml)

Souji Action deletes all GitHub Actions Caches created for branches related to
the context of the triggered workflow event.
Souji Action is a GitHub Action that deletes all GitHub Actions Caches related
to the context of the triggered workflow event, without any configuration
required.

## Usage

Expand All @@ -32,5 +33,4 @@ jobs:
For instance, when a Pull Request created in the branch `feat/awesome-feature`
is "merged" or "closed," a workflow event is triggered and the workflow is
executed. At this time, all GitHub Actions Caches created under the merge ref
`refs/pull/{pull_request_number}/merge` and the head ref
`refs/heads/feat/awesome-feature` are deleted.
`refs/pull/{pull_request_number}/merge` are deleted.
17 changes: 17 additions & 0 deletions src/internal/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const convertRef = ((str, { refType }) => {
if (str === null || str === undefined) return null
if (str.startsWith('refs/')) return str
switch (refType) {
case 'branch':
return `refs/heads/${str}`
case 'tag':
return `refs/tags/${str}`
case 'pull':
return `refs/pull/${str}/merge`
default:
return null
}
}) satisfies (
str: string | undefined | null,
{}: { refType: 'branch' | 'tag' | 'pull' }

Check failure on line 16 in src/internal/utils.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Unexpected empty object pattern

Check failure on line 16 in src/internal/utils.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Unexpected empty object pattern
) => string | null
48 changes: 18 additions & 30 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import * as v from 'valibot'

const OptionalStringSchema = v.optional(v.string())
import { getRef } from './ref'

const deleteRefActionsCache = async (
octokit: ReturnType<typeof github.getOctokit>,
Expand Down Expand Up @@ -41,37 +39,27 @@ export async function run(): Promise<void> {
const octokit = github.getOctokit(token)

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

// MEMO: payloadから取得できるのは確認したけど、型何もついてない
const payload = github.context.payload
const prNumber = payload.pull_request?.number
const headRef = v.parse(
OptionalStringSchema,
payload.pull_request?.head?.ref
)
const ref = v.parse(OptionalStringSchema, payload.ref)
const ref = getRef({ eventName, payload })

if (prNumber) {
// fire when event is pull_request or pull_request_target or pull_request_review or pull_request_review_comment
core.info(`delete cache for refs/pull/${prNumber}/merge`)
await deleteRefActionsCache(octokit, repo, `refs/pull/${prNumber}/merge`)
core.info('done ✅')
}
if (headRef) {
// fire when event is pull_request or pull_request_target or pull_request_review or pull_request_review_comment
core.info(`delete cache for refs/heads/${headRef}`)
await deleteRefActionsCache(octokit, repo, `refs/heads/${headRef}`)
core.info('done ✅')
}
if (ref) {
// fire when event is workflow_dispatch or push
core.info(`delete cache for ${ref}`)
await deleteRefActionsCache(octokit, repo, ref)
core.info('done ✅')
if (ref === null) {
core.info('Could not determine deletion target.')
core.info(
'If you suspect this is a bug, please consider raising an issue to help us address it promptly.'
)
return
}
core.info(`Delete cache for ${ref}`)
await deleteRefActionsCache(octokit, repo, ref)
core.info('Done ✅')
} catch (error) {
// Fail the workflow run if an error occurs
if (error instanceof Error) core.setFailed(error.message)
if (error instanceof Error) {
core.setFailed(error.message)
core.info(
'If you suspect this is a bug, please consider raising an issue to help us address it promptly.'
)
}
}
}
94 changes: 94 additions & 0 deletions src/ref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import * as v from 'valibot'
import { WebhookPayload } from '@actions/github/lib/interfaces'
import {
OptionalStringSchema,
NullableStringSchema,
StringSchema
} from './schema'
import { convertRef } from './internal/utils'

export const getRef = (({ eventName, payload }) => {
switch (eventName) {
case 'check_run':
return convertRef(
v.parse(
NullableStringSchema,
payload.check_run.check_suite.head_branch
),
{ refType: 'branch' }
)
case 'check_suite':
return convertRef(
v.parse(NullableStringSchema, payload.check_suite.head_branch),
{ 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
})
case 'deployment_status':
return convertRef(
v.parse(OptionalStringSchema, payload.workflow_run?.head_branch),
{
refType: 'branch'
}
)
case 'issue_comment':
return convertRef(
v.parse(StringSchema, payload.issue?.number.toString()),
{
refType: 'pull'
}
)
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'
})
case 'push':
return v.parse(StringSchema, payload.ref)
case 'registry_package':
return convertRef(
v.parse(
OptionalStringSchema,
payload.registry_package?.package_version?.release?.tag_name
),
{
refType: 'tag'
}
)
case 'release':
return convertRef(v.parse(StringSchema, payload.release.tag_name), {
refType: 'tag'
})
case 'workflow_dispatch':
return v.parse(StringSchema, payload.ref)
case 'workflow_run':
return convertRef(
v.parse(NullableStringSchema, payload.workflow_run.head_branch),
{
refType: 'branch'
}
)
default:
throw new Error(`${eventName} event is not supported.`)
}
}) satisfies ({}: {

Check failure on line 91 in src/ref.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Unexpected empty object pattern

Check failure on line 91 in src/ref.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Unexpected empty object pattern
eventName: string
payload: WebhookPayload
}) => string | null
5 changes: 5 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { nullable, optional, string } from 'valibot'

export const StringSchema = string()
export const OptionalStringSchema = optional(string())
export const NullableStringSchema = nullable(string())
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"newLine": "lf"
"newLine": "lf",
"noUncheckedIndexedAccess": true
},
"exclude": ["./dist", "./node_modules", "./__tests__", "./coverage"]
}

0 comments on commit 5cd782e

Please sign in to comment.