Removed /usr/local from CDPATH
[clearscm.git] / JIRA / updateWatchLists.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 =pod
6
7 =head1 NAME updateWatchLists.pl
8
9 Copy CC lists from Bugzilla -> JIRA
10
11 =head1 VERSION
12
13 =over
14
15 =item Author
16
17 Andrew DeFaria <Andrew@ClearSCM.com>
18
19 =item Revision
20
21 $Revision: #1 $
22
23 =item Created
24
25 Thu Mar 20 10:11:53 PDT 2014
26
27 =item Modified
28
29 $Date: 2014/05/23 $
30
31 =back
32
33 =head1 SYNOPSIS
34
35  Updates JIRA watchlists by copying the CC list information from Bugzilla
36  
37   $ updateWatchLists.pl [-login <login email>] [-products product1,
38                         product2,...] [-[no]exec]
39                         [-verbose] [-help] [-usage]
40
41   Where:
42
43     -v|erbose:       Display progress output
44     -he|lp:          Display full help
45     -usa|ge:         Display usage
46     -[no]e|xec:      Whether or not to update JIRA. -noexec says only 
47                      tell me what you would have updated.
48     -use|rname:      Username to log into JIRA with (Default: jira-admin)
49     -p|assword:      Password to log into JIRA with (Default: jira-admin's 
50                      password)
51     -bugzillaserver: Machine where Bugzilla lives (Default: bugs-dev)
52     -jiraserver:     Machine where Jira lives (Default: jira-dev)
53     -bugi|ds:        Comma separated list of BugIDs to process
54     -f|ile:          File of BugIDs, one per line
55
56 =head1 DESCRIPTION
57
58 This script updates JIRA watchlists by copying the CC List information from
59 Bugzilla to JIRA.
60
61 =cut
62
63 use FindBin;
64 use lib "$FindBin::Bin/lib";
65
66 $| = 1;
67
68 use DBI;
69 use Display;
70 use Logger;
71 use TimeUtils;
72 use Utils;
73 use JIRAUtils;
74 use BugzillaUtils;
75 use JIRA::REST;
76
77 use Getopt::Long; 
78 use Pod::Usage;
79
80 # Login should be the email address of the bugzilla account which has 
81 # priviledges to create products and components
82 our %opts = (
83   exec           => 0,
84   bugzillaserver => $ENV{BUGZILLASERVER} || 'bugs-dev',
85   jiraserver     => $ENV{JIRASERVER}     || 'jira-dev:8081',
86   username       => 'jira-admin',
87   password       => 'jira-admin',
88   usage          => sub { pod2usage },
89   help           => sub { pod2usage (-verbose => 2)},
90   verbose        => sub { set_verbose },
91 );
92
93 our ($log, %total);
94
95 my ($bugzilla, $jira);
96
97 sub main () {
98   my $startTime = time;
99   
100   GetOptions (
101     \%opts,
102     'verbose',
103     'usage',
104     'help',
105     'exec!',
106     'quiet',
107     'username=s',
108     'password=s',
109     'bugids=s@',
110     'file=s',
111     'jiraserver=s',
112     'bugzillaserver=s',
113   ) or pod2usage;
114   
115   $log = Logger->new;
116   
117   if ($opts{file}) {
118     open my $file, '<', $opts{file} 
119       or die "Unable to open $opts{file} - $!";
120       
121     $opts{bugids} = [<$file>];
122     
123     chomp @{$opts{bugids}};
124   } else {
125     my @bugids;
126     
127     push @bugids, (split /,/, join (',', $_)) for (@{$opts{bugids}}); 
128   
129     $opts{bugids} = [@bugids];
130   } # if
131   
132   pod2usage 'Must specify -bugids <bugid>[,<bugid>,...] or -file <filename>'
133     unless $opts{bugids};
134
135   openBugzilla $opts{bugzillaserver}
136     or $log->err ("Unable to connect to $opts{bugzillaserver}", 1);
137     
138   Connect2JIRA ($opts{username}, $opts{password}, $opts{jiraserver})
139     or $log->err ("Unable to connect to $opts{jiraserver}", 1);
140   
141   for (@{$opts{bugids}}) {
142     my $issue = findIssue $_;
143     
144     if ($issue =~ /^Future JIRA Issue/ or $issue =~ /^Unable to find/) {
145       $log->msg ($issue);
146     } else {
147       my %watchers = getWatchers $_;
148       
149       $log->msg ('Found ' . scalar (keys %watchers) . " watchers for JIRA Issue $issue");
150       
151       my $result = updateIssueWatchers ($issue, %watchers);
152       
153       if ($result =~ /^Unable to/) {
154         $total{'Missing JIRA Issues'}++;
155         
156         $log->err ($result);
157       } else {
158         $total{'Issues updated'}++;
159       } # if
160     } # if
161   } # for
162
163   display_duration $startTime, $log;
164   
165   Stats (\%total, $log) unless $opts{quiet};
166   
167   return  0;
168 } # main
169
170 exit main;