簡體   English   中英

如何使用TFS REST API以指定用戶的身份對工作項進行更改

[英]How to use TFS REST API for making changes to work item as specified user

我需要像TFS REST API 那樣的特定用戶對TFS 工作項進行更改

所以:

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

我下一步要做什么?

Rest API不支持模擬用戶,SOAP API支持模擬用戶。

這里有用戶聲音來建議該功能,您可以對其進行投票以在將來的版本中實現該功能。

要使用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來更新工作項:

例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM