Klemens Altmanninger
Klemens Altmanninger

Reputation: 395

TFS 2017 Update 2 - Use REST API to trigger Pull Request Validation Build

The TFS 2017 Update 2 added the feature for manually starting pull request builds.

Manual triggers are useful for things like automated test runs that might take a long time to run, and you only really need to run once before completing the pull request

TFS Update Releasenotes

As seen in Builds REST API Reference I would now want to trigger a validateShelfset Build, as pressing the button

Queue Build for Pull request validation

would do. When I compared my JSON Request builds with the "Button pressed" builds via http://MYTFS:8080/tfs/COLLECTION/PROJECT/_apis/build/builds?api-version=3.0 the only notable difference was

reason: "validateShelveset",

and the parameterlist. So I changed my JSON to:

POST http://MYTFS:8080/tfs/COLLECTION/PROJECT/_apis/build/builds?api-version=3.0

Calling POST content:

{
  "definition": {
    "id": 2
  },
  "sourceBranch": "refs/pull/26/merge",
  "reason": "validateShelveset",
  "parameters": "{\"system.pullRequest.pullRequestId\":\"26\",\"system.pullRequest.sourceBranch\":\"refs/heads/feature/myfeaturebranch\",\"system.pullRequest.targetBranch\":\"refs/heads/develop\"}"
}

But while web-UI started builds are seen as Pull Request builds, and have the reason validateShelveset

Pull Request Build

the POST started builds are seen as private.

enter image description here

I even tried (just to see whether using the reason field would be recognized by TFS) the reason: checkInShelveset, which TFS Recognized correctly as such a build.

Basically all I want to do is to emulate the "Queue build" button in a cmdline interface, and when I look at the Build API it seems as if I am doing the right thing; am I missing something obvious? The documentation does not cover how to add the (quite newly added feature) manual Pull request builds.

Upvotes: 0

Views: 512

Answers (1)

Andy Li-MSFT
Andy Li-MSFT

Reputation: 30372

Based on my test, you can use the REST API with PATCH method to trigger the Pull Request Validation Build.

  1. Use the API tracking tools such as Fiddler to track API.
  2. Trigger the Pull Request Validation Build with PATCH method use the tracked REST API (invoke the REST API directly, no need to provide the JSON content.):

eg:

PATCH http://server:8080/tfs/DefaultCollection/feb1793b-4d91-4be4-8373-02216ec5c36b/_apis/policy/Evaluations/0891d5a4-fee9-4751-8bc6-ff403c2860f1?api-version=3.2-preview 

enter image description here

Upvotes: 1

Related Questions