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

feat: 様々なイベントに対応 #21

Merged
merged 1 commit into from
Jan 22, 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
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.
176 changes: 150 additions & 26 deletions dist/index.js

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

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 }: { refType: 'branch' | 'tag' | 'pull' }
) => 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.'
)
}
}
}
Loading
Loading