Skip to content

Commit e9ac229

Browse files
authored
Merge pull request #315 from github/commit-time-checks
Commit Time Checks 🔒 🕐
2 parents 535e75d + 9bfda32 commit e9ac229

File tree

7 files changed

+534
-2
lines changed

7 files changed

+534
-2
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {commitSafetyChecks} from '../../src/functions/commit-safety-checks'
2+
import * as core from '@actions/core'
3+
4+
const debugMock = jest.spyOn(core, 'debug').mockImplementation(() => {})
5+
6+
var data
7+
var context
8+
beforeEach(() => {
9+
jest.clearAllMocks()
10+
jest.spyOn(core, 'debug').mockImplementation(() => {})
11+
12+
context = {
13+
payload: {
14+
comment: {
15+
created_at: '2024-10-15T12:00:00Z'
16+
}
17+
}
18+
}
19+
20+
data = {
21+
commit: {
22+
author: {
23+
date: '2024-10-15T11:00:00Z'
24+
}
25+
}
26+
}
27+
})
28+
29+
test('checks a commit and finds that it is safe (date)', async () => {
30+
expect(await commitSafetyChecks(context, data)).toStrictEqual({
31+
message: 'success',
32+
status: true
33+
})
34+
expect(debugMock).toHaveBeenCalledWith(
35+
'2024-10-15T12:00:00Z is not older than 2024-10-15T11:00:00Z'
36+
)
37+
})
38+
39+
test('checks a commit and finds that it is not safe (date)', async () => {
40+
data.commit.author.date = '2024-10-15T12:00:01Z'
41+
42+
expect(await commitSafetyChecks(context, data)).toStrictEqual({
43+
message:
44+
'### ⚠️ Cannot proceed with deployment\n\nThe latest commit is not safe for deployment. It was authored after the trigger comment was created.',
45+
status: false
46+
})
47+
expect(debugMock).toHaveBeenCalledWith(
48+
'2024-10-15T12:00:00Z is older than 2024-10-15T12:00:01Z'
49+
)
50+
})
51+
52+
test('raises an error if the date format is invalid', async () => {
53+
data.commit.author.date = '2024-10-15T12:00:uhoh'
54+
await expect(commitSafetyChecks(context, data)).rejects.toThrow(
55+
'Invalid date format. Please ensure the dates are valid UTC timestamps.'
56+
)
57+
})

__tests__/main.test.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import * as core from '@actions/core'
1414
import * as isDeprecated from '../src/functions/deprecated-checks'
1515
import * as nakedCommandCheck from '../src/functions/naked-command-check'
1616
import * as validDeploymentOrder from '../src/functions/valid-deployment-order'
17+
import * as commitSafetyChecks from '../src/functions/commit-safety-checks'
1718
import {COLORS} from '../src/functions/colors'
1819

1920
const setOutputMock = jest.spyOn(core, 'setOutput')
@@ -76,7 +77,8 @@ beforeEach(() => {
7677
id: 123,
7778
user: {
7879
login: 'monalisa'
79-
}
80+
},
81+
created_at: '2024-10-21T19:11:18Z'
8082
}
8183
}
8284

@@ -96,6 +98,17 @@ beforeEach(() => {
9698
}),
9799
createDeploymentStatus: jest.fn().mockImplementation(() => {
98100
return {data: {}}
101+
}),
102+
getCommit: jest.fn().mockImplementation(() => {
103+
return {
104+
data: {
105+
commit: {
106+
author: {
107+
date: '2024-10-15T12:00:00Z'
108+
}
109+
}
110+
}
111+
}
99112
})
100113
},
101114
pulls: {
@@ -127,6 +140,14 @@ beforeEach(() => {
127140
sha: null
128141
}
129142
})
143+
jest
144+
.spyOn(commitSafetyChecks, 'commitSafetyChecks')
145+
.mockImplementation(() => {
146+
return {
147+
status: true,
148+
message: 'success'
149+
}
150+
})
130151
jest
131152
.spyOn(validDeploymentOrder, 'validDeploymentOrder')
132153
.mockImplementation(() => {
@@ -809,6 +830,17 @@ test('detects an out of date branch and exits', async () => {
809830
}),
810831
createDeploymentStatus: jest.fn().mockImplementation(() => {
811832
return {data: {}}
833+
}),
834+
getCommit: jest.fn().mockImplementation(() => {
835+
return {
836+
data: {
837+
commit: {
838+
author: {
839+
date: '2024-10-15T12:00:00Z'
840+
}
841+
}
842+
}
843+
}
812844
})
813845
}
814846
}
@@ -877,6 +909,28 @@ test('fails prechecks', async () => {
877909
expect(validDeploymentOrderMock).not.toHaveBeenCalled()
878910
})
879911

912+
test('fails commitSafetyChecks', async () => {
913+
jest
914+
.spyOn(commitSafetyChecks, 'commitSafetyChecks')
915+
.mockImplementation(() => {
916+
return {
917+
status: false,
918+
message:
919+
'### ⚠️ Cannot proceed with deployment... a scary commit was found'
920+
}
921+
})
922+
jest.spyOn(actionStatus, 'actionStatus').mockImplementation(() => {
923+
return undefined
924+
})
925+
expect(await run()).toBe('failure')
926+
expect(saveStateMock).toHaveBeenCalledWith('bypass', 'true')
927+
expect(setFailedMock).toHaveBeenCalledWith(
928+
'### ⚠️ Cannot proceed with deployment... a scary commit was found'
929+
)
930+
931+
expect(validDeploymentOrderMock).not.toHaveBeenCalled()
932+
})
933+
880934
test('runs the .help command successfully', async () => {
881935
github.context.payload.comment.body = '.help'
882936
jest.spyOn(help, 'help').mockImplementation(() => {

dist/index.js

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)