If you’re using YouTrack bug and issue tracker, you probably already know that there is an API for automating bulk tasks. Unfortunately, as of this blog posting, there is not a lot of information on how to use the new API. Many of the blog posts on the Internet reference methods that worked in the previous version, but not in the current version. So I’m going to give you a jump start on how to use the new API.
The new API is completely asynchronous. Keep that in mind, when you don’t get results or Visual Studio complains about your syntax.
Let’s just jump right in and do some sample code. Start a .Net console application and include the following NuGet package:
Install-Package YouTrackSharp -Version 3.5.0
Copy this code and modify as you desire:
class Program { private static BearerTokenConnection _connection; static void Main(string[] args) { _connection = new BearerTokenConnection("https://youtrack.jetbrains.net", "perm:abcdef..."); ListProjects(); ListIssues(); CreateIssue().Wait(); Console.ReadKey(); } static void ListProjects() { var projectService = _connection.CreateProjectsService(); var projects = projectService.GetAccessibleProjects().Result; foreach (var project in projects) { Console.WriteLine(project.ShortName); } } static void ListIssues() { var issueService = _connection.CreateIssuesService(); var issues = issueService.GetIssues().Result; foreach (var issue in issues) { Console.WriteLine(issue.Summary); } } static async Task CreateIssue() { dynamic issue = new Issue(); issue.Summary = "My story"; issue.Description = "Long description of the story"; issue.Type = "Bug"; issue.State = "Submitted"; issue.Priority = "Minor"; var issueService = _connection.CreateIssuesService(); var result = await issueService.CreateIssue("PROJECT NAME", issue); // result will return the issue id that was created in YouTrack } }
I used the bearer token connection manager, but there is also a user name password connection manager that you can use (called UsernamePasswordConnection).
The first two methods called from Main demonstrate how to read projects and issues. They are pretty straightforward. Notice that I used the “.Result” property to extract the asynchronous results.
In order to send something to the YouTrack API, you’ll need to use the await parameter. To do that, create an “async Task” method and call it using the “.Wait()” method. If you try to execute the issueService.CreateIssue() method from a non-async method, it will not perform the action.
The result from the CreateIssue call will return the issue ID that is created in YouTrack. You’ll need to replace the project name in the CreateIssue method call with a valid project name. That will be used to assign your issue to that project.
The Issue class is a dynamic class, so you’ll need to declare it as a dynamic type. This means that you will have to specifically spell out the properties (like Summary, Description, etc.). Auto-complete does not work for obvious reasons. You can use any property name used in your YouTrack system by spelling it the same.
Finally…
If your property in YouTrack contains a space in the name, then you’ll have to use the SetField method, like this:
static async Task CreateIssue() { dynamic issue = new Issue(); issue.Summary = "My story"; issue.Description = "Long description of the story"; issue.Type = "Bug"; issue.State = "Submitted"; issue.Priority = "Minor"; issue.SetField("My Field", "The data required"); var issueService = connection.CreateIssuesService(); var result = await issueService.CreateIssue("PROJECT NAME", issue); // result will return the issue id that was created in YouTrack }