Skip to content

Commit 656ddd2

Browse files
authored
fix: in some cases usernames are not correctly parsed (#20)
* test: add test cases that are failing * wip
1 parent a9a359c commit 656ddd2

File tree

7 files changed

+107
-32
lines changed

7 files changed

+107
-32
lines changed

serverless.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ custom:
1010
dev: '23178'
1111
prod: '23186'
1212
botName:
13-
dev: '@AllContributorsDev'
14-
prod: '@AllContributorsBot'
13+
dev: 'AllContributorsDev'
14+
prod: 'AllContributorsBot'
1515
logLevel:
1616
dev: debug
1717
prod: debug # TODO change to info soon

src/processIssueComment.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const ContentFiles = require('./ContentFiles')
66
const getUserDetails = require('./utils/getUserDetails')
77
const parseComment = require('./utils/parse-comment')
88

9+
const isMessageForBot = require('./utils/isMessageForBot')
910
const { GIHUB_BOT_NAME } = require('./utils/settings')
1011
const { AllContributorBotError } = require('./utils/errors')
1112

@@ -69,13 +70,6 @@ async function processIssueComment({ context, commentReply }) {
6970

7071
const commentBody = context.payload.comment.body
7172
const parsedComment = parseComment(commentBody)
72-
if (!parsedComment.action) {
73-
commentReply.reply(`I could not determine your intention.`)
74-
commentReply.reply(
75-
`Basic usage: @${GIHUB_BOT_NAME} please add jakebolam for code, doc`,
76-
)
77-
return
78-
}
7973

8074
if (parsedComment.action === 'add') {
8175
await processAddContributor({
@@ -89,26 +83,24 @@ async function processIssueComment({ context, commentReply }) {
8983
return
9084
}
9185

92-
commentReply.reply(`I'm not sure how to ${parsedComment.action}`)
86+
commentReply.reply(`I could not determine your intention.`)
9387
commentReply.reply(
94-
`Basic usage: @${GIHUB_BOT_NAME} please add jakebolam for code, doc`,
88+
`Basic usage: @${GIHUB_BOT_NAME} please add jakebolam for code, doc and infra`,
89+
)
90+
commentReply(
91+
`For other usage see the [documentation](https://github.com/all-contributors/all-contributors-bot#usage)`,
9592
)
9693
return
9794
}
9895

99-
function hasMentionedBotName(context) {
100-
const commentBody = context.payload.comment.body
101-
const hasMentionedBotName = commentBody.includes(GIHUB_BOT_NAME)
102-
return hasMentionedBotName
103-
}
104-
10596
async function processIssueCommentSafe({ context }) {
10697
if (context.isBot) {
10798
context.log.debug('From a bot, exiting')
10899
return
109100
}
110101

111-
if (!hasMentionedBotName(context)) {
102+
const commentBody = context.payload.comment.body
103+
if (!isMessageForBot(commentBody)) {
112104
context.log.debug('Message not for us, exiting')
113105
return
114106
}

src/utils/isMessageForBot.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { GIHUB_BOT_NAME } = require('./settings')
2+
3+
function isMessageForBot(message) {
4+
const isMessageForBot = message.includes(`@${GIHUB_BOT_NAME}`)
5+
return isMessageForBot
6+
}
7+
8+
module.exports = isMessageForBot

src/utils/parse-comment/index.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ validContributionTypes.forEach(type => {
4242
const plugin = {
4343
words: {
4444
...Contributions,
45+
add: 'Action',
4546
},
4647
patterns: {
4748
// 'add #person for #Contribution': 'AddContributor',
@@ -51,7 +52,23 @@ const plugin = {
5152
nlp.plugin(plugin)
5253

5354
function parseAddComment(doc, action) {
54-
const who = doc.match(`${action} [.]`).data()[0].normal
55+
const who = doc
56+
.match(`${action} [.]`)
57+
.normalize({
58+
whitespace: true, // remove hyphens, newlines, and force one space between words
59+
case: false, // keep only first-word, and 'entity' titlecasing
60+
numbers: false, // turn 'seven' to '7'
61+
punctuation: true, // remove commas, semicolons - but keep sentence-ending punctuation
62+
unicode: false, // visually romanize/anglicize 'Björk' into 'Bjork'.
63+
contractions: false, // turn "isn't" to "is not"
64+
acronyms: false, //remove periods from acronyms, like 'F.B.I.'
65+
parentheses: false, //remove words inside brackets (like these)
66+
possessives: false, // turn "Google's tax return" to "Google tax return"
67+
plurals: false, // turn "batmobiles" into "batmobile"
68+
verbs: false, // turn all verbs into Infinitive form - "I walked" → "I walk"
69+
honorifics: false, //turn 'Vice Admiral John Smith' to 'John Smith'
70+
})
71+
.data()[0].text
5572

5673
// TODO: handle plurals (e.g. some said docs)
5774
const contributions = doc
@@ -72,17 +89,17 @@ function parseAddComment(doc, action) {
7289
function parseComment(message) {
7390
const doc = nlp(message)
7491

75-
if (doc.verbs().data().length === 0) {
76-
return {}
77-
}
92+
const action = doc
93+
.match('#Action')
94+
.normalize()
95+
.out('string')
7896

79-
const action = doc.verbs().data()[0].normal
8097
if (action === 'add') {
8198
return parseAddComment(doc, action)
8299
}
83100

84101
return {
85-
action,
102+
action: false,
86103
}
87104
}
88105

test/setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ const nock = require('nock')
22

33
nock.disableNetConnect()
44

5-
process.env.BOT_NAME = '@AllContributorsBotTest'
5+
process.env.BOT_NAME = 'AllContributorsBotTest'

test/utils/isMessageForBot.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const isMessageForBot = require('../../src/utils/isMessageForBot')
2+
3+
describe('isMessageForBot', () => {
4+
const testBotName = 'AllContributorsBotTest'
5+
6+
test('For us', () => {
7+
expect(
8+
isMessageForBot(
9+
`@${testBotName} please add jakebolam for doc, infra and code`,
10+
),
11+
).toBe(true)
12+
13+
expect(
14+
isMessageForBot(
15+
`@AllContributorsBotTest please add jakebolam for doc, infra and code`,
16+
),
17+
).toBe(true)
18+
})
19+
20+
test('Not for us', () => {
21+
expect(
22+
isMessageForBot(
23+
`${testBotName} please add jakebolam for doc, infra and code`,
24+
),
25+
).toBe(false)
26+
27+
expect(
28+
isMessageForBot(`Please add jakebolam for doc, infra and code`),
29+
).toBe(false)
30+
})
31+
})
Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
11
const parseComment = require('../../../src/utils/parse-comment')
2-
const { GIHUB_BOT_NAME } = require('../../../src//utils/settings')
32

43
describe('parseComment', () => {
4+
const testBotName = 'AllContributorsBotTest'
5+
56
test('Basic intent to add', () => {
67
expect(
78
parseComment(
8-
`@${GIHUB_BOT_NAME} please add jakebolam for doc, infra and code`,
9+
`@${testBotName} please add jakebolam for doc, infra and code`,
910
),
1011
).toEqual({
1112
action: 'add',
1213
who: 'jakebolam',
1314
contributions: ['doc', 'infra', 'code'],
1415
})
1516
})
17+
18+
test('Basic intent to add - non name username', () => {
19+
expect(
20+
parseComment(`@${testBotName} please add tbenning for design`),
21+
).toEqual({
22+
action: 'add',
23+
who: 'tbenning',
24+
contributions: ['design'],
25+
})
26+
})
27+
28+
test('Basic intent to add - captialized username', () => {
29+
expect(
30+
parseComment(`@${testBotName} please add Rbot25_RULES for tool`),
31+
).toEqual({
32+
action: 'add',
33+
who: 'Rbot25_RULES',
34+
contributions: ['tool'],
35+
})
36+
})
37+
38+
test('Intent unknown', () => {
39+
expect(
40+
parseComment(`@${testBotName} please lollmate for tool`),
41+
).toEqual({
42+
action: false,
43+
})
44+
})
1645
//
1746
// test('Basic intent to add (with plurals)', () => {
1847
// expect(
19-
// parseComment(
20-
// `@${GIHUB_BOT_NAME} please add jakebolam for docs, infra and code`,
21-
// ),
48+
// parseComment(`@${testBotName} please add dat2 for docs`),
2249
// ).toEqual({
2350
// action: 'add',
24-
// who: 'jakebolam',
25-
// contributions: ['doc', 'infra', 'code'],
51+
// who: 'dat2',
52+
// contributions: ['doc'],
2653
// })
2754
// })
2855
})

0 commit comments

Comments
 (0)