Hopefully final adjustments to certbot
[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.0 $
28
29 =item Created:
30
31 Monday, April 25 2022
32
33 =item Modified:
34
35 Monday, April 25 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. ART-1928) can be just a number and if so "ART-" 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 =cut
73
74 use feature 'say';
75 use experimental qw(signatures);
76
77 use Getopt::Long;
78 use Pod::Usage;
79
80 use FindBin;
81
82 use lib "$FindBin::Bin/../lib";
83
84 use JIRA      ();
85 use GetConfig ();
86
87 my $conf = "$ENV{HOME}/.jira";
88
89 my %opts = GetConfig::GetConfig($conf);
90
91 $opts{usage} = sub { pod2usage };
92 $opts{help}  = sub {
93     pod2usage( -verbose => 2 );
94 };
95
96 sub display_info ($case) {
97     say "$case->{key}: $case->{fields}{summary}";
98
99     my $assignee;
100
101     if ( $opts{assignee} ) {
102         if ( $case->{fields}{assignee} ) {
103             $assignee = "$case->{fields}{assignee}{displayName} <$case->{fields}{assignee}{emailAddress}>";
104         }
105         else {
106             $assignee = 'Unassigned';
107         }
108
109         say "Assigned to: $assignee";
110     }
111
112     if ( $opts{reporter} ) {
113         say "Reporter: $case->{fields}{reporter}{displayName} <$case->{fields}{reporter}{emailAddress}>";
114     }
115
116     if ( $opts{status} ) {
117         say "Status: $case->{fields}{status}{name}";
118     }
119
120     return;
121 }
122
123 GetOptions(
124     \%opts,
125     'usage',
126     'help',
127     'assignee',
128     'reporter',
129     'status',
130     'username=s',
131     'password=s',
132     'server=s',
133 ) || pod2usage;
134
135 pod2usage("ERROR: A case ID required\n") unless $ARGV[0];
136
137 $opts{server} //= 'jira.cpanel.net';
138
139 my $jira = JIRA->new(%opts);
140
141 my @cases = @ARGV;
142
143 for my $caseID (@cases) {
144     if ( $caseID =~ /^(\d+)$/ ) {
145         $caseID = "ART-$1";
146     }
147
148     my $case;
149     my $status = 0;
150
151     eval { $case = $jira->getIssue($caseID); };
152
153     if ( $jira->status() == 401 ) {
154         die "Unable to authenticate username/password\n";
155     }
156     elsif ( $jira->status() == 404 ) {
157         say STDERR "ERROR: $caseID does not exist";
158     }
159     elsif ( $jira->status == 200 ) {
160         display_info($case) if $jira->status == 200;
161
162     }
163     else {
164         die "ERROR: Unknown status returned - $status\n";
165     }
166 }