UNPKG

7.75 kBtext/coffeescriptView Raw
1# Description:
2# A hubot script to list and recurrently remind you about open pull requests.
3# Optionally receive direct messages when you are assigned to a pull
4# request in your organization or for a specific repo or set of repos.
5#
6# Dependencies:
7# - coffeescript
8# - cron
9# - octokat
10# - moment
11# - underscore
12# - fuse.js
13#
14# Configuration:
15# HUBOT_GITHUB_TOKEN - Github Application Token
16# HUBOT_GITHUB_WEBHOOK_SECRET - Optional, if you are using webhooks and have a secret set this for additional security checks on payload delivery
17# HUBOT_GITHUB_URL - Set this value if you are using Github Enterprise default: `https://api.github.com`
18# HUBOT_GITHUB_ORG - Github Organization Name (the one in the url)
19# HUBOT_GITHUB_REPOS_MAP (format: "{"web":["frontend","web"],"android":["android"],"ios":["ios"],"platform":["web"]}"
20#
21# Commands:
22# hubot github open [for <user>] - Shows a list of open pull requests for the repo of this room [optionally for a specific user]
23# hubot github remind hh:mm - I'll remind about open pull requests in this room at hh:mm every weekday.
24# hubot github list reminders - See all pull request reminders for this room.
25# hubot github reminders in every room - Be nosey and see when other rooms have their reminders set
26# hubot github delete hh:mm reminder - If you have a reminder at hh:mm, I'll delete it.
27# hubot github delete all reminders - Deletes all reminders for this room.
28#
29# Author:
30# ndaversa
31
32_ = require 'underscore'
33Adapters = require "./adapters"
34Config = require "./config"
35Github = require "./github"
36Reminders = require "./reminders"
37Utils = require "./utils"
38
39class GithubBot
40
41 constructor: (@robot) ->
42 return new GithubBot @robot unless @ instanceof GithubBot
43 Utils.robot = @robot
44 @reminders = new Reminders @robot, "github-reminders", (room) ->
45 Github.PullRequests.openForRoom room
46 @webhook = new Github.Webhook @robot
47 switch @robot.adapterName
48 when "slack"
49 @adapter = new Adapters.Slack @robot
50 else
51 @adapter = new Adapters.Generic @robot
52
53 @registerWebhookListeners()
54 @registerEventListeners()
55 @registerRobotResponses()
56
57 send: (context, message) ->
58 @adapter.send context, message
59
60 registerWebhookListeners: ->
61 disableDisclaimer = """
62 If you wish to stop receiving notifications for pull request assignments, reply with:
63 > github disable notifications
64 """
65
66 @robot.on "GithubPullRequestAssigned", (pr, sender) =>
67 @robot.logger.debug "Sending PR assignment notice to #{pr.assignee.name}, sender is #{sender?.name}"
68 @adapter.dm pr.assignee,
69 text: """
70 You have just been assigned to a pull request #{if sender then "by #{sender.name}" else ""}
71 """
72 author: sender
73 footer: disableDisclaimer
74 attachments: [ pr.toAttachment() ]
75
76 registerEventListeners: ->
77 @robot.on "GithubPullRequestsOpenForRoom", (prs, room) =>
78 if prs.length is 0
79 message = "No matching pull requests found"
80 else
81 attachments = (pr.toAttachment() for pr in prs)
82 message = attachments: attachments
83 @send message: room: room, message
84
85 registerRobotResponses: ->
86
87 @robot.respond /(?:github|gh|git) (allow|start|enable|disallow|disable|stop)( notifications)?/i, (msg) =>
88 [ __, state ] = msg.match
89 switch state
90 when "allow", "start", "enable"
91 @adapter.enableNotificationsFor msg.message.user
92 @send msg, """
93 Github pull request notifications have been *enabled*
94
95 You will start receiving notifications when you are assigned to a pull request on Github
96
97 If you wish to _disable_ them just send me this message:
98 > github disable notifications
99 """
100 when "disallow", "stop", "disable"
101 @adapter.disableNotificationsFor msg.message.user
102 @send msg, """
103 Github pull request notifications have been *disabled*
104
105 You will no longer receive notifications when you are assigned to a pull request on Github
106
107 If you wish to _enable_ them again just send me this message:
108 > github enable notifications
109 """
110
111 @robot.respond /(?:github|gh|git) delete all reminders/i, (msg) =>
112 remindersCleared = @reminders.clearAllForRoom(Utils.findRoom(msg))
113 @send msg, """
114 Deleted #{remindersCleared} reminder#{if remindersCleared is 1 then "" else "s"}.
115 No more reminders for you.
116 """
117
118 @robot.respond /(?:github|gh|git) delete ([0-5]?[0-9]:[0-5]?[0-9]) reminder/i, (msg) =>
119 [__, time] = msg.match
120 remindersCleared = @reminders.clearForRoomAtTime(Utils.findRoom(msg), time)
121 if remindersCleared is 0
122 @send msg, "Nice try. You don't even have a reminder at #{time}"
123 else
124 @send msg, "Deleted your #{time} reminder"
125
126 @robot.respond /(?:github|gh|git) remind(?:er)? ((?:[01]?[0-9]|2[0-4]):[0-5]?[0-9])$/i, (msg) =>
127 [__, time] = msg.match
128 room = Utils.findRoom(msg)
129 @reminders.save room, time
130 @send msg, "Ok, from now on I'll remind this room about open pull requests every weekday at #{time}"
131
132 @robot.respond /(?:github|gh|git) list reminders$/i, (msg) =>
133 reminders = @reminders.getForRoom(Utils.findRoom(msg))
134 if reminders.length is 0
135 @send msg, "Well this is awkward. You haven't got any github reminders set :-/"
136 else
137 @send msg, "You have pull request reminders at the following times: #{_.map(reminders, (reminder) -> reminder.time)}"
138
139 @robot.respond /(?:github|gh|git) reminders in every room/i, (msg) =>
140 reminders = @reminders.getAll()
141 if reminders.length is 0
142 @send msg, "No, because there aren't any."
143 else
144 @send msg, """
145 Here's the reminders for every room: #{_.map(reminders, (reminder) -> "\nRoom: #{reminder.room}, Time: #{reminder.time}")}
146 """
147
148 @robot.respond /(github|gh|git) help/i, (msg) =>
149 @send msg, """
150 I can remind you about open pull requests for the repo that belongs to this channel
151 Use me to create a reminder, and then I'll post in this room every weekday at the time you specify. Here's how:
152
153 #{@robot.name} github open [for <user>] - Shows a list of open pull requests for the repo of this room [optionally for a specific user]
154 #{@robot.name} github reminder hh:mm - I'll remind about open pull requests in this room at hh:mm every weekday.
155 #{@robot.name} github list reminders - See all pull request reminders for this room.
156 #{@robot.name} github reminders in every room - Be nosey and see when other rooms have their reminders set
157 #{@robot.name} github delete hh:mm reminder - If you have a reminder at hh:mm, I'll delete it.
158 #{@robot.name} github delete all reminders - Deletes all reminders for this room.
159 """
160
161 @robot.respond /(?:github|gh|git) (?:prs|open)(?:\s+(?:for|by)\s+(?:@?)(.*))?/i, (msg) =>
162 [__, who] = msg.match
163
164 if who is 'me'
165 who = msg.message.user?.name?.toLowerCase()
166
167 if who?
168 who = @robot.brain.userForName who
169 who = who.name
170
171 Github.PullRequests.openForRoom(msg.message.room, who)
172
173 @robot.hear /(?:https?:\/\/github\.com\/([a-z0-9-]+)\/)([a-z0-9-_.]+)\/pull\/(\d+)\/?\s*/i, (msg) =>
174 [ url, org, repo, number ] = msg.match
175 Github.PullRequest.fromUrl("#{Config.github.url}/repos/#{org}/#{repo}/pulls/#{number}")
176 .then (pr) =>
177 @robot.emit "JiraFindTicketMatches", "#{pr.title} #{pr.body}", (matches) =>
178 if matches
179 msg.match = _(matches).unique()
180 @robot.emit "JiraPrepareResponseForTickets", msg
181
182module.exports = GithubBot