Skip to content
Open
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
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20.6.0
20.19.6
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ There was a need for this action as currently available actions...

```yaml
steps:
- uses: step-security/dispatch-workflow@v1
- uses: step-security/dispatch-workflow@v2
id: workflow-dispatch
name: 'Dispatch Workflow using workflow_dispatch Method'
with:
Expand All @@ -39,7 +39,7 @@ steps:

```yaml
steps:
- uses: step-security/dispatch-workflow@v1
- uses: step-security/dispatch-workflow@v2
id: repository-dispatch
name: 'Dispatch Workflow using repository_dispatch Method'
with:
Expand Down Expand Up @@ -120,7 +120,7 @@ This functionality is **disabled by default**, but can be enabled with the `disc

```yaml
steps:
- uses: step-security/dispatch-workflow@v1
- uses: step-security/dispatch-workflow@v2
id: dispatch-with-discovery
name: "Dispatch Workflow With Discovery"
with:
Expand Down Expand Up @@ -224,7 +224,7 @@ By default, this GitHub Action has no outputs. However, when discovery mode is *

```yaml
steps:
- uses: step-security/dispatch-workflow@v1
- uses: step-security/dispatch-workflow@v2
id: wait-repository-dispatch
name: 'Dispatch Using repository_dispatch Method And Wait For Run-ID'
with:
Expand Down Expand Up @@ -286,7 +286,7 @@ types must be wrapped in **quotes** to successfully dispatch the workflow.

```yaml
# Invalid ❌
- uses: step-security/dispatch-workflow@v1
- uses: step-security/dispatch-workflow@v2
id: workflow-dispatch
name: 'Dispatch Using workflow_dispatch Method'
with:
Expand All @@ -299,7 +299,7 @@ types must be wrapped in **quotes** to successfully dispatch the workflow.
}

# Valid 🟢
- uses: step-security/dispatch-workflow@v1
- uses: step-security/dispatch-workflow@v2
id: workflow-dispatch
name: 'Dispatch Using workflow_dispatch Method'
with:
Expand All @@ -323,7 +323,7 @@ When interacting with the GitHub REST API, it's beneficial to handle potential f
- `time-multiple`: The factor by which the `starting-delay-ms` is multiplied for each reattempt, influencing the delay duration.

```yaml
- uses: step-security/dispatch-workflow@v1
- uses: step-security/dispatch-workflow@v2
id: custom-backoff
name: 'Dispatch with custom exponential backoff parameters'
with:
Expand Down
18 changes: 13 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,24 @@ function workflowDispatch(distinctId) {
if (!config.ref) {
throw new Error(`workflow_dispatch: An input to 'ref' was not provided`);
}
// https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event
const response = yield octokit.rest.actions.createWorkflowDispatch({
// GitHub released a breaking change to the createWorkflowDispatch API that resulted in a change where the returned
// status code changed to 200, from 204. At the time, the @octokit/types had not been updated to reflect this change.
//
// Given that we are in an interim state where the API behaviour, but the public documentation has not been updated
// to reflect this change, and GitHub has not yet released any updates on this topic. I can going to play the safe
// route and assume that the response status code could be either 200 or 204.
//
// Reference: https://github.com/orgs/community/discussions/9752#discussioncomment-15295321
// Documentation: https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event
const response = (yield octokit.rest.actions.createWorkflowDispatch({
owner: config.owner,
repo: config.repo,
workflow_id: config.workflow,
ref: config.ref,
inputs
});
if (response.status !== 204) {
throw new Error(`workflow_dispatch: Failed to dispatch action, expected 204 but received ${response.status}`);
}));
if (response.status !== 200 && response.status !== 204) {
throw new Error(`workflow_dispatch: Failed to dispatch action, expected 200 or 204 but received ${response.status}`);
}
core.info(`✅ Successfully dispatched workflow using workflow_dispatch method:
repository: ${config.owner}/${config.repo}
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dispatch-workflow",
"version": "2.0.0",
"version": "2.0.1",
"private": true,
"description": "A GitHub action to dispatch a remote GitHub workflow and optionally retrieve its information",
"main": "lib/index.js",
Expand Down
33 changes: 29 additions & 4 deletions src/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,32 @@ describe('API', () => {
init(mockActionConfig)
})

it('should resolve after a successful dispatch', async () => {
// GitHub released a breaking change to the createWorkflowDispatch API that resulted in a change where the returned
// status code changed to 200, from 204.
//
// Given that we are in an interim state where the API behaviour, but the public documentation has not been updated
// to reflect this change, and GitHub has not yet released any updates on this topic. I can going to play the safe
// route and assume that the response status code could be either 200 or 204. I've added a test case that supports both
// potential status codes.
//
// Reference: https://github.com/orgs/community/discussions/9752#discussioncomment-15295321
// Documentation: https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event
it('should resolve after a successful dispatch with a 200 status', async () => {
jest
.spyOn(mockOctokit.rest.actions, 'createWorkflowDispatch')
.mockReturnValue(
Promise.resolve({
headers: null,
url: '',
data: undefined,
status: 200
})
)

await workflowDispatch('')
})

it('should resolve after a successful dispatch with a 204 status', async () => {
jest
.spyOn(mockOctokit.rest.actions, 'createWorkflowDispatch')
.mockReturnValue(
Expand All @@ -112,7 +137,7 @@ describe('API', () => {
)

await expect(workflowDispatch('')).rejects.toThrow(
`Failed to dispatch action, expected 204 but received ${errorStatus}`
`Failed to dispatch action, expected 200 or 204 but received ${errorStatus}`
)
})

Expand All @@ -126,7 +151,7 @@ describe('API', () => {

return {
data: undefined,
status: 204
status: 200
}
})

Expand All @@ -147,7 +172,7 @@ describe('API', () => {

return {
data: undefined,
status: 204
status: 200
}
})

Expand Down
19 changes: 14 additions & 5 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as github from '@actions/github'
import {getConfig, ActionConfig, DispatchMethod} from '../action'
import {getBranchNameFromRef} from '../utils'
import {Octokit, WorkflowRun, WorkflowRunResponse} from './api.types'
import type {OctokitResponse} from '@octokit/types'

let config: ActionConfig
let octokit: Octokit
Expand All @@ -25,18 +26,26 @@ export async function workflowDispatch(distinctId: string): Promise<void> {
if (!config.ref) {
throw new Error(`workflow_dispatch: An input to 'ref' was not provided`)
}
// https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event
const response = await octokit.rest.actions.createWorkflowDispatch({
// GitHub released a breaking change to the createWorkflowDispatch API that resulted in a change where the returned
// status code changed to 200, from 204. At the time, the @octokit/types had not been updated to reflect this change.
//
// Given that we are in an interim state where the API behaviour, but the public documentation has not been updated
// to reflect this change, and GitHub has not yet released any updates on this topic. I can going to play the safe
// route and assume that the response status code could be either 200 or 204.
//
// Reference: https://github.com/orgs/community/discussions/9752#discussioncomment-15295321
// Documentation: https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event
const response = (await octokit.rest.actions.createWorkflowDispatch({
owner: config.owner,
repo: config.repo,
workflow_id: config.workflow,
ref: config.ref,
inputs
})
})) as OctokitResponse<never, 204 | 200>

if (response.status !== 204) {
if (response.status !== 200 && response.status !== 204) {
throw new Error(
`workflow_dispatch: Failed to dispatch action, expected 204 but received ${response.status}`
`workflow_dispatch: Failed to dispatch action, expected 200 or 204 but received ${response.status}`
)
}

Expand Down
Loading