-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTFS2012.cs
More file actions
272 lines (202 loc) · 10.5 KB
/
TFS2012.cs
File metadata and controls
272 lines (202 loc) · 10.5 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Countersoft.Gemini.Commons.Entity;
using Countersoft.Gemini.Extensibility.Apps;
using Countersoft.Foundation.Commons.Extensions;
using Countersoft.Gemini.Commons;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Net;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.Framework.Client;
using System.Collections.ObjectModel;
using System.IO;
using Countersoft.Gemini.Contracts;
using Countersoft.Gemini.Commons.Dto;
using Countersoft.Gemini.Commons.System;
using Countersoft.Gemini;
namespace Saucery
{
public class TFS2012
{
private string Username { get; set; }
private string Password { get; set; }
private string Uri { get; set; }
public static bool IsBasicAuth { get; set; }
public TFS2012()
{
try
{
IsBasicAuth = System.Configuration.ConfigurationManager.AppSettings["gemini.tfs.basicauth"].ToBool();
}
catch
{
}
}
public string GetFileContent(GeminiContext gemini, int issueid, string filename, string fullfilename, string workspace, string changesetid, string fileid, string repositoryUrl, bool getPreviousRevision = false)
{
ConnectByImplementingCredentialsProvider connect = new ConnectByImplementingCredentialsProvider();
ICredentials iCred = new NetworkCredential(Username, Password);
connect.setLoginDetails(Username, Password, workspace);
connect.GetCredentials(new Uri(Uri), iCred);
TfsConfigurationServer configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(new Uri(Uri));
configurationServer.Credentials = iCred;
if (TFS2012.IsBasicAuth)
{
configurationServer.ClientCredentials = new TfsClientCredentials(new BasicAuthCredential(iCred));
}
else
{
configurationServer.ClientCredentials = new TfsClientCredentials(new WindowsCredential(iCred));
}
configurationServer.EnsureAuthenticated();
CatalogNode catalogNode = configurationServer.CatalogNode;
ReadOnlyCollection<CatalogNode> tpcNodes = catalogNode.QueryChildren(new Guid[] { CatalogResourceTypes.ProjectCollection },false, CatalogQueryOptions.None);
foreach (CatalogNode tpcNode in tpcNodes)
{
Guid tpcId = new Guid(tpcNode.Resource.Properties["InstanceId"]);
TfsTeamProjectCollection tpc = configurationServer.GetTeamProjectCollection(tpcId);
if (TFS2012.IsBasicAuth) tpc.ClientCredentials = new TfsClientCredentials(new BasicAuthCredential(iCred));
VersionControlServer versionControl = (VersionControlServer)tpc.GetService(typeof(VersionControlServer));
Item file = null;
try
{
//IF file was just added in tfs
if (fileid.ToInt() == 0)
{
Item tmpFile = null;
//Need to catch error if file was deleted, we'll get an error and call the file with parameters as below.
//This would only happen to newly added file, which will not have a itemid as we wouldn't know what it is on first commit of a file
try
{
tmpFile = versionControl.GetItem(string.Concat(fullfilename, "/", filename));
}
catch (VersionControlException ex)
{
tmpFile = versionControl.GetItem(fullfilename + "/" + filename, VersionSpec.Latest, DeletedState.Deleted);
}
if (tmpFile != null)
{
fileid = tmpFile.ItemId.ToString();
}
}
if (fileid.ToInt() > 0)
{
if (getPreviousRevision)
{
if (changesetid.ToInt() > 1)
{
file = versionControl.GetItem(fileid.ToInt(), changesetid.ToInt() - 1, true);
}
}
else
{
file = versionControl.GetItem(fileid.ToInt(), changesetid.ToInt());
}
if (file != null)
{
if (file.DeletionId > 0)
{
return string.Empty;
}
else
{
using (Stream stream = file.DownloadFile())
{
StreamReader rdr = new StreamReader(stream);
return rdr.ReadToEnd();
}
}
}
}
}
catch (VersionControlException ex)
{
GeminiApp.LogException(ex, false);
return string.Empty;
}
catch (Exception ex)
{
GeminiApp.LogException(ex, false);
return string.Empty;
}
}
return string.Empty;
}
public class ConnectByImplementingCredentialsProvider : ICredentialsProvider
{
private string Username { get; set; }
private string Password { get; set; }
private string Workspace { get; set; }
public ICredentials GetCredentials(Uri uri, ICredentials iCredentials)
{
return new NetworkCredential(Username, Password, Workspace);
}
public void NotifyCredentialsAuthenticated(Uri uri)
{
throw new ApplicationException("Unable to authenticate");
}
public void setLoginDetails(string authUsername, string authPassword, string authWorkspace)
{
Username = authUsername;
Password = authPassword;
Workspace = authWorkspace;
}
}
public void SaveLoginDetails(UserDto user, UserWidgetDataDetails userData, GeminiContext gemini)
{
UserWidgetData<List<UserWidgetDataDetails>> userDataRaw = gemini.UserWidgetStore.Get<List<UserWidgetDataDetails>>(user.Entity.Id, Constants.AppId, Constants.ControlId);
if (userDataRaw == null)
{
var data = new UserWidgetData<List<UserWidgetDataDetails>>();
data.Value = new List<UserWidgetDataDetails>();
data.Value.Add(userData);
gemini.UserWidgetStore.Save(user.Entity.Id, Constants.AppId, Constants.ControlId, data.Value);
}
else
{
var tmpUser = userDataRaw.Value.Find(f => f.RepositoryUrl == userData.RepositoryUrl && f.Provider == userData.Provider);
// If a password for this rep already exist, update the details only
if (tmpUser != null)
{
var index = userDataRaw.Value.FindIndex(f => f.RepositoryUrl == userData.RepositoryUrl && f.Provider == userData.Provider);
userDataRaw.Value[index].Password = userData.Password;
userDataRaw.Value[index].Username = userData.Username;
}
else
{
// Add a new user authentication for this user
userDataRaw.Value.Add(userData);
}
gemini.UserWidgetStore.Save(user.Entity.Id, Constants.AppId, Constants.ControlId, userDataRaw.Value);
}
}
public string CreateAuthenticationForm(string url, string repositoryURL, string filename)
{
StringBuilder form = new StringBuilder();
form.Append(string.Format("<div><a href='{0}'>{0}<a></div>", repositoryURL));
form.Append(string.Format("<form id='authentication_form' action='apps/saucery/authenticate/{0}' method='post'>", SourceControlProvider.TFS2012));
form.Append("<input type='text' name='username' placeholder='username' id='username'/>");
form.Append("<input class='margin-bottom-5' type='password' name='password' placeholder='password' id='password'/><br />");
form.Append("<input type='button' class='button-primary button-small' name='tfs_login' id='tfs_login' value='Login'/>");
form.Append("<input type='button' class='button-secondary button-small margin-left-5 cancel' name='tfs_cancel' id='tfs_cancel' value='Cancel'/>");
form.Append(string.Format("<input id='repositoryurl' type='hidden' name='repositoryurl' value='{0}'/>", repositoryURL));
form.Append(string.Format("<input id='filename' type='hidden' name='filename' value='{0}'/>", filename));
form.Append("</form>");
return form.ToString();
}
public bool AuthenticateUser(UserDto user, string repositoryUrl, GeminiContext gemini)
{
UserWidgetData<List<UserWidgetDataDetails>> userDataRaw = gemini.UserWidgetStore.Get<List<UserWidgetDataDetails>>(user.Entity.Id, Constants.AppId, Constants.ControlId);
if (userDataRaw == null) return false;
var data = userDataRaw.Value.Find(f => f.RepositoryUrl == repositoryUrl && f.Provider == SourceControlProvider.TFS2012);
if (data == null) return false;
Username = data.Username;
Password = SecretsHelper.Decrypt(data.Password, SecretsHelper.EncryptionKey);
Uri = data.RepositoryUrl;
return true;
}
}
}