Skip to content

Commit

Permalink
Add support for Cruise Control in the wizard.
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztof-lorenc committed May 15, 2019
1 parent 75bfb17 commit b1dea0f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 6 deletions.
87 changes: 86 additions & 1 deletion src/Soloplan.WhatsON.CruiseControl/CruiseControlProjectPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,101 @@

namespace Soloplan.WhatsON.CruiseControl
{
public class CruiseControlProjectPlugin : SubjectPlugin
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Soloplan.WhatsON.ServerBase;

public class CruiseControlProjectPlugin : SubjectPlugin, IProjectsListQuerying, IAssignServerProject
{
public CruiseControlProjectPlugin()
: base(typeof(CruiseControlProject))
{
}

/// <summary>
/// Gets a value indicating whether this plugin supports wizards.
/// </summary>
public override bool SupportsWizard => true;

public override Subject CreateNew(SubjectConfiguration configuration)
{
return new CruiseControlProject(configuration);
}

/// <summary>
/// Gets the projects.
/// </summary>
/// <param name="address">The address.</param>
/// <returns>
/// The projects from the server.
/// </returns>
public async Task<IList<ServerProjectTreeItem>> GetProjects(string address)
{
var result = new List<ServerProjectTreeItem>();
var server = this.GetServerManager().GetServer(address, false);
var allProjects = await server.GetAllProjects();
var serverProjects = new List<ServerProjectTreeItem>();
foreach (var project in allProjects.CruiseControlProject)
{
var serverProjectTreeItem = new ServerProjectTreeItem();
serverProjectTreeItem.Name = project.Name;
serverProjectTreeItem.Address = address;

if (string.IsNullOrWhiteSpace(project.ServerName))
{
result.Add(serverProjectTreeItem);
}
else
{
var serverProject = serverProjects.FirstOrDefault(s => s.Name == project.ServerName);
if (serverProject == null)
{
serverProject = new ServerProjectTreeItem();
serverProject.Name = project.ServerName;
serverProjects.Add(serverProject);
result.Add(serverProject);
}

serverProject.ServerProjects.Add(serverProjectTreeItem);
}
}

return result;
}

/// <summary>
/// Assigns the <see cref="T:Soloplan.WhatsON.ServerProject" /> to <see cref="T:Soloplan.WhatsON.ConfigurationItem" />.
/// </summary>
/// <param name="serverProject">The server project.</param>
/// <param name="configurationItemsSupport">The configuration items provider.</param>
/// <param name="serverAddress">The server address.</param>
public void AssignServerProject(ServerProject serverProject, IConfigurationItemsSupport configurationItemsSupport, string serverAddress)
{
configurationItemsSupport.GetConfigurationByKey(CruiseControlProject.ProjectName).Value = serverProject.Name;
configurationItemsSupport.GetConfigurationByKey(ServerSubject.ServerAddress).Value = serverAddress;
}

/// <summary>
/// Gets the server manager.
/// </summary>
/// <returns>The server manager.</returns>
private ICruiseControlServerManagerPlugIn GetServerManager()
{
var serverManager = PluginsManager.Instance.PlugIns.OfType<ICruiseControlServerManagerPlugIn>().ToList();
var pluginCount = serverManager.Count;
if (pluginCount != 1)
{
if (pluginCount < 1)
{
throw new InvalidOperationException($"No plugin of type {typeof(ICruiseControlServerManagerPlugIn)} found.");
}

throw new InvalidOperationException($"More then one plugins of type {typeof(ICruiseControlServerManagerPlugIn)} found.");
}

return serverManager.FirstOrDefault();
}
}
}
9 changes: 9 additions & 0 deletions src/Soloplan.WhatsON.CruiseControl/CruiseControlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public async Task<CruiseControlJob> GetProjectStatus(CancellationToken cancellat
return (await this.cache).CruiseControlProject.FirstOrDefault(job => job.Name == projectName);
}

/// <summary>
/// Gets all projects from the CC server.
/// </summary>
/// <returns>The list of all projects.</returns>
public async Task<CruiseControlJobs> GetAllProjects()
{
return await this.GetStatusAsync<CruiseControlJobs>(default(CancellationToken), this.address);
}

private async Task<TModel> GetStatusAsync<TModel>(CancellationToken cancellationToken, string requestUrl)
where TModel : class
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CruiseControlServerManagerPlugIn : ICruiseControlServerManagerPlugI
{
private IDictionary<Uri, CruiseControlServer> servers = new Dictionary<Uri, CruiseControlServer>();

public CruiseControlServer GetServer(string address)
public CruiseControlServer GetServer(string address, bool addToCache = true)
{
var uri = new Uri(address);
CruiseControlServer server;
Expand All @@ -24,7 +24,11 @@ public CruiseControlServer GetServer(string address)
}

server = new CruiseControlServer(uri.AbsoluteUri);
this.servers[uri] = server;
if (addToCache)
{
this.servers[uri] = server;
}

return server;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ namespace Soloplan.WhatsON.CruiseControl
{
public interface ICruiseControlServerManagerPlugIn : IPlugIn
{
CruiseControlServer GetServer(string address);
CruiseControlServer GetServer(string address, bool addToCache = true);
}
}
3 changes: 1 addition & 2 deletions src/Soloplan.WhatsON.Jenkins/JenkinsProjectPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace Soloplan.WhatsON.Jenkins
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NLog;
using Soloplan.WhatsON.Jenkins.Model;
Expand Down Expand Up @@ -61,7 +60,7 @@ public async Task<IList<ServerProjectTreeItem>> GetProjects(string address)
/// <param name="serverAddress">The server address.</param>
public void AssignServerProject(ServerProject serverProject, IConfigurationItemsSupport configurationItemsSupport, string serverAddress)
{
// for now, we extract the prject name from the address
// for now, we extract the project name from the address
var projectNameWithoutAddress = serverProject.Address.Substring(serverAddress.Length, serverProject.Address.Length - serverAddress.Length - 1).Trim('/');
if (projectNameWithoutAddress.StartsWith("job", StringComparison.CurrentCultureIgnoreCase))
{
Expand Down

0 comments on commit b1dea0f

Please sign in to comment.