简体   繁体   English

如何在PowerShell中使用REST API在TFS中创建错误?

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

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. 我试图使用下面的代码在PowerShell中使用REST API在TFS中创建一个错误,但是我无法弄清楚如何用这些参数和数据的名称填充$Bug变量。

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. 我可以在这里找到C#的示例,但不能找到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. 您需要创建一个JSON主体以使用REST API在PowserShell中创建工作项,并且Content-Type应该为application/json-patch+json ,还应使用PATCH方法。 See Create a work item for details. 有关详细信息,请参见创建工作项

You can reference below sample PowerShell script to create a bug: 您可以参考以下示例PowerShell脚本来创建错误:

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用REST API获取TFS(Team Foundation Server)工作项(错误,任务)详细信息? - How to get TFS(Team Foundation Server) work items(bug,task) details using REST api? 如何使用Django Rest Framework创建登录API? - How do I create a login API using Django Rest Framework? 在 Powershell for TFS 2015.2 中使用 REST API 时如何保护您的密码而不在脚本中使用它 - While using REST API in Powershell for TFS 2015.2 how to secure your password without using it in the script 如何使用ReST在TFS中找到任务的积压父级 - How do I find the backlog parent of a task in TFS using ReST 如何在REST API中创建堆栈? - How do I create a stack in a REST API? 如何使用 PowerShell 将 REST API 答案转换为正确的 XML? - How do I convert a REST API answer to proper XML using PowerShell? 如何使用Bugzilla REST API发布错误 - How to post a bug using Bugzilla REST API 使用PowerShell在TFS 2015 REST API中构建队列 - Queue Build in TFS 2015 REST API with PowerShell 通过 powershell 使用 REST API 添加 TFS 标签 - Add TFS Tag with REST API via powershell 如何在C#中使用TFS rest API在现有用户故事下创建子任务? - How to create child tasks under existing user story using TFS rest API in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM