Skip to content

Commit

Permalink
Merge pull request #67 from skowront/issue18
Browse files Browse the repository at this point in the history
Set default value for Blue Ocean URL by the existence of the plugin
  • Loading branch information
krzysztof-lorenc authored Aug 18, 2020
2 parents 1a73c7a + 7152266 commit 4d4b1ae
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace Soloplan.WhatsON.CruiseControl.GUI
{
using System;
using System.Windows.Navigation;
using Microsoft.Win32;
using Soloplan.WhatsON.Configuration;
using Soloplan.WhatsON.GUI.Common.BuildServer;
using Soloplan.WhatsON.GUI.Common.ConnectorTreeView;
Expand Down
12 changes: 10 additions & 2 deletions src/Soloplan.WhatsON.CruiseControl/CruiseControlConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public CruiseControlConnector(ConnectorConfiguration configuration)
/// <returns>true when fine, false when url is broken.</returns>
public override async Task<bool> CheckServerURL()
{
return false;
return await this.IsReachableUrl(this.Address);
}

/// <summary>
Expand All @@ -50,7 +50,7 @@ public override async Task<bool> CheckServerURL()
/// <returns>true when fine, false when url is broken.</returns>
public override async Task<bool> CheckProjectURL()
{
return false;
return await this.IsReachableUrl(this.directAddress);
}

protected override async Task<Status> GetCurrentStatus(CancellationToken cancellationToken)
Expand All @@ -75,6 +75,14 @@ protected override async Task<Status> GetCurrentStatus(CancellationToken cancell
}
}

if (projectData == null)
{
var status = new Status();
status.ErrorMessage = "Project not available";
status.InvalidBuild = true;
return status;
}

if (projectData.LastBuildLabel == null)
{
var status = new Status();
Expand Down
2 changes: 1 addition & 1 deletion src/Soloplan.WhatsON.CruiseControl/CruiseControlPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public CruiseControlPlugin()
{
}

public override Connector CreateNew(ConnectorConfiguration configuration)
public override Connector CreateNew(ConnectorConfiguration configuration, bool? checkRedirect = null)
{
return new CruiseControlConnector(configuration);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ private async Task PrepareProjectsList()
/// <returns>The task.</returns>
private async Task LoadProjectsFromPlugin(Tuple<ConnectorPlugin, ProjectViewModelList> listQueryingPlugin)
{
var serverProjects = await listQueryingPlugin.Item1.GetProjects(this.ProposedServerAddress);
var serverProjects = await listQueryingPlugin.Item1.GetProjects(this.ProposedServerAddress.Last() != '/' ? this.ProposedServerAddress += '/' : this.ProposedServerAddress);
foreach (var serverProject in serverProjects.OrderBy(x => x.Name))
{
var newProject = listQueryingPlugin.Item2.AddProject(serverProject);
Expand Down
2 changes: 1 addition & 1 deletion src/Soloplan.WhatsON.GUI/TrayHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ private void MainWindowConfigurationApplied(object sender, ValueEventArgs<Applic
this.scheduler.UnobserveAll();
foreach (var connectorConfiguration in this.configuration.ConnectorsConfiguration)
{
var connector = PluginManager.Instance.GetConnector(connectorConfiguration);
var connector = PluginManager.Instance.GetConnector(connectorConfiguration, true);
this.scheduler.Observe(connector);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Soloplan.WhatsON.Jenkins/JenkinsConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public class JenkinsConnector : Connector

public const string ConnectorDisplayName = "Jenkins";

/// <summary>
/// The redirect plugin tag.
/// </summary>
/// <summary>
/// The redirect plugin tag.
/// </summary>
public const string RedirectPlugin = "RedirectPlugin";

private const long TicksInMillisecond = 10000;
Expand Down
36 changes: 35 additions & 1 deletion src/Soloplan.WhatsON.Jenkins/JenkinsPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Soloplan.WhatsON.Jenkins
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Xml.Serialization;
using NLog;
using Soloplan.WhatsON.Composition;
using Soloplan.WhatsON.Configuration;
Expand All @@ -23,15 +24,48 @@ public class JenkinsPlugin : ConnectorPlugin
/// </summary>
private static readonly Logger log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType?.ToString());

private static Dictionary<string, bool> blueOceanCache = new Dictionary<string, bool>();

public JenkinsPlugin()
: base(typeof(JenkinsConnector))
{
}

public override Connector CreateNew(ConnectorConfiguration configuration)
public override Connector CreateNew(ConnectorConfiguration configuration, bool? checkRedirect = null)
{
log.Debug("Creating new JenkinsProject based on configuration {configuration}", new { configuration.Name, configuration.Identifier });
var jenkinsProject = new JenkinsConnector(configuration, new JenkinsApi());

if (checkRedirect == null)
{
return jenkinsProject;
}
else if (checkRedirect == false)
{
return jenkinsProject;
}

if (blueOceanCache.ContainsKey(jenkinsProject.Address))
{
jenkinsProject.Configuration.GetConfigurationByKey("RedirectPlugin").Value = blueOceanCache[jenkinsProject.Address].ToString();
configuration.GetConfigurationByKey("RedirectPlugin").Value = blueOceanCache[jenkinsProject.Address].ToString();
return jenkinsProject;
}

var task = Task.Run(async () => await jenkinsProject.IsReachableUrl(JenkinsApi.UrlHelper.ProjectUrl(jenkinsProject) + JenkinsApi.UrlHelper.RedirectPluginUrlSuffix));
task.Wait();
var result = task.Result;
if (result == true)
{
jenkinsProject.Configuration.GetConfigurationByKey("RedirectPlugin").Value = "True";
configuration.GetConfigurationByKey("RedirectPlugin").Value = "True";
blueOceanCache.Add(jenkinsProject.Address, true);
}
else
{
blueOceanCache.Add(jenkinsProject.Address, false);
}

return jenkinsProject;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Soloplan.WhatsON/Composition/ConnectorPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ protected ConnectorPlugin(Type connectorType)

public string Description { get; }

public abstract Connector CreateNew(ConnectorConfiguration configuration);
public abstract Connector CreateNew(ConnectorConfiguration configuration, bool? checkRedirect = null);

/// <summary>
/// Assigns the <see cref="Project"/> to <see cref="ConfigurationItem"/>.
/// </summary>
/// <param name="project">The server project.</param>
/// <param name="configurationItemsSupport">The configuration items provider.</param>
/// <param name="serverAddress">The server address.</param>
public abstract void Configure(Project project, IConfigurationItemProvider configurationItemsSupport, string serverAddress=null);
public abstract void Configure(Project project, IConfigurationItemProvider configurationItemsSupport, string serverAddress = null);

/// <summary>
/// Gets the projects.
Expand Down
8 changes: 4 additions & 4 deletions src/Soloplan.WhatsON/Composition/PluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public ConnectorPlugin GetPlugin(ConnectorConfiguration connectorConfiguration)
/// <param name="connectorConfiguration">The connector configuration.</param>
/// <returns>Creates new connector with given configuration.</returns>
/// <exception cref="InvalidOperationException">Couldn't find plugin for a type: {connectorConfiguration.TypeName}.</exception>
public Connector CreateNewConnector(ConnectorConfiguration connectorConfiguration)
public Connector CreateNewConnector(ConnectorConfiguration connectorConfiguration, bool? checkRedirect = null)
{
var plugin = this.ConnectorPlugins.FirstOrDefault(p => p.Name == connectorConfiguration.Type);
if (plugin == null)
Expand All @@ -135,7 +135,7 @@ public Connector CreateNewConnector(ConnectorConfiguration connectorConfiguratio
Connector newConnector;
try
{
newConnector = plugin.CreateNew(connectorConfiguration);
newConnector = plugin.CreateNew(connectorConfiguration, checkRedirect);
}
catch (Exception e)
{
Expand All @@ -153,15 +153,15 @@ public Connector CreateNewConnector(ConnectorConfiguration connectorConfiguratio
/// <param name="connectorConfiguration">The connector configuration.</param>
/// <returns>A new connector with given configuration.</returns>
/// <exception cref="InvalidOperationException">Couldn't find plugin for a type: {connectorConfiguration.TypeName}.</exception>
public Connector GetConnector(ConnectorConfiguration connectorConfiguration)
public Connector GetConnector(ConnectorConfiguration connectorConfiguration, bool? checkRedirect = null)
{
var connector = this.connectors.FirstOrDefault(s => s != null && s.Configuration.Identifier == connectorConfiguration.Identifier);
if (connector != null)
{
return connector;
}

return this.CreateNewConnector(connectorConfiguration);
return this.CreateNewConnector(connectorConfiguration, checkRedirect);
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Soloplan.WhatsON/Model/Connector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ public void AddSnapshot(Status status)

public virtual async Task<bool> IsReachableUrl(string urlInput)
{
var url = urlInput;
bool testStatus;
WebRequest request = WebRequest.Create(urlInput);
WebRequest request = WebRequest.Create(url);
request.Timeout = 50; //in ms.
try
{
Expand Down

0 comments on commit 4d4b1ae

Please sign in to comment.