Skip to content

fix job outcome reporting on cancelled job #22

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

Merged
merged 1 commit into from
Apr 29, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ steps:
tinybird_token: ${{ secrets.TINYBIRD_TOKEN }}
tinybird_datasource: <your-data-source>
workflow_id: <custom-workflow-id>
outcome: ${{ (contains(needs.*.result, 'failure') && 'failure') || 'success' }}
outcome: ${{ ((contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')) && 'failure') || 'success' }}
```

## Inputs
Expand Down
152 changes: 141 additions & 11 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { getOctokit } from '@actions/github'
import { createWorkflowEvent } from '../src/tb'

jest.mock('../src/tb', () => ({
createWorkflowEvent: jest.fn()
createWorkflowEvent: jest.fn(),
pushToTinybird: jest.fn()
}))

jest.mock('@actions/core', () => ({
info: jest.fn(),
getInput: jest.fn(),
setFailed: jest.fn(),
setSecret: jest.fn()
Expand All @@ -29,25 +31,111 @@ jest.mock('@actions/github', () => ({
getOctokit: jest.fn()
}))

// mock the getWorkflowRunAttempt
const mockOctokit = {
rest: {
actions: {
async getWorkflowRunAttempt() {
return { data: { run_started_at: '2020-01-22T19:33:08Z' } }
},
listJobsForWorkflowRunAttempt: jest.fn()
}
}
}
;(getOctokit as jest.Mock).mockReturnValue(mockOctokit)

describe('run', () => {
beforeEach(() => {
jest.clearAllMocks()
})

it('should send custom outcome', async () => {
// mock the getWorkflowRunAttempt
const mockOctokit = {
rest: {
actions: {
async getWorkflowRunAttempt() {
return { data: { run_started_at: '2020-01-22T19:33:08Z' } }
// mock getInput
// eslint-disable-next-line no-extra-semi
;(getInput as jest.Mock).mockImplementation((inputName: string) => {
switch (inputName) {
case 'outcome':
return 'custom_outcome'
default:
return 'mocked-input'
}
})

// run the function
await run()

// assertions
expect(createWorkflowEvent).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
expect.anything(),
'custom_outcome'
)
})

it('should send send successful on all successful jobs', async () => {
mockOctokit.rest.actions.listJobsForWorkflowRunAttempt.mockReturnValue({
data: {
jobs: [
{
name: 'Successful Job 1',
conclusion: 'success'
},
{
name: 'Successful Job 2',
conclusion: 'success'
}
]
}
})

// mock getInput
;(getInput as jest.Mock).mockImplementation((inputName: string) => {
switch (inputName) {
case 'outcome':
return undefined
default:
return 'mocked-input'
}
})

// run the function
await run()

// assertions
expect(createWorkflowEvent).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
expect.anything(),
'success'
)
})

it('should send send failed on a failed job', async () => {
mockOctokit.rest.actions.listJobsForWorkflowRunAttempt.mockImplementation(
async () => {
return {
data: {
jobs: [
{
name: 'Failed Job',
conclusion: 'failure'
},
{
name: 'Successful Job',
conclusion: 'success'
}
]
}
}
}
}
;(getOctokit as jest.Mock).mockReturnValueOnce(mockOctokit)
)

// mock getInput
;(getInput as jest.Mock).mockImplementation((inputName: string) => {
switch (inputName) {
case 'outcome':
return 'custom_outcome'
return undefined
default:
return 'mocked-input'
}
Expand All @@ -61,7 +149,49 @@ describe('run', () => {
expect.anything(),
expect.anything(),
expect.anything(),
'custom_outcome'
'failure'
)
})

it('should send send failed on a cancelled job', async () => {
mockOctokit.rest.actions.listJobsForWorkflowRunAttempt.mockImplementation(
async () => {
return {
data: {
jobs: [
{
name: 'Cancelled Job',
conclusion: 'cancelled'
},
{
name: 'Successful Job',
conclusion: 'success'
}
]
}
}
}
)

// mock getInput
;(getInput as jest.Mock).mockImplementation((inputName: string) => {
switch (inputName) {
case 'outcome':
return undefined
default:
return 'mocked-input'
}
})

// run the function
await run()

// assertions
expect(createWorkflowEvent).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
expect.anything(),
'failure'
)
})
})
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion dist/index.js

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

5 changes: 4 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ export async function run(): Promise<void> {
let failed = false
for (const job of jobs.data.jobs) {
core.info(`Job ${job.name} has conclusion: ${job.conclusion}`)
failed = failed || job.conclusion === 'failure'
failed =
failed ||
job.conclusion === 'failure' ||
job.conclusion === 'cancelled'
}
outcome = failed ? 'failure' : 'success'
}
Expand Down