Skip to content

Commit

Permalink
Fix bug with importing channels where a channel wouldn't be imported …
Browse files Browse the repository at this point in the history
…as the channel was updated instead.

Also remove the default channel if we're importing a new project and a channel named `Default` does not exist.
  • Loading branch information
hnrkndrssn committed Mar 14, 2016
1 parent 89c673f commit ce01420
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions source/Octopus.Cli/Importers/ProjectImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ProjectImporter : BaseImporter

public bool ReadyToImport { get { return validatedImportSettings != null && !validatedImportSettings.ErrorList.Any(); } }
public IEnumerable<string> ErrorList { get { return validatedImportSettings.ErrorList; } }

public bool KeepExistingProjectChannels { get; set; }

class ValidatedImportSettings : BaseValidatedImportSettings
{
Expand Down Expand Up @@ -432,19 +432,18 @@ void ImportDeploymentProcess(DeploymentProcessResource deploymentProcess,
Repository.DeploymentProcesses.Modify(existingDeploymentProcess);
}

IEnumerable<KeyValuePair<string, ChannelResource>> ImportProjectChannels(IEnumerable<ChannelResource> channels, ProjectResource importedProject, IDictionary<string, LifecycleResource> channelLifecycles)
IEnumerable<KeyValuePair<string, ChannelResource>> ImportProjectChannels(List<ChannelResource> channels, ProjectResource importedProject, IDictionary<string, LifecycleResource> channelLifecycles)
{
Log.Debug("Importing the channels for the project");
var projectChannels = Repository.Projects.GetChannels(importedProject).Items;
var defaultChannel = projectChannels.FirstOrDefault(c => c.IsDefault);
var newDefaultChannel = channels.FirstOrDefault(nc => nc.IsDefault);
var defaultChannelUpdated = false;

foreach (var channel in channels)
{
var existingChannel =
projectChannels.FirstOrDefault(c => c.Name.Equals(channel.Name, StringComparison.OrdinalIgnoreCase)) ??
((channel.IsDefault && defaultChannel != null)
? defaultChannel
: null);
projectChannels.FirstOrDefault(c => c.Name.Equals(channel.Name, StringComparison.OrdinalIgnoreCase));

if (existingChannel != null)
{
Expand All @@ -456,9 +455,16 @@ IEnumerable<KeyValuePair<string, ChannelResource>> ImportProjectChannels(IEnumer
{
existingChannel.LifecycleId = channelLifecycles[channel.LifecycleId].Id;
}
if (existingChannel.IsDefault && !existingChannel.Name.Equals(newDefaultChannel?.Name, StringComparison.OrdinalIgnoreCase))
{
existingChannel.IsDefault = false;
}
existingChannel.Rules.Clear();
existingChannel.Rules.AddRange(channel.Rules);

if (existingChannel.Name.Equals(defaultChannel?.Name))
{
defaultChannelUpdated = true;
}
yield return
new KeyValuePair<string, ChannelResource>(channel.Id, Repository.Channels.Modify(existingChannel));
}
Expand All @@ -471,16 +477,16 @@ IEnumerable<KeyValuePair<string, ChannelResource>> ImportProjectChannels(IEnumer
{
channel.LifecycleId = channelLifecycles[channel.LifecycleId].Id;
}
if (projectChannels.Any(c => c.IsDefault) && channel.IsDefault)
{
channel.IsDefault = false;
}

yield return
new KeyValuePair<string, ChannelResource>(channel.Id, Repository.Channels.Create(channel));
}
}


if (!KeepExistingProjectChannels && !defaultChannelUpdated)
{
Repository.Channels.Delete(defaultChannel);
}
}

ProjectResource ImportProject(ProjectResource project, string projectGroupId, IDictionary<string, LibraryVariableSetResource> libraryVariableSets)
Expand All @@ -489,6 +495,7 @@ ProjectResource ImportProject(ProjectResource project, string projectGroupId, ID
var existingProject = Repository.Projects.FindByName(project.Name);
if (existingProject != null)
{
KeepExistingProjectChannels = true;
Log.Debug("Project already exist, project will be updated with new settings");
existingProject.ProjectGroupId = projectGroupId;
existingProject.DefaultToSkipIfAlreadyInstalled = project.DefaultToSkipIfAlreadyInstalled;
Expand Down

0 comments on commit ce01420

Please sign in to comment.