summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'extensions/ExternalBugs/lib/Util.pm')
-rw-r--r--extensions/ExternalBugs/lib/Util.pm191
1 files changed, 191 insertions, 0 deletions
diff --git a/extensions/ExternalBugs/lib/Util.pm b/extensions/ExternalBugs/lib/Util.pm
new file mode 100644
index 000000000..1ce382bc2
--- /dev/null
+++ b/extensions/ExternalBugs/lib/Util.pm
@@ -0,0 +1,191 @@
+# 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/.
+
+package Bugzilla::Extension::ExternalBugs::Util;
+
+use strict;
+use warnings;
+use 5.10.1;
+
+use base qw(Exporter);
+@Bugzilla::Extension::ExternalBugs::Util::EXPORT = qw(
+ perform_substs
+ mail_error
+ sfdc_get_directory
+ resolve_ext_tracker_url
+);
+
+use Bugzilla;
+use Bugzilla::Constants;
+use Bugzilla::Error;
+use Bugzilla::Util qw(remote_ip trim);
+use Bugzilla::Mailer;
+use File::Path qw(mkpath);
+use File::Slurp qw(write_file);
+use File::Spec::Functions qw(catdir catfile);
+
+=pod
+
+=head1 NAME
+
+Bugzilla::Extension::ExternalBugs::Util - Generic utility functions for bugzilla external bugs support.
+
+=head1 SYNOPSIS
+
+ use Bugzilla::Extension::ExternalBugs::Util;
+
+ # Function for creating proper url for accessing the external bug/issue
+ perform_subst($url_template, { id => $bug_id });
+
+=head1 DESCRIPTION
+
+This package contains various utility functions which do not belong anywhere
+else.
+
+B<It is not intended as a general dumping group for something which
+people feel might be useful somewhere, someday>. Do not add methods to this
+package unless it is intended to be used for a significant number of files,
+and it does not belong anywhere else.
+
+=head1 FUNCTIONS
+
+=over
+
+=item C<perform_substs>
+
+=over
+
+=item B<Description>
+
+Returns a properly formatted url based on template substitutions to an external bug/issue.
+
+=item B<Params>
+
+=item B<Returns>
+
+string - Full url to the external bug/issue
+
+=back
+
+=back
+
+=cut
+
+sub perform_substs {
+ my ($str, $substs) = (@_);
+ $str =~ s/%([a-z]*)%/(defined $substs->{$1} ? $substs->{$1} : $1)/eg;
+ return $str;
+}
+
+sub mail_error {
+ my ($error, $extra) = @_;
+
+ my $host
+ = eval { require Sys::Hostname; Sys::Hostname::hostname() } || 'undefined';
+ my $stack
+ = eval { require Carp; Carp::longmess("stack backtrace") } || 'undefined';
+ my $user = eval { require Bugzilla; Bugzilla->user->identity } || 'undefined';
+
+ # avoid SMTP injection attack
+ $error =~ s/[\r\n]//g;
+
+ warn $error;
+
+ # Send email to bugzilla owner about the problem
+ my $err_msg = "To: " . Bugzilla->params->{mail_errors_to} . "\n";
+ $err_msg .= "From: bugzilla\@redhat.com\n";
+ $err_msg .= "Subject: Error \$\@: $error\n\n";
+ $err_msg .= "Host: $host\n";
+ $err_msg .= "Pid: $$\n";
+ $err_msg .= "User: $user\n";
+ $err_msg .= "Remote Address: " . remote_ip() . "\n";
+ $err_msg .= "Stack Trace:\n$stack\n";
+ $err_msg .= "Extra Data:\n$extra\n" if $extra;
+
+ MessageToMTA($err_msg);
+
+ return;
+}
+
+sub sfdc_get_directory {
+ my $type = shift;
+ my $server = shift;
+ my $wsdl_xml = shift;
+
+ # Determine the directory
+ my $directory = catdir(bz_locations()->{'datadir'}, 'sfdc', $type, $server);
+
+ # If the directory does not exist, create it
+ if (!-e $directory) {
+ eval { mkpath($directory, 0, 0755) };
+ if ($@) {
+ ThrowCodeError('eval_error', {command => "mkpath $directory", msg => $@});
+ }
+ }
+
+ # If WSDL is provided, and the WSDL file does not exist, create that
+ return $directory if !$wsdl_xml;
+
+ my $wsdl_file = catfile($directory, "BugWebServices$type.wsdl.xml");
+ if (!-e $wsdl_file) {
+ $wsdl_xml =~ s/__SFDC_SERVER__/$server/;
+ eval { write_file($wsdl_file, \$wsdl_xml) };
+ if ($@) {
+ ThrowCodeError('eval_error',
+ {command => "write_file $wsdl_file, ...", msg => $@});
+ }
+ }
+
+ return $directory;
+}
+
+sub resolve_ext_tracker_url {
+ my ($bug_url) = @_;
+
+ my ($tracker_id, $tracker_url);
+
+ my $all_trackers = Bugzilla->request_cache->{external_tracker_objs};
+ unless (defined($all_trackers)) {
+ $all_trackers = [Bugzilla::Extension::ExternalBugs::Type->get_all];
+ Bugzilla->request_cache->{external_tracker_objs} = $all_trackers;
+ }
+
+ foreach my $tracker (@$all_trackers) {
+ my $url = $tracker->full_url;
+
+ # Strip off the %id% so we can match the start of the URL.
+ $url =~ s/\%id\%//;
+
+ # Now match the remainder of the URL, see if we find a match
+ if (index($bug_url, $url, 0) == 0) {
+
+ # We need to get the %id% value. To do that we strip off the $url and we should
+ # be left with the %id%.
+ my $url_len = length($url);
+ $tracker_url = substr($bug_url, $url_len);
+
+ $tracker_id = $tracker->id;
+ last;
+ }
+ }
+
+ unless (defined $tracker_id) {
+
+ # External tracker not resolved
+ ThrowUserError('external_url_not_found');
+ }
+
+ return ($tracker_id, $tracker_url);
+}
+
+
+=pod
+
+=head1 SEE ALSO
+
+L<Bugzilla>
+
+=cut
+
+1;