The present code does have features for tickets but is hardcoded; this
patch is a little big more generic to function with at-1.2.3.
It also adds a new function, get_links() which returns a hash of
arrays of linktype => [id,id...] information.
Here is some example code that uses the patch below:
($s_id) = $rt->search(type => 'asset', query => "Name=\'test1\'");
($d_id) = $rt->search(type => 'asset', query => "Name=\'test2\'");
$rt->link(type=>'asset',src=>$s_id,dst=>$d_id,link_type=>'RunsOn');
my $links = $rt->get_links(type=>'asset',id=>$s_id);
I have not tested the code with tickets so functionality may have
broken there. Asset functionality works properly.
--- /usr/lib/perl5/site_perl/5.8.8/RT/Client/REST.pm 2010-07-06
01:22:31.000000000 -0700
+++ REST.pm 2011-06-06 10:38:34.000000000 -0700
@@ -167,6 +167,51 @@
return $k;
}
+sub get_links {
+ my $self = shift;
+
+ $self->_assert_even(@_);
+
+ my %opts = @_;
+
+ my $type = $self->_valid_type(delete($opts{type}) || 'ticket');
+ my $id = $self->_valid_numeric_object_id(delete($opts{id}));
+
+ my $form = form_parse(
+ $self->_submit("$type/$id/links/$id")->decoded_content
+ );
+ my ($c, $o, $k, $e) = @{$$form[0]};
+
+ if (!@$o && $c) {
+ RT::Client::REST::Exception->_rt_content_to_exception($c)->throw;
+ }
+
+ # Turn the links into id lists
+ foreach my $key (keys(%$k)) {
+ try {
+ $self->_valid_link_type($key);
+ my @list = split(/\s*,\s*/,$k->{$key});
+ my @newlist = ();
+ foreach my $val (@list) {
+ if ($val =~ /\/(\d+)$/) {
+ # We just want the ids, not the URI
+ push(@newlist,$1);
+ } else {
+ # Something we don't recognise
+ push(@newlist,$val);
+ }
+ }
+ # Copy the newly created list
+ $k->{$key} = ();
+ $k->{$key} = \@newlist;
+ } catch RT::Client::REST::InvalidParameterValueException with {
+ # Skip it because the keys are not always valid e.g., 'id'
+ }
+ }
+
+ return $k;
+}
+
sub get_transaction_ids {
my $self = shift;
@@ -375,7 +420,7 @@
return;
}
-sub link_tickets {
+sub link {
my $self = shift;
$self->_assert_even(@_);
my %opts = @_;
@@ -383,8 +428,9 @@
@opts{qw(src dst)};
my $ltype = $self->_valid_link_type(delete($opts{link_type}));
my $del = (exists($opts{'unlink'}) ? 1 : '');
+ my $type = $self->_valid_type(delete($opts{type}) || 'ticket');
- $self->_submit("ticket/link", {
+ $self->_submit("$type/link", {
id => $src,
rel => $ltype,
to => $dst,
@@ -394,7 +440,10 @@
return;
}
-sub unlink_tickets { shift->link_tickets(@_, unlink => 1) }
+sub link_tickets { shift->link(@_, type => 'ticket') }
+
+sub unlink { shift->link(@_, unlink => 1) }
+sub unlink_tickets { shift->link(@_, type => 'ticket', unlink => 1) }
sub _ticket_action {
my $self = shift;
@@ -657,7 +706,7 @@
sub _valid_link_type {
my ($self, $type) = @_;
my @types = qw(DependsOn DependedOnBy RefersTo ReferredToBy HasMember
- MemberOf);
+ MemberOf RunsOn IsRunning ComponentOf HasComponent);
unless (grep { lc($type) eq lc($_) } @types) {
RT::Client::REST::InvalidParameterValueException->throw(