How do I create a bug in TFS using REST API in PowerShell?

痴心易碎 提交于 2019-12-11 03:09:25

问题


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)}



来源:https://stackoverflow.com/questions/46308106/how-do-i-create-a-bug-in-tfs-using-rest-api-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!