public void Export <T>(string filePath, ExportMetadata metadata, T exportObject) { var x = exportObject.ToDynamic(metadata); var serializerSettings = JsonSerialization.GetDefaultSerializerSettings(); var serializedObject = JsonConvert.SerializeObject(x, serializerSettings); fileSystem.WriteAllBytes(filePath, Encoding.UTF8.GetBytes(serializedObject)); log.DebugFormat("Export file {0} successfully created.", filePath); }
public static dynamic ToDynamic(this object value, ExportMetadata metadata) { IDictionary<string, object> expando = new ExpandoObject(); if (value is IEnumerable) { var items = new List<object>(); var values = value as IEnumerable; foreach (var v in values) { items.Add(GetExpandoObject(v)); } expando.Add("Items", items); expando.Add("$Meta", metadata); } else { expando = GetExpandoObject(value); expando.Add("$Meta", metadata); } return expando; }
protected override void Export(Dictionary<string, string> parameters) { if (string.IsNullOrWhiteSpace(parameters["Name"])) throw new CommandException("Please specify the name of the project to export using the paramater: --name=XYZ"); var projectName = parameters["Name"]; Log.Debug("Finding project: " + projectName); var project = Repository.Projects.FindByName(projectName); if (project == null) throw new CommandException("Could not find project named: " + projectName); Log.Debug("Finding project group for project"); var projectGroup = Repository.ProjectGroups.Get(project.ProjectGroupId); if (projectGroup == null) throw new CommandException("Could not find project group for project " + project.Name); Log.Debug("Finding variable set for project"); var variables = Repository.VariableSets.Get(project.VariableSetId); if (variables == null) throw new CommandException("Could not find variable set for project " + project.Name); Log.Debug("Finding deployment process for project"); var deploymentProcess = Repository.DeploymentProcesses.Get(project.DeploymentProcessId); if (deploymentProcess == null) throw new CommandException("Could not find deployment process for project " + project.Name); Log.Debug("Finding NuGet feed for deployment process..."); var nugetFeeds = new List<ReferenceDataItem>(); foreach (var step in deploymentProcess.Steps) { foreach (var action in step.Actions) { string nugetFeedId; if (action.Properties.TryGetValue("Octopus.Action.Package.NuGetFeedId", out nugetFeedId)) { Log.Debug("Finding NuGet feed for step " + step.Name); var feed = Repository.Feeds.Get(nugetFeedId); if (feed == null) throw new CommandException("Could not find NuGet feed for step " + step.Name); if (nugetFeeds.All(f => f.Id != nugetFeedId)) { nugetFeeds.Add(new ReferenceDataItem(feed.Id, feed.Name)); } } } } Log.Debug("Finding action templates for project"); var actionTemplates = new List<ReferenceDataItem>(); foreach (var step in deploymentProcess.Steps) { foreach (var action in step.Actions) { string templateId; if (action.Properties.TryGetValue("Octopus.Action.Template.Id", out templateId)) { Log.Debug("Finding action template for step " + step.Name); var template = actionTemplateRepository.Get(templateId); if (template == null) throw new CommandException("Could not find action template for step " + step.Name); if (actionTemplates.All(t => t.Id != templateId)) { actionTemplates.Add(new ReferenceDataItem(template.Id, template.Name)); } } } } var libraryVariableSets = new List<ReferenceDataItem>(); foreach (var libraryVariableSetId in project.IncludedLibraryVariableSetIds) { var libraryVariableSet = Repository.LibraryVariableSets.Get(libraryVariableSetId); if (libraryVariableSet == null) { throw new CommandException("Could not find Library Variable Set with Library Variable Set Id " + libraryVariableSetId); } libraryVariableSets.Add(new ReferenceDataItem(libraryVariableSet.Id, libraryVariableSet.Name)); } LifecycleResource lifecycle = null; if (project.LifecycleId != null) { lifecycle = Repository.Lifecycles.Get(project.LifecycleId); if (lifecycle == null) { throw new CommandException("Could not find lifecycle with Id " + project.LifecycleId + " for project " + project.Name); } } var export = new ProjectExport { Project = project, ProjectGroup = new ReferenceDataItem(projectGroup.Id, projectGroup.Name), VariableSet = variables, DeploymentProcess = deploymentProcess, NuGetFeeds = nugetFeeds, ActionTemplates = actionTemplates, LibraryVariableSets = libraryVariableSets, Lifecycle = lifecycle != null ? new ReferenceDataItem(lifecycle.Id, lifecycle.Name) : null }; var metadata = new ExportMetadata { ExportedAt = DateTime.Now, OctopusVersion = Repository.Client.RootDocument.Version, Type = typeof (ProjectExporter).GetAttributeValue((ExporterAttribute ea) => ea.Name), ContainerType = typeof (ProjectExporter).GetAttributeValue((ExporterAttribute ea) => ea.EntityType) }; FileSystemExporter.Export(FilePath, metadata, export); }
protected override void Export(Dictionary <string, string> parameters) { if (string.IsNullOrWhiteSpace(parameters["Name"])) { throw new CommandException("Please specify the name of the project to export using the paramater: --name=XYZ"); } var projectName = parameters["Name"]; Log.Debug("Finding project: " + projectName); var project = Repository.Projects.FindByName(projectName); if (project == null) { throw new CommandException("Could not find project named: " + projectName); } Log.Debug("Finding project group for project"); var projectGroup = Repository.ProjectGroups.Get(project.ProjectGroupId); if (projectGroup == null) { throw new CommandException("Could not find project group for project " + project.Name); } Log.Debug("Finding variable set for project"); var variables = Repository.VariableSets.Get(project.VariableSetId); if (variables == null) { throw new CommandException("Could not find variable set for project " + project.Name); } Log.Debug("Finding deployment process for project"); var deploymentProcess = Repository.DeploymentProcesses.Get(project.DeploymentProcessId); if (deploymentProcess == null) { throw new CommandException("Could not find deployment process for project " + project.Name); } Log.Debug("Finding NuGet feed for deployment process..."); var nugetFeeds = new List <ReferenceDataItem>(); foreach (var step in deploymentProcess.Steps) { foreach (var action in step.Actions) { string nugetFeedId; if (action.Properties.TryGetValue("Octopus.Action.Package.NuGetFeedId", out nugetFeedId)) { Log.Debug("Finding NuGet feed for step " + step.Name); var feed = Repository.Feeds.Get(nugetFeedId); if (feed == null) { throw new CommandException("Could not find NuGet feed for step " + step.Name); } if (nugetFeeds.All(f => f.Id != nugetFeedId)) { nugetFeeds.Add(new ReferenceDataItem(feed.Id, feed.Name)); } } } } Log.Debug("Finding action templates for project"); var actionTemplates = new List <ReferenceDataItem>(); foreach (var step in deploymentProcess.Steps) { foreach (var action in step.Actions) { string templateId; if (action.Properties.TryGetValue("Octopus.Action.Template.Id", out templateId)) { Log.Debug("Finding action template for step " + step.Name); var template = actionTemplateRepository.Get(templateId); if (template == null) { throw new CommandException("Could not find action template for step " + step.Name); } if (actionTemplates.All(t => t.Id != templateId)) { actionTemplates.Add(new ReferenceDataItem(template.Id, template.Name)); } } } } var libraryVariableSets = new List <ReferenceDataItem>(); foreach (var libraryVariableSetId in project.IncludedLibraryVariableSetIds) { var libraryVariableSet = Repository.LibraryVariableSets.Get(libraryVariableSetId); if (libraryVariableSet == null) { throw new CommandException("Could not find Library Variable Set with Library Variable Set Id " + libraryVariableSetId); } libraryVariableSets.Add(new ReferenceDataItem(libraryVariableSet.Id, libraryVariableSet.Name)); } var export = new ProjectExport { Project = project, ProjectGroup = new ReferenceDataItem(projectGroup.Id, projectGroup.Name), VariableSet = variables, DeploymentProcess = deploymentProcess, NuGetFeeds = nugetFeeds, ActionTemplates = actionTemplates, LibraryVariableSets = libraryVariableSets }; var metadata = new ExportMetadata { ExportedAt = DateTime.Now, OctopusVersion = Repository.Client.RootDocument.Version, Type = typeof(ProjectExporter).GetAttributeValue((ExporterAttribute ea) => ea.Name), ContainerType = typeof(ProjectExporter).GetAttributeValue((ExporterAttribute ea) => ea.EntityType) }; FileSystemExporter.Export(FilePath, metadata, export); }
protected override void Export(Dictionary <string, string> paramDictionary) { if (string.IsNullOrWhiteSpace(paramDictionary["Project"])) { throw new CommandException("Please specify the project name using the parameter: --project=XYZ"); } if (string.IsNullOrWhiteSpace(paramDictionary["ReleaseVersion"])) { throw new CommandException("Please specify the release, or range of releases using the parameter: --releaseVersion=1.0.0 for a single release, or --releaseVersion=1.0.0->1.0.3 for a range of releases"); } var projectName = paramDictionary["Project"]; var releaseVersion = paramDictionary["ReleaseVersion"]; Log.Debug("Finding project: " + projectName); var project = Repository.Projects.FindByName(projectName); if (project == null) { throw new CommandException("Could not find a project named: " + projectName); } Log.Debug("Finding releases for project..."); var releases = Repository.Projects.GetReleases(project); var releasesToExport = new List <ReleaseResource>(); SemanticVersion minVersionToExport; SemanticVersion maxVersionToExport; if (releaseVersion.IndexOf("->", StringComparison.Ordinal) > 0) { var releaseVersions = releaseVersion.Split(new[] { "->" }, StringSplitOptions.RemoveEmptyEntries); if (releaseVersions.Count() > 2) { throw new CommandException("Incorrect format for exporting multiple releases, please specify the release versions as 1.0.0->1.0.3"); } minVersionToExport = SemanticVersion.Parse(releaseVersions[0]); maxVersionToExport = SemanticVersion.Parse(releaseVersions[1]); } else if (releaseVersion.IndexOf("-", StringComparison.Ordinal) > 0) { var releaseVersions = releaseVersion.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries); if (releaseVersions.Count() > 2) { throw new CommandException("Incorrect format for exporting multiple releases, please specify the release versions as 1.0.0-1.0.3"); } minVersionToExport = SemanticVersion.Parse(releaseVersions[0]); if (!SemanticVersion.TryParse(releaseVersions[1], out maxVersionToExport)) { minVersionToExport = SemanticVersion.Parse(releaseVersion); maxVersionToExport = minVersionToExport; } } else { minVersionToExport = SemanticVersion.Parse(releaseVersion); maxVersionToExport = minVersionToExport; } while (releases.Items.Count > 0) { foreach (var release in releases.Items) { var version = SemanticVersion.Parse(release.Version); if (minVersionToExport <= version && version <= maxVersionToExport) { Log.Debug("Found release " + version); releasesToExport.Add(release); if (minVersionToExport == maxVersionToExport) { break; } } } if (((minVersionToExport == maxVersionToExport) && releasesToExport.Count == 1) || !releases.HasLink("Page.Next")) { break; } releases = Repository.Client.List <ReleaseResource>(releases.Link("Next")); } var metadata = new ExportMetadata { ExportedAt = DateTime.Now, OctopusVersion = Repository.Client.RootDocument.Version, Type = typeof(ReleaseExporter).GetAttributeValue((ExporterAttribute ea) => ea.Name), ContainerType = typeof(ReleaseExporter).GetAttributeValue((ExporterAttribute ea) => ea.EntityType) }; FileSystemExporter.Export(FilePath, metadata, releasesToExport); }
protected override void Export(Dictionary<string, string> paramDictionary) { if (string.IsNullOrWhiteSpace(paramDictionary["Project"])) throw new CommandException("Please specify the project name using the parameter: --project=XYZ"); if (string.IsNullOrWhiteSpace(paramDictionary["ReleaseVersion"])) throw new CommandException("Please specify the release, or range of releases using the parameter: --releaseVersion=1.0.0 for a single release, or --releaseVersion=1.0.0->1.0.3 for a range of releases"); var projectName = paramDictionary["Project"]; var releaseVersion = paramDictionary["ReleaseVersion"]; Log.Debug("Finding project: " + projectName); var project = Repository.Projects.FindByName(projectName); if (project == null) throw new CouldNotFindException("a project named", projectName); Log.Debug("Finding releases for project..."); var releases = Repository.Projects.GetReleases(project); var releasesToExport = new List<ReleaseResource>(); SemanticVersion minVersionToExport; SemanticVersion maxVersionToExport; if (releaseVersion.IndexOf("->", StringComparison.Ordinal) > 0) { var releaseVersions = releaseVersion.Split(new[] { "->" }, StringSplitOptions.RemoveEmptyEntries); if (releaseVersions.Count() > 2) throw new CommandException("Incorrect format for exporting multiple releases, please specify the release versions as 1.0.0->1.0.3"); minVersionToExport = SemanticVersion.Parse(releaseVersions[0]); maxVersionToExport = SemanticVersion.Parse(releaseVersions[1]); } else if (releaseVersion.IndexOf("-", StringComparison.Ordinal) > 0) { var releaseVersions = releaseVersion.Split(new[] {'-'}, StringSplitOptions.RemoveEmptyEntries); if (releaseVersions.Count() > 2) throw new CommandException("Incorrect format for exporting multiple releases, please specify the release versions as 1.0.0-1.0.3"); minVersionToExport = SemanticVersion.Parse(releaseVersions[0]); if (!SemanticVersion.TryParse(releaseVersions[1], out maxVersionToExport)) { minVersionToExport = SemanticVersion.Parse(releaseVersion); maxVersionToExport = minVersionToExport; } } else { minVersionToExport = SemanticVersion.Parse(releaseVersion); maxVersionToExport = minVersionToExport; } while (releases.Items.Count > 0) { foreach (var release in releases.Items) { var version = SemanticVersion.Parse(release.Version); if (minVersionToExport <= version && version <= maxVersionToExport) { Log.Debug("Found release " + version); releasesToExport.Add(release); if (minVersionToExport == maxVersionToExport) { break; } } } if (((minVersionToExport == maxVersionToExport) && releasesToExport.Count == 1) || !releases.HasLink("Page.Next")) { break; } releases = Repository.Client.List<ReleaseResource>(releases.Link("Next")); } var metadata = new ExportMetadata { ExportedAt = DateTime.Now, OctopusVersion = Repository.Client.RootDocument.Version, Type = typeof (ReleaseExporter).GetAttributeValue((ExporterAttribute ea) => ea.Name), ContainerType = typeof (ReleaseExporter).GetAttributeValue((ExporterAttribute ea) => ea.EntityType) }; FileSystemExporter.Export(FilePath, metadata, releasesToExport); }