Tag Archives: YouTrack

Migrating from YouTrack to JIRA

Recently I wanted to migrate about 400 issues and 350 attachments from a YouTrack OnDemand instance to a JIRA InCloud instance. JIRA doesn’t provide an importer that is compatible with YouTrack, so I coded a quick .Net C# application that migrated the data for me.

I started with quick list of my must- and nice-to-haves:

Entity Information to migrate
Projects Name
Issues Title, description, state and priority
Issues Attachments belonging to the issue
Issues Comments, including date and author
Entity Information to migrate
Issues Reporter and assignee
Issues Tags
Projects and Issues Components
Issues Affected and fixed version information
Issues Relationship between issues (duplicate/relates-to etc etc)
Issues Historical information such as when the issue was transitioned from one state to another

I didn’t want to migrate or convert between YouTrack’s WIKI formatting used in issue description/comments and the JIRA way of formatting those fields. In fact it turns out that these formats are very similar, so that was a pleasant surpise when I was finished.

The first choice…REST or Import plugin?

The first choice I had to make was between JIRA’s REST API or JIRA’s JSON Import plugin. I opted for the plugin because the REST-API tends to completely ignore information such as state, dates and users. Being able to control the content of these fields is really crucial for data migration.

Getting the issues out of YouTrack…YouTrack and YouTrackSharp challenges

I already blogged how to get issues and attachments out of YouTrack, so there weren’t too many surprises:

  • YouTrackSharp won’t return the description of a project
  • YouTrackSharp won’t return an issue’s tags (a.k.a. labels) or comments. You need to call IssueManager.GetIssue() for each issue returned from instead of IssueManager.GetAllIssuesForProject()
  • The fieldnames and their types are different between the IssueManager.GetIssue() and IssueManager.GetAllIssuesForProject() calls
  • Version numbers associated with an issue in fields affectedVersion and FixedVersion are stored as a CSV string, not as a ICollection in YouTrackSharp
  • The text of a comment is usually returned as the .Text member of the dynamic object. However, I’ve seen a few issues where its returned as a .text member. In C# this difference in case is significant. I used the following approach to handle both cases:
    try {
        ExportComment.Text = Comment.Text;
    catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException) {
        ExportComment.Text = Comment.text;

Importing the issues into JIRA

JSON Import documentation

The JSON structure that JIRA can import is documented on Overview and details.

The import plug-in basically does the following. Firstly it will create all the users listed in the JSON. Secondly it will create the projects, components and versions. Thirdly it will import the issues into each project. If the issue contains an attachment, it will download it from the specified URL/webserver into your JIRA instance’s datastore and attach it to the issue. Finally it will create any links between the issues.

Don’t worry about the license limit on the number of users. The plug-in will create them all, but any user above your license limit wont be granted access to the JIRA application and wont count towards the license. Also, after the import is complete, its fine to delete the attachments from your URL.

So the requirements for my application were:

  1. Be able to list all distinct users that are referenced somewhere in an issue, comment or attachment
  2. Per project, be able to list all distinct components that are referenced somewhere in an issue
  3. Per project, be able to list all distinct versions that are referenced somewhere in an issue field
  4. Be able to list all distinct relationships between issues. These relationships could in theory be cross-project
  5. Per issue, be able to translate YouTrack’s values for fields into JIRA’s equivalent. Specifically the following:
    • YouTrack’s state field to JIRA’s state and resolution
    • YouTrack’s default 4 priority values to JIRA’s default 6 priority values
    • YouTrack’s name for Issue types to JIRA’s name for Issue types
  6. Be able to translate YouTrack’s usernames to JIRA’s usernames. I had a few users that existed in both systems with slightly different usernames
  7. Be able to place the downloaded attachments from YouTrack on webserver that JIRA can access and write that URL into the JSON datastructure.

Jira JSON import gotcha’s

  • You are not allowed to supply the resolved date in the issue object. created and updated are fine though
  • The JSON import documentation doesn’t make it clear that you can control what the key of an imported issue should be using the key property of an issue object. If you don’t supply this property, then JIRA will simply give each issue a key equal to the order in which its listed in the JSON. This will almost always be a problem as its very common for issues to in YouTrack to have been deleted
  • YouTrack can handle 1 issue containing multiple attachments with the same name. The JIRA JSON import will throw the following exception and will stop importing more attachments for the issue. I only had 1 issue that had 2 attachments with the same name in YouTrack and I removed one of them
    com.atlassian.jira.plugins.importer.external.ExternalException: com.atlassian.jira.web.util.AttachmentException: Could not save attachment to storage: java.io.FileNotFoundException: /data/service/j2ee_jira/catalina-base/temp/jira-importers-plugin-downloader-2621864330368162205.tmp (No such file or directory)
    	at com.atlassian.jira.plugins.importer.imports.importer.impl.ExternalUtils.attachFile(ExternalUtils.java:354)
    	at com.atlassian.jira.plugins.importer.imports.importer.impl.DefaultJiraDataImporter.createIssue(DefaultJiraDataImporter.java:944)
    	at com.atlassian.jira.plugins.importer.imports.importer.impl.DefaultJiraDataImporter.importIssues(DefaultJiraDataImporter.java:764)
    	at com.atlassian.jira.plugins.importer.imports.importer.impl.DefaultJiraDataImporter.doImport(DefaultJiraDataImporter.java:390)
    Caused by: com.atlassian.jira.web.util.AttachmentException: Could not save attachment to storage: java.io.FileNotFoundException: /data/service/j2ee_jira/catalina-base/temp/jira-importers-plugin-downloader-2621864330368162205.tmp (No such file or directory)
    	at com.atlassian.jira.issue.managers.DefaultAttachmentManager.createAttachmentOnDisk(DefaultAttachmentManager.java:473)
  • YouTrack and Jira have a different interpretation of the direction of the Duplicate issue links.
                        Assume that in YouTrack the follwing link exists: YouTrack: XXX-27 is duplicated by XXX-1
                        Then the IssueLink object will look like this:
                                SourceId	"XXX-27"	string
    		                    TargetId	"XXX-1"	string
    		                    TypeInward	"duplicates"	string
    		                    TypeName	"Duplicate"	string
    		                    TypeOutward	"is duplicated by"	string
                        If we translate that to JIRA's JSON format 
                          "name": "Duplicate",
                          "sourceId": "XXX-27",
                          "destinationId": "XXX-1"
                        Then JIRA will report that XXX-27 duplicates XXX-1. Ergo,for the Duplicate type, we need to swap Source and Target
  • If you have a private installation of JIRA, then you can control the format of the project key. However, in OnDemand instances, the key is restricted to only upper- and lowercase letters, you cant change that. I had 2 projects in YouTrack whose key contained numbers. I could have written a few lines of code to replace the numbers with some letters, but in my case it was far easier to modify the project in YouTrack and remove the numbers.

How to programatically download attachments from YouTrack

In a previous post I showed how to retrieve your bugs, comments and attachments from YouTrack. However, actually downloading each attachment is a little more work. Assume that the variable Url contains a value like "http://xxxxx.myjetbrains.com/youtrack/_persistent/yyyyy.doc?file=xx-xxx&v=0&c=false"

The following raises a HTTP NotFound exception:

var foo = Connection.Get(Url); //YouTrackSharp.Infrastructure.Connection

As does this:

String basePath = new System.Uri(Url).PathAndQuery;
var foo = Connection.Get(basePath );

The following will raise a HTTP NotAuthorized exception because we are not supplying the authentication cookie to the web-server.

byte[] file = new System.Net.WebClient().DownloadData(Url);

I looked at the YouTrackSharp.Infrastructure.Connection class and I didn’t see anything that would let me retrieve the authentication cookie.

Instead of writing code to manually implement the YouTrack REST API, I decided to take a different approach. I decided to modify YouTrackSharp itself to give me access to the authentication cookie.
I removed the NuGet package YouTrackSharp from my solution.
I downloaded the source of YouTrackSharp from GitHub and unzipped it next to my own project.
I included src\YouTrackSharp\YouTrackSharp.csproj into my solution
I modified src\YouTrackSharp\Infrastructure to add the public modifier as follows:

public CookieCollection _authenticationCookie;

I changed my own program to download the file as follows:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);
req.CookieContainer = new CookieContainer();
req.Method = "GET";
WebResponse response = req.GetResponse();
System.IO.Stream Stream = response.GetResponseStream();
var fileStream = System.IO.File.Create(String.Format("C:\\tmp\\{0}", Name));

Exporting your bugs out of YouTrack

If you need to export your bug reports from your YouTrack instance, this is how to get it done using C#. Its a small example that will print all the issues,comments and attachments for each project in your instance.

Don’t forget to enable the REST API in your YourTrack settings page.

Create a new project called YouTrackExport and use NuGet to install the YouTrackSharp package

PM> Install-Package YouTrackSharp
Attempting to resolve dependency 'EasyHttp (≥'.
Attempting to resolve dependency 'JsonFX (≥ 2.0.1209.2802)'.
Installing 'JsonFx 2.0.1209.2802'.

The following code will print out the information:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Dynamic;
using YouTrackSharp.Infrastructure;
using YouTrackSharp.Projects;
using YouTrackSharp.Issues;
using YouTrackSharp.Admin;
using System.Net;

namespace YouTrackExport
    class Program
        static void Main(string[] args)
            String Username = "xxx";
            String Password = "yyy";
            String Site = "zzz.myjetbrains.com";

            YouTrackSharp.Infrastructure.Connection Connection;
            YouTrackSharp.Projects.ProjectManagement ProjectManager;
            IEnumerable<YouTrackSharp.Projects.Project> Projects;
            YouTrackSharp.Issues.IssueManagement IssueManager;
            IEnumerable<YouTrackSharp.Issues.Issue> Issues;
            IEnumerable<YouTrackSharp.Issues.Comment> Comments;

            Connection = new Connection(Site, 80, false, "youtrack");
            Connection.Authenticate(Username, Password);
            ProjectManager = new ProjectManagement(Connection);
            Projects    = ProjectManager.GetProjects();
            foreach (Project project in Projects)
                Console.WriteLine(String.Format("Found project {0} - {1} ", project.ShortName, project.Name));
                IssueManager = new IssueManagement(Connection);
                Issues = IssueManager.GetAllIssuesForProject(project.ShortName); 
                //An issue in youtrack can have many fields that we dont know at compile time.
                //Therefore its a .Net DynamicObject
                foreach (dynamic Issue in Issues) 
                    Console.WriteLine(String.Format("\tFound issue {0}", Issue.Id));

                    Comments = IssueManager.GetCommentsForIssue(Issue.Id);
                    foreach (Comment Comment in Comments)
                        Console.WriteLine(String.Format("\t\tComment: {0} - {1} - {2}", Comment.Author, Comment.Created, Comment.Text));

                    foreach (dynamic Attachment in Issue.Attachments)
                        String Name = Attachment.name;
                        String Url = Attachment.url;
                        String Author = Attachment.authorLogin;
                        String Id = Attachment.id;
                        String Group = Attachment.group;
                        long Created = Attachment.created;
                        Console.WriteLine(String.Format("\t\tAttachment: {0} - {1} - {2}", Name, Author, Url));

Some gotcha’s that I encountered

The following code wont work if your YouTrack instance is hosted in the cloud:

var connection = new Connection("http://zzz.myjetbrains.com/youtrack"); 
var connection = new Connection("zzz.myjetbrains.com/youtrack"); 
var connection = new Connection("zzz.myjetbrains.com"); 

The following code will raise a HTTPException: NotFound Not Found exception. You need to to use the ShortName member:

Issues = IssueManager.GetAllIssuesForProject(project.Name);