0

我正在尝试编写一些 PowerShell 函数来与我们的 Atlassian JIRA 系统(JIRA 5.2,下载版)进行交互。不幸的是,我通过反复试验发现Invoke-RestMethod似乎不起作用(不支持身份验证标头),因此我编写了一个名为 Invoke-JiraMethod 的简单函数。我可以确认此方法适用于 GET 请求;我已经能够使用它来让 Jira 对象满足我的心愿。但是,一旦我尝试创建问题,我就开始收到 HTTP 400 / Bad request 错误。

我已按照此处的步骤获取问题元数据,并且正在填写输入对象中的所有必填字段。谁能帮我弄清楚如何解决 400 错误?如果需要,我可以提供更多信息 - 我只是不想超出问题的描述。:)

Function Invoke-JiraMethod
{
    <#
    .Synopsis
        Low-level function that directly invokes a REST method on JIRA
    .Description
        Low-level function that directly invokes a REST method on JIRA.  This is designed for 
        internal use.
    #>
    [CmdletBinding()]
    param
    (
        [ValidateSet("Get","Post")] [String] $Method,
        [String] $URI,
        [String] $Username,
        [String] $Password
    )

    process
    {
        $token = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("${Username}:${Password}"))

        $webRequest = [System.Net.WebRequest]::Create($URI)
        $webRequest.Method = $Method.ToUpper()

        $webRequest.AuthenticationLevel = "None"
        $webRequest.Headers.Add('Authorization', "Basic $token")
        #$webRequest.Headers.Add('Authorization', $token)
        $webRequest.PreAuthenticate = $true
        $webRequest.ContentType = "application/json"

        Write-Verbose "Invoking JIRA method $Method with URI $URI"
        $response = $webRequest.GetResponse()
        $requestStream = $response.GetResponseStream()
        $readStream = New-Object -TypeName System.IO.StreamReader -ArgumentList $requestStream
        $json = $readStream.ReadToEnd()
        $readStream.Close()
        $response.Close()

        $result = $json | ConvertFrom-Json

        Write-Output $result
    }
}

Function New-JiraIssue
{
    param
    (
        [Parameter(Mandatory = $true,
                   Position = 0)]
        [string] $ProjectKey,

        [Parameter(Mandatory = $true,
                   Position = 1)]
        [string] $IssueType,

        [Parameter(Mandatory = $false)]
        [string] $Priority = 3,

        [Parameter(Mandatory = $true,
                   Position = 2)]
        [string] $Summary,

        [Parameter(Mandatory = $true,
                   Position = 3)]
        [string] $Description,

        [Parameter(Mandatory = $true,
                   Position = 4)]
        [string] $Location,

        [Parameter(Mandatory = $true,
                   Position = 5)]
        [string] $Phone,

        [Parameter(Mandatory = $false)]
        [string] $Reporter,

        [Parameter(Mandatory = $false)]
        [PSCredential] $Credential
    )

    process
    {
        $ProjectObject = New-Object -TypeName PSObject -Property @{"key"=$ProjectKey}
        $IssueTypeObject = New-Object -TypeName PSObject -Property @{"id"=$IssueType}

        if ( -not ($Reporter))
        {
            Write-Verbose "Reporter not specified; defaulting to $JiraDefaultUser"
            $Reporter = $JiraDefaultUser
        }

        $ReporterObject = New-Object -TypeName PSObject -Property @{"name"=$Reporter}

        $fields = New-Object -TypeName PSObject -Property ([ordered]@{
            "project"=$ProjectObject;
            "summary"=$Summary;
            "description"=$Description;
            "issuetype"=$IssueTypeObject;
            "priority"=$Priority;
            "reporter"=$ReporterObject;
            "labels"="";
            $CustomFields["Location"]=$Location;
            $CustomFields["Phone"]=$Phone;
            })


        $json = New-Object -TypeName PSObject -Property (@{"fields"=$fields}) | ConvertTo-Json

        Write-Verbose "Created JSON object:`n$json"

        # https://muwebapps.millikin.edu/jira/rest/api/latest/issue/IT-2806
        # $result = Invoke-RestMethod -Uri $JiraURLIssue -Method Post -ContentType "application/json" -Body $json -Credential $Credential

        if ($Username -or $Password)
        {
            $result = (Invoke-JiraMethod -Method Post -URI "${JiraURLIssue}" -Username $Username -Password $Password)
        } else {
            $result = (Invoke-JiraMethod -Method Post -URI "${JiraURLIssue}" -Username $JiraDefaultUser -Password $JiraDefaultPassword)
        }

        Write-Output $result
    }
}

提前致谢!

4

1 回答 1

0

我收到错误 400,因为我放入 json 数据的问题类型 ID 号没有映射到任何东西。提琴手帮我诊断。

我使用这段代码来确定通过invoke-restmethod的-Headers选项对jira进行身份验证:http: //poshcode.org/3461

然后我将 json 数据放入一个 here 字符串中,然后运行 ​​invoke-restmethod。

下面的代码位。用您从 jira 管理员获得的实际项目和问题类型 ID 替换“任何有效的...类型 ID 号”。

$uri = "$BaseURI/rest/api/2/issue" 


    $jsonString = @'
{
    "fields": {
       "project":
       {
          "id": "any valid project id number"
       },
       "summary": "No REST for the Wicked.",
       "description": "Creating of an issue using ids for projects and issue types using the REST API",
       "issuetype": {
          "id": "any valid issue type id number"
       }
   }
}  
'@  


        $headers = Get-HttpBasicHeader $Credentials

        Invoke-RestMethod -uri $uri -Headers $headers  -Method Post -Body $jsonString -ContentType "application/json"
于 2014-06-18T05:39:13.690 回答