Exporter les tâches Jira vers le fichier MPP du projet MS
Dans cet article, nous allons montrer comment exporter des tâches de JIRA à l’aide de l’API REST à MPP format de fichier à l’aide d’Aspose.tasks pour .NET.
Exporter des tâches Jira vers MPP
Voici le code simplifié qui démontre les idées clés qui permettent de créer votre propre solution pour exporter des tâches Jira à MPP ou à tout format d’exportation pris en charge par Aspose.tasks pour .NET.
Afin d’exporter des tâches de Jira au format MPP, les étapes suivantes doivent être effectuées:
- Faites une demande à l’API JIRA pour sélectionner les tâches à exporter.
- Créez une instance de classe de projet.
- Traversez le JSON résultant et créez un objet de tâche pour chaque entité “problème” dans le JSON résultant.
- Implémentez la logique pour définir les champs Task.
- Enregistrez l’instance de la classe de projet dans un fichier dans le format requis (MPP dans cet exemple).
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}
This example demonstrates how to retrieve tasks from Jira using the REST API, convert them into Task
objects with Aspose.Tasks, and export them to the MPP format. The process begins with an HTTP request to the Jira API using basic authentication. The received JSON data is parsed and stored in a dictionary for further processing. The TasksBuilder
class is responsible for constructing the task hierarchy by iterating through issues and creating corresponding tasks and subtasks. Each task is assigned extended attributes that include the issue key and a link to the original Jira issue. The implementation includes a placeholder for setting task start and finish dates, as well as duration, which should be defined according to the project’s requirements. Finally, the constructed project is saved to a file in the MPP format.