Skip to content

Commit

Permalink
Merge pull request #315 from github/commit-time-checks
Browse files Browse the repository at this point in the history
Commit Time Checks 🔒 🕐
  • Loading branch information
GrantBirki authored Oct 21, 2024
2 parents 535e75d + 9bfda32 commit e9ac229
Show file tree
Hide file tree
Showing 7 changed files with 534 additions and 2 deletions.
57 changes: 57 additions & 0 deletions __tests__/functions/commit-safety-checks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {commitSafetyChecks} from '../../src/functions/commit-safety-checks'
import * as core from '@actions/core'

const debugMock = jest.spyOn(core, 'debug').mockImplementation(() => {})

var data
var context
beforeEach(() => {
jest.clearAllMocks()
jest.spyOn(core, 'debug').mockImplementation(() => {})

context = {
payload: {
comment: {
created_at: '2024-10-15T12:00:00Z'
}
}
}

data = {
commit: {
author: {
date: '2024-10-15T11:00:00Z'
}
}
}
})

test('checks a commit and finds that it is safe (date)', async () => {
expect(await commitSafetyChecks(context, data)).toStrictEqual({
message: 'success',
status: true
})
expect(debugMock).toHaveBeenCalledWith(
'2024-10-15T12:00:00Z is not older than 2024-10-15T11:00:00Z'
)
})

test('checks a commit and finds that it is not safe (date)', async () => {
data.commit.author.date = '2024-10-15T12:00:01Z'

expect(await commitSafetyChecks(context, data)).toStrictEqual({
message:
'### ⚠️ Cannot proceed with deployment\n\nThe latest commit is not safe for deployment. It was authored after the trigger comment was created.',
status: false
})
expect(debugMock).toHaveBeenCalledWith(
'2024-10-15T12:00:00Z is older than 2024-10-15T12:00:01Z'
)
})

test('raises an error if the date format is invalid', async () => {
data.commit.author.date = '2024-10-15T12:00:uhoh'
await expect(commitSafetyChecks(context, data)).rejects.toThrow(
'Invalid date format. Please ensure the dates are valid UTC timestamps.'
)
})
56 changes: 55 additions & 1 deletion __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as core from '@actions/core'
import * as isDeprecated from '../src/functions/deprecated-checks'
import * as nakedCommandCheck from '../src/functions/naked-command-check'
import * as validDeploymentOrder from '../src/functions/valid-deployment-order'
import * as commitSafetyChecks from '../src/functions/commit-safety-checks'
import {COLORS} from '../src/functions/colors'

const setOutputMock = jest.spyOn(core, 'setOutput')
Expand Down Expand Up @@ -76,7 +77,8 @@ beforeEach(() => {
id: 123,
user: {
login: 'monalisa'
}
},
created_at: '2024-10-21T19:11:18Z'
}
}

Expand All @@ -96,6 +98,17 @@ beforeEach(() => {
}),
createDeploymentStatus: jest.fn().mockImplementation(() => {
return {data: {}}
}),
getCommit: jest.fn().mockImplementation(() => {
return {
data: {
commit: {
author: {
date: '2024-10-15T12:00:00Z'
}
}
}
}
})
},
pulls: {
Expand Down Expand Up @@ -127,6 +140,14 @@ beforeEach(() => {
sha: null
}
})
jest
.spyOn(commitSafetyChecks, 'commitSafetyChecks')
.mockImplementation(() => {
return {
status: true,
message: 'success'
}
})
jest
.spyOn(validDeploymentOrder, 'validDeploymentOrder')
.mockImplementation(() => {
Expand Down Expand Up @@ -809,6 +830,17 @@ test('detects an out of date branch and exits', async () => {
}),
createDeploymentStatus: jest.fn().mockImplementation(() => {
return {data: {}}
}),
getCommit: jest.fn().mockImplementation(() => {
return {
data: {
commit: {
author: {
date: '2024-10-15T12:00:00Z'
}
}
}
}
})
}
}
Expand Down Expand Up @@ -877,6 +909,28 @@ test('fails prechecks', async () => {
expect(validDeploymentOrderMock).not.toHaveBeenCalled()
})

test('fails commitSafetyChecks', async () => {
jest
.spyOn(commitSafetyChecks, 'commitSafetyChecks')
.mockImplementation(() => {
return {
status: false,
message:
'### ⚠️ Cannot proceed with deployment... a scary commit was found'
}
})
jest.spyOn(actionStatus, 'actionStatus').mockImplementation(() => {
return undefined
})
expect(await run()).toBe('failure')
expect(saveStateMock).toHaveBeenCalledWith('bypass', 'true')
expect(setFailedMock).toHaveBeenCalledWith(
'### ⚠️ Cannot proceed with deployment... a scary commit was found'
)

expect(validDeploymentOrderMock).not.toHaveBeenCalled()
})

test('runs the .help command successfully', async () => {
github.context.payload.comment.body = '.help'
jest.spyOn(help, 'help').mockImplementation(() => {
Expand Down
84 changes: 84 additions & 0 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.

Loading

0 comments on commit e9ac229

Please sign in to comment.