使用TfvcHttpClient Clas获取最新的签入信息

时间:2017-11-03 14:39:10

标签: tfs azure-devops azure-devops-rest-api

尝试使用来自控制台应用程序的客户端API从Team Foundation Server中的特定文件夹中使用TfvcHttpClient类获取最新的已签入信息。

请帮助我如何实现它?我有个人访问令牌,下面提到的是能够通过使用它连接:

代码:

string uri = _uri;

string personalAccessToken = _personalAccessToken;

string project = _project;

string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalAccessToken)));


//create wiql object
        var wiql = new
        {
            query = "Select [State], [Title] " +
                    "From WorkItems " +
                    "Where [Work Item Type] = 'Bug' " +
                    "And [System.TeamProject] = '" + project + "' " +
                    "And [System.State] <> 'Closed' " +
                    "Order By [State] Asc, [Changed Date] Desc"
        };

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(uri);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);

            //serialize the wiql object into a json string   
            var postValue = new StringContent(JsonConvert.SerializeObject(wiql), Encoding.UTF8, "application/json"); //mediaType needs to be application/json for a post call

            //send query to REST endpoint to return list of id's from query
            var method = new HttpMethod("POST");
            var httpRequestMessage = new HttpRequestMessage(method, uri + "/_apis/wit/wiql?api-version=2.2") { Content = postValue };
            var httpResponseMessage = client.SendAsync(httpRequestMessage).Result;
}

我尝试过以下提到的代码来实现:

 VssConnection connection = new VssConnection(serverUrl, new 
 VssBasicCredential(string.Empty, _personalAccessToken));

            var buildServer = connection.GetClient<BuildHttpClient>(); // connect to the build server subpart
            var sourceControlServer = connection.GetClient<Microsoft.TeamFoundation.SourceControl.WebApi.TfvcHttpClient>(); // connect to the TFS source control subpart

            var changesets = buildServer.GetChangesBetweenBuildsAsync("client-rsa", 1, 5).Result;
            foreach (var changeset in changesets)
            {
                var csDetail = sourceControlServer.GetChangesetAsync("client-rsa", Convert.ToInt32(changeset.Id.Replace("C", string.Empty).Trim()), includeDetails: true).Result;
                var checkinNote = csDetail.CheckinNotes?.FirstOrDefault(_ => _.Name == "My check-in note");
                if (checkinNote != null)
                {
                    Console.WriteLine("{0}: {1}", changeset.Id, changeset.Message);
                    Console.WriteLine("Check-in note: {0}", checkinNote.Value);
                }
                else
                    Console.WriteLine("Warning: {0} has no check-in note", changeset.Id);
            } 

1 个答案:

答案 0 :(得分:1)

以下是使用TfvcHttpClient获取最新变更集信息的简单示例代码:

    string purl = "https://xxx.visualstudio.com";
    string projectname = "projectname";

    VssCredentials creds = new VssClientCredentials();
    creds.Storage = new VssClientCredentialStorage();
    VssConnection vc = new VssConnection(new Uri(purl),creds);

    TfvcHttpClient thc = vc.GetClient<TfvcHttpClient>();

    TfvcChangesetSearchCriteria tcsc = new TfvcChangesetSearchCriteria();

    //Specify the server path of the folder
    tcsc.ItemPath = "$/XXXX/XXXX";
    //Get the entire history of the specified path
    List<TfvcChangesetRef> changerefs = thc.GetChangesetsAsync(projectname,null,null,null,null,tcsc).Result;
    //Get the latest changeset ref
    TfvcChangesetRef changeref = changerefs.First();

    //Get the changeset
    TfvcChangeset changeset = thc.GetChangesetAsync(projectname,changeref.ChangesetId).Result;

    //Get the detailed changes
    List<TfvcChange> changes = thc.GetChangesetChangesAsync(changeref.ChangesetId).Result;
    foreach (TfvcChange cg in changes)
    {
        //Code to read detail information
    }
相关问题