如何在TFS中通过C#从服务挂钩的“状态”中获取价值?

时间:2018-09-07 08:59:59

标签: tfs azure-devops

我的TFS项目中有服务挂钩。如何通过C#获取服务挂钩的信息?我需要钩子的名称和值StateEnabledDisabled)。

有可能吗?

State

1 个答案:

答案 0 :(得分:1)

您可以使用REST API来获取状态值。请参阅Get a list of subscriptions

例如PowerShell:

Param(
   [string]$baseUrl = "https://{account}.visualstudio.com/DefaultCollection/_apis/hooks/subscriptions",  
   [string]$user = "username",
   [string]$token = "password"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))   
$response = (Invoke-RestMethod -Uri $baseUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
$hooks = $response.value 

$states = @()
foreach($hook in $hooks){

    $customObject = new-object PSObject -property @{
          "consumer" = $hook.consumerId
          "id" = $hook.id
          "eventType" = $hook.eventType
          "state" = $hook.status
        } 
    $states += $customObject        
}   
$states | Select `
                consumer, 
                id, 
                eventType,
                state

要使用C#调用REST API,可以引用以下线程:How do I make calls to a REST api using c#?

您还可以参考以下示例:

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace GetBuildsREST
{
    class Program
    {
        public static void Main()
        {
            Task t = GetBuildsREST();
            Task.WaitAll(new Task[] { t });
        }
        private static async Task GetBuildsREST()
        {
            try
            {
                var username = "domain\\user";
                var password = "password";

                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(
                        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(
                            System.Text.ASCIIEncoding.ASCII.GetBytes(
                                string.Format("{0}:{1}", username, password))));

                    using (HttpResponseMessage response = client.GetAsync(
                                "https://{account}.visualstudio.com/DefaultCollection/_apis/hooks/subscriptions?api-version=2.0").Result)
                    {
                        response.EnsureSuccessStatusCode();
                        string responseBody = await response.Content.ReadAsStringAsync();
                        Console.WriteLine(responseBody);
                        //Do somthing to get the vale of the state....
                        //var obj = JObject.Parse(responseBody);
                        //var state = (string)obj["value"]["status"];
                        //or var state = (string)obj.SelectToken("value.status");

                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

enter image description here