]> snippets.scripts.mit.edu Git - Scripts/git/.git/blame - rt/BarnOwl/lib/BarnOwl/Module/RT.pm
BarnOwl::Module::RT: Launch rt asynchronously, so we don’t block the UI
[Scripts/git/.git] / rt / BarnOwl / lib / BarnOwl / Module / RT.pm
CommitLineData
83cedfd6
PW
1use warnings;
2use strict;
3
4=head1 NAME
5
6BarnOwl::Module::Rt
7
8=head1 DESCRIPTION
9
10Foo
11
12=cut
13
14package BarnOwl::Module::RT;
e09e9b71 15use AnyEvent::Util qw(run_cmd);
83cedfd6
PW
16use Text::ParseWords;
17
18our $VERSION = '1.0.1';
19
20my %queuemap;
21my %commands = (
22 "((?:set|add|del)\\s.*)", "edit ticket/\$t \$1",
23 "status\\s(deleted|resolved|rejected|open|new|waiting)", "edit ticket/\$t set status=\$1",
24 "(d|del|delete)", "edit ticket/\$t set status=deleted",
25 "(r|res|resolve)", "edit ticket/\$t set status=resolved",
83ebffa9 26 "(rej|reject|rejected)", "edit ticket/\$t set status=rejected",
83cedfd6
PW
27 "show", "show -l ticket/\$t/history",
28 "show (\\d+)", "show -l ticket/\$1/history",
29 "list", 'rt list -o +Created "((Status=new or Status=stalled or Status=open) and (Queue=\'$q\'))"',
30 "list (\\w+)", 'rt list -o +Created "((Status=new or Status=stalled or Status=open) and (Queue=\'\$1\'))"',
31 "merge (\\d+)", "rt merge \$t \$1",
32 "(take|untake|steal)", "rt \$1 \$t",
33 "(?:owner|give)\\s(\\w+)", "edit ticket/\$t set owner=\$1",
34 );
35
36
37
38my $cfg = BarnOwl::get_config_dir();
39my $file_path = "$cfg/rtqueuemap";
40if(-r "$file_path") {
41 open(my $fh, "<:encoding(UTF-8)", "$file_path") or die("Unable to read $file_path:$!\n");
42 while(defined(my $line = <$fh>)) {
43 next if $line =~ /^\s+#/;
44 next if $line =~ /^\s+$/;
45 my ($class, $q) = quotewords('\s+', 0, $line);
46 $queuemap{lc($class)} = $q;
47 }
48 close($fh);
49}
50
0dd4bf77 51$file_path = "$cfg/rtcommands";
83cedfd6
PW
52if(-r "$file_path") {
53 open(my $fh, "<:encoding(UTF-8)", "$file_path") or die("Unable to read $file_path:$!\n");
54 while(defined(my $line = <$fh>)) {
55 next if $line =~ /^\s+#/;
56 next if $line =~ /^\s+$/;
57 my ($match, $command) = quotewords('\s+', 0, $line);
58 $commands{$match} = $command;
59 }
60 close($fh);
61}
62
63
64
65sub cmd_rt{
66 shift @_;
67 my $args = join(' ', @_);
68 my $m = owl::getcurmsg();
69 my ($ticket) = $m->instance =~ m/^\D*(\d{7})\D*$/;
70 my ($class) = $m->class =~ /^(?:un)*(.+?)(?:[.]d)*$/i;
71 my $queue = $queuemap{$class};
72
73 for my $key (keys %commands)
74 {
75 my $value = $commands{$key};
76 if($args =~ m/^\s*$key\s*$/){
77 my $match = $value;
78 my @numargs = @+;
79 #my $match = qr/\Q$key\E/
80 if($value =~ m/\$t/){
81 if(!$ticket){
82 BarnOwl::error("Command 'rt " . $args . "' requires a message with ticket number selected");
83 return;
84 }
85 $match =~ s/\$t/$ticket/;
86 }
87 if($value =~ m/\$q/){
88 if(!$queue){
89 BarnOwl::error("Command 'rt " . $args . "' requires a class in rtqueuemap selected");
90 return;
91 }
92 $match =~ s/\$q/$queue/;
93 }
94 for my $digit ($value =~ m/\$(\d)/g){
95 $args =~ m/^\s*$key\s*$/;
96 my $replace = substr($args, $-[$digit], $+[$digit] - $-[$digit]);
97 $match =~ s/\$$digit/$replace/;
98 }
99 return run_rt_command( quotewords('\s+', 0, $match) );
100 }
101 }
102
103 BarnOwl::error("No Matching RT command found for: '" . $args . "'" );
104 return;
105}
106
107sub run_rt_command{
e09e9b71
AK
108 my $out;
109 run_cmd(
110 ["athrun", "tooltime", "rt", @_],
111 "<", "/dev/null",
112 ">", \$out,
113 "2>", \$out,
114 )->cb(sub {
115 my $ret = shift->recv;
116 $ret == 0 or die("RT returned $ret");
117 if (($out =~ tr/\n//) == 1) {
118 BarnOwl::message($out);
119 } else {
120 BarnOwl::popless_text($out);
121 }
122 });
83cedfd6
PW
123}
124
125BarnOwl::new_command("rt",
126 \&cmd_rt,
127 {
128 summary => "rt commands in barnowl",
129 usage => "rt <args>",
130 description => <<END_DESCR
131
132Examples:
133 rt [set|add|del] <args> - runs rt (set|add|del) with relevent args - Dangerous if not careful
134 rt [d|del|delete] - mark a ticket deleted
135 rt [r|res|resolve] - mark a ticked resolved
83ebffa9 136 rt [rej|reject|rejected] - mark a ticked rejected
83cedfd6
PW
137 rt status [deleted|resolved|new|open|waiting|rejected] - set status of a ticket
138 rt show - show detailed history of selected ticket
139 rt show <ticket> - show history of <ticket>
140 rt list - list open tickets of current queue
141 rt list <queue> - lists open tickets of <queue>
142 rt merge <ticket> - merges current ticket with <ticket>
143 rt [take|untake|steal] - takes, untakes, or steals ticket
144 rt [owner|give] <user> - gives selected ticket to <user>
145
146config:
147 Go to help.mit.edu to set you rt password
148
149 In ~\/.rtrc add the following lines:
150 user <username>
151 passwd <password>
152
153
154rtqueuemap:
155 ~\/.owl\/rtqueuemap is a list of queues in the form of
156 class queue
157 which is used for the queue in commands like the rt list function which select the current queue
158 help "Some Help Queue"
159rtcommands:
160 ~\/.owl\/rtcommands is a file where you can put custom
161 commands to map the barnowl rt module with the rt command
162 line tool
163
164 It is a good place to put custom queries which will be used frequently.
165 Examples:
0dd4bf77 166 "list-owner (\\w+)" "rt list -o +Created \"((Status=new or Status=stalled or Status=open) and (Queue='\$q') and 'Owner='\$1')\""
83cedfd6
PW
167
168 \$t is the current ticket
169 \$q is the current queue
170 \$[digit] matches control groups in the first reg-exp
171END_DESCR
172 });
173
174
175sub cmd_rt_reply {
176 my $cmd = shift;
177 my $type = "comment";
178 if ($cmd eq "rt-reply"){
179 $type = "correspond";
180 }
181
182 my $m = owl::getcurmsg();
183 my ($ticket) = $m->instance =~ m/^\D*(\d{7})\D*$/;
184 if (!$ticket){
185 BarnOwl::error("Command: '" . $cmd . "' requires a message with a ticket number selected");
186 return;
187 }
188
189 if(@_) {
190 return run_rt_command("rt", $type, "-m", join(" ", @_), $ticket);
191 }
192 return BarnOwl::start_edit_win($cmd . " ticket " . $ticket, sub {run_rt_command("rt", $type, "-m", @_, $ticket)});
193
194}
195
196BarnOwl::new_command("rt-reply",
197 \&cmd_rt_reply,
198 {
199 summary => "Reply to current ticket",
200 usage => "rt-reply [message]",
201 description => <<END_DESCR
202Replies to the currently selected ticket.
203END_DESCR
204 });
205
206BarnOwl::new_command("rt-comment",
207 \&cmd_rt_reply,
208 {
209 summary => "Comment on the current ticket",
210 usage => "rt-reply [message]",
211 description => <<END_DESCR
212Comments on the currently selected ticket.
213END_DESCR
214 });
215
216
217#owl::command('bindkey recv "M-r" command reply-un');
218
219
2201;