How do I create a bug in TFS using REST API in Pow

2019-07-12 16:31发布

问题:

I am trying to create a bug in TFS using REST API in PowerShell with the code below, but I'm unable to figure out how to fill the $Bug variable with names of those param's and data.

Param(
   [string]$vstsAccount = "MyAccountName",
   [string]$projectName = "ProjectName",
   [string]$keepForever = "true",
   [string]$user = "",
   [string]$token = "Mytoken"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
#$uri = "https://$($vstsAccount).visualstudio.com/$($projectName)/_apis/wit/workitems/$Bug?api-version=2.2"
$result = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

I could find a sample for C# here, but not for PowerShell. Any help would be appreciated.

Cheers

回答1:

You need to create a JSON body to use the REST API to create a work item in PowserShell, and the Content-Type should be application/json-patch+json, also use PATCH method. See Create a work item for details.

You can reference below sample PowerShell script to create a bug:

Param(
   [string]$baseurl = "http://server:8080/tfs/DefaultCollection",
   [string]$projectName = "ProjectName",
   [string]$keepForever = "true",
   [string]$user = "username",
   [string]$token = "token"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

function CreateJsonBody
{

    $value = @"
[
  {
    "op": "add",
    "path": "/fields/System.Title",
    "value": "0925Bug"
  },
  {
    "op": "add",
    "path": "/fields/System.AreaPath",
    "value": "LCScrum"
  },

  {
    "op": "add",
    "path": "/fields/System.IterationPath",
    "value": "LCScrum\\Sprint 1"
  },

  {
    "op": "add",
    "path": "/fields/System.Tags",
    "value": "Tag0921;Tag0926;Tag0927;Tag0928"
  },

  {
    "op": "add",
    "path": "/fields/Microsoft.VSTS.Common.Activity",
    "value": "Development"
  },

  {
    "op": "add",
    "path": "/fields/Microsoft.VSTS.Scheduling.Effort",
    "value": "8"
  },
  {
    "op": "add",
    "path": "/fields/Microsoft.VSTS.Common.ValueArea",
    "value": "Business"
  },
  {
    "op": "add",
    "path": "/fields/Microsoft.VSTS.Common.Severity",
    "value": "3 - Medium"
  },
  {
        "op": "add",
        "path": "/relations/-",
        "value":
        {
            "rel": "System.LinkTypes.Dependency-Forward",
            "url": "http://server:8080/tfs/DefaultCollection/_apis/wit/workItems/324",
            "attributes":
            {
               "usage": "workItemLink",
               "editable": false,
               "enabled": true,
               "acyclic": true,
               "directional": true,
               "singleTarget": true,
               "topology": "dependency"
            }
        }
    },
    {
        "op": "add",
        "path": "/relations/-",
        "value":
        {
            "rel": "System.LinkTypes.Hierarchy-Reverse",
            "url": "http://server:8080/tfs/DefaultCollection/_apis/wit/workItems/58",
            "attributes":
            {
              "usage": "workItemLink",
              "editable": false,
              "enabled": true,
              "acyclic": true,
              "directional": true,
              "singleTarget": false,
              "topology": "tree"
            }
        }
    }
]
"@

 return $value
}

$json = CreateJsonBody

$uri = "$baseurl/$($projectName)/_apis/wit/workitems/"+"$"+"bug?api-version=2.2"
Write-Host $uri
$result = Invoke-RestMethod -Uri $uri -Method Patch -Body $json -ContentType "application/json-patch+json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}