blib/lib/Hubot/Scripts/standup.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 181 CC 15 SCAR 32.2
CC
15
Cov
0%
load
56.0
postYammer
30.0
nextPerson
12.0
CRAP 25.1
1 package Hubot::Scripts::standup;
2 {
3 $Hubot::Scripts::standup::VERSION = '0.0.1';
4 }
5  
6 # ABSTRACT: Agile standup bot ala tender
7 use strict;
8 use warnings;
9 use List::Util 'shuffle';
10 use DateTime;
11 use JSON::XS;
12  
13 sub load {
14 my ( $class, $robot ) = @_;
15 $robot->respond(
16 qr/(?:cancel|stop) standup *$/i,
17 sub {
18 my $msg = shift;
19 delete $robot->brain->{data}{standup}{$msg->message->user->{room}};
20 $msg->send("Standup cancelled");
21 }
22 );
23  
24 $robot->respond(
25 qr/standup for (.*) *$/i,
26 sub {
27 my $msg = shift;
28 my $room = $msg->message->user->{room};
29 my $group = trim($msg->match->[0]);
30 if ($robot->brain->{data}{standup}{$room}) {
31 $msg->send("The standup for " . $robot->brain->{data}{standup}{$room}{$group} . " is in progress! Cancel it first with 'cancel standup'");
32 return;
33 }
34  
35 my @attendees;
36 while (my ($key, $user) = each %{ $robot->brain->{data}{users} }) {
37 my @roles = @{ $user->{roles} ||= [] };
38 if (grep { m/an? $group member/ } @roles or grep { m/a member of $group/ } @roles) {
39 push @attendees, $user;
40 }
41 }
42  
43 if (@attendees) {
44 $robot->brain->{data}{standup}{$room} = {
45 group => $group,
46 start => DateTime->now->epoch,
47 attendees => \@attendees,
48 remaining => [shuffle @attendees],
49 log => [],
50 };
51  
52 my $who = join ', ', map { $_->{name} } @attendees;
53 $msg->send("OK, let's start the standup: $who");
54 nextPerson($robot, $room, $msg);
55 }
56 }
57 );
58  
59 $robot->respond(
60 qr/(?:next(?: person)?|done)/i,
61 sub {
62 my $msg = shift;
63 return unless $robot->brain->{data}{standup}{$msg->message->user->{room}};
64 nextPerson($robot, $msg->message->user->{room}, $msg);
65 }
66 );
67  
68 $robot->respond(
69 qr/standup\?? *$/i,
70 sub {
71 my $msg = shift;
72 $msg->send((
73 "<who> is a member of <team> - tell hubot who is the member of <team>'s standup",
74 "standup for <team> - start the standup for <team>",
75 "cancel standup - cancel the current standup",
76 "next - say when your updates for the standup is done",
77 ));
78 }
79 );
80  
81 $robot->catchAll(
82 sub {
83 my $msg = shift;
84 my $standup = $robot->brain->{data}{standup};
85 return unless $standup->{$msg->message->user->{room}};
86 push @{ $standup->{$msg->message->user->{room}}{log} ||= [] }, {
87 message => $msg->message,
88 time => DateTime->now->epoch,
89 }
90 }
91 );
92  
93 $robot->respond(
94 qr/post (.*) standup logs? to (\d*) *$/i,
95 sub {
96 my $msg = shift;
97 my ($group, $group_id) = @{ $msg->match };
98 warn "$group, $group_id";
99 $robot->brain->{data}{yammerGroups}{$group} = $group_id;
100 if (my $buff = $robot->brain->{data}{tempYammerBuffer}{group}) {
101 postYammer($robot, $group, $msg->message->user->{room}, $msg, $buff);
102 delete $robot->brain->{data}{tempYammerBuffer}{$group};
103 }
104 }
105 );
106  
107 ### post yammer
108 $robot->brain->on(
109 'standupLog',
110 sub {
111 my ($e, $group, $room, $response, $logs) = @_;
112 postYammer($robot, $group, $room, $response, $logs);
113 }
114 );
115 }
116  
117 sub trim {
118 my $str = shift;
119 $str =~ s/(^\s+|\s+$)//g;
120 return $str;
121 }
122  
123 sub nextPerson {
124 my ($robot, $room, $msg) = @_;
125 my $standup = $robot->brain->{data}{standup}{$room};
126 if (scalar @{ $standup->{remaining} } == 0) {
127 my $duration = DateTime->now - DateTime->from_epoch(epoch => $standup->{start});
128 my $howlong = $duration->minutes
129 ? sprintf("%s minutes and %s seconds", $duration->minutes, $duration->seconds)
130 : sprintf("%s seconds", $duration->seconds);
131 $msg->send("All done! Standup was $howlong");
132 $robot->brain->emit('standupLog', $standup->{group}, $room, $msg, $standup->{log});
133 delete $robot->brain->{data}{standup}{$room};
134 } else {
135 $standup->{current} = shift @{ $standup->{remaining} };
136 $msg->send($standup->{current}{name} . ' your turn');
137 }
138 }
139  
140 sub postYammer {
141 my ($robot, $group, $room, $response, $logs) = @_;
142 my $group_id = getYammerGroup($robot, $group);
143 if (!$group_id) {
144 $response->send("Tell me which Yammer group to post archives. Say 'hubot post $group standup logs to <GROUP_ID>'. Use Group ID 0 if you don't need archives.");
145 $robot->brain->{data}{tempYammerBuffer}{$group} = $logs;
146 } elsif ($group_id == 0) {
147 # do nothing
148 } else {
149 my $body = makeBody($robot, $group, $logs);
150 $response->http('https://www.yammer.com/api/v1/messages.json')
151 ->header({
152 Authorization => "Bearer $ENV{HUBOT_STANDUP_YAMMER_TOKEN}",
153 Accept => 'application/json',
154 })
155 ->query({
156 group_id => $group_id,
157 body => $body,
158 topic0 => 'standup',
159 })
160 ->post(
161 sub {
162 my ($body, $hdr) = @_;
163 if ($hdr->{Status} !~ m/^2/) {
164 $response->send("Posting to the group $group_id FAILED: $hdr->{Reason}");
165 } else {
166 my $data = decode_json($body);
167 if ($data->{messages}) {
168 $response->send("Posted to Yammer: " . $data->{messages}[0]{web_url});
169 } else {
170 $response->send("Posting to the group $group_id FAILED: $body");
171 }
172 }
173 }
174 );
175 }
176 }
177  
178 sub getYammerGroup {
179 my ($robot, $group) = @_;
180 $robot->brain->{data}{yammerGroups} ||= {};
181 return $robot->brain->{data}{yammerGroups}{$group};
182 }
183  
184 sub makeBody {
185 my ($robot, $group, $logs) = @_;
186 my $dt = DateTime->now;
187 my $body = "Standup log for $group: $dt\n======================\n";
188 my $prev = '';
189 for my $log (@$logs) {
190 if ($log->{message}{user}{name} ne $prev) {
191 my $name = $log->{message}{user}{name};
192 if ($log->{message}{user}{yammerName}) {
193 $name = "@" . $log->{message}{user}{yammerName};
194 }
195 $body .= "\n$name:\n";
196 }
197 $body .= $log->{message}{text} . "\n";
198 $prev = $log->{message}{user}{name};
199 }
200  
201 return $body;
202 }
203  
204 1;
205  
206  
207 =pod
208  
209 =head1 NAME
210  
211 Hubot::Scripts::standup - Agile standup bot ala tender
212  
213 =head1 SYNOPSIS
214  
215 hubot standup? - show help for standup
216  
217 =head1 SETUP
218  
219 First, you tell hubot who is a member for a particular team for the standup, using the C<roles> commands with "(who) is a (team) member".
220  
221 Let's take "engineering" team for example.
222  
223 miyagawa: hubot miyagawa is an engineering member
224 hubot: Ok, miyagawa is an engineering member
225 miyagawa: hubot john is an engineering member
226 hubot: Ok, john is an engineering member
227 miyagawa: hubot davidlee is an engineering member
228 hubot: Ok, davidlee is an engineering member
229  
230 You can create as many teams as you want.
231  
232 =head2 START THE STANDUP
233  
234 Hubot won't schedule the standup for you (yet), you have to start it by yourself when it's time.
235  
236 miyagawa: hubot standup for engineering
237 hubot: Ok, let's start the standup: miyagawa, john, davidlee
238 hubot: john: your turn
239  
240 Hubot remembers who should participate the standup, and will tell whose turn is the next. Tell what you did yesterday, will do today, anything blocked. and say "next" (or "done") when you're done.
241  
242 john: Done some pretty nice hack yesterday.
243 john: I will work on another cool stuff today.
244 john: I'm not blocked
245 john: hubot next
246 hubot: davidlee: your turn
247  
248 When the user is offline or away for a second, tell hubot to skip the user.
249  
250 miyagawa: hubot skip davidlee
251 hubot: Will skip davidlee
252 hubot: miyagawa: your turn
253  
254 Once the last user is done, hubot will tell you how long the standup was.
255  
256 miyagawa: I'm working on some nice stuff, and will continue doing so today.
257 miyagawa: hubot next
258 hubot: All done! Standup was 5 minutes and 24 seconds.
259  
260 =head2 CONFIGURATION
261  
262 =over
263  
264 =item HUBOT_STANDUP_YAMMER_TOKEN
265  
266 =back
267  
268 the bot will post the standup archive to Yammer. You need to set a valid Yammer OAuth2 token to C<HUBOT_STANDUP_YAMMER_TOKEN> environment variable.
269  
270 Here's how to get a valid Yammer OAuth2 token with the standard OAuth2 authorization flow.
271  
272 See L<Yammer documentation|https://developer.yammer.com/api/oauth2.html> for more details.
273  
274 =over
275  
276 =item * Register a new application on Yammer at `https://www.yammer.com/<DOMAIN>/client_applications/new`. Leave the callback URLs empty
277  
278 =item * Take notes of your `consumer_key` and `consumer_secret`
279  
280 =item * Make a new bot user on Yammer (optional). This is the user who will post archives as.
281  
282 =item * Sign in as the new bot user on Yammer if necessary
283  
284 =item * Go to `https://www.yammer.com/dialog/oauth?client_id=<consumer_key>`
285  
286 =item * There's an authorization dialog. Authorize the app
287  
288 =item * Look at the URL bar and there's a `code=<CODE>` query parameter in there, copy that.
289  
290 =item * `curl https://www.yammer.com/oauth2/access_token?code=<CODE>&client_id=<consumer_key>&client_secret=<consumer_secret>`
291  
292 =item * you'll get a big JSON that contains `access_token` -> `token`
293  
294 =back
295  
296 Now set the token to C<HUBOT_STANDUP_YAMMER_TOKEN> and Hubot will ask which group ID the log should be posted to. Use the group ID 0 to turn off the feature for a group.
297  
298 =head1 SEE ALSO
299  
300 L<https://github.com/miyagawa/hubot-standup>
301  
302 =head1 AUTHOR
303  
304 Hyungsuk Hong <hshong@perl.kr>
305  
306 =head1 COPYRIGHT AND LICENSE
307  
308 This software is copyright (c) 2012 by Hyungsuk Hong.
309  
310 This is free software; you can redistribute it and/or modify it under
311 the same terms as the Perl 5 programming language system itself.
312  
313 =cut