#!/usr/bin/perl -w # -*-CPerl-*-
#
# $Id: bugzilla.alert,v 1.2 2000/04/05 06:06:48 michael Exp $
#
# bugzilla.alert - Mon alert to log a bug in Bugzilla database
#
# USAGE
#
#   bugzilla.alert [--priority=priority] [--severity=severity] 
#                  [--assignee=assignee-email] [--cc=cc-email] 
#                  [--reported-url=reported-url] 
#                  [--http-userid=userid --http-password=password]
#                  bugzilla-url userid password product-name component-name 
#                    reporter 
#
#   bugzilla.alert will take the first line of STDIN and use it for the 
#   summary; the remaining lines of STDIN will be used for the long
#   description.
#
# AUTHOR
# 
#   Michael S. Fischer, <michael@auctionwatch.com>
# 
#   Copyright (C) 2000 AuctionWatch.com.
#  
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation; either version 2
#   of the License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# NOTES
# 
#   URL for posting: bugzilla-url/post_bug.cgi
#
#   CGI Parameters:
#       Name                 Default Value
#       ==================================================================
#       reporter             "monitor"
#       product              product-name (required)
#       component            component-name (required)
#       assigned_to          assignee (optional, defaults to "opsalert")
#       cc                   cc (optional)
#       bug_file_loc         url (optional)
#       short_desc           first line of STDIN
#       comment              1-N lines of STDIN 
#       form_name            "enter_bug"
#       Bugzilla_login       userid (required)
#       Bugzilla_password    password (required)

use strict;
use Getopt::Long;
use LWP::UserAgent;
use HTTP::Request::Common qw (POST);

my ($rurl, $pri, $severity, $ccmail, $amail, $comment, $httpuser, $httppass);

# Handle command-line args
GetOptions("priority=s" => \$pri,
	   "severity=s" => \$severity,
	   "cc=s" => \$ccmail,
	   "assignee=s" => \$amail,
	   "reported-url=s" => \$rurl,
	   "http-userid=s" => \$httpuser,
	   "http-password=s" => \$httppass);

@ARGV == 6 || usage();

usage() if $httpuser && ! $httppass;

my $burl = shift;
my $buser = shift;
my $bpass = shift;
my $prod = shift;
my $comp = shift;
my $rmail = shift;
my $short_desc = <STDIN>; # Read first line
$comment .= $_ while <STDIN>; # Read rest

# Set up LWP user agent
my $ua = new LWP::UserAgent;
$ua->agent('bugzilla.alert' . $ua->agent);

# Set up HTTP request
my $req = POST $burl . "/post_bug.cgi",
  ["reporter" => $rmail,
   "product" => $prod,
   "component" => $comp,
   "assigned_to" => $amail,
   "cc" => $ccmail,
   "bug_file_loc" => $rurl,
   "short_desc" => $short_desc,
   "comment" => $comment,
   "form_name" => "enter_bug",
   "Bugzilla_login" => $buser,
   "Bugzilla_password" => $bpass];

$req->authorization_basic($httpuser, $httppass) if $httpuser;

# Post it!
my $resp = $ua->request($req);

if ($resp->is_success()) {
  exit 0;
}
else {
  die $resp->error_as_HTML();
}


sub usage { 
  print <<EOF;
 USAGE:  bugzilla.alert [--priority=priority] [--severity=severity] 
                  [--assignee=assignee-email] [--cc=cc-email] 
                  [--reported-url=reported-url] 
                  [--http-userid=userid --http-password=password]
                  bugzilla-url bugzilla-userid bugzilla-password 
                  product-name component-name reporter
                     
EOF
  exit 1;
}