Changed jira to handle branch names that are just numbers
[clearscm.git] / bin / jira
1 #!/usr/local/cpanel/3rdparty/bin/perl
2
3 # cpanel - t/medium/Cpanel-LogReader.t             Copyright 2022 cPanel, L.L.C.
4 #                                                           All rights reserved.
5 # copyright@cpanel.net                                         http://cpanel.net
6 # This code is subject to the cPanel license. Unauthorized copying is prohibited
7
8 use strict;
9 use warnings;
10
11 =pod
12
13 =head1 NAME jira
14
15 Get info about a JIRA case
16
17 =head1 VERSION
18
19 =over
20
21 =item Author
22
23 Andrew DeFaria <Andrew.DeFaria@WebPros.com>
24
25 =item Revision:
26
27 $Revision: 1.1 $
28
29 =item Created:
30
31 Monday, April 25 2022
32
33 =item Modified:
34
35 Friday, October 21, 2022
36
37 =back
38
39 =head1 SYNOPSIS
40
41  Usage: jira [-assignee]  [-reporter] [-username <username>] [-password <password>] [-server <server>]
42              <case ID(s)>
43
44  Where:
45
46  -usa|ge:      Displays this usage
47  -h|elp:       Display full help
48  -a|ssginee:   Display assignee info
49  -r|eporter:   Display Reporter info
50  -st|atus:     Display status
51  -use|rname:   Username for JIRA
52  -p|assword:   Password for JIRA
53  -se|rver:     JIRA server (Default: jira.cpanel.net)
54  <case ID(s)>
55
56 =head1 DESCRIPTION
57
58 This script looks up a JIRA case and displays its summary. It can also display
59 the reporter and assignee. More fields can be added later on.
60
61 Note: Case ID (e.g. CPANEL-1928) can be just a number and if so "CPANEL-" will be
62 prepended.
63
64 Credentials should be put in ~/.jira and the file properly secured
65
66     username: <username>
67     password: <password>
68     server:   <jira.cpanel.net>
69
70 If server is not specified jira.cpanel.net will be assumed.
71
72 Note: If you don't specify a case ID then an attempt will be made to determine
73       the case ID from the branch name.
74
75 =cut
76
77 use feature 'say';
78 use experimental qw(signatures);
79
80 use Getopt::Long;
81 use Pod::Usage;
82
83 use FindBin;
84
85 use lib "$FindBin::Bin/../lib";
86
87 use JIRA      ();
88 use GetConfig ();
89 use Utils     ();
90
91 my $conf = "$ENV{HOME}/.jira";
92
93 my %opts = GetConfig::GetConfig($conf);
94
95 $opts{usage} = sub { pod2usage };
96 $opts{help}  = sub {
97     pod2usage( -verbose => 2 );
98 };
99
100 sub display_info ($case) {
101     say "$case->{key}: $case->{fields}{summary}";
102
103     my $assignee;
104
105     if ( $opts{assignee} ) {
106         if ( $case->{fields}{assignee} ) {
107             $assignee = "$case->{fields}{assignee}{displayName} <$case->{fields}{assignee}{emailAddress}>";
108         }
109         else {
110             $assignee = 'Unassigned';
111         }
112
113         say "Assigned to: $assignee";
114     }
115
116     if ( $opts{reporter} ) {
117         say "Reporter: $case->{fields}{reporter}{displayName} <$case->{fields}{reporter}{emailAddress}>";
118     }
119
120     if ( $opts{status} ) {
121         say "Status: $case->{fields}{status}{name}";
122     }
123
124     return;
125 }
126
127 GetOptions(
128     \%opts,
129     'usage',
130     'help',
131     'assignee',
132     'reporter',
133     'status',
134     'username=s',
135     'password=s',
136     'server=s',
137 ) || pod2usage;
138
139 unless ( $ARGV[0] ) {
140
141     # Try to get case number from branch
142     my ( $status, @output ) = Utils::Execute("git branch --show-current 2>&1");
143
144     unless ($status) {
145         if ($output[0] =~ /(\w+-)*(\d+)/) {
146             $ARGV[0] = $2;
147         }
148     }
149 }
150
151 pod2usage("ERROR: A case ID required\n") unless $ARGV[0];
152
153 $opts{server} //= 'jira.cpanel.net';
154
155 my $jira = JIRA->new(%opts);
156
157 my @cases = @ARGV;
158
159 for my $caseID (@cases) {
160     if ( $caseID =~ /^(\d+)$/ ) {
161         $caseID = "CPANEL-$1";
162     }
163
164     my $case;
165     my $status = 0;
166
167     eval { $case = $jira->getIssue($caseID); };
168
169     if ( $jira->status() == 401 ) {
170         die "Unable to authenticate username/password\n";
171     }
172     elsif ( $jira->status() == 404 ) {
173         say STDERR "ERROR: $caseID does not exist";
174     }
175     elsif ( $jira->status == 200 ) {
176         display_info($case) if $jira->status == 200;
177
178     }
179     else {
180         die "ERROR: Unknown status returned - $status\n";
181     }
182 }