Integrate and change a VSTS pull request

2 minute read

You might have seen it with GitHub; when you do a pull request there will most likely be a build being kicked off and that influences the state of the pull request. A failed build (or any other check) is shown on the Pull Request page.

Visual Studio Team System can do the same; you can define a build that needs to be successful before the Pull Request can be completed. You also can indicate who needs to approve the PR and even that all the remarks need to be resolved. However, you can also do some more interesting tricks by adding additional services that interface with the Pull Request. Such a service allows you to intercept the changes, determine if the PR can continue and block the PR when not allowed to be merged back into the target branch.

For example to check if the code applies to certain rules and conventions, if it has passed a certain qualification from an external system or contains a standard license header.

There is a good tutorial from Microsoft on how to build a nodejs application to do this so I won’t repeat that here. Interesting is that you can use ngrok to map a public domain name to a local port so you can test this from your own machine.


Besides changing the state of the PR (and make it blocked), you can also output comments to the PR. You can do this on individual files and even blocks of code in the file.

Creating a comment is exposed by the vsts node library:

var comments # {
        "comments": [
            "content": "I like your *code* style!",
            "commentType" : 1,
            "parentCommentId" : 0
        "properties": {
            "Microsoft.TeamFoundation.Discussion.SupportsMarkdown": {
                "type": "System.Int32",
                "value": 1
        "status": 1

    vstsGit.createComment(comments, repoId, pullRequestId, 0, projectId).then( result #> {
    }).catch(result #> {

Best is to check with the documentation here as the node library is not up to date.

When you create a PR and have your application configured under service hooks, you will get the following in your PR screen:


When the service hook is defined as a PR integration, it will show up in the branch policies page as an exteral service.

Here you can also make this a mandatory rule. So it has to be approved before it can be merged. If you try to complete a PR and the condition did not apply, you will get a nice warning.


I placed the code on GitHub; this contains some simple logic to mark a PR to pending when you include a WIP in the title or mark it as error when it sees an error word in the text. These are just examples, you would check if it complies with your rules. It might need to have a correct description or certain artifacts.

The code will also add a comment to the PR. You can make threaded conversations, set the state and even annotate files.

You need to set your own Personal Access Token and VSTS address. I used a .env file which you can put in the same folder as your app.js.




Leave a Comment

Your email address will not be published. Required fields are marked *