const Github = require('../services/github')
const Slack = require('../services/slack')
const User = require('../services/user')

let lastRun = new Date(Date.now()).toISOString()
const GitHubActivityToSlack = () => {
  return Github.getRepoActivity().then(activies => {
    return activies.filter(activity => {
      return (['IssueCommentEvent', 'PullRequestReviewCommentEvent'].includes(activity.type) 
              && activity.payload.comment.body.includes('@') 
              && lastRun < activity.created_at)
    })
  }).then(activies => {
    if (activies.length) {
      lastRun = activies[0].created_at
    }
    return activies.filter(activity => activity.payload.comment.body.indexOf('@') !== -1)
  }).then(activies => {
    const replies = {}
    const mectionedUsers = []
    activies.map(activity => {
      const slackActor = User.getUserIdByServiceId('github', activity.actor)
      const caller = slackActor && `<@${slackActor}>` || activity.actor
      const commentBody = activity.payload.comment.body.replace(/@(\w+)/g, (text, match) => {
        const mentionedUser = User.getUserIdByServiceId('github', match)
        mentionedUser && mectionedUsers.push(mentionedUser)
        return mentionedUser && `<@${mentionedUser}>` || text
      })
      const content = `${caller} mentioned you on ${activity.payload.comment.html_url}\n${commentBody}`
      mectionedUsers.forEach(slackUserId => {
        if (!replies[slackUserId]) {
          replies[slackUserId] = []
        }
        replies[slackUserId].push(content)
      })
    })
    return replies
  }).then(userContents => {
    Object.keys(userContents).forEach((slackUserId) => {
      Slack.sendMessage(slackUserId, userContents[slackUserId].join('\n'))
      Slack.sendMessage(process.env.SLACK_REPORT_CHANNEL_ID, userContents[slackUserId].join('\n'))
    })
  }).catch(err => console.log(err))
}

module.exports = GitHubActivityToSlack

@@ -786,35 +786,117 @@
     
-activies.map(activity
+const replaceGitHubUserIntoSlackUser = (body) =%3E %7B%0A      return body.replace(/@(%5Cw+)/g, (text, match)
  =%3E %7B%0A
+  
     
@@ -903,25 +903,28 @@
   const 
-slackActo
+mentionedUse
 r = User
@@ -959,147 +959,212 @@
 b', 
-activity.actor)%0A      const caller = slackActor && %60%3C@$%7BslackActor%7D%3E%60 %7C%7C activity.actor
+match)%0A        mentionedUser && mectionedUsers.push(mentionedUser)%0A        return mentionedUser && %60%3C@$%7BmentionedUser%7D%3E%60 %7C%7C text%0A      %7D)
 %0A   
+ %7D%0A
    
+ 
 const 
-commentB
+replacePRNumberIntoUrl = (b
 ody
+)
  =
- activity.payload.comment.
+%3E %7B%0A      return 
 body
@@ -1165,36 +1165,36 @@
 n body.replace(/
-@(%5Cw
+#(%5Cd
 +)/g, (text, mat
@@ -1182,37 +1182,40 @@
 (%5Cd+)/g, (text, 
-match
+prNumber
 ) =%3E %7B%0A        c
@@ -1217,202 +1217,409 @@
     
-const mentionedUser = User.getUserIdByServiceId('github', match)%0A        mentionedUser && mectionedUsers.push(mentionedUser)%0A        return mentionedUser && %60%3C@$%7BmentionedUser%7D%3E%60 %7C%7C text%0A      %7D
+return %60https://github.com/$%7Bprocess.env.GITHUB_OWNER%7D/$%7Bprocess.env.GITHUB_REPO%7D/$%7BprNumber%7D%60%0A      %7D)%0A    %7D%0A%0A    activies.map(activity =%3E %7B%0A      const slackActor = User.getUserIdByServiceId('github', activity.actor)%0A      const caller = slackActor && %60%3C@$%7BslackActor%7D%3E%60 %7C%7C activity.actor%0A      const commentBody = replacePRNumberIntoUrl(replaceGitHubUserIntoSlackUser(activity.payload.comment.body)
 )%0A  
@@ -1637,16 +1637,31 @@
 ontent =
+ commentBody %7C%7C
  %60$%7Bcall

Format pr number into url