Skip to content

Commit

Permalink
feat: add parse comment first pass
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebolam committed Jan 11, 2019
1 parent de50707 commit 1cacd88
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 274 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"@probot/serverless-lambda": "^0.2.0",
"all-contributors-cli": "^5.7.0",
"compromise": "^11.13.0",
"probot": "^7.4.0"
},
"devDependencies": {
Expand Down
71 changes: 50 additions & 21 deletions src/processIssueComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,19 @@ const OptionsConfig = require('./OptionsConfig')
const ContentFiles = require('./ContentFiles')

const getUserDetails = require('./utils/getUserDetails')
// const parseComment = require('./parse-comment')
const parseComment = require('./utils/parse-comment')

const { GIHUB_BOT_NAME } = require('./utils/settings')
const { AllContributorBotError } = require('./utils/errors')

async function processIssueComment({ context, commentReply }) {
const repository = new Repository({
...context.repo(),
github: context.github,
})
const optionsConfig = new OptionsConfig({
repository,
commentReply,
})
await optionsConfig.fetch()

// TODO parse comment and gain intentions
// const { who, contributions } = parseComment(commentBody)
// We had trouble reading your comment. Basic usage:\n\n\`@${GIHUB_BOT_NAME} please add jakebolam for code\`
const who = 'jakebolam'
const contributions = ['code']

async function processAddContributor({
context,
commentReply,
repository,
optionsConfig,
who,
contributions,
}) {
const { name, avatar_url, profile } = await getUserDetails({
github: context.github,
username: who,
Expand Down Expand Up @@ -55,9 +46,7 @@ async function processIssueComment({ context, commentReply }) {
title: `docs: add ${who} as a contributor`,
body: `Adds ${who} as a contributor for ${contributions.join(
', ',
)}.\n\nThis was requested by ${commentReply.replyingToWho()} [in this comment](${
commentReply.replyingToWhere()
})`,
)}.\n\nThis was requested by ${commentReply.replyingToWho()} [in this comment](${commentReply.replyingToWhere()})`,
filesByPath: filesByPathToUpdate,
branchName: `all-contributors/add-${who}`,
})
Expand All @@ -67,6 +56,46 @@ async function processIssueComment({ context, commentReply }) {
)
}

async function processIssueComment({ context, commentReply }) {
const repository = new Repository({
...context.repo(),
github: context.github,
})
const optionsConfig = new OptionsConfig({
repository,
commentReply,
})
await optionsConfig.fetch()

const commentBody = context.payload.comment.body
const parsedComment = parseComment(commentBody)
if (!parsedComment.action) {
commentReply.reply(`I could not determine your intention.`)
commentReply.reply(
`Basic usage: @${GIHUB_BOT_NAME} please add jakebolam for code, doc`,
)
return
}

if (parsedComment.action === 'add') {
await processAddContributor({
context,
commentReply,
repository,
optionsConfig,
who: parsedComment.who,
contributions: parsedComment.contributions,
})
return
}

commentReply.reply(`I'm not sure how to ${parsedComment.action}`)
commentReply.reply(
`Basic usage: @${GIHUB_BOT_NAME} please add jakebolam for code, doc`,
)
return
}

function hasMentionedBotName(context) {
const commentBody = context.payload.comment.body
const hasMentionedBotName = commentBody.includes(GIHUB_BOT_NAME)
Expand Down
128 changes: 88 additions & 40 deletions src/utils/parse-comment/index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,89 @@
// const chrono = require('chrono-node')
//
// const matcher = /^remind @?([^\s]+)(?: to )?(.*)$/
//
// const parser = new chrono.Chrono()
// parser.refiners.push(require('./lib/refiners/start-of-day'))
//
// const options = {
// forwardDate: true,
// startOfDay: 9
// }
//
// module.exports = (input, from) => {
// const match = input.match(matcher)
// if (!match) {
// // This doesn't look like a reminder, so bail early
// return null
// }
//
// // Pull out the initial matches
// let [, who, what] = match
//
// // Use chrono to extract the `when` from the `what`
// const when = parser.parse(what, from, options)
//
// if (when.length < 1) {
// // What kind of reminder doesn't have a date?
// return null
// }
//
// // Remove any time expressions from the `what`
// when.forEach(w => {
// what = what.replace(w.text, '')
// })
//
// // Clean up whitespace and common connecting words
// what = what.trim()
// what = what.replace(/^(to|that) /, '').replace(/ on$/, '')
//
// return {who, what, when: when[0].start.date()}
const nlp = require('compromise')

const validContributionTypes = [
'blog',
'bug',
'code',
'design',
'doc',
'eventOrganizing',
'example',
'financial',
'fundingFinding',
'ideas',
'infra',
'platform',
'plugin',
'question',
'review',
'security',
'talk',
'test',
'tool',
'translation',
'tutorial',
'userTesting',
'video',
]

// TODO
// const contributionTypeMappings = {
// 'event organizing': 'eventOrganizing',
// 'funding finding': 'fundingFinding',
// 'user testing': 'user testing',
// }

const Contributions = {}

validContributionTypes.forEach(type => {
Contributions[type] = 'Contribution'
})

const plugin = {
words: {
...Contributions,
},
patterns: {
// 'add #person for #Contribution': 'AddContributor',
// "i can't (log|sign|get) in to my? #Software": 'LoginIssue'
},
}
nlp.plugin(plugin)

function parseAddComment(doc, action) {
const who = doc.match(`${action} [.]`).data()[0].normal

// TODO: handle plurals (e.g. some said docs)
const contributions = doc
.match('#Contribution')
.data()
.map(data => {
// This removes whitespace, commas etc
return data.normal
})

return {
action: 'add',
who,
contributions,
}
}

function parseComment(message) {
const doc = nlp(message)

if (doc.verbs().data().length === 0) {
return {}
}

const action = doc.verbs().data()[0].normal
if (action === 'add') {
return parseAddComment(doc, action)
}

return {
action,
}
}

module.exports = parseComment
14 changes: 0 additions & 14 deletions src/utils/parse-comment/refiners/start-of-day.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/fixtures/issue_comment.created.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"created_at": "2019-01-10T08:36:52Z",
"updated_at": "2019-01-10T08:36:52Z",
"author_association": "MEMBER",
"body": "@AllContributorsBot please add jakebolam for code"
"body": "@AllContributorsBot please add jakebolam for code, doc and infra"
},
"repository": {
"id": 164268911,
Expand Down
135 changes: 26 additions & 109 deletions test/utils/parse-comment/index.test.js
Original file line number Diff line number Diff line change
@@ -1,111 +1,28 @@
describe('parse-comment', () => {
test('Example test', () => {
expect(true).toBe(true)
const parseComment = require('../../../src/utils/parse-comment')
const { GIHUB_BOT_NAME } = require('../../../src//utils/settings')

describe('parseComment', () => {
test('Basic intent to add', () => {
expect(
parseComment(
`@${GIHUB_BOT_NAME} please add jakebolam for doc, infra and code`,
),
).toEqual({
action: 'add',
who: 'jakebolam',
contributions: ['doc', 'infra', 'code'],
})
})
//
// test('Basic intent to add (with plurals)', () => {
// expect(
// parseComment(
// `@${GIHUB_BOT_NAME} please add jakebolam for docs, infra and code`,
// ),
// ).toEqual({
// action: 'add',
// who: 'jakebolam',
// contributions: ['doc', 'infra', 'code'],
// })
// })
})

// process.env.TZ = 'UTC'
//
// const expect = require('expect')
// const parseReminder = require('..')
//
// // All time expressions will be relative to Wednesday, July 5 at 4:03:02.0 UTC
// const REFERENCE_DATE = new Date(2017, 6, 5, 4, 3, 2, 0)
//
// describe('parse-reminder', () => {
// const examples = {
// 'nothing to see here': null,
//
// 'remind me nope': null,
//
// 'remind me to call the doctor tomorrow': {
// who: 'me', when: new Date(2017, 6, 6, 9, 0, 0, 0), what: 'call the doctor'
// },
//
// 'remind me to send invites at 3pm tomorrow': {
// who: 'me', when: new Date(2017, 6, 6, 15, 0, 0, 0), what: 'send invites'
// },
//
// 'remind me 8/28 to wish Jamie happy birthday': {
// who: 'me', when: new Date(2017, 7, 28, 9, 0, 0, 0), what: 'wish Jamie happy birthday'
// },
//
// 'remind me to wish Jamie happy birthday on 8/28': {
// who: 'me', when: new Date(2017, 7, 28, 9, 0, 0, 0), what: 'wish Jamie happy birthday'
// },
//
// 'remind me in 10 minutes to change the laundry': {
// who: 'me', when: new Date(2017, 6, 5, 4, 13, 2, 0), what: 'change the laundry'
// },
//
// 'remind me tomorrow 10 PM to eat': {
// who: 'me', when: new Date(2017, 6, 6, 22, 0, 0, 0), what: 'eat'
// },
//
// 'remind me on 18 Feb to be alive': {
// who: 'me', when: new Date(2018, 1, 18, 9, 0, 0, 0), what: 'be alive'
// },
//
// 'remind me Tuesday to watch pogo': {
// who: 'me', when: new Date(2017, 6, 11, 9, 0, 0, 0), what: 'watch pogo'
// },
//
// 'remind me Tuesday': {
// who: 'me', when: new Date(2017, 6, 11, 9, 0, 0, 0), what: ''
// },
//
// 'remind me to go to the dentist on Aug 4 at 3': {
// // FIXME: add refiner to prefer work hours for times without am/pm
// who: 'me', when: new Date(2017, 7, 4, 3, 0, 0, 0), what: 'go to the dentist'
// },
//
// 'remind me to followup with Kathy in 2 weeks': {
// who: 'me', when: new Date(2017, 6, 19, 9, 0, 0, 0), what: 'followup with Kathy'
// },
//
// 'remind me in 2 weeks to followup with Kathy': {
// who: 'me', when: new Date(2017, 6, 19, 9, 0, 0, 0), what: 'followup with Kathy'
// },
//
// 'remind me at 4:00 to check in with Dan': {
// // FIXME: fix future refiner to work with times later in the day
// who: 'me', when: new Date(2017, 6, 5, 4, 0, 0, 0), what: 'check in with Dan'
// },
//
// 'remind me 10am to go to work': {
// // FIXME: fix future refiner to work with times later in the day
// who: 'me', when: new Date(2017, 6, 5, 10, 0, 0, 0), what: 'go to work'
// },
//
// 'remind me that the oil needs changed on Oct 4': {
// who: 'me', when: new Date(2017, 9, 4, 9, 0, 0, 0), what: 'the oil needs changed'
// },
//
// 'remind me next Friday to open the pod bay doors': {
// who: 'me', when: new Date(2017, 6, 14, 9, 0, 0, 0), what: 'open the pod bay doors'
// },
//
// 'remind me tomorrow at noon to buy the new iPhone': {
// who: 'me', when: new Date(2017, 6, 6, 12, 0, 0, 0), what: 'buy the new iPhone'
// },
//
// 'remind someone to have coffee tomorrow at noon': {
// who: 'someone', when: new Date(2017, 6, 6, 12, 0, 0, 0), what: 'have coffee'
// },
//
// 'remind @meaku to have coffee tomorrow at noon': {
// who: 'meaku', when: new Date(2017, 6, 6, 12, 0, 0, 0), what: 'have coffee'
// },
//
// 'remind @probot/everyone to have coffee tomorrow at noon': {
// who: 'probot/everyone', when: new Date(2017, 6, 6, 12, 0, 0, 0), what: 'have coffee'
// }
// }
//
// // eslint-disable-next-line guard-for-in
// for (const example in examples) {
// it(`parses "${example}"`, () => {
// expect(parseReminder(example, REFERENCE_DATE)).toEqual(examples[example])
// })
// }
// })
Loading

0 comments on commit 1cacd88

Please # to comment.