.net istemci kitaplıklarını kullanarak Azure DevOps Services bir hata oluşturma

Azure DevOps Services | Azure DevOps Server 2020 | Azure DevOps Server 2019 | TFS 2018-TFS 2013

Yeni bir hata (veya herhangi bir iş öğesi) oluşturmak oldukça basit bir işlemdir. Yalnızca alan değerlerini ayarlamanız ve REST uç noktasına bir JSON-Patch nesnesi göndermeniz yeterlidir.

Önkoşullar

Bir iş öğesi oluşturabilmeniz için önce aşağıdakilere sahip olmanız gerekir.

Visual Studio bir C# projesi oluşturma

Visual Studio içinde c# programlama hakkında bilgi edinmek için, Visual Studio c# programlama belgelerini bulun

C# kod içeriği

Aşağıdaki kod örneğinde birkaç şey vardır:

  1. Kimlik Doğrulaması
    1. PAT kullanarak kimlik bilgileri oluşturma
    2. Azure DevOps Services urı 'niz ve kimlik bilgileriyle bir vssconnection oluşturma
  2. VSSConnection 'larınızı kullanarak istemciyi alma
  3. Hata oluşturma
    1. Alan değerlerini ayarlamak için nesne dizisi oluşturma
    2. Bu diziyi seri hale getirilmiş JSON nesnesine Dönüştür
    3. Bu seri hale getirilmiş JSON nesnesini REST uç noktasına gönder

C# kod parçacığı

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
using Microsoft.VisualStudio.Services.WebApi.Patch;
using Microsoft.VisualStudio.Services.WebApi;
using System.Net.Http.Headers;
using System.Net.Http;
using Newtonsoft.Json;

 
public class CreateBug 
{
    readonly string _uri;
    readonly string _personalAccessToken;
    readonly string _project;

    /// <summary>
    /// Constructor. Manually set values to match your organization. 
    /// </summary>
    public CreateBug()
    {
        _uri = "https://dev.azure.com/{orgName}";
        _personalAccessToken = "personal access token";
        _project = "project name";
    }

    /// <summary>
    /// Create a bug using the .NET client library
    /// </summary>
    /// <returns>Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.WorkItem</returns>    
    public WorkItem CreateBugUsingClientLib()
    {
        Uri uri = new Uri(_uri);
        string personalAccessToken = _personalAccessToken;
        string project = _project;

        VssBasicCredential credentials = new VssBasicCredential("", _personalAccessToken);
        JsonPatchDocument patchDocument = new JsonPatchDocument();

        //add fields and their values to your patch document
        patchDocument.Add(
            new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path = "/fields/System.Title",
                Value = "Authorization Errors"
            }
        );

        patchDocument.Add(
            new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path = "/fields/Microsoft.VSTS.TCM.ReproSteps",
                Value = "Our authorization logic needs to allow for users with Microsoft accounts (formerly Live Ids) - http:// msdn.microsoft.com/library/live/hh826547.aspx"
            }
        );

        patchDocument.Add(
            new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path = "/fields/Microsoft.VSTS.Common.Priority",
                Value = "1"
            }
        );

        patchDocument.Add(
            new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path = "/fields/Microsoft.VSTS.Common.Severity",
                Value = "2 - High"
            }
        );
        VssConnection connection = new VssConnection(uri, credentials);
        WorkItemTrackingHttpClient workItemTrackingHttpClient = connection.GetClient<WorkItemTrackingHttpClient>();

        try
        {
            WorkItem result = workItemTrackingHttpClient.CreateWorkItemAsync(patchDocument, project, "Bug").Result;

            Console.WriteLine("Bug Successfully Created: Bug #{0}", result.Id);

            return result;
        }
        catch (AggregateException ex)
        {
            Console.WriteLine("Error creating bug: {0}", ex.InnerException.Message);
            return null;
        }
    }
}

Sonraki adımlar