2015-07-13 17 views
11

Tôi đã cài đặt TFS 2015 RC2 tại chỗ. Tôi đang cố gắng sử dụng REST API để xếp hàng một bản dựng trong vNext definiton.Cách kích hoạt bản dựng trong TFS 2015 bằng REST API

Tôi đang sử dụng mẫu mã từ VSO với các sửa đổi nhỏ (chủ yếu là thay đổi URL và phương thức xác thực để làm việc với TFS tại chỗ).

Có hai lệnh gọi API REST tôi đang sử dụng.

Đầu tiên là: GET http://mytfssrv:8080/tfs/DefaultCollection/myproject/_apis/build/definitions/

nào trả về tất cả quy định dự án xây dựng các định nghĩa: xây dựng định nghĩa với ID 1, đó là một định nghĩa XAML build Tôi không quan tâm đến xếp hàng trong và xây dựng định nghĩa với ID 2, đó là định nghĩa xây dựng vNext - đó là nơi II muốn xếp hàng bản dựng của tôi

Lưu ý rằng tôi bỏ qua phần? Api-version = 1.0 - đó là vì nếu không, tôi chỉ nhận định nghĩa XAML.

Cuộc gọi thứ hai là phải xếp hàng một xây dựng mới trong vNext xây dựng định nghĩa:

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

với các dữ liệu sau:

{"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} 

Câu trả lời tôi nhận được từ máy chủ là:

TF215016: Định nghĩa bản dựng 2 không tồn tại. Chỉ định định nghĩa xây dựng hợp lệ và thử lại.

Tôi đã thử thay đổi phiên bản API, thay đổi dữ liệu bài theo nhiều cách khác nhau nhưng chưa bao giờ thành công.

Bất kỳ ý tưởng nào về cách chữa TFS khỏi DID?

Trả lời

7

TFS 2015 RC2 sử dụng API mới (phiên bản 2.0 xem trước.2). Mẫu VSO tôi đã đề cập trong câu hỏi đã lỗi thời và không liên quan khi bạn muốn xếp hàng một bản dựng mới.

Hiện tại, không có tài liệu nào nhưng cổng web sử dụng API REST để chỉ Fiddler đi.

Đây là mã:

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"); 

Và đây là lớp với các thông số mới cho xây dựng các yêu cầu:

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

Đây là tài liệu hiện tại: https://www.visualstudio.com/integrate/api/build/builds#Queueabuild – gregjhogan

0

Đây là "ví dụ về mã ví dụ" mà tôi yêu cầu trong tiền thưởng.

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); 
    } 
} 
Các vấn đề liên quan