简体   繁体   English

是否可以通过Azure DevOps REST API获取一批文本内容?

[英]Is it possible to get a batch of text content through Azure DevOps REST API?

I need to get (not download) the content from 10.000~ manifest files within a project in Azure DevOps, but I don't manage to achieve this.我需要从 Azure DevOps 中的项目中的 10.000~ 清单文件中获取(而不是下载)内容,但我无法实现这一点。 I have found several ways to retrieve the content from one file at a time, but in this context, it is neither an efficient nor sustainable solution.我已经找到了多种方法来一次从一个文件中检索内容,但在这种情况下,它既不是有效的也不是可持续的解决方案。 I have managed to retrieve all files of a particular file type by checking if the file path ends with the name of the file, then using the TfvcHttpClientBase.GetItemsBatch method.通过检查文件路径是否以文件名结尾,然后使用 TfvcHttpClientBase.GetItemsBatch 方法,我设法检索了特定文件类型的所有文件。 However, this method does not return the item's content.但是,此方法不返回项目的内容。

Program.cs程序.cs

using Microsoft.TeamFoundation.SourceControl.WebApi;

AzureRest azureRest = new AzureRest();
var tfvcItems = azureRest.GetTfvcItems();
List<TfvcItemDescriptor> itemDescriptorsList = new List<TfvcItemDescriptor>();
foreach(var item in tfvcItems)
{
//Example manifest file .NET
    if (item.Path.EndsWith("packages.config"))
    {
            var itemDescriptor = new TfvcItemDescriptor()
            {
                Path = item.Path,
                RecursionLevel = VersionControlRecursionType.None,
                Version = "",
                VersionOption = TfvcVersionOption.None,
                VersionType = TfvcVersionType.Latest
            };
            itemDescriptorsList.Add(itemDescriptor);
    }
}
TfvcItemDescriptor[] itemDescriptorsArray = itemDescriptorsList.ToArray();
var itemBatch = azureRest.GetTfvcItemsBatch(itemDescriptorsArray);
foreach(var itemList in itemBatch)
{
    foreach(var itemListList in itemList)
    {
        Console.WriteLine("Content: " + itemListList.Content); //empty/null
        Console.WriteLine("ContentMetadata: " + itemListList.ContentMetadata); //not empty/null
    }
}

AzureRest.cs AzureRest.cs

using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
public class AzureRest
    {
        const string ORG_URL = "https://org/url/url";
        const string PROJECT = "Project";
        const string PAT = "PersonalAccessToken";

        private string GetTokenConfig()
        {
            return PAT;
        }

        private string GetProjectNameConfig()
        {
            return PROJECT;
        }

        private VssConnection Authenticate()
        {
            string token = GetTokenConfig();
            string projectName = GetProjectNameConfig();
            var credentials = new VssBasicCredential(string.Empty, token);
            var connection = new VssConnection(new Uri(ORG_URL), credentials);
            return connection;
        }
        public List<TfvcItem> GetTfvcItems()
        {
            var connection = Authenticate();
            using (TfvcHttpClient tfvcClient = connection.GetClient<TfvcHttpClient>())
            {
                var tfvcItems = tfvcClient.GetItemsAsync(scopePath: "/Path", recursionLevel: VersionControlRecursionType.Full, true).Result;
                return tfvcItems;
            }
        }
        public List<List<TfvcItem>> GetTfvcItemsBatch(TfvcItemDescriptor[] itemDescriptors)
        {
            TfvcItemRequestData requestData = new TfvcItemRequestData()
            {
                IncludeContentMetadata = true,
                IncludeLinks = true,
                ItemDescriptors = itemDescriptors
            };
            var connection = Authenticate();
            using (TfvcHttpClient tfvcClient = connection.GetClient<TfvcHttpClient>())
            {
                var tfvcItems = tfvcClient.GetItemsBatchAsync(requestData).Result;
                return tfvcItems;
            }
        }
    }
}

For reference:以供参考:
I have tested the codes you shared and when debugging at "itemDescriptorsList" and have found that there is no content specified in it, so that's why you cannot get the txt content.我已经测试了你分享的代码和在“itemDescriptorsList”调试时发现其中没有指定内容,所以你无法获取txt内容。 在此处输入图像描述 You should first check and add the content property into the "itemDescriptorsList".您应该首先检查内容属性并将其添加到“itemDescriptorsList”中。

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

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