简体   繁体   English

Azure DevOps 使用 API 获取工作项通知

[英]Azure DevOps Get work item notification using API

I want to make a listener that fires an event when any work item gets added/updated/deleted etc.我想创建一个监听器,当任何工作项被添加/更新/删除等时触发一个事件。

Current code当前代码

using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.Notification;
using Microsoft.VisualStudio.Services.WebApi;
using System;

namespace DevOpsApiTest
{
    class Connector
    {
        public void ConnectToDevOps()
        {
            try
            {
                Uri uri = new Uri("https://dev.azure.com/Org");
                VssCredentials creds = new VssBasicCredential("Username", "Password");
                VssConnection connection = new VssConnection(uri, creds);

                WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();
                WorkItem workitem = witClient.GetWorkItemAsync("TestProject", 1).Result;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

How to make a listener that fires an event when I add/update/delete work item on the DevOps project?当我在 DevOps 项目上添加/更新/删除工作项时,如何创建一个触发事件的侦听器?

You can utilize a service hook您可以使用服务挂钩

Service hook publishers define a set of events.服务挂钩发布者定义一组事件。 Subscriptions listen for the events and define actions to take based on the event.订阅侦听事件并根据事件定义要采取的操作。 Subscriptions also target consumers, which are external services that can run their own actions, when an event occurs.订阅还以消费者为目标,消费者是可以在事件发生时运行自己的操作的外部服务。

You can start looking into web hooks , which is easy to get started with, provided that you can publish your piece of code above on a publicly accessible url您可以开始研究web 挂钩,这很容易上手,前提是您可以在可公开访问的 url 上发布上面的代码

Webhooks provide a way to send a JSON representation of an event to any service. Webhook 提供了一种将事件的 JSON 表示发送到任何服务的方法。 All that is required is a public endpoint (HTTP or HTTPS).所需要的只是一个公共端点(HTTP 或 HTTPS)。

The is configured in your project settings and you can configure three of them to fire events on work item created , work item updated and work item deleted respectively.在您的项目设置中配置,您可以配置其中三个以分别在work item createdwork item updatedwork item deleted时触发事件。

The web hook sends a HTTP request to the endpoint you specify with a json payload containing information about the event. web 挂钩向您指定的端点发送 HTTP 请求,其中包含有关事件的信息的json 有效负载 You would have to modify your code to act as a server accepting those requests instead of being a client.您必须修改代码以充当接受这些请求的服务器而不是客户端。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM