如何使用REST API触发TFS 2015中的构建

时间:2015-07-13 05:52:13

标签: c# rest tfs

我在本地安装了TFS 2015 RC2。我正在尝试使用REST API在vNext定义中对构建进行排队。

我正在使用VSO中的代码示例稍作修改(主要是更改URL和身份验证方法以使用内部部署TFS)。

我正在使用两个REST API调用。

首先是: 获取http://mytfssrv:8080/tfs/DefaultCollection/myproject/_apis/build/definitions/

返回所有指定的项目构建定义: ID为1的构建定义,这是一个我不想排队的XAML构建定义 并使用ID 2构建定义,这是vNext构建定义 - 这就是我想要对构建进行排队的地方

请注意,我省略了?api-version = 1.0部分 - 那是因为如果我不这样做,我只获得XAML构建定义。

第二个调用是在vNext构建定义中对新构建进行排队:

POST http://mytfssrv:8080/tfs/DefaultCollection/myptoject/_apis/build/requests?api-version=1.0

包含以下数据:

{"definition":{"id":**2**},"reason":"Manual","priority":"Normal","queuePosition":0,"queueTime":"0001-01-01T00:00:00","requestedBy":null,"id":0,"status":null,"url":null,"builds":null}

我从服务器获得的响应是​​:

  

TF215016:构建定义2不存在。指定有效的构建定义,然后重试。

我尝试更改API版本,以各种方式更改帖子数据,但从未成功过。

知道如何从DID中治愈TFS吗?

3 个答案:

答案 0 :(得分:8)

TFS 2015 RC2使用新的API(版本2.0-preview.2)。我在问题中提到的VSO样本已经过时,并且当您希望对新构建进行排队时不相关。

目前,没有文档,但Web门户使用REST API,所以只是Fiddler。

以下是代码:

var buildRequestPOSTData =
                    new BuildRequest()
                    {
                        Definition = new Definition()
                        {
                            Id = firstBuildDefinition.Id
                        },
                        Project = new Project { Id = "project guid" },
                        Queue = new Queue {  Id = 1 },
                        Reason = 1,
                        sourceBranch = "$Branch"
                    };

                responseBody = await QueueBuildAsync(client, buildRequestPOSTData, _baseUrl + "build/Builds");

以下是具有构建请求的新参数的类:

public class BuildRequest
{
    [JsonProperty(PropertyName = "definition")]
    public Definition Definition { get; set; }

    [JsonProperty(PropertyName = "demands")]
    public string Demands { get; set; }

    [JsonProperty(PropertyName = "parameters")]
    public IEnumerable<string> Parameters { get; set; }

    [JsonProperty(PropertyName = "project")]
    public Project Project { get; set; }

    [JsonProperty(PropertyName = "queue")]
    public Queue Queue { get; set; }

    [JsonProperty(PropertyName = "reason")]
    public int Reason { get; set; }

    [JsonProperty(PropertyName = "sourceBranch")]
    public string sourceBranch { get; set; }

    [JsonProperty(PropertyName = "sourceVersion")]
    public string RequestedBy { get; set; }
}

public class Definition
{
    [JsonProperty(PropertyName = "id")]
    public int Id { get; set; }
}

public class Queue
{
    [JsonProperty(PropertyName = "id")]
    public int Id { get; set; }
}

public class Project
{
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }
}

答案 1 :(得分:1)

这就像没有REST的魅力

var tfsurl = new Uri("http://localhost:8080/tfs/<***projectname***>/");    
var ttpc = new TfsTeamProjectCollection(tfsurl);
var bhc = ttpc.GetClient<BuildHttpClient>();
var builds = bhc.GetBuildsAsync("<***projectname***>").Result;
var build = builds
    .Where(x => x != null && x.Definition.Name.Equals("***buildDefinitionName***>"))
    .OrderByDescending(y => y.LastChangedDate)
    .FirstOrDefault();

bhc.QueueBuildAsync(build);

答案 2 :(得分:0)

这是&#34;代码示例的示例&#34;我在赏金中要求。

using Microsoft.TeamFoundation.Build.WebApi;
using Microsoft.VisualStudio.Services.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
internal class TfsBuildHelper
{
    private readonly VssConnection connection;

    private readonly BuildHttpClient client;

    internal TfsBuildHelper(Uri tpcUrl)
    {
        this.connection = new VssConnection(tpcUrl, new VssClientCredentials(true));
        this.client = connection.GetClient<BuildHttpClient>();
    }

    /// <summary>
    /// Returns the build definitions for a specific team project.
    /// </summary>
    public async Task<IEnumerable<DefinitionReference>> GetBuildDefinitionsFromTeamProject(string teamProject)
    {
        return await this.client.GetDefinitionsAsync(project: teamProject, type: DefinitionType.Build);
    }

    /// <summary>
    /// Return build numbers for specific team project and build definition.
    /// </summary>
    public async Task<IEnumerable<string>> GetAvailableBuildNumbers(string teamProject, string buildDefinition)
    {
        var builds = await this.client.GetBuildsAsync(project: teamProject, type: DefinitionType.Build);
        return builds.Select(b => b.BuildNumber);
    }
}