【问题标题】:How to list all the files that are in TFS GIT repo using REST API如何使用 REST API 列出 TFS GIT 存储库中的所有文件
【发布时间】:2018-11-21 02:42:13
【问题描述】:

所有, 我正在尝试使用 REST API 获取 TFS GIT 中特定存储库中的所有文件的列表。 我找到了下面的,但它只显示“scopePath=/buld.xml”后面提到的特定文件名的内容,它只显示文件build.xml的内容。

但我正在尝试,只是列出特定存储库中的所有文件,而不提及特定文件名。

请帮帮我。

https://{accountName}.visualstudio.com/{project}/_apis/git/repositories/{repositoryId}/items?items?scopePath=/&api-version=4.1

【问题讨论】:

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


    【解决方案1】:

    你可以使用下面的api:

    https://{accountName}.visualstudio.com/{project}/_apis/git/repositories/{repositoryId}/items?recursionLevel=Full&api-version=4.1
    

    【讨论】:

    • 你好 Cece,我试过你建议它不起作用的那个我用过不同的 API 版本,比如 4.0 也仍然不起作用
    • 您使用的是哪个版本的 TFS?你得到了什么错误?我正在使用 VSTS 进行测试,它可以工作。
    • 我正在使用版本 16.122.27409.2 及其本地 TFS,我没有收到任何错误,但我没有得到我想要的东西。以下是我得到的信息。 "count":1,"value":[{"objectId":"id","gitObjectType":"tree","commitId":"ID","path":"/","isFolder":true, "url":"URL/_apis/git/repositories/repodi/items//…"}]}
    • 请仔细检查您是否在api中指定了正确的projectrepositoryId。我刚刚用 TFS 16.131.27701.1 进行了测试,它也可以工作。我添加了一个屏幕截图供您参考。
    • 感谢@CeceDong-MSFT - 这确实帮助了我。我和 OP 有同样的问题 - 我只会看到“/”文件夹。但是,如果我将文件名简单地放在 URL 中,我就可以获取文件(但无法列出它们),例如.../items/My_Project.sln?versionType=Branch&versionOptions=None
    【解决方案2】:

    这也可以使用 VisualStudioOnline 库来实现(在撰写评论之日,它变成了 AzureDevOps):Microsoft.TeamFoundationServer.Client、Microsoft.VisualStudio.Services.Client。

    首先,您需要创建access token。然后只需使用以下代码:

    VssBasicCredential credintials = new VssBasicCredential(String.Empty, "YOUR SECRET CODE HERE");
    VssConnection connection = new VssConnection(new Uri("https://yourserverurl.visualstudio.com/"), credintials);
    GitHttpClient client = connection.GetClient<GitHttpClient>();
    
    List<GitRepository> repositories = await client.GetRepositoriesAsync(true); // or use GetRepositoryAsync()
    var repo = repositories.FirstOrDefault(r => r.Name == "Some.Repo.Name");
    
    GitVersionDescriptor descriptor = new GitVersionDescriptor()
    {
        VersionType = GitVersionType.Branch,
        Version = "develop",
        VersionOptions = GitVersionOptions.None
    };
    
    List<GitItem> items = await client.GetItemsAsync(repo.Id, scopePath: "/", recursionLevel: VersionControlRecursionType.Full, versionDescriptor: descriptor);
    

    在底层它使用的是 REST API。所以如果你用 c# lang 尝试同样的效果,最好将它委托给 lib。

    【讨论】:

      【解决方案3】:

      你需要先调用 items 端点,它会给你一个 objectId(gitObjectType 应该是“tree”):

      http://{tfsURL}/tfs/{collectionId}/{teamProjectId}/_apis/git/repositories/{repositoryId}/items?recursionLevel=Full&api-version=4.1
      

      然后调用trees端点列出树中的对象:

      http://{tfsURL}/tfs/{collectionId}/{teamProjectId}/_apis/git/repositories/{repositoryId}/trees/{objectId}?api-version=4.1
      

      测试

      【讨论】:

        猜你喜欢
        • 2019-12-06
        • 1970-01-01
        • 2011-08-26
        • 2021-05-22
        • 1970-01-01
        • 2019-01-25
        • 2018-02-07
        • 1970-01-01
        • 2018-06-20
        相关资源
        最近更新 更多