· 6 years ago · Jan 06, 2020, 06:32 PM
1
2function New-JIRATaskFromBugzilla {
3 <#
4 .SYNOPSIS
5 Creates new JIRA tasks by importing a Bugzilla bug.
6 .DESCRIPTION
7 Creates new JIRA tasks by importing a Bugzilla bug. ATL-93:
8 * Create new JIRA task w/ BZ summary/desc/etc. in matching category
9 * Add label BZ:[BUGID] to JIRA task
10 * Add whitespace label to BZ with JIRA:[TaskID]
11 * Cycle through BZ comments adding by desc date
12 * Perhaps manually if needed: (Attachments?) (Assignee?)
13 * Cycle through DOA-287 bug check-ins
14 .PARAMETER bug
15 Required. Bug or list of bugs to import.
16 .EXAMPLE
17 New-JIRATaskFromBugzilla -bug 34804
18 .NOTES
19 DK 12/8/2017 -
20
21 #>
22
23
24 [CmdletBinding()]
25 param(
26 [parameter(Mandatory=$True,ValueFromPipeline=$True)][string[]] $bug
27 #[parameter(Mandatory=$False,ValueFromPipeline=$True)][string] $assignee,
28 #[parameter(Mandatory=$False,ValueFromPipeline=$True)][string] $projectkey="DOA"
29 )
30
31
32 :ForEachBZBug foreach ($bzbug in $bug) {
33
34 $tasksummary,$taskdescription,$taskpriority,$taskprojectkey,$tempassignee,$taskAssignee,$BZComponentName,$BZProductName,$BZDeadline,$BZDevDeadline,$BZQADeadline,$BZWhiteboard,$BZStatus,$BZResolution,$JiraComponentName="","","","","","","","","","","","","","",""
35 $labels=@()
36
37 #set variables we want to retrieve from BZ.
38
39 $query="select bugs.bugs.short_desc,bugs.bugs.bug_status,bugs.bugs.resolution,status_whiteboard,bugs.bugs.cf_client,bugs.bugs.priority,bugs.bugs.product_id,bugs.products.name,bugs.bugs.component_id,bugs.components.name AS componentname, bugs.profiles.login_name,bugs.profiles.realname,bugs.bugs.cf_qa_priority_date,bugs.bugs.cf_dev_priority_date,bugs.bugs.deadline from bugs.bugs JOIN bugs.profiles on bugs.bugs.assigned_to=bugs.profiles.userid JOIN bugs.products ON bugs.bugs.product_ID=bugs.products.id JOIN bugs.components ON bugs.bugs.component_id=bugs.components.ID where bugs.bug_ID=$bzbug"
40 $result =(Get-MySQL -Query $query)
41
42 if ($result.count -lt 2) {
43 #invalid results
44 write-verbose "Less than 1 row returned from query. Skipping"
45 continue ForEachBZBug
46 }
47
48 $result=$result[0]
49 $tasksummary=$result.short_desc
50 $taskpriority=$result.bug_severity
51 $BZWhiteboard=$result.status_whiteboard
52 $BZStatus=$result.bug_status
53 $BZResolution=$result.resolution
54 $BZDeadline=$result.deadline
55 $BZQADeadline=$result.cf_qa_priority_date
56 $BZDevDeadline=$result.cf_dev_priority_date
57
58 $query="select * from longdescs JOIN bugs.profiles on bugs.longdescs.WHO=bugs.profiles.userID where bug_ID=$bzbug ORDER BY COMMENT_ID ASC"
59 $commentsresult=(Get-MySQL -Query $query)
60
61 $taskdescription=$tasksummary
62 $taskdescription+="" + $commentsresult[0].thetext
63
64 switch ($taskpriority)
65 {
66 "major" { $taskpriority=2 }
67 "normal" { $taskpriority=3}
68 "critical" { $taskpriority=2 }
69 "blocker" { $taskpriority=1 }
70 "trivial" { $taskpriority=4 }
71 "minor" { $taskpriority=4 }
72 default { $taskpriority=3 }
73 }
74
75 $taskAssignee=$result.login_name
76 $taskAssigneeRealName=$result.realname
77 $BZProductName=$result.name
78 $BZComponentName=$result.componentname
79 $taskprojectkey="DOA"
80 $JiraComponentName="General Task"
81 $labels=($BZWhiteboard -split ",")
82
83 $JiraMappings=@(Import-Csv Configs\BZ-JIRAmappings.csv)
84
85 foreach ($item in $JiraMappings) {
86 write-verbose "Checking if (($($item.BZ_Product) -eq $BZProductName) -and ($($item.BZ_Component) -eq $BZComponentName)) " #todebug
87 if (($($item.BZ_Product) -eq $BZProductName) -and ($($item.BZ_Component) -eq $BZComponentName)) {
88 $taskprojectkey=$item.JIRA_Project
89 $JiraComponentName=$item.JIRA_Component
90 write-verbose "Matched this bug to JIRA project type $taskprojectkey and component $JiraComponentName"
91 break
92 }
93 }
94
95 #make sure given assignee really exists and/or try to find if not.
96 $jirauser=(find-jirauser $taskAssignee)
97 if (-not ($jirauser.key)) {
98 write-verbose "User $taskAssignee not found in JIRA - overriding by BZ RealName match $taskAssigneeRealName"
99 $usersearch=(Find-JIRAUser -searchstring $taskAssigneeRealName)
100 if ($($usersearch[0].key)) {
101 $assignee= $usersearch[0].key
102 write-verbose "Match found for user by realname - set to $assignee"
103 }
104 } else {
105 $taskAssignee=($jirauser.key)
106 write-verbose "Jira User found but setting taskAssignee to key - $taskAssignee"
107 }
108 #query project type for issue types and set accordingly (default to -- ? task type?)
109
110 #Create the new JIRA task.
111
112 $taskdescription=$taskdescription -replace '[^\x00-\x7F]+', ''
113 $tasksummary=$tasksummary -replace '[^\x00-\x7F]+', ''
114 $tasksummary=$tasksummary.replace('"',"'")
115 write-host "summary is now $tasksummary" -ForegroundColor Green
116 write-host "Task description is now $taskdescription" -ForegroundColor Green
117 write-verbose "Calling New-JIRATask -projectKey $taskprojectkey -summary $($tasksummary | ConvertTo-Json) -description $($taskdescription | ConvertTo-Json) -priority $taskpriority -assignee $taskAssignee -component $JiraComponentName`n"
118 $returnitem=New-JIRATask -projectKey $taskprojectkey -summary "$tasksummary" -description "$taskdescription" -priority $taskpriority -assignee $taskAssignee -component "$JiraComponentName"
119 $JiraTaskID=$returnitem.key
120
121 if ($JiraTaskID -notlike "*-*") {
122 Write-Error "Error inserting into JIRA! check API return."
123 return
124 }
125
126 $jiramigrationcomment="Migration of bug from Bugzilla bug # $bzbug to JIRA task ID $JiraTaskID (http://asdsjira.4yoursoul.net/browse/$JiraTaskID) took place at $(get-dateWithTZ)"
127 write-host $jiramigrationcomment
128
129
130 #Add comments back into new task.
131 $i=1
132 foreach ($comment in $commentsresult) {
133 $attachmenttext=""
134 if ($($comment.login_name) ) {
135 if ($($comment.type) -eq "5") {
136 #attachment
137 $attachmenttext="Attachment: [http://bugzilla.4yoursoul.net/attachment.cgi?id=$($comment.extra_data)]"
138 $attachquery="SELECT * FROM bugs.attachments where bug_ID=$bzbug and attach_ID=$($comment.extra_data)"
139 $attachresult=(Get-MySQL -Query $attachquery)
140 write-verbose "Querying attachments: $attachquery - $(($attachresult.count)-1) items returned"
141
142 if ($attachresult[0].description) {
143 $attachmenttext="Attachment added - $($attachresult[0].description) - Filename: $($attachresult[0].filename) : [http://bugzilla.4yoursoul.net/attachment.cgi?id=$($comment.extra_data)] `n`n"
144 }
145 #could cross join attach_data and download
146 }
147
148 $comment="{panel:title=Bugzilla $BzBug Comment #$i - $($comment.bug_when) - By $($comment.login_name) |borderStyle=dashed|borderColor=#ccc|titleBGColor=#E8894A|bgColor=#F9E1D1}" + $attachmenttext + $($comment.thetext) + "{panel}"
149 $comment=$comment -replace "\[Error","[BugzillaError"
150 write-host "Adding comment $i - to $JiraTaskID"
151 New-JIRAComment -comment "$comment" -taskid $JiraTaskID
152 $i++
153 }
154 }
155
156 #Add JIRA: status label back into BZ whiteboard ---
157 $BZWhiteboard=($BZWhiteboard,"JIRA:$JiraTaskID") -join ","
158 $updatequery="update bugs.bugs set bugs.bugs.status_whiteboard='$BZWhiteboard' where bug_ID=$bzbug"
159 $updatequeryresults=(Get-MySQL -Query $updatequery)
160
161
162 #Add Labels back into new task/ also add label for "BZ:$bzbug"
163 $labels+="BZ:$bzbug"
164 foreach ($mylabel in $labels) {
165 if ($mylabel.length -gt 0) {
166 Add-JIRATaskLabel -label $mylabel -taskID $JiraTaskID
167 }
168 }
169
170 #Adjust status to match?
171
172 #Set Deadline?
173 if (-not ([string]::IsNullOrEmpty($BZDeadline))) {
174 Set-JIRATaskReleaseDate -taskID $JiraTaskID -date $BZDeadline
175 write-verbose "Setting release date to $BZDeadline"
176 }
177 if (-not ([string]::IsNullOrEmpty($BZQADeadline))) {
178 Set-JIRATaskQADate -taskID $JiraTaskID -date $BZQADeadline
179 write-verbose "Setting QA date to $BZQADeadline"
180 }
181 if (-not ([string]::IsNullOrEmpty($BZDevDeadline))) {
182 write-verbose "Setting DEV date to $BZDevDeadline"
183 Set-JIRATaskDevDate -taskID $JiraTaskID -date $BZDevDeadline
184 }
185
186 #add BZ watchers list to JIRA
187 $query="SELECT bugs.profiles.login_name FROM bugs.cc join bugs.profiles ON bugs.cc.who = bugs.profiles.userid where bug_ID=$bzbug"
188 $loginnames=(Get-MySQL -Query $query)
189 foreach ($login in $loginnames) {
190 if ($login.login_name) {
191 #add login.login_name to JIRA watch list for task
192 Set-JIRATaskWatchers -watcher $($login.login_name) -taskID $JiraTaskID
193 }
194 }
195
196 #add final comment to BZ bug that was migrated FROM.
197 $jiramigrationcomment += "`n`nClosing bugzilla bug $bzbug due to transfer. Old status was $BZStatus - Resolution: $BZResolution"
198 new-comment -bug $bzbug -comment $jiramigrationcomment
199
200 #close out BZ
201 $updatequery="update bugs.bugs set bugs.bugs.bug_status='CLOSED', bugs.bugs.resolution='INVALID' where bug_ID=$bzbug"
202 $updatequeryresults=(Get-MySQL -Query $updatequery)
203
204 #update svn logs - this itself adds a comment
205 Move-SVNBZCheckinsToJIRA -bugzillabugID $bzbug -JIRATaskID $JiraTaskID
206 new-comment -bug $JiraTaskID -comment $jiramigrationcomment
207 }
208}