Post Issue to Jira Using PowerShell 2.0

Some time ago I was looking for a skeleton of a PowerShell 2.0 script that would allow me to post an issue to Jira. Didn’t find one, so had to come up with my own.

# Change those strings to whatever is specific to environment
[string] $SERVER = “localhost:8080”
[string] $USER = “powershelluser”
[string] $PWD = “password”
# Get Project ID from Administration -> Projects, e.g. 
[string] $PROJECTID = “10123”
# Specify fields that need to be posted
# NOTE: You must provide all required fields
# Example below submits 4 fields: 
#    assignee – automatically assigned to default user of the project
#    issuetype – selected type 3 = task
#    summary and description, which are read from elsewhere
[string] $FIELDS = “issuetype=3&priority=3&assignee=-1&summary={0}&description={1}”

# Those constants normally do not change
[string] $LOGIN_URL = http://{0}/login.jsp?os_username={1}&os_password={2}&os_destination=/secure/”
[string] $CREATE_URL = http://{0}/secure/CreateIssueDetails.jspa?pid={1}&{2}&Create=Create&atl_token={3}”
[string] $TOKEN_PREFIX = ‘<meta id=”atlassian-token” name=”atlassian-token” content=”‘
[string] $TOKEN_SUFFIX = ‘”>’

function loginToJira($server, $user, $pwd) {

[string] $postUrl = $url -f  $user, $pwd

[System.Net.WebRequest] $webRequest = [System.Net.WebRequest]::Create($postUrl)
$webRequest.CookieContainer = New-object System.Net.CookieContainer
[System.Net.WebResponse] $webResponse = $webRequest.getResponse()
# TO DO: Response can be verified here
# One of the ways to check if request was successful is to read $webResponse.StatusDescription, e.g.
Write-Host “status = “ $webResponse.StatusDescription;

$resStream = $webResponse.GetResponseStream()
[System.IO.StreamReader] $streamReader = New-Object System.IO.StreamReader -argumentList $resStream
$res = $streamReader.ReadToEnd()
$start = $res.IndexOf($TOKEN_PREFIX, 0+ $TOKEN_PREFIX.Length
$end = $res.IndexOf($TOKEN_SUFFIX, $start)
$token = $res.substring($start, $end  $start)

return $webRequest.CookieContainer, $token
}

function createJiraIssue($server, $session, $token, $project_id, $summary, $description) {

[string] $formattedFields = $FIELDS -f [System.Web.HttpUtility]::UrlEncode($summary), [System.Web.HttpUtility]::UrlEncode($description)
[string] $postUrl = $CREATE_URL -f $server, $project_id, $formattedFields, $token

[System.Net.WebRequest]$webRequest = [System.Net.WebRequest]::Create($postUrl);
$webRequest.CookieContainer = $session
[System.Net.WebResponse]$webResponse = $webRequest.getResponse()
# TO DO: Response can be verified here (similar to login)

$resStream = $webResponse.GetResponseStream()
[System.IO.StreamReader]$streamReader = New-Object System.IO.StreamReader -argumentList $resStream
$res = $streamReader.ReadToEnd()
# TO DO: Here it can be verified if the issue was posted successully, by looking into $res
# If issue is not posted, Jira will return HTML similar to the page seeing by the user who tried to post issue with missing / problematic fields
}

if($args.count -ne 3) {
showHelp
break
}
Add-Type -AssemblyName System.Web
$session, $token = loginToJira $SERVER $USER $PWD
$testsummary = “This is a test – issue summary”
$testdscription = “This is a test.`n`nIssue description usually contains several lines of text”
createJiraIssue $session $token $PROJECTID $testsummary $testdscription

Advertisements
Post Issue to Jira Using PowerShell 2.0