Skip to content

Instantly share code, notes, and snippets.

@sva1000
Last active November 30, 2022 10:09
Show Gist options
  • Save sva1000/afe8b1a71f5c781b4a97826e449d8124 to your computer and use it in GitHub Desktop.
Save sva1000/afe8b1a71f5c781b4a97826e449d8124 to your computer and use it in GitHub Desktop.
A simple example which shows how to export issues from Jira using REST API and save them to MPP using Aspose.Tasks for .NET library.
using System;
using System.Collections.Generic;
using System.Net.Http;
using Aspose.Tasks;
using Newtonsoft.Json.Linq;
using Task = Aspose.Tasks.Task;
namespace JiraToMpp
{
class Program
{
private static readonly string OutputFileName = @"c:\output.mpp";
private static readonly string LicenseFileName = @"c:\Aspose.Tasks.NET.SHA256.lic";
private static readonly string JiraHost = @"https://issue.test.local";
private static readonly string UserName = "user_name";
private static readonly string UserApiKey = "user_api_key";
static async System.Threading.Tasks.Task Main()
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
SetLicense();
HttpClient client = new HttpClient();
AddBasicAuthorizationHeader(client);
string endPointAddress = JiraHost + "/rest/api/2/search?jql=ORDER%20BY%20Created&maxResults=100&startAt=0";
var result = await client.GetStringAsync(endPointAddress);
dynamic deserialized = JObject.Parse(result);
var issues = deserialized.issues;
Project project = new Project();
Dictionary<string, dynamic> rawTasks = new Dictionary<string, dynamic>();
HashSet<string> subTasksSet = new HashSet<string>();
foreach (var issue in issues)
{
rawTasks.Add((string)issue.key, issue);
if (issue.fields.subtasks != null && issue.fields.subtasks.Count > 0)
{
foreach (var subTask in issue.fields.subtasks)
{
subTasksSet.Add((string)subTask.key);
}
}
}
var builder = new TasksBuilder(project, rawTasks, subTasksSet);
builder.Build();
project.Save(OutputFileName, SaveFileFormat.Mpp);
}
private static void SetLicense()
{
License l = new License();
using (var fs2 = System.IO.File.OpenRead(LicenseFileName))
{
l.SetLicense(fs2);
}
}
private static void AddBasicAuthorizationHeader(HttpClient client)
{
string headerValue = string.Format("{0}:{1}", UserName, UserApiKey);
var bytes = System.Text.Encoding.UTF8.GetBytes(headerValue);
var encodedText = Convert.ToBase64String(bytes);
client.DefaultRequestHeaders.Add("Authorization", "Basic " + encodedText);
}
}
internal sealed class TasksBuilder
{
private readonly Project project;
private readonly Dictionary<string, dynamic> jsonTasks;
private readonly HashSet<string> subTasks;
private readonly HashSet<string> processedIssues = new HashSet<string>();
private ExtendedAttributeDefinition linkExtendedAttribute;
private ExtendedAttributeDefinition jiraKeyExtendedAttribute;
public TasksBuilder(Project project, Dictionary<string, dynamic> jsonTasks, HashSet<string> subTasks)
{
this.project = project;
this.jsonTasks = jsonTasks;
this.subTasks = subTasks;
this.SetUpExtendedAttributes();
}
public void Build()
{
foreach (var kv in this.jsonTasks)
{
if (this.processedIssues.Contains(kv.Key))
{
continue;
}
if (this.subTasks.Contains(kv.Key))
{
continue;
}
this.ProcessIssue(kv.Value, this.project.RootTask.Children);
this.processedIssues.Add(kv.Key);
}
}
private void ProcessIssue(dynamic issue, TaskCollection parentCollection)
{
var taskName = (string)issue.fields["summary"];
Task task = parentCollection.Add(taskName);
Console.WriteLine("Added task " + taskName);
// TODO : implement logic for setting task's start, finish and duration.
this.MapExtendedAttributes(issue, task);
if (issue.fields.subtasks == null)
{
return;
}
foreach (var subTask in issue.fields.subtasks)
{
var subTaskKey = (string)subTask.key;
var rawSubTask = this.jsonTasks[subTaskKey];
this.ProcessIssue(rawSubTask, task.Children);
}
}
private void MapExtendedAttributes(dynamic rawIssue, Task task)
{
var key = (string)rawIssue.key;
var link = (string)rawIssue.self;
task.ExtendedAttributes.Add(this.jiraKeyExtendedAttribute.CreateExtendedAttribute(key));
task.ExtendedAttributes.Add(this.linkExtendedAttribute.CreateExtendedAttribute(link));
}
private void SetUpExtendedAttributes()
{
this.linkExtendedAttribute = ExtendedAttributeDefinition.CreateTaskDefinition(ExtendedAttributeTask.Text1, "Link to the issue");
this.jiraKeyExtendedAttribute = ExtendedAttributeDefinition.CreateTaskDefinition(ExtendedAttributeTask.Text2, "Issue Key");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment