Posts Tagged: powershell


3
Oct 09

YouTrack Bugs from Anywhere with Powershell

I really dig the new issue tracker from the JetBrains folks formerly codenamed Charisma.  The in-browser experience is on the bleeding edge.

I spend a lot of time dreaming up or finding issues and bugs when I am not at my development machine.  When this happens often times I do not want to launch YouTrack and get stuck in; I would rather quickly log the issue and come back to it later to categorize, investigate or assign. 

YouTrack has an undocumented REST API for needs like this.  I have ported their Java example to a quick and dirty powershell script:

param($summary, $project, $usr, $pwd, $youtrack_url)

$cookies = new-object Net.CookieContainer

function http-post([string]$url, [string]$postData) {
    $buffer = [text.encoding]::ascii.getbytes($postData)
    [net.httpWebRequest] $req = [net.webRequest]::create($url)
    $req.method = "POST"
    $req.ContentType = "application/x-www-form-urlencoded"
    $req.ContentLength = $buffer.length
    $req.TimeOut = 5000
    $req.CookieContainer = $cookies
    $reqst = $req.getRequestStream()
    $reqst.write($buffer, 0, $buffer.length)
    $reqst.flush()
    $reqst.close()
    [net.httpWebResponse] $res = $req.getResponse()
    $resst = $res.getResponseStream()
    $sr = new-object IO.StreamReader($resst)
    $results = $sr.ReadToEnd()
    # save cookies for next request
    foreach($cookie in $res.Cookies) {
        $cookies.Add($cookie)
    }
    $results
}

if (([xml](http-post "$youtrack_url/rest/user/login" "login=$usr&password=$pwd")).login -match "ok") {
    $id = ([xml](http-post "$youtrack_url/rest/issue" "project=$project&summary=$summary")).issue.id[0]
    return "issue $id created in youtrack..."
}

I look forward to refactoring this script into a full featured library as the need arises and the YouTrack REST API becomes documented. 

In the meantime I keep this script on my portable USB drive.  I have a home grown application launcher that I like to use to execute scripts like the one above.

image

So when I am in the lab being a “user” and I find a bug, I just type “<project code> bug <summary>” as illustrated above and a new issue will be opened in YouTrack.  Later in the afternoon when I am at my desk being a “developer” I can fire up YouTrack and take it from there.