Skip to content

Commit

Permalink
Record index in check's external_id
Browse files Browse the repository at this point in the history
  • Loading branch information
szapp committed Apr 12, 2024
1 parent 2adedeb commit ec78a80
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 15 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ To remove the additional status check, call this GitHub Action with an authentic
For more details the issue, see [here](https://github.com/peter-murray/workflow-application-token-action#readme).
Always leave the additional input `cleanup-token` at its default.

Nevertheless, this is a optional cosmetic enhancement and this GitHub action works fine without.
> [!Tip]
> This is only an optional cosmetic enhancement and this GitHub action works fine without.

```yaml
name: scripts
Expand All @@ -112,7 +113,7 @@ permissions:
jobs:
parsiphae:
name: Run Parsiphae on scripts
if: github.event_name != 'check_run' || startsWith(github.event.check_run.name, 'Parsiphae') # Adjust to check name
if: github.event_name != 'check_run' || github.event.check_run.external_id == join(github.workflow, '-0')
runs-on: ubuntu-latest
steps:
- uses: actions/create-github-app-token@v1
Expand All @@ -129,3 +130,6 @@ jobs:
cache: # Optional
token: ${{ steps.app-token.outputs.token }}
```

> [!Note]
> This procedure only works reasonably well if `parsiphae-action` is only called once in the workflow file.
10 changes: 6 additions & 4 deletions __tests__/cleanup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ jest.mock('@actions/github', () => {
},
context: {
eventName: 'check_run',
workflow: 'workflow.yml',
payload: {
action: 'completed',
check_run: {
head_sha: 'abc123',
external_id: 'workflow.yml-0',
name: 'Patch Validator',
html_url: 'https://example.com/check_run',
conclusion: 'success',
Expand Down Expand Up @@ -100,7 +102,7 @@ describe('cleanup', () => {
github.context.eventName = 'check_run'
github.context.payload.action = 'completed'
github.context.payload.check_run.conclusion = 'failure'
github.context.payload.check_run.name = 'Wrong name'
github.context.payload.check_run.external_id = 'workflow.yml-1'

const result = await workflow()

Expand All @@ -114,14 +116,14 @@ describe('cleanup', () => {
expect(core.summary.addHeading).not.toHaveBeenCalled()
expect(core.summary.addRaw).not.toHaveBeenCalled()
expect(core.summary.write).not.toHaveBeenCalled()
expect(core.setFailed).toHaveBeenCalledWith('This action is only intended to be run on the "CheckName" check run')
expect(core.setFailed).toHaveBeenCalledWith('This action is only intended to be run on the first check run of the workflow only')
})

it('should delete workflow runs and set exit code if the event is check_run and action is completed', async () => {
github.context.eventName = 'check_run'
github.context.payload.action = 'completed'
github.context.payload.check_run.conclusion = 'success'
github.context.payload.check_run.name = 'CheckName'
github.context.payload.check_run.external_id = 'workflow.yml-0'
listWorkflowRunsForRepoMock.mockResolvedValueOnce({
data: {
workflow_runs: [{ id: 1, event: 'push' }],
Expand Down Expand Up @@ -181,7 +183,7 @@ describe('cleanup', () => {
github.context.eventName = 'check_run'
github.context.payload.action = 'completed'
github.context.payload.check_run.conclusion = 'failure'
github.context.payload.check_run.name = 'CheckName: some file'
github.context.payload.check_run.external_id = 'workflow.yml-0'
deleteWorkflowRunMock.mockRejectedValueOnce(new Error('Delete error'))

const result = await workflow()
Expand Down
2 changes: 2 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ jest.mock('@actions/github', () => {
repo: 'repo',
},
sha: 'sha',
workflow: 'workflow.yml',
},
}
})
Expand Down Expand Up @@ -109,6 +110,7 @@ describe('action', () => {
repo: 'repo',
name: 'Testing: fail.d',
head_sha: 'sha',
external_id: 'workflow.yml-0',
started_at: expect.any(String),
completed_at: expect.any(String),
conclusion: 'failure',
Expand Down
8 changes: 4 additions & 4 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { setTimeout } from 'timers/promises'
export async function workflow(): Promise<boolean> {
// Only for completed check runs
if (github.context.eventName !== 'check_run' || github.context.payload.action !== 'completed') return false
const octokit = github.getOctokit(core.getInput('cleanup-token'))
const checkName = core.getInput('check-name')

// Check if the triggering check run is the correct one
if (!github.context.payload.check_run.name.startsWith(checkName)) {
if (github.context.payload.check_run.external_id !== `${github.context.workflow}-0`) {
// This workflow run here will then be also deleted by the correctly triggered run
core.setFailed(`This action is only intended to be run on the "${checkName}" check run`)
core.setFailed('This action is only intended to be run on the first check run of the workflow only')
return true
}

const octokit = github.getOctokit(core.getInput('cleanup-token'))

// Let all running workflows finish
let status: boolean
do {
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ For more details on Parsiphae, see [Lehona/Parsiphae@${parVer}](${link}).`
...github.context.repo,
name: checkRunName,
head_sha: github.context.sha,
external_id: `${github.context.workflow}-${idx}`,
started_at: startedAt.toISOString(),
completed_at: new Date().toISOString(),
conclusion: numErr ? 'failure' : 'success',
Expand Down

0 comments on commit ec78a80

Please sign in to comment.