Skip to content
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

Improve debug logging #21

Merged
merged 2 commits into from
Apr 11, 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
133 changes: 132 additions & 1 deletion __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const github = require('@actions/github')
const main = require('../src/main')

// Mock the GitHub Actions core library
const debugMock = jest.spyOn(core, 'debug').mockImplementation()
const getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
const setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation()
Expand Down Expand Up @@ -110,7 +111,137 @@ describe('action', () => {

// Assert
expect(runMock).toHaveReturned()
expect(core.setOutput).toHaveBeenCalledWith('commit-hash', 'hash3')
expect(setOutputMock).toHaveBeenCalledWith('commit-hash', 'hash3')
})

it('outputs debug information as expected', async () => {
// Arrange
core.getInput.mockImplementation(name => {
switch (name) {
case 'github-token':
return 'token'
case 'workflow-id':
return 'workflowId'
case 'branch':
return 'branch'
case 'debug':
return 'true'
default:
return ''
}
})

octokitMock.rest.actions.listWorkflowRuns.mockResolvedValue({
data: {
workflow_runs: [
{
head_commit: {
id: 'hash1',
timestamp: '2022-01-01T00:00:00Z'
}
},
{
head_commit: {
id: 'hash3',
timestamp: '2024-01-01T00:00:00Z'
}
},
{
head_commit: {
id: 'hash2',
timestamp: '2023-01-01T00:00:00Z'
}
}
]
}
})

// Act
await main.run()

// Assert
expect(runMock).toHaveReturned()
expect(debugMock).toHaveBeenNthCalledWith(
1,
'Debug mode is enabled. Inputs: github-token=***, workflow-id=workflowId, branch=branch'
)
expect(debugMock).toHaveBeenNthCalledWith(
2,
'workflowRuns:',
JSON.stringify(
[
{
head_commit: {
id: 'hash1',
timestamp: '2022-01-01T00:00:00Z'
}
},
{
head_commit: {
id: 'hash3',
timestamp: '2024-01-01T00:00:00Z'
}
},
{
head_commit: {
id: 'hash2',
timestamp: '2023-01-01T00:00:00Z'
}
}
],
null,
2
)
)
expect(debugMock).toHaveBeenNthCalledWith(
3,
'headCommits:',
JSON.stringify(
[
{
id: 'hash1',
timestamp: '2022-01-01T00:00:00Z'
},
{
id: 'hash3',
timestamp: '2024-01-01T00:00:00Z'
},
{
id: 'hash2',
timestamp: '2023-01-01T00:00:00Z'
}
],
null,
2
)
)
expect(debugMock).toHaveBeenNthCalledWith(
4,
'sortedHeadCommits:',
JSON.stringify(
[
{
id: 'hash1',
timestamp: '2022-01-01T00:00:00Z'
},
{
id: 'hash2',
timestamp: '2023-01-01T00:00:00Z'
},
{
id: 'hash3',
timestamp: '2024-01-01T00:00:00Z'
}
],
null,
2
)
)
expect(debugMock).toHaveBeenNthCalledWith(
5,
'lastSuccessCommitHash:',
JSON.stringify('hash3')
)
})

it('fails if there are no successful workflow runs', async () => {
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.
10 changes: 5 additions & 5 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.

10 changes: 5 additions & 5 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function run() {
return
}
if (debug) {
console.log(
core.debug(
`Debug mode is enabled. Inputs: github-token=***, workflow-id=${workflowId}, branch=${branch}`
)
}
Expand All @@ -55,7 +55,7 @@ async function run() {
// Extract the workflow runs from the response
const workflowRuns = res.data.workflow_runs
if (debug) {
console.log('workflowRuns:', JSON.stringify(workflowRuns, null, 2))
core.debug('workflowRuns:', JSON.stringify(workflowRuns, null, 2))
}

// Fail the run if no previous successful workflow runs were found
Expand All @@ -71,7 +71,7 @@ async function run() {
return workflowRun.head_commit
})
if (debug) {
console.log('headCommits:', JSON.stringify(headCommits, null, 2))
core.debug('headCommits:', JSON.stringify(headCommits, null, 2))
}

// Sort the commits in ascending order (oldest to newest)
Expand All @@ -83,7 +83,7 @@ async function run() {
return 0
})
if (debug) {
console.log(
core.debug(
'sortedHeadCommits:',
JSON.stringify(sortedHeadCommits, null, 2)
)
Expand All @@ -93,7 +93,7 @@ async function run() {
const lastSuccessCommitHash =
sortedHeadCommits[sortedHeadCommits.length - 1].id
if (debug) {
console.log(
core.debug(
'lastSuccessCommitHash:',
JSON.stringify(lastSuccessCommitHash, null, 2)
)
Expand Down
Loading