This code is actually working (without requiring entering credentials):
function ConvertTo-Base64($string) {
$bytes = [System.Text.Encoding]::UTF8.GetBytes($string);
$encoded = [System.Convert]::ToBase64String($bytes);
return $encoded;
}
function Get-HttpBasicHeader([string]$username, [string]$password, $Headers = @{}) {
$b64 = ConvertTo-Base64 "$($username):$($Password)"
$Headers["Authorization"] = "Basic $b64"
$Headers["X-Atlassian-Token"] = "nocheck"
return $Headers
}
function add_comment([string]$issueKey,[string]$comment) {
$body = ('{"body": "'+$comment+'"}')
$comment=(Invoke-RestMethod -uri ($restapiuri +"issue/$issueKey/comment") -Headers $headers -Method POST -ContentType "application/json" -Body $body).id
return $comment
}
$restapiuri = "https://jira.netic.dk/rest/api/2/"
$headers = Get-HttpBasicHeader "user" "password"
add_comment "TEST-201" "[~test.user] please handle the issue." |
In the Latter, I have switched to this code (the Authentication in the above does not work very well - good stuff from here: http://poshcode.org/5471):
# Connect parameters for JIRA REST
$jirausername="user"
$jirapassword="password"
$jirarestapiuri = "https://jira.netic.dk/rest/api/2/"
$bytes = [System.Text.Encoding]::UTF8.GetBytes("$jirausername`:$jirapassword")
$global:encodedCredentials = [System.Convert]::ToBase64String($bytes)
Try
{
$resturi="https://jira.server.dk/rest/api/2/issue/" + $jirakey
$WebRequest = [System.Net.WebRequest]::Create($resturi)
$WebRequest.Headers["Authorization"] = "Basic " + $global:encodedCredentials;
$WebRequest.Method = "PUT"
$WebRequest.ContentType = "application/json"
$PostStr = [System.Text.Encoding]::UTF8.GetBytes($json)
$WebRequest.ContentLength = $PostStr.Length
$requestStream = $WebRequest.GetRequestStream()
$requestStream.Write($PostStr, 0,$PostStr.length)
$requestStream.Close()
[System.Net.WebResponse] $resp = $WebRequest.GetResponse();
$rs = $resp.GetResponseStream();
[System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;
[string] $results = $sr.ReadToEnd();
$resp.Close()
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Host ($jirakey + ": " + $FailedItem + " - The error message was " + $ErrorMessage)
} |
REST Documentation: https://docs.atlassian.com/jira/REST/latest/ and