Skip to content

add support for manually setting outcome #21

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 4 commits into from
Apr 24, 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
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ steps:

- `if: always()` is necessary to also execute this step in case a previous step already failed (otherwise this step would be skipped and `failure` workflow data will never be sent).
- If you have this action in an explicit job, make sure to also define the `always()` condition _and_ set the all previous jobs as dependencies on for the job:
```
```yaml
jobs:
...
push-to-tinybird:
Expand All @@ -49,6 +49,23 @@ steps:
...
```

### Usage in callable workflows
In callable workflows, the usual outcome detection might not work correctly, since the job outcome of all jobs in the composite workflow would be detected.
It also isn't possible to access the `needs` context from within the GitHub action in order to analyze only the outcome of the dependencies of this specific sub-workflow.
We introduced the `outcome` input for this usecase to manually set a outcome which should be sent to Tinybird:
```yaml
steps:
- name: Push to Tinybird
if: always()
uses: localstack/tinybird-workflow-push@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
tinybird_token: ${{ secrets.TINYBIRD_TOKEN }}
tinybird_datasource: <your-data-source>
workflow_id: <custom-workflow-id>
outcome: ${{ (contains(needs.*.result, 'failure') && 'failure') || 'success' }}
```

## Inputs

### `github_token`
Expand All @@ -66,3 +83,8 @@ steps:
### `workflow_id`

**Optional**: This is the ID of the workflow sent to tinybird. (default: the output of `github.context.workflow`)

### `outcome`

**Optional**: Manually override the outcome reported to Tinybird.
(default: the outcome is calculated using the worst outcome of all jobs in the current workflow run attempt)
67 changes: 67 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { run } from '../src/main'
import { getInput } from '@actions/core'
import { getOctokit } from '@actions/github'
import { createWorkflowEvent } from '../src/tb'

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

jest.mock('@actions/core', () => ({
getInput: jest.fn(),
setFailed: jest.fn(),
setSecret: jest.fn()
}))

jest.mock('@actions/github', () => ({
context: {
payload: {
pull_request: {
number: 1
}
},
runId: 'test_run_id',
repo: {
owner: 'localstack',
repo: 'tinybird-workflow-push'
}
},
getOctokit: jest.fn()
}))

describe('run', () => {
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' } }
}
}
}
}
;(getOctokit as jest.Mock).mockReturnValueOnce(mockOctokit)

// mock getInput
;(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'
)
})
})
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ inputs:
required: true
tinybird_datasource:
description: 'The Tinybird datasource to which to push the data to'
required: true
required: false
default: 'ci_workflows'
workflow_id:
description: 'The id of the workflow'
outcome:
description: >
Optional input to manually override the outcome reported to Tinybird.
By default the outcome is calculated using the worst outcome of all jobs in the current workflow run attempt.
required: false


runs:
Expand Down
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.
28 changes: 17 additions & 11 deletions dist/index.js

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

30 changes: 18 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,38 @@ export async function run(): Promise<void> {
run_id: github.context.runId,
attempt_number
})

const started_at = response.data.run_started_at
if (!started_at) {
throw new Error('Could not get the start time of the workflow')
}
const now = new Date().toISOString()

const jobs = await octokit.rest.actions.listJobsForWorkflowRunAttempt({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
run_id: github.context.runId,
attempt_number
})
// check if "outcome" is set as an input, effectively overwriting the detection
let outcome = core.getInput('outcome')
if (!outcome) {
// it "outcome" is not set, check the status of every job in the workflow
const jobs = await octokit.rest.actions.listJobsForWorkflowRunAttempt({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
run_id: github.context.runId,
attempt_number
})

let failed = false
for (const job of jobs.data.jobs) {
core.info(`Job ${job.name} has conclusion: ${job.conclusion}`)
failed = failed || job.conclusion === 'failure'
let failed = false
for (const job of jobs.data.jobs) {
core.info(`Job ${job.name} has conclusion: ${job.conclusion}`)
failed = failed || job.conclusion === 'failure'
}
outcome = failed ? 'failure' : 'success'
}

const workflow_id: string = core.getInput('workflow_id')

const workflowEvent = await createWorkflowEvent(
started_at,
now,
workflow_id,
failed ? 'failure' : 'success'
outcome
)

const tb_token: string = core.getInput('tinybird_token')
Expand Down