1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | _ = require 'underscore'
|
33 | Adapters = require "./adapters"
|
34 | Config = require "./config"
|
35 | Github = require "./github"
|
36 | Reminders = require "./reminders"
|
37 | Utils = require "./utils"
|
38 |
|
39 | class 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 |
|
182 | module.exports = GithubBot
|