c# - TFS 2015 Service挂钩发布对Slack的HTTP调用

时间:2018-01-18 17:25:08

标签: tfs azure-devops-rest-api

一直致力于创建一个自定义TFS服务挂钩,它将向Slack发布(HTTP)通知。

我的要求是当Bug工作项的状态更改为Inprogress时,将HTTP调用POST为松弛。 (我已经使用TFS服务器端插件实现了相同的功能。不幸的是,我不得不选择Service Hook over Plugin)

我在TFS 2017 onprem上使用PAT尝试了下面的代码,不幸的是它破了。我做错了什么?我希望我的代码在没有PAT的2015.2中工作。

有人可以帮忙吗?

public async static void CreateServiceHook(string collName, string projName, string projId, string AccessToken)
{
    try
    {
        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);

           string url = "https://" + collName + ".visualstudio.com/DefaultCollection/_apis/projects/" + projName + "?includecapabilities=true&api-version=1.0";
            HttpRequestMessage req = new HttpRequestMessage(new HttpMethod("GET"), url);
            var response = client.SendAsync(req).Result;
            string contents = await response.Content.ReadAsStringAsync();

        }
        //create service hook
        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
            var request = new
            {
                publisherId = "tfs",
                eventType = "workitem.created",
                consumerId = "webHooks",
                consumerActionId = "httpRequest",
                scope = "project",
                publisherInputs = new {
                  //  buildStatus = "",
                    projectId = projId 
                },
                consumerInputs = new
                {
                    url = "https://slack.api.com/services/XXXXXXXXXX
                }

            };
            var response = client.PostAsync("https://" + collName + ".visualstudio.com/DefaultCollection/_apis/hooks/subscriptions/?api-version=1.0",
                new StringContent(JsonConvert.SerializeObject(request).ToString(),
                    Encoding.UTF8, "application/json"))
                    .Result;

            if (response.IsSuccessStatusCode)
            {
                dynamic content = JsonConvert.DeserializeObject(
                    response.Content.ReadAsStringAsync()
                    .Result);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
} 

1 个答案:

答案 0 :(得分:0)

TFS具有默认的松弛服务挂钩,可以将消息发布到通道。因此,您可以使用web hooks消费者,而不是使用slack消费者。而且,只有当Bug工作项的状态更新时,才能实现向Slack发布消息。

API如下所示:

POST http://tfsserver:8080/tfs/DefaultCollection/_apis/hooks/subscriptions?api-version=3.2
Content-Type: application/json
{
"consumerActionId":"postMessageToChannel",
"consumerId":"slack",
"consumerInputs":{
    "url":"https://hooks.slack.com/services/xxxxxx"},
    "eventType":"workitem.updated",
    "publisherId":"tfs",
    "publisherInputs":{
        "areaPath":"",
        "workItemType":"Bug",
        "changedFields":"System.State",
        "projectId":"77e3c775-dc30-4354-xxxx-xxxxxxxxxxxx"},
        "scope":1
}