Integrate VSTS with Youtrack

Youtrack is a great issue tracking tool. It’s light weight and has very intuitive UI. These two features makes it a great tool for customer issue reporting. Bad part is, that it currently doesn’t have any support for VSTS, so if you would like to update tickets from VSTS it doesn’t provide any out of the box solution. However Youtrack has a very easy to use REST API which can used to build custom integration.

Update Youtrack ticket from VSTS

VSTS doesn’t also provide any tools to integrate with Youtrack either, so to update tickets from VSTS, we need to use the REST API. VSTS has a ”native” support for azure functions, so linking VSTS into azure function and implementing small function we can easily integrate these two great tools.

Create new Azure Function

First create an new Azure Function project (from Visual Studio) which provides basic building blocks for our integration. By default Azure Function refers Newtonsoft.Json 9.0, which is too old version for our use. Add Newtonsoft.Json version 10.2 Nuget package and YouTrackSharp package (which requires at least version 10 from Newtonsoft.Json).
YouTrackSharp provides some very easy to use functions to interact with Youtrack REST API. It’s a much faster way to implement REST calls than by coding them manually.

Create permanent token from Youtrack

Connection to Youtrack is done with BearerTokenConnection class, with this class we can use permanent token to login into Youtrack and it’s a much better way than using our own user-name and password (which we don’t want to check in into version control).
From Youtrack, click Update personal… link from own profile page to get into authentication page
From Authentication page, switch to Authencation tab and click New token… button

Code that function

After creating a connection with BearerTokenConnection we can call CreateIssueService method to retrieve a service which provides methods to update, delete and create new tickets.

var connection = new BearerTokenConnection(””,”perm:…”);
var issueService = connection.CreateIssuesService();

With issue service we can iterate all the tickets we want to by providing Youtrack query as a string parameter:

foreach(var issue in await issueService.GetIssues(”State: {Waiting for deployment}”))
     await issueService.ApplyCommand(issue.Id, ”assigned to me”);

Updating issue is done with ApplyCommand call.

Full example code to update all Waiting for deployment tickets into Deployed state.

public static class Function1
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, ”get”, ”post”, Route = null)]HttpRequestMessage req, TraceWriter log)
   var connection = new BearerTokenConnection(””,”perm:…”);
            var issueService = connection.CreateIssuesService();
    foreach(var issue in await issueService.GetIssues(”State {Waiting for deployment}”))
           await issueService.ApplyCommand(issue.Id, ”Deployed”);
    return req.CreateResponse(HttpStatusCode.OK);

Call Azure Function from VSTS

To call a Azure Function from VSTS, we need to add a new agentless phase to our release definition

Agentless phase can contain a Azure Function call which has only two mandatory settings.
1) Set Azure Function url with /api/ChangeYoutrackStatusToDeployed route
2) Add function key (which is created in Azure Portal)

Now when the release definition is run, VSTS calls our Azure Function, which updates all the ”Waiting for Deployment” tickets into ”Deployed” state.