VSTS Rest API return specific item from Changeset

2019-08-20 06:09发布

问题:

I'm calling the REST API to return all of the files that are changed from a particular changeset, what I really want to do is just return the URL property of a specific item that has a known path.

So what I have now is the Changeset API calling

https://someplace.visualstudio.com/_apis/tfvc/changesets/19483/changes

This return something like

{
  "count": 10,
  "value": [
    {
      "item": {
        "version": 19483,
        "size": 882,
        "hashValue": "ACWU0KSlO+jbsSJB5IwU4Q==",
        "path": "$/WaveDatabases/MasterData/Custom Scripts/2017-07-17-120218 28 user/MigrationScript.sql",
        "url": "https://someplace.visualstudio.com/_apis/tfvc/items/$/WaveDatabases/MasterData/Custom%20Scripts/2017-07-17-120218%2028%20user/MigrationScript.sql?versionType=Changeset&version=19483"
      },
      "changeType": "add, edit, encoding"
    },
    {
      "item": {
        "version": 19483,
        "size": 55,
        "hashValue": "Wur9rYW/rRYcvRWoVUZO7A==",
        "path": "$/WaveDatabases/MasterData/Custom Scripts/2017-07-17-120218 28 user/ReadonlyMetadata.json",
        "url": "https://someplace.visualstudio.com/_apis/tfvc/items/$/WaveDatabases/MasterData/Custom%20Scripts/2017-07-17-120218%2028%20user/ReadonlyMetadata.json?versionType=Changeset&version=19483"
      },
      "changeType": "add, edit, encoding"
    },
    {
      "item": {
        "version": 19483,
        "size": 379,
        "hashValue": "vHCQymsTXiZVuLMpeoShNg==",
        "path": "$/WaveDatabases/MasterData/Tables/Cust.test.sql",
        "url": "https://someplace.visualstudio.com/_apis/tfvc/items/$/WaveDatabases/MasterData/Tables/Cust.test.sql?versionType=Changeset&version=19483"
      },
      "changeType": "edit"
    }
  ]
}

This returns an array of all of the files in that changeset. I know that in this changeset there will always be a file called MigrationScript.sql underneath some unknown folder. What I want to do is to find a way that will just return that 1 element in the array that has the MigrationScript.sql in it. Additional I want to only return the url property for that 1 element.

This needs to be accomplished through the URL because of the tools that want to use this I can't write code to parse out the results.

回答1:

You can’t accomplish it through the Changeset REST API URL directly.

You can build a web API app to retrieve data by using REST API, then return the corresponding data that you want, after that you can specify web API URL in that tool.

Simple code (Using Microsoft Team Foundation Server Extended Client package)

var u = new Uri("https://starain.visualstudio.com");
     VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "[personal access token]"));           
 var connection = new VssConnection(u, c);
            var tfvcClient = connection.GetClient<TfvcHttpClient>();
           var changes= tfvcClient.GetChangesetChangesAsync(id: 1130).Result;
            foreach(var currentChange in changes.Where(ci=>ci.Item.Path.EndsWith("UnitTest1.cs")))
            {
                string url = currentChange.Item.Url;
            }