-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobssupport.pm
133 lines (119 loc) · 3.17 KB
/
obssupport.pm
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
130
131
132
133
#!/usr/bin/perl -w
package obssupport;
use strict;
require Exporter;
our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
$VERSION = "1.0";
@ISA = qw(Exporter);
@EXPORT = qw(
&addentry &diffhash &addsrinfo &addsrlinks
);
our %srinfo=();
our $debug=0;
sub addentry($$$)
{my($bugmap, $bugid,$sr)=@_;
my $h=$bugmap->{$bugid}||{};
my %h=%$h; # deep copy to allow diffhash to work
$h{$sr}=1;
$bugmap->{$bugid}=\%h;
}
sub addsrinfo($$)
{ my($sr,$extra)=@_;
$srinfo{$sr}=$extra;
}
use XMLRPC::Lite; # From the SOAP::Lite Module
use JSON::XS;
use config;
my $bugzillahandle;
sub bugzillahandle()
{
$bugzillahandle=XMLRPC::Lite->proxy("https://apibugzilla.suse.com/xmlrpc.cgi") if(!$bugzillahandle);
return $bugzillahandle;
}
sub die_on_fault
{ my $soapresult = shift;
if ($soapresult->fault)
{
die $soapresult->faultcode . ' ' . $soapresult->faultstring;
}
}
sub proxycall($$)
{ my($func, $params)=@_;
my $proxy = bugzillahandle();
$params->{Bugzilla_login} = $config::username;
$params->{Bugzilla_api_key} = $config::password;
return $proxy->call($func, $params);
}
sub getsummary($)
{ my($bugid)=@_;
my $soapresult;
my $result;
eval {
$soapresult = proxycall('Bug.get', {ids=>[$bugid]});
$result = $soapresult->{_content}->[4]->{params}->[0]->{bugs}->[0]->{summary};
};
return $result;
}
sub getbug($)
{ my($bugid)=@_;
my $soapresult;
eval {$soapresult = proxycall('Bug.comments', {ids=>[$bugid]});};
$soapresult ||= {_content=>[0,1,2,3,"failed $@"]};
}
sub bugjson($)
{ my $soapresult=shift;
my $coder = JSON::XS->new->ascii->pretty->allow_nonref->allow_blessed->convert_blessed;
my $bugjson=$coder->encode ($soapresult->{_content}->[4]);
}
sub filtersr($@)
{ my($bugjson, @sr)=@_;
my @sr2=();
return @sr2 if($bugjson=~m/\Afailed /);
# drop linked SRs:
foreach my $sr (@sr) {
next if $bugjson=~m/request\/show\/$sr\b/;
push(@sr2, $sr); # keep sr
}
return @sr2;
}
sub srurlplusinfo(@)
{
return join("",map {
my $sr=$_;
my $info="";
if(my $i=$srinfo{$sr}) {$info=" $i"}
common::srurl($sr.$info)."\n";
} @_);
}
sub addbugcomment($$;$)
{ my($bugid, $comment, $p)=@_;
$p||=0;
my $soapresult2 = proxycall('Bug.add_comment', {id => $bugid, comment => $comment, is_private=>$p, private=>$p, isprivate=>$p});
die_on_fault($soapresult2);
}
sub addsrlinks($@)
{ my($bugid, @sr)=@_;
return 2 unless $bugid=~s/^bnc#//; # ignore others for now
return 2 if $bugid < 1040000; # ignore bugs older than 5y
eval { # catch die
my @sr2=@sr;
if(!$debug) { @sr2=filtersr(bugjson(getbug($bugid)), @sr);}
return unless @sr2;
my $comment="This is an autogenerated message for $config::bsname integration:\nThis bug ($bugid) was mentioned in\n".srurlplusinfo(@sr2)."\n";
if(!$debug) {
my $summary=getsummary($bugid);
if(!$summary) {
print "error - https://bugzilla.suse.com/show_bug.cgi?id=$bugid does not have a summary - does it even exist?\nSkipping...\n";
return 1;
}
print "adding to https://bugzilla.suse.com/show_bug.cgi?id=$bugid\n> $summary\n$comment\n";
addbugcomment($bugid, $comment, $config::privatecomment);
} else {
print "debug: would have added:\n$comment\n";
}
};
return 1 unless $@; # all OK
warn $@; # error
return 0;
}
1;