如何在 PowerShell 中使用 REST API 在 TFS 中创建错误? [英] How do I create a bug in TFS using REST API in PowerShell?

查看:20
本文介绍了如何在 PowerShell 中使用 REST API 在 TFS 中创建错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PowerShell 中的 REST API 和下面的代码在 TFS 中创建一个错误,但我无法弄清楚如何使用这些参数的名称填充 $Bug 变量和数据.

参数([string]$vstsAccount = "MyAccountName",[string]$projectName = "项目名称",[string]$keepForever = "true",[字符串]$user = "",[string]$token = "Mytoken")# Base64 对个人访问令牌 (PAT) 进行适当编码$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)}

我可以找到 C# 的示例

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

解决方案

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

这篇关于如何在 PowerShell 中使用 REST API 在 TFS 中创建错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆