-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRT3_Jira_Mapper.pm
312 lines (267 loc) · 7.69 KB
/
RT3_Jira_Mapper.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env perl
package RT3_Jira_Mapper;
use warnings;
use strict;
our @ISA = qw(Exporter);
our @EXPORT = qw( AnonUser setAnonUser mapUser mapPriority mapQueueToProject mapResolution mapCFNames mapCFProc isAnon getRTanonuserlog loadJiraIKMapFile getJiraIssueFromRTTicket getRTTicketFromJiraIssue mapLinkType );
# Modify this to point to the Request Tracker installation location
use lib "/opt/rt3/lib";
# Import RT's API
use RT::Interface::CLI qw(CleanEnv loc);
use RT::Tickets;
use JellyWriter;
# The username to use as the reporter when an RT transaction is from a non-JIRA user
my $_anonuser = "anon";
sub AnonUser {
return $_anonuser;
}
sub setAnonUser {
my $uname=shift;
$_anonuser=$uname if $uname;
}
my %anonuserlog; # List of RT users that are treated as the anonymous user, key is username, value is count
# Map of RT users to Jira users
my %usermap = (
'rob' => 'rnewman',
'[email protected]' => 'rmellors',
'[email protected]' => 'lastiz',
'RT_System' => 'davis',
'Nobody' => 'davis',
'[email protected]' => 'eakins',
'[email protected]' => 'davis',
'[email protected]' => 'eakins',
'[email protected]' => 'danny',
'[email protected]' => 'leyer',
'[email protected]' => 'pavlis',
'[email protected]' => 'sauter',
'[email protected]' => 'davis',
'[email protected]' => 'busby',
'[email protected]' => 'rnitz',
'[email protected]' => 'jkim',
'[email protected]' => 'vernon',
'[email protected]' => 'dechavez',
'[email protected]' => 'dechavez',
'[email protected]' => 'jbytof',
'[email protected]' => 'thim',
'[email protected]' => 'delia',
'root' => 'davis',
'[email protected]' => 'reyes',
'[email protected]' => 'hafner',
'[email protected]' => 'k9pv',
'[email protected]' => 'eakins',
'[email protected]' => 'judy',
'[email protected]' => 'judy',
'[email protected]' => 'tshansen',
'[email protected]' => 'tshansen',
'[email protected]' => 'tshansen',
'kent' => 'lindquist',
'[email protected]' => 'aclemesh',
'taimi' => 'tmulder',
'gulsum' => 'gkarasu',
);
# RT states to Jira resolutions
my %resolutionmap = (
rejected => "Won't Fix",
deleted => "Won't Fix",
resolved => "Fixed",
);
# Map of RT priority numbers to Jira priorities
my %prioritymap = (
3 => "Major",
2 => "Critical",
4 => "Minor",
1 => "Blocker",
5 => "Trivial",
99 => "Blocker",
0 => "Trivial",
15 => "Major",
10 => "Major",
);
my %queuemap = (
General => "SYS",
LACOFD => "LACOFD",
ROADNet => "ROADNET",
ANZA => "ANZA",
SysAdmin => "SYS",
webapps => "WWW",
Backups => "BACKUP",
WebDev => "WWW",
USArray => "TA",
HiSeasNet => "HSN",
PBO => "PBO",
);
# Map of RT Custom Field names to their Jira names, and a processing function to handle multple values for a field.
# defaults are specifed in the "-default-" record
#
# If no jiraname is specified, it defaults to the RT Field Name
my %cfmap = (
"Documentation Link" => {
jiraname => "Documentation Link",
procfunc => \&JellyWriter::multiurl2textfield,
},
"Vendor Ticket #" => {
jiraname => "Vendor Ticket Id",
},
"Sat Station" => {
jiraname => "Satellite Station",
procfunc => \&JellyWriter::multitext2multiselect,
},
Server => {
jiraname => "Hosts Affected",
},
"-default-" => {
procfunc => \&JellyWriter::multitext2textfield,
}
);
# Hashes to map jira issue keys to rt tickets
my %jira2rt = ();
my %rt2jira = ();
# Map of rt link types to Jira link types
my %linktypemap = (
RefersTo => "refers to",
MemberOf => "is member of",
DependsOn => "depends on",
);
sub mapLinkType {
my $linktype=shift;
return $linktypemap{$linktype};
}
sub mapResolution($){
my $rtstatus=shift;
my $res="Fixed";
$res = $resolutionmap{$rtstatus} if exists $resolutionmap{$rtstatus};
return $res;
}
# Function to map RT users to Jira users
#
# Since we are not creating new Jira users automatically, any unknown email
# address needs to be mapped to an anonymous user. This mimics the behavior of
# the Jira "Mail Service Handler" with createusers="false" and
# reporterusername="anon"
# This function expects to get passed an RT::User object
sub mapUser($) {
my $rtuser=shift; # an RT::User object
my $name; #bare RT username
my $jirauser; #username to return
$name=$rtuser->Name; # Cache the username from the RT::User object
if ($rtuser->Privileged){
# If the user is a privileged RT user, we assume the user exists in Jira,
# rather than returning an anonymous user
$jirauser=$name;
# We then check to see if the RT username has been mapped to a different
# Jira username
if (exists $usermap{$name}){
$jirauser=$usermap{$name};
}
}
else {
# The user is not a privileged RT user
# but it may be mapped to a real jira user
# Remap the username if the username is in the map
if (exists $usermap{$name}){
$jirauser=$usermap{$name};
}
else {
# We have an anonymous user. Log it.
$jirauser=AnonUser();
$anonuserlog{$name}++;
}
}
return $jirauser;
}
# Function to map RT priorities to Jira Priorities
sub mapPriority($){
my $priority=shift;
my $default = "Minor";
return $default if ($priority eq "");
if ( exists($prioritymap{$priority}) ){
$priority = $prioritymap{$priority};
} else {
print STDERR "Fallback to Jira priority $default for RT priority $priority\n";
$priority = $default;
}
return $priority;
}
# Map RT Quenename to Jira project Key
sub mapQueueToProject($){
my $queue=shift;
my $default="SYS";
return $default if ($queue eq "");
if (exists $queuemap{$queue}) {
$queue=$queuemap{$queue};
} else {
print STDERR "Fallback to $default project for RT queue $queue\n";
$queue=$default;
}
return $queue;
}
# Map RT Custom Field Names to Jira Custom Field Names
sub mapCFNames ($) {
my $field=shift;
$field=$cfmap{$field}{'jiraname'} if (exists $cfmap{$field}{'jiraname'});
return $field;
}
# Map RT Custom Fields to a processing function
# returns a reference to a function
sub mapCFProc ($) {
my $field=shift;
if (exists $cfmap{$field}{'procfunc'}) {
return $cfmap{$field}{'procfunc'};
}
return $cfmap{'-default-'}{'procfunc'};
}
# isAnon takes a ref to a RT::User object and determines if the user is an anonymous user.
# The following logic applies:
# if RT::User says it's a privileged user in RT, isAnon returns false
# elsif mapUser has an entry for the RTUser that is not $anonuser, isAnon returns false
# else isAnon returns true
sub isAnon($){
my $rtUser=shift;
my $name = $rtUser->Name;
if ($rtUser->Privileged) {
return 0;
}
elsif ( grep (/^$name$/,keys(%usermap))) {
# user was found in the usermap, so we'll assume it's in Jira
return 0;
}
# User is anonymous
# don't log, we'll let mapUser do the logging in anonuserlog
return 1;
}
# Returns a list of users that were treated as anonymous
sub getRTanonuserlog {
return keys %anonuserlog;
}
# load a file of jira issues mapped to rt tickets from a file
# file format looks like this:
# JIRA_ISSUE_KEY:RT_TICKET_NUM
#
# for example:
# WWW-33:1759
# WWW-32:1756
# WWW-31:1726
# WWW-30:1725
# WWW-29:1723
sub loadJiraIKMapFile {
my $fname=shift;
my ($jira, $rt);
open DFILE, $fname or die "Can't open issue key map file $fname";
while (<DFILE>) {
chomp;
($jira,$rt) = split (":", $_);
$jira2rt{$jira}=$rt;
$rt2jira{$rt}=$jira;
}
close DFILE;
}
sub getJiraIssueFromRTTicket {
my $rt=shift;
return $rt2jira{$rt};
}
sub getRTTicketFromJiraIssue {
my $jira=shift;
return $jira2rt{$jira};
}
# Module loaded ok
return 1;