-
Notifications
You must be signed in to change notification settings - Fork 0
/
JIRAComments.ps1
82 lines (63 loc) · 2.4 KB
/
JIRAComments.ps1
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
<#
@TechJLS3
JIRA Last Comment Extractor
3/16/2018
Call Script from Command Line:
e.g. .\JIRAComments.ps1 -TicketNo "JT-63,JT-253,jt-243,Jt-69,jT-169,BI-141,BM-1000"
#>
#Provide list of comma delimited JIRA Tickets
param (
[string]$TicketNo
)
#Handles Authenication and API Call
function Get-Data([string]$username, [string]$password, [string]$url) {
#Source: https://pallabpain.wordpress.com/2016/09/14/rest-api-call-with-basic-authentication-in-powershell/
# Step 1. Create a username:password pair
$credPair = "$($username):$($password)"
# Step 2. Encode the pair to Base64 string
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($credPair))
# Step 3. Form the header and add the Authorization attribute to it
$headers = @{ Authorization = "Basic $encodedCredentials" }
# Step 4. Make the GET request
$responseData = Invoke-WebRequest -Uri $url -Method Get -Headers $headers -UseBasicParsing
return $responseData
}
#Stuff
##User Name in corporate environments
$username = $env:UserName
#Otherswise, type it in.
#$username = Read-Host "Enter Username"
##Password Handling
$SecurePassword = Read-Host "Enter Pass" -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)
$pass = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
#Splits Comma Delims
$ticketArray = $TicketNo -split ','
$joinedObject = foreach ($jid in $ticketArray){
$error.clear()
try {
$url = "https://jira/rest/api/2/issue/$jid/comment"
$jira = Get-Data $username $pass $url
$json = ($jira.Content | Out-String) | ConvertFrom-Json
<#For Debugging
Write-Host $jid
$url
#>
#Get Comment Data
$data = Write-Output $json.comments.body
if ([string]::IsNullOrEmpty($data)){
[pscustomobject]@{IssueID = $jid; Comment = "No Recorded Comment";} | Write-Output
}
else{
[pscustomobject]@{IssueID = $jid; Comment = ($data|out-string);} | Write-Output
}
}
catch {
[pscustomobject]@{IssueID = $jid; Comment = "Error (Ticket likely does not exist)";} | Write-Output
}
}
#outputs file as CSV
$joinedObject|export-csv -Force "C:\Users\TechJLS3\Desktop\JIRAComments.csv"
<# For Debugging
$JoinedObject|Out-GridView
#>