Skip to content

Commit

Permalink
Set exit code based on parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
szapp committed Apr 11, 2024
1 parent b715eb1 commit 2adedeb
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 26 deletions.
23 changes: 19 additions & 4 deletions __tests__/cleanup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const listWorkflowRunsForRepoMock = jest.fn(async (_params) => ({
}))
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const deleteWorkflowRunMock = jest.fn(async (_params) => {})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const forceCancelWorkflowRunMock = jest.fn(async (_params) => {})
jest.mock('@actions/github', () => {
return {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand All @@ -36,6 +38,7 @@ jest.mock('@actions/github', () => {
getWorkflowRun: getWorkflowRunMock,
listWorkflowRuns: listWorkflowRunsMock,
listWorkflowRunsForRepo: listWorkflowRunsForRepoMock,
forceCancelWorkflowRun: forceCancelWorkflowRunMock,
deleteWorkflowRun: deleteWorkflowRunMock,
},
},
Expand Down Expand Up @@ -124,12 +127,14 @@ describe('cleanup', () => {
workflow_runs: [{ id: 1, event: 'push' }],
},
})
forceCancelWorkflowRunMock.mockRejectedValueOnce(new Error('Cancel error'))

const result = await workflow()

expect(result).toBe(true)
expect(timers.setTimeout).toHaveBeenCalledWith(15000)
expect(timers.setTimeout).toHaveBeenCalledTimes(2)
expect(timers.setTimeout).toHaveBeenCalledWith(5000)
expect(timers.setTimeout).toHaveBeenCalledTimes(3)
expect(listWorkflowRunsForRepoMock).toHaveBeenCalledWith({
...github.context.repo,
status: 'in_progress',
Expand All @@ -153,6 +158,10 @@ describe('cleanup', () => {
head_sha: github.context.payload.check_run.head_sha,
})
expect(core.info).toHaveBeenCalledWith('Runs to delete: 1(in_progress), 3(completed)')
expect(forceCancelWorkflowRunMock).toHaveBeenCalledWith({
...github.context.repo,
run_id: 1,
})
expect(deleteWorkflowRunMock).toHaveBeenCalledWith({
...github.context.repo,
run_id: 1,
Expand All @@ -178,7 +187,8 @@ describe('cleanup', () => {
const result = await workflow()

expect(result).toBe(true)
expect(timers.setTimeout).toHaveBeenCalledWith(15000)
expect(timers.setTimeout).toHaveBeenCalledWith(5000)
expect(timers.setTimeout).toHaveBeenCalledTimes(2)
expect(listWorkflowRunsForRepoMock).toHaveBeenCalledWith({
...github.context.repo,
status: 'in_progress',
Expand All @@ -193,6 +203,11 @@ describe('cleanup', () => {
workflow_id: 123,
head_sha: github.context.payload.check_run.head_sha,
})
expect(core.info).toHaveBeenCalledWith('Runs to delete: 1(in_progress), 3(completed)')
expect(forceCancelWorkflowRunMock).toHaveBeenCalledWith({
...github.context.repo,
run_id: 1,
})
expect(deleteWorkflowRunMock).toHaveBeenCalledWith({
...github.context.repo,
run_id: 1,
Expand All @@ -204,8 +219,8 @@ describe('cleanup', () => {
expect(core.summary.addHeading).toHaveBeenCalledWith(github.context.payload.check_run.name)
expect(core.summary.addRaw).toHaveBeenCalledWith(`<a href="${github.context.payload.check_run.html_url}">Details</a>`, true)
expect(core.summary.write).toHaveBeenCalledWith({ overwrite: false })
expect(core.info).toHaveBeenCalledWith(`\u001b[32m${new Error('Delete error')}\u001b[0m`)
expect(core.info).toHaveBeenCalledWith(`\u001b[31m${new Error('Delete error')}\u001b[0m`)
expect(core.setFailed).not.toHaveBeenCalled()
expect(process.exitCode).toBe(core.ExitCode.Failure)
expect(process.exitCode).toBe(core.ExitCode.Success)
})
})
39 changes: 28 additions & 11 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.

29 changes: 22 additions & 7 deletions src/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,32 @@ export async function workflow(): Promise<boolean> {
head_sha: github.context.payload.check_run.head_sha,
})

// Delete all workflow runs except the current one
// Exclude the current workflow run
const workflows = workflow_runs.filter((w) => w.id !== github.context.runId)

// Find success across all workflow runs (non-check runs)
const failure = workflows.filter((w) => w.event !== 'check_run').some((w) => ['failure', 'cancelled'].includes(w.conclusion ?? ''))

// Delete all workflow runs
core.info(`Runs to delete: ${workflows.map((w) => `${w.id}(${w.status})`).join(', ')}`)
Promise.allSettled(
workflows.map((w) =>
octokit.rest.actions
await Promise.allSettled(
workflows.map(async (w) => {
if (w.status === 'in_progress') {
await octokit.rest.actions
.forceCancelWorkflowRun({
...github.context.repo,
run_id: w.id,
})
.catch((error) => core.info(`\u001b[31m${error}\u001b[0m`))
.then(() => setTimeout(5000))
}
await octokit.rest.actions
.deleteWorkflowRun({
...github.context.repo,
run_id: w.id,
})
.catch((error) => core.info(`\u001b[32m${error}\u001b[0m`))
)
.catch((error) => core.info(`\u001b[31m${error}\u001b[0m`))
})
)

// The summary of the workflow runs is unfortunately not available in the API
Expand All @@ -69,7 +83,8 @@ export async function workflow(): Promise<boolean> {
.write({ overwrite: false })

// To be able to use a badge, we need to set the exit code
process.exitCode = Number(github.context.payload.check_run.conclusion !== 'success')
// Note: This does not reflect the status of the check run but across all workflow runs
process.exitCode = Number(failure)

// True means we stop here
return true
Expand Down
9 changes: 6 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export async function run(): Promise<void> {
const octokit = github.getOctokit(githubToken)

// Process input file(s) asynchronously
await Promise.all(
const failed = await Promise.all(
files.map(async (file, idx) => {
const srcfile = stripWorkspace(file)
const extFlag = file.replace(regexExtSrc, 's').replace(regexExtD, 'i')
Expand Down Expand Up @@ -224,7 +224,7 @@ For more details on Parsiphae, see [Lehona/Parsiphae@${parVer}](${link}).`
)
.then(async (summary) => {
// Build summary
core.startGroup('Generate summary')
core.info('Generate summary')
summary.sort((a, b) => a.idx - b.idx)
await core.summary
.addHeading(`${checkName} Results`)
Expand All @@ -247,12 +247,15 @@ For more details on Parsiphae, see [Lehona/Parsiphae@${parVer}](${link}).`
]),
])
.write({ overwrite: false })
core.endGroup()
return summary.some((s) => s.numErr > 0)
})
.catch((error) => {
/* istanbul ignore next */
throw error
})

// Set workflow status
process.exitCode = Number(failed)
} catch (error) {
const msg: string = error instanceof Error ? error.message : String(error)
core.setFailed(msg)
Expand Down

0 comments on commit 2adedeb

Please sign in to comment.