如何使用TFS REST API以指定用户对工作项进行更改

问题描述 投票:0回答:1

我需要像Make Changes to a TFS Work Item as a specific user这样的东西但是对于TFS REST API。

所以:

new WorkItemTrackingHttpClient(new Uri("http://server:8080/tfs"), new VssCredentials(...));

我接下来的步骤是什么?

c# rest tfs
1个回答
1
投票

不支持Rest API中的模拟用户,SOAP API支持它。

有一个user voice here建议功能,你可以去投票,以实现在未来发布...

要使用REST API更新工作项,您可以尝试以下示例

public class TFSClient
{
    public WorkItemTrackingHttpClient WorkItem { get; set; }
    public TFSClient()
    {            
        VssCredentials vssCred = new VssCredentials(new WindowsCredential(true));
        WorkItem = new WorkItemTrackingHttpClient(new Uri(TFSServer.Url), vssCred);
    }
}
 public static object UpdateWorkItemByID(int id)
    {
        try
        {
            JsonPatchDocument patchDocument = new JsonPatchDocument
            {
                new JsonPatchOperation()
                {                       
                    Operation = Operation.Add,
                    Path = ItemField.History,
                    Value = "Teste"
                }
            };            
            return  new TFSClient().WorkItem.UpdateWorkItemAsync(patchDocument, id).Result;              

        }

        catch (Exception e)
        {
            throw e;
        }
    }

您还可以通过直接调用特定用户的REST API来更新工作项:

e.f.:

VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.WindowsCredential(new NetworkCredential("username", "password", "domain")));

或者使用PowerShell:

Param(
   [string]$baseurl = "http://server:8080/tfs/DefaultCollection",  
   [string]$workitemid = "39",
   [string]$user = "Domain\user",
   [string]$token = "password"
)

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

function CreateJsonBody
{

    $value = @"
[
  {
    "op": "test",
    "path": "/rev",
    "value": 7
  },
  {
    "op": "add",
    "path": "/fields/System.Title",
    "value": "test0909ddd"
  }

]

"@

 return $value
}

$json = CreateJsonBody

$uri = "$baseurl/_apis/wit/workitems/$($workitemid)?api-version=2.2" #_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)}
© www.soinside.com 2019 - 2024. All rights reserved.