【问题标题】:Create Issue on JIRA using REST API and Powershell使用 REST API 和 Powershell 在 JIRA 上创建问题
【发布时间】:2012-10-20 13:58:25
【问题描述】:

我一直在尝试与 JIRA REST API(版本 = 5.2-m06-4)进​​行交互

我的代码如下所示:

function ConvertTo-Base64($string) {
   $bytes  = [System.Text.Encoding]::UTF8.GetBytes($string);
   $encoded = [System.Convert]::ToBase64String($bytes); 

   return $encoded;
}

$reqBody = [System.Text.Encoding]::UTF8.GetBytes($data)

        try{

                $authenticationDetails = ConvertTo-Base64 '$username:$password'
                Write-Host('Opening a connection to {0}' -f $url)

                $req = [System.Net.WebRequest]::Create($Url)
                $req.Headers.Add("AUTHORIZATION", $headerValue);
                $req.Method = "POST"
                $req.ContentType = "application/json"         
                $req.Timeout = $Timeout.TotalMilliseconds

                $req.Credentials = New-Object System.Net.NetworkCredential -ArgumentList $username, $password 
                $req.PreAuthenticate = $true

                $req.ContentLength = $reqBody.Length   
                $reqStream = $req.GetRequestStream()            
                $reqStream.Write($reqBody, 0, $reqBody.Length)
                $reqStream.Close()

                Write-Host($data)

                $resp = $req.GetResponse()
                Write-Verbose $resp
                Write-Output $resp.StatusCode
        }
        catch [System.Net.WebException]{
                if ($_.Exception -ne $null -and $_.Exception.Response -ne $null) {
                        $errorResult = $_.Exception.Response.GetResponseStream()
                        $errorText = (New-Object System.IO.StreamReader($errorResult)).ReadToEnd()
                        Write-Warning "The remote server response: $errorText"
                        Write-Output $_.Exception.Response.StatusCode
                        Write-Host $_
                } else {
                        throw $_
                }
        }

传递JSON的代码如下:

$jsonString = '{"fields": {"project":{ "key": "CCB"},"components": "' + $COMPONENT + '"},"customfield_11502" : "' + $DEPLOYMENT_DATE + '","Summary": "' + $description + '","issuetype": {"name": "配置更改"}}}'

我遇到的问题是,当我尝试以这种方式发布数据时,我被告知摘要、customfield_11502 和组件不是屏幕的一部分。当我尝试删除 JSON 的这些属性时,我被告知我无权发布问题。

这里正确的错误信息是什么?是不是我没有被授权,如果是这样,屏幕上缺少的字段真的不是问题吗?

当通过 REST API 传递用户名和密码时,我对其进行 Base64 编码并将其传递到标头中。我有什么明显的遗漏吗?

【问题讨论】:

  • 您的 Authorization 标头值不应该以 Basic 开头,然后是 Base64 编码的用户名:密码值。

标签: powershell-2.0 jira


【解决方案1】:

天哪,这看起来是一种过于复杂的方法。我有大量的 JIRA 自动化与 powershell 一起使用,我只是使用 cURL(因为我懒得将它们全部转换为 Invoke-RESTblahblah),在那里我将我的 URL 作为字符串传递并有一个配置文件可以完成所有操作其他的东西。

作为替代方法,这对您有什么好处吗?

【讨论】:

  • 你能给我们举个例子吗?告诉提问者他们的方式过于复杂(也许!)是一回事,但要帮助他们,向他们展示你是如何做到的 :-)
  • 我无法正确显示代码。下面使用替代设置自定义字段:'{"fields":{"customfield_10149":{"id":"'+$projectid+'"}}}' | Out-file -encoding ascii SetProject.json $URL = '-X PUT --data @SetProject.json https://myinstance.jira.com/rest/api/latest/issue/'+$Issue cmd /c "curl.exe $URL" Remove-Item SetProject.json 我在许多不同的地方使用这种方法;虽然我没有使用上面显示的错误捕获(我遇到这个问题的次数......一年多可能不到 10 次),但我怀疑它可以相对容易地添加。
【解决方案2】:

当使用 powershell 与任何 REST API 进行通信时,我强烈建议您迁移到 powershell v3,这样您不仅可以使用 Invoke-RestMethod 还可以使用 ConvertTo[From]-Json cmdlet。也就是说,我还没有尝试让基本身份验证工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多