Created
February 17, 2015 08:51
-
-
Save manaten/e06acd4f65f6e9a93146 to your computer and use it in GitHub Desktop.
gheのissuecommentを各人にslackDMで通知
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Description: | |
# Watch and emit github events. | |
# see https://developer.github.com/v3/activity/events/types/ | |
# | |
# Dependencies: | |
# cron, request, config, lodash | |
# | |
# Configuration: | |
# config.github.organizations 監視したいgheのorg名をキーとするオブジェクト | |
# ex) github: | |
# organizations: | |
# xxxx: | |
# channel: 'xxxx' | |
# | |
# Commands: | |
# None | |
# | |
cron = require('cron').CronJob | |
request = require 'request' | |
config = require 'config' | |
_ = require 'lodash' | |
module.exports = (robot) -> | |
new cron | |
cronTime: '0 * * * * *' | |
start: true | |
timeZone: "Asia/Tokyo" | |
onTick: -> | |
_.keys(config.github.organizations).forEach (orgName) -> | |
request | |
uri: "xxxx/#{orgName}/events" | |
json: true | |
method: 'GET' | |
timeout: 30 * 1000 | |
headers: | |
Authorization: "token #{process.env.GHE_TOKEN}" | |
'If-None-Match': robot.brain.data.gheFeed?[orgName]?.etag or '' | |
, (error, response, events) -> | |
return console.log error if error | |
return if response.statusCode is 304 | |
lastGetMS = robot.brain.data.gheFeed?[orgName]?.lastGetMS or new Date().getTime() | |
events | |
.filter (event) -> | |
new Date(event.created_at).getTime() > lastGetMS | |
.forEach (event) -> | |
robot.logger.info "emit github:#{event.type}" | |
# イベントハンドラのうちどれかが行儀悪いとエラーがここまで飛んでしまうため、try-catch | |
try | |
robot.emit "github:#{event.type}", event.payload | |
catch e | |
robot.logger.warn e | |
if events[0]?.created_at | |
lastGetMS = new Date(events[0]?.created_at).getTime() | |
else | |
lastGetMS = new Date().getTime() | |
robot.brain.data.gheFeed = {} unless robot.brain.data.gheFeed? | |
robot.brain.data.gheFeed[orgName] = | |
lastGetMS: lastGetMS | |
etag: response.headers.etag | |
robot.brain.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Description: | |
# Notify github issue comments. | |
# | |
# Dependencies: | |
# ghe-event.coffee, config | |
# | |
# Configuration: | |
# config.githubSlackUser github -> slack のユーザー名対応表 | |
# ex) | |
# githubSlackUser: | |
# 'manaten': 'manaten'd | |
# | |
# Commands: | |
# None | |
config = require 'config' | |
module.exports = (robot) -> | |
sendDM = (githubUserName, message) -> | |
slackUserName = config.githubSlackUser?[githubUserName] | |
return unless slackUserName? | |
userId = robot.adapter.client.getUserByName(slackUserName)?.id | |
return unless userId? | |
if robot.adapter.client.getDMByID(userId)? | |
robot.send {room: slackUserName}, message | |
else | |
robot.adapter.client.openDM userId | |
# openをハンドルする手段がなさそうなので、仕方なくsetTimeout | |
setTimeout => | |
robot.send {room: slackUserName}, message | |
, 1000 | |
sendPRComment = (issueUser, commentUser, commentBody, commentUrl) -> | |
message = ":octocat: #{commentUser}: #{commentUrl}\n" + commentBody.replace /\r\n/, '\n' | |
unless commentUser is issueUser | |
sendDM issueUser, message | |
# @ついてるそれぞれのユーザーにも送る | |
(commentBody.match(/@[-_\w]+/g) or []).forEach (matchedUser) -> | |
sendDM matchedUser.replace(/^@/, ''), message | |
robot.on 'github:IssueCommentEvent', (entry) -> | |
return if entry.action isnt 'created' | |
return if !process.env.PRODUCTION? | |
issueUser = entry.issue.user.login | |
commentUser = entry.comment.user.login | |
commentBody = entry.comment.body | |
commentUrl = entry.comment.html_url | |
sendPRComment issueUser, commentUser, commentBody, commentUrl | |
robot.on 'github:PullRequestReviewCommentEvent', (entry) -> | |
return if entry.action isnt 'created' | |
return if !process.env.PRODUCTION? | |
issueUser = entry.pull_request.user.login | |
commentUser = entry.comment.user.login | |
commentBody = entry.comment.body | |
commentUrl = entry.comment.html_url | |
sendPRComment issueUser, commentUser, commentBody, commentUrl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment