diff --git a/BuildClient/BuildClient.csproj b/BuildClient/BuildClient.csproj index 781fa7c..537d7f2 100644 --- a/BuildClient/BuildClient.csproj +++ b/BuildClient/BuildClient.csproj @@ -43,54 +43,55 @@ ..\packages\Autofac.Wcf.3.0.0\lib\net40\Autofac.Integration.Wcf.dll - + False - ..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.Build.Client.dll + ..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\hwshzicg.nz2\Microsoft.TeamFoundation.Build.Client.dll True - + False - ..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.Build.Common.dll + ..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\hwshzicg.nz2\Microsoft.TeamFoundation.Build.Common.dll True - + False - ..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.Client.dll + ..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\hwshzicg.nz2\Microsoft.TeamFoundation.Client.dll True - + False - ..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.Common.dll + ..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\hwshzicg.nz2\Microsoft.TeamFoundation.Common.dll True - + False - ..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.VersionControl.Client.dll + ..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\hwshzicg.nz2\Microsoft.TeamFoundation.VersionControl.Client.dll True - + False - ..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.VersionControl.Common.dll + ..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\hwshzicg.nz2\Microsoft.TeamFoundation.VersionControl.Common.dll True - + False - ..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.VersionControl.Common.Integration.dll + ..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\hwshzicg.nz2\Microsoft.TeamFoundation.VersionControl.Common.Integration.dll True - - False - ..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v4.5\Microsoft.VisualStudio.Services.Client.dll - True + + ..\..\..\..\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\hwshzicg.nz2\Microsoft.VisualStudio.Services.Client.dll - - False - ..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v4.5\Microsoft.VisualStudio.Services.Common.dll + + ..\..\..\..\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\hwshzicg.nz2\Microsoft.VisualStudio.Services.Common.dll + + + ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll True + @@ -129,6 +130,7 @@ + diff --git a/BuildClient/BuildManager.cs b/BuildClient/BuildManager.cs index 0e7a700..088ccd4 100644 --- a/BuildClient/BuildManager.cs +++ b/BuildClient/BuildManager.cs @@ -35,8 +35,8 @@ public void StartProcessing(object stateInfo) Tracing.Server.TraceInformation("Server: StartProcessing (loop)"); _lockExpiryTimer = - new Timer(PollBuildServer, null, Timeout.Infinite, Timeout.Infinite); - _lockExpiryTimer.Change(0, Timeout.Infinite); + new Timer(PollBuildServer, null, Timeout.Infinite, GetPollingPeriod()); + _lockExpiryTimer.Change(0, GetPollingPeriod()); } private int GetPollingPeriod() @@ -55,7 +55,8 @@ private void PollBuildServer(object state) () => Tracing.Client.TraceInformation( "Getting list of build store events"), - () => _lockExpiryTimer.Change(DueTime, GetPollingPeriod()) + () => { } + //() => _lockExpiryTimer.Change(DueTime, GetPollingPeriod()) ); } catch (Exception exception) diff --git a/BuildClient/ClientApplication.cs b/BuildClient/ClientApplication.cs index 71031d0..86cb794 100644 --- a/BuildClient/ClientApplication.cs +++ b/BuildClient/ClientApplication.cs @@ -49,7 +49,7 @@ private static IContainer LoadContainer() var builder = new ContainerBuilder(); builder.RegisterType().As(); builder.RegisterType().As(); - builder.RegisterType().As(); + builder.RegisterType().As(); builder.RegisterType().As().SingleInstance(); builder.Register(c => new TfsServiceProvider(c.Resolve().TeamFoundationUrl,c.Resolve())) .As().SingleInstance(); diff --git a/BuildClient/VsoBridge.cs b/BuildClient/VsoBridge.cs new file mode 100644 index 0000000..2295ff5 --- /dev/null +++ b/BuildClient/VsoBridge.cs @@ -0,0 +1,145 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; +using BuildClient.Configuration; +using Newtonsoft.Json; + +namespace BuildClient +{ + public class VsoBridge: IBuildEventSystem, IDisposable + { + private readonly IBuildConfigurationManager _buildConfigurationManager; + private static readonly string Version = "2.0"; + private readonly HttpClient _client; + private readonly ResponseList _projects; + private readonly List> _builds; + + public VsoBridge(IBuildConfigurationManager buildConfigurationManager) + { + _buildConfigurationManager = buildConfigurationManager; + + + var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($":{_buildConfigurationManager.TfsAccountPassword}")); + var projectsToMonitor = _buildConfigurationManager + .BuildMappers.ToArray(); + + _client = new HttpClient + { + BaseAddress = new Uri(_buildConfigurationManager.TeamFoundationUrl) + }; + _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials); + _client.DefaultRequestHeaders.Accept.Clear(); + _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + + //connect to the REST endpoint + var response = _client.GetAsync("_apis/projects?stateFilter=All&api-version=1.0").Result; + + + _builds = new List>(); + //check to see if we have a succesfull respond + if (!response.IsSuccessStatusCode) return; + + //set the viewmodel from the content in the response + var value = response.Content.ReadAsStringAsync().Result; + _projects = JsonConvert.DeserializeObject>(value); + + var query = from element in projectsToMonitor + from proj in _projects.Value.Where(p => p.Name == element.TfsProjectToMonitor) + select new + { + proj.Id, + BuildName = element.TfsBuildToMonitor + }; + + foreach (var source in query) + { + var defnUrl = + $"{_buildConfigurationManager.TeamFoundationUrl}/DefaultCollection/{source.Id}/_apis/build/definitions?api-version={Version}&query=Name eq '{source.BuildName}'&statusFilter=completed&$top=1"; + + var defnResponse = _client.GetAsync(defnUrl).Result; + if (!defnResponse.IsSuccessStatusCode) return; + + var buildDefn = defnResponse.Content.ReadAsStringAsync().Result; + var viewModel = JsonConvert.DeserializeObject>(buildDefn); + + _builds.AddRange( + viewModel.Value.Select(buildDefnDto => new Tuple(source.Id, buildDefnDto.Id, new BuildData + { + BuildName = source.BuildName + }))); + } + } + + public IEnumerable GetBuildStoreEvents() + { + return from b in _builds + let url = + $"{_buildConfigurationManager.TeamFoundationUrl}/DefaultCollection/{b.Item1}/_apis/build/builds?api-version={Version}&definitions={b.Item2}&$top=1" + let buildResponse = _client.GetAsync(url).Result + let buildValue = buildResponse.Content.ReadAsStringAsync().Result + let dto = JsonConvert.DeserializeObject>(buildValue) + let first = dto.Value.FirstOrDefault() + select new BuildStoreEventArgs + { + Data = new BuildData + { + BuildName = b.Item3.BuildName, + BuildRequestedFor = first.RequestedFor.DisplayName, + EventType = BuildStoreEventType.Build, + Quality = "", + Status = (BuildExecutionStatus)Enum.Parse(typeof(BuildExecutionStatus), first.Result ?? first.Status ?? "Unknown", true) + }, + Type = BuildStoreEventType.Build + }; + } + + public void Dispose() + { + _client?.Dispose(); + } + } + + public class ProjectDto + { + public Guid Id { get; set; } + public string Name { get; set; } + + public string State { get; set; } + + public string Url { get; set; } + + public int Revision { get; set; } + } + + public class ResponseList + { + public int Count { get; set; } + + public T[] Value { get; set; } + } + + public class BuildDto + { + public string BuildNumber { get; set; } + public string Result { get; set; } + + public string Status { get; set; } + + public Person RequestedFor { get; set; } + } + + public class Person + { + public string DisplayName { get; set; } + } + + public class BuildDefnDto + { + public int Id { get; set; } + } +} diff --git a/BuildClient/packages.config b/BuildClient/packages.config index 5a1acdf..7d6762e 100644 --- a/BuildClient/packages.config +++ b/BuildClient/packages.config @@ -2,5 +2,6 @@ + \ No newline at end of file