This repository was archived by the owner on Nov 9, 2017. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathxmlrpc-test.pl
More file actions
executable file
·129 lines (116 loc) · 3.33 KB
/
Copy pathxmlrpc-test.pl
File metadata and controls
executable file
·129 lines (116 loc) · 3.33 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/perl -w
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
use strict;
use warnings;
use 5.10.1;
use Cwd qw(abs_path);
use File::Basename;
use FindBin qw($RealBin);
use Getopt::Lucid qw(:all);
use HTTP::Cookies;
use SOAP::Lite;
use Sys::Hostname qw(hostname);
use XMLRPC::Lite;
use Data::Dumper;
$Data::Dumper::Terse = 1;
$Data::Dumper::Sortkeys = 1;
my $opt = Getopt::Lucid->getopt([
Param('username|user|u')->default('glob@example.com'),
Param('password|pass|p')->default('secret'),
Param('api_key|key|k'),
Param('token|t'),
Param('url')->default('http://' . hostname() . '/%s/xmlrpc.cgi'),
Switch('pretty_xml|pretty-xml|x'),
])->validate;
if ($opt->get_api_key) {
bugzilla('api_key', $opt->get_api_key);
} elsif ($opt->get_token) {
bugzilla('token', $opt->get_token);
} else {
bugzilla(
'User.login', {
login => $opt->get_username,
password => $opt->get_password,
remember => 1,
}
);
}
bugzilla(
'Bug.get', {
ids => [ 999 ],
}
);
#
# helpers
#
sub _debug_transport {
my $r = shift;
(my $content = $r->as_string) =~ s/\n*$/\n/;
if ($opt->get_pretty_xml) {
my ($header, $body) = $content =~ /^(.+\n\n)(.+)$/s;
eval {
eval 'use XML::LibXML';
$content = $header . XML::LibXML->new->load_xml(string => $body)->toString(1);
};
}
if ($r->isa('HTTP::Request')) {
print ">>>\n$content>>>\n";
} elsif ($r->isa('HTTP::Response')) {
print "<<<\n$content<<<\n";
} else {
die;
}
return undef;
}
sub bugzilla {
my ($method, $params, $token) = @_;
state $rpc;
my @auth_fields = qw(api_key token);
if (grep { $_ eq $method } @auth_fields) {
$rpc->{$method} = $params;
return;
}
$rpc->{cookie_jar} //= HTTP::Cookies->new( ignore_discard => 1 );
if (!$rpc->{proxy}) {
my $dir = _find_base_directory('.')
or _find_base_directory($RealBin)
or die "failed to find working environment\n";
$rpc->{proxy} = XMLRPC::Lite->proxy(
sprintf($opt->get_url, $dir),
cookie_jar => $rpc->{cookie_jar},
);
foreach my $handler (qw( request_send response_done )) {
$rpc->{proxy}->transport->set_my_handler( $handler => \&_debug_transport );
}
}
foreach my $field (@auth_fields) {
$params->{$field} = $rpc->{$field} if $rpc->{$field};
}
my $soapresult;
eval {
$soapresult = $rpc->{proxy}->call($method, $params);
if ($soapresult->fault) {
my ($package, $filename, $line) = caller;
die $soapresult->faultcode . ' ' . $soapresult->faultstring .
" in SOAP call near $filename line $line.\n";
}
};
if ($@) {
print $@;
exit;
}
$rpc->{token} = $soapresult->result->{token}
if $method eq 'User.login';
return $soapresult->result;
}
sub _find_base_directory {
my ($path) = @_;
$path = abs_path($path);
while (1) {
return '' if $path eq '' || $path eq '/';
return basename($path) if -e "$path/localconfig";
$path = abs_path("$path/..");
}
}