-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
142 lines (106 loc) · 4.63 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using Attachment = Microsoft.TeamFoundation.WorkItemTracking.Client.Attachment;
namespace TFSJiraConversion
{
class Program
{
static void Main(string[] args)
{
var options = new Options();
if (!CommandLine.Parser.Default.ParseArguments(args, options))
{
Console.WriteLine(options.GetUsage());
return;
}
// Connect to the desired Team Foundation Server
TfsTeamProjectCollection tfsServer = new TfsTeamProjectCollection(new Uri(options.ServerNameUrl));
// Authenticate with the Team Foundation Server
tfsServer.Authenticate();
// Get a reference to a Work Item Store
var workItemStore = new WorkItemStore(tfsServer);
var project = GetProjectByName(workItemStore, options.ProjectName);
if (project == null)
{
Console.WriteLine($"Could not find project '{options.ProjectName}'");
return;
}
var query = GetWorkItemQueryByName(workItemStore, project, options.QueryPath);
if (query == null)
{
Console.WriteLine($"Could not find query '{options.QueryPath}' in project '{options.ProjectName}'");
return;
}
var queryText = query.QueryText.Replace("@project", $"'{project.Name}'");
Console.WriteLine($"Executing query '{options.QueryPath}' with text '{queryText}'");
var count = workItemStore.QueryCount(queryText);
Console.WriteLine($"Exporting {count} work items");
var workItems = workItemStore.Query(queryText);
foreach (WorkItem workItem in workItems)
{
StoreAttachments(workItem, options.OutputPath);
}
}
private static void StoreAttachments(WorkItem workItem, string outputPath)
{
// Get attachments
var request = new System.Net.WebClient
{
Credentials = System.Net.CredentialCache.DefaultCredentials
};
// NOTE: If you use custom credentials to authenticate with TFS then you would most likely
// want to use those same credentials here
if (workItem.Attachments.Count == 0)
return;
Console.WriteLine($"Storing {workItem.Attachments.Count} attachments for work item {workItem.Id}");
foreach (Attachment attachment in workItem.Attachments)
{
// Display the name & size of the attachment
Console.WriteLine($" Attachment: '{attachment.Name}' ({attachment.Length} bytes)");
var name = $"{workItem.Id} - {attachment.Name}";
// Save the attachment to a local file
request.DownloadFile(attachment.Uri, Path.Combine(outputPath, name));
}
}
static Project GetProjectByName(WorkItemStore workItemStore, string projectName)
{
return workItemStore.Projects.Cast<Project>().FirstOrDefault(project => project.Name == projectName);
}
static QueryDefinition GetWorkItemQueryByName(WorkItemStore workItemStore, Project project, string queryPath)
{
var hierarchy = workItemStore.GetQueryHierarchy(project);
foreach (var item in hierarchy)
{
if (item.Path == queryPath && item is QueryDefinition)
return item as QueryDefinition;
if (!(item is QueryFolder))
continue;
var query = GetWorkItemQueryByName(item as QueryFolder, queryPath);
if (query != null)
return query;
}
return null;
}
static QueryDefinition GetWorkItemQueryByName(QueryFolder folder, string queryPath)
{
foreach (var item in folder)
{
Console.WriteLine($"{item.Name} {item.Path}");
if (item.Path == queryPath && item is QueryDefinition)
return item as QueryDefinition;
if (!(item is QueryFolder))
continue;
var query = GetWorkItemQueryByName(item as QueryFolder, queryPath);
if (query != null)
return query;
}
return null;
}
}
}