Export Jira tasks to MS Project MPP file

Contents
[ Hide Show ]

In this article we are going to show how to export tasks from Jira using REST API to MPP file format using Aspose.Tasks for .NET.

Export Jira tasks to MPP

Here is the simplified code which demonstrates the key ideas which allows your to build your own solution to export Jira tasks to MPP or any export format supported by Aspose.Tasks for .NET.

In order to export tasks from Jira to MPP format the following steps should be performed:

  1. Make a request to Jira API to select the tasks to export.
  2. Create an instance of Project class.
  3. Traverse the resulting JSON and create a Task object for each “issue” entity in the resulting JSON.
  4. Implement logic for setting Task.Start, Finish and Duration fields.
  5. Save the instance of Project class to a file in the required format (MPP in this example).
  1using System;
  2using System.Collections.Generic;
  3using System.Net.Http;
  4using Aspose.Tasks;
  5using Newtonsoft.Json.Linq;
  6using Task = Aspose.Tasks.Task;
  7
  8namespace JiraToMpp
  9{
 10    class Program
 11    {
 12        private static readonly string OutputFileName = @"c:\output.mpp";
 13        private static readonly string LicenseFileName = @"c:\Aspose.Tasks.NET.SHA256.lic";
 14        private static readonly string JiraHost = @"https://issue.test.local";
 15        private static readonly string UserName = "user_name";
 16        private static readonly string UserApiKey = "user_api_key";
 17
 18        static async System.Threading.Tasks.Task Main()
 19        {
 20            System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
 21            SetLicense();
 22
 23            HttpClient client = new HttpClient();
 24
 25            AddBasicAuthorizationHeader(client);
 26
 27            string endPointAddress = JiraHost + "/rest/api/2/search?jql=ORDER%20BY%20Created&maxResults=100&startAt=0";
 28            var result = await client.GetStringAsync(endPointAddress);
 29            dynamic deserialized = JObject.Parse(result);
 30            var issues = deserialized.issues;
 31
 32            Project project = new Project();
 33            Dictionary<string, dynamic> rawTasks = new Dictionary<string, dynamic>();
 34            HashSet<string> subTasksSet = new HashSet<string>();
 35
 36            foreach (var issue in issues)
 37            {
 38                rawTasks.Add((string)issue.key, issue);
 39
 40                if (issue.fields.subtasks != null && issue.fields.subtasks.Count > 0)
 41                {
 42                    foreach (var subTask in issue.fields.subtasks)
 43                    {
 44                        subTasksSet.Add((string)subTask.key);
 45                    }
 46                }
 47
 48            }
 49
 50            var builder = new TasksBuilder(project, rawTasks, subTasksSet);
 51            builder.Build();
 52
 53            project.Save(OutputFileName, SaveFileFormat.Mpp);
 54        }
 55
 56        private static void SetLicense()
 57        {
 58            License l = new License();
 59            using (var fs2 = System.IO.File.OpenRead(LicenseFileName))
 60            {
 61                l.SetLicense(fs2);
 62            }
 63        }
 64
 65        private static void AddBasicAuthorizationHeader(HttpClient client)
 66        {
 67            string headerValue = string.Format("{0}:{1}", UserName, UserApiKey);
 68            var bytes = System.Text.Encoding.UTF8.GetBytes(headerValue);
 69            var encodedText = Convert.ToBase64String(bytes);
 70            client.DefaultRequestHeaders.Add("Authorization", "Basic " + encodedText);
 71        }
 72    }
 73
 74    internal sealed class TasksBuilder
 75    {
 76        private readonly Project project;
 77        private readonly Dictionary<string, dynamic> jsonTasks;
 78        private readonly HashSet<string> subTasks;
 79        private readonly HashSet<string> processedIssues = new HashSet<string>();
 80
 81        private ExtendedAttributeDefinition linkExtendedAttribute;
 82        private ExtendedAttributeDefinition jiraKeyExtendedAttribute;
 83
 84        public TasksBuilder(Project project, Dictionary<string, dynamic> jsonTasks, HashSet<string> subTasks)
 85        {
 86            this.project = project;
 87            this.jsonTasks = jsonTasks;
 88            this.subTasks = subTasks;
 89            this.SetUpExtendedAttributes();
 90        }
 91
 92        public void Build()
 93        {
 94            foreach (var kv in this.jsonTasks)
 95            {
 96                if (this.processedIssues.Contains(kv.Key))
 97                {
 98                    continue;
 99                }
100
101                if (this.subTasks.Contains(kv.Key))
102                {
103                    continue;
104                }
105
106                this.ProcessIssue(kv.Value, this.project.RootTask.Children);
107                this.processedIssues.Add(kv.Key);
108            }
109        }
110
111        private void ProcessIssue(dynamic issue, TaskCollection parentCollection)
112        {
113            var taskName = (string)issue.fields["summary"];
114
115            Task task = parentCollection.Add(taskName);
116
117            Console.WriteLine("Added task " + taskName);
118            
119            // TODO : implement logic for setting task's start, finish and duration.
120
121            this.MapExtendedAttributes(issue, task);
122
123            if (issue.fields.subtasks == null)
124            {
125                return;
126            }
127
128            foreach (var subTask in issue.fields.subtasks)
129            {
130                var subTaskKey = (string)subTask.key;
131                var rawSubTask = this.jsonTasks[subTaskKey];
132                this.ProcessIssue(rawSubTask, task.Children);
133            }
134        }
135
136        private void MapExtendedAttributes(dynamic rawIssue, Task task)
137        {
138            var key = (string)rawIssue.key;
139            var link = (string)rawIssue.self;
140
141            task.ExtendedAttributes.Add(this.jiraKeyExtendedAttribute.CreateExtendedAttribute(key));
142            task.ExtendedAttributes.Add(this.linkExtendedAttribute.CreateExtendedAttribute(link));
143        }
144
145        private void SetUpExtendedAttributes()
146        {
147            this.linkExtendedAttribute = ExtendedAttributeDefinition.CreateTaskDefinition(ExtendedAttributeTask.Text1, "Link to the issue");
148            this.jiraKeyExtendedAttribute = ExtendedAttributeDefinition.CreateTaskDefinition(ExtendedAttributeTask.Text2, "Issue Key");
149        }
150    }
151}
view raw JiraToMpp.cs hosted with ❤ by GitHub
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.