Skip to content

Commit a41d7bd

Browse files
authored
Merge pull request #419 from badideasforsale/better-trigger-detection
Better trigger detection
2 parents 48285b1 + e908cee commit a41d7bd

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

__tests__/functions/trigger-check.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ test('checks a message and finds a global trigger', async () => {
3434
const body = 'I want to .deploy'
3535
const trigger = '.deploy'
3636
expect(await triggerCheck(body, trigger)).toBe(false)
37+
expect(debugMock).toHaveBeenCalledWith(
38+
`comment body does not start with trigger: ${color}.deploy${colorReset}`
39+
)
3740
})
3841

3942
test('checks a message and finds a trigger with an environment and a variable', async () => {
@@ -52,3 +55,27 @@ test('checks a message and does not find global trigger', async () => {
5255
`comment body does not start with trigger: ${color}.deploy${colorReset}`
5356
)
5457
})
58+
59+
test('does not match when body starts with a longer command sharing prefix', async () => {
60+
const body = '.deploy-two to prod'
61+
const trigger = '.deploy'
62+
expect(await triggerCheck(body, trigger)).toBe(false)
63+
expect(debugMock).toHaveBeenCalledWith(
64+
`comment body starts with trigger but is not complete: ${color}.deploy${colorReset}`
65+
)
66+
})
67+
68+
test('does not match when immediately followed by alphanumeric', async () => {
69+
const body = '.deploy1'
70+
const trigger = '.deploy'
71+
expect(await triggerCheck(body, trigger)).toBe(false)
72+
expect(debugMock).toHaveBeenCalledWith(
73+
`comment body starts with trigger but is not complete: ${color}.deploy${colorReset}`
74+
)
75+
})
76+
77+
test('matches when followed by a newline (whitespace)', async () => {
78+
const body = `.deploy\ndev`
79+
const trigger = '.deploy'
80+
expect(await triggerCheck(body, trigger)).toBe(true)
81+
})

dist/index.js

Lines changed: 9 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.

src/functions/trigger-check.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ export async function triggerCheck(body, trigger) {
1414
return false
1515
}
1616

17+
// Ensure the trigger match is complete: either end-of-string or followed by whitespace
18+
const nextChar = body[trigger.length]
19+
if (nextChar && !/\s/.test(nextChar)) {
20+
core.debug(
21+
`comment body starts with trigger but is not complete: ${COLORS.highlight}${trigger}${COLORS.reset}`
22+
)
23+
return false
24+
}
25+
1726
core.info(
1827
`✅ comment body starts with trigger: ${COLORS.highlight}${trigger}${COLORS.reset}`
1928
)

0 commit comments

Comments
 (0)