Skip to content

Commit 3a8313d

Browse files
authored
Support workflow_dispatch event type (#31)
1 parent bdea69b commit 3a8313d

File tree

6 files changed

+87
-9
lines changed

6 files changed

+87
-9
lines changed

dist/index.js

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/message.test.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,52 @@ describe('postMessage', () => {
167167
})
168168
})
169169

170+
describe('first summary workflow_dispatch event', () => {
171+
beforeEach(async () => {
172+
github.context.eventName = 'workflow_dispatch'
173+
github.context.sha = '05b16c3beb3a07dceaf6cf964d0be9eccbc026e8'
174+
175+
octokit.rest.repos.getCommit = jest.fn(async () => ({
176+
data: {
177+
commit: {
178+
message: 'COMMIT-MESSAGE',
179+
url: 'github.com/commit'
180+
}
181+
}
182+
})) as any
183+
184+
ts = await postMessage(octokit, slack)
185+
})
186+
187+
it('should fetch commit', () => {
188+
expect(octokit.rest.repos.getCommit).toHaveBeenCalledWith({
189+
owner: 'namoscato',
190+
repo: 'action-testing',
191+
ref: '05b16c3beb3a07dceaf6cf964d0be9eccbc026e8'
192+
})
193+
})
194+
195+
it('should post slack message', () => {
196+
expect(slack.postMessage).toHaveBeenCalledWith(
197+
expect.objectContaining({
198+
text: 'Deploying action-testing: COMMIT-MESSAGE',
199+
blocks: expect.arrayContaining([
200+
{
201+
type: 'context',
202+
elements: expect.arrayContaining([
203+
{
204+
type: 'image',
205+
alt_text: 'workflow_dispatch event',
206+
image_url: EVENT_NAME_IMAGE_MAP['workflow_dispatch']
207+
}
208+
])
209+
}
210+
])
211+
})
212+
)
213+
})
214+
})
215+
170216
describe('stage push', () => {
171217
beforeEach(async () => {
172218
process.env.INPUT_THREAD_TS = '1662768000' // 2022-09-10T00:00:00.000Z
@@ -513,7 +559,7 @@ describe('postMessage', () => {
513559

514560
it('should throw error', () => {
515561
expect(error.message).toBe(
516-
'Unsupported "issues" event (currently supported events include: pull_request, push, schedule)'
562+
'Unsupported "issues" event (currently supported events include: pull_request, push, schedule, workflow_dispatch)'
517563
)
518564
})
519565
})

src/github/context.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export const EVENT_NAME_IMAGE_MAP: Record<SupportedEventName, string> = {
1515
'https://user-images.githubusercontent.com/847532/193414326-5aaf5449-0c81-4a66-9b19-4e5e6baeee9e.png',
1616
push: 'https://user-images.githubusercontent.com/847532/193413878-d5fcd559-401d-4954-a44c-36de5d6a7adf.png',
1717
schedule:
18-
'https://user-images.githubusercontent.com/847532/193414289-3b185a3b-aee8-40f9-99fe-0615d255c8dd.png'
18+
'https://user-images.githubusercontent.com/847532/193414289-3b185a3b-aee8-40f9-99fe-0615d255c8dd.png',
19+
workflow_dispatch:
20+
'https://user-images.githubusercontent.com/847532/197601879-3bc8bf73-87c0-4216-8de7-c55d34993ef1.png'
1921
} as const
2022

2123
export function getContextBlock(duration?: Duration): ContextBlock {

src/github/summary.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
isPullRequestEvent,
1212
isPushEvent,
1313
isScheduleEvent,
14+
isWorkflowDispatchEvent,
1415
SupportedContext
1516
} from './webhook'
1617

@@ -105,7 +106,7 @@ async function getEventLink(octokit: OctokitClient): Promise<Link> {
105106
}
106107
}
107108

108-
if (isScheduleEvent(context)) {
109+
if (isScheduleEvent(context) || isWorkflowDispatchEvent(context)) {
109110
const commit = (
110111
await octokit.rest.repos.getCommit({
111112
...context.repo,

src/github/webhook.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import type {PullRequestEvent, PushEvent, User} from '@octokit/webhooks-types'
55
export const SUPPORTED_EVENT_NAMES = [
66
'pull_request',
77
'push',
8-
'schedule'
8+
'schedule',
9+
'workflow_dispatch'
910
] as const
1011

1112
export type SupportedEventName = typeof SUPPORTED_EVENT_NAMES[number]
@@ -22,16 +23,26 @@ export interface ScheduleEvent {
2223
schedule: string
2324
}
2425

26+
export interface WorkflowDispatchEvent {
27+
workflow: string
28+
}
29+
2530
export type PullRequestContext = Context<'pull_request', PullRequestEvent>
2631

2732
export type PushContext = Context<'push', PushEvent>
2833

2934
export type ScheduleContext = Context<'schedule', ScheduleEvent>
3035

36+
export type WorkflowDispatchContext = Context<
37+
'workflow_dispatch',
38+
WorkflowDispatchEvent
39+
>
40+
3141
export type SupportedContext =
3242
| PullRequestContext
3343
| PushContext
3444
| ScheduleContext
45+
| WorkflowDispatchContext
3546

3647
/**
3748
* @see https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request
@@ -58,6 +69,15 @@ export function isScheduleEvent(
5869
return 'schedule' === context.eventName
5970
}
6071

72+
/**
73+
* @see https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_dispatch
74+
*/
75+
export function isWorkflowDispatchEvent(
76+
context: GitHubContext
77+
): context is WorkflowDispatchContext {
78+
return 'workflow_dispatch' === context.eventName
79+
}
80+
6181
export function isSupportedEvent(
6282
context: GitHubContext
6383
): context is SupportedContext {

0 commit comments

Comments
 (0)