/
Program.cs
218 lines (180 loc) · 9.52 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Octopus.Client;
using Octopus.Client.Model;
namespace OctopusHelper
{
internal class Program
{
private static void Main(string[] args)
{
try
{
var parametersDictionary = new Dictionary<ParameterEnum, string>();
if (args.Length == 4)
{
parametersDictionary.Add(ParameterEnum.ServerUrl, args[0].Trim());
parametersDictionary.Add(ParameterEnum.ApiKey, args[1].Trim());
parametersDictionary.Add(ParameterEnum.ProjectName, args[2].Trim());
parametersDictionary.Add(ParameterEnum.OutputPath, args[3].Trim());
}
else
{
Console.WriteLine($"Required the parameter of ServerUrl, ApiKey and ProjectName");
Environment.Exit(1);
}
var endpoint = new OctopusServerEndpoint(parametersDictionary[ParameterEnum.ServerUrl], parametersDictionary[ParameterEnum.ApiKey]);
var repository = new OctopusRepository(endpoint);
var project = repository.Projects.FindOne(p => p.Name == parametersDictionary[ParameterEnum.ProjectName]);
var variablesList = new List<VariableViewModel>();
//Dictionary to get Names from Ids
var scopeNames = repository.Environments.FindAll().ToDictionary(x => x.Id, x => x.Name);
repository.Machines.FindAll().ToList().ForEach(x => scopeNames[x.Id] = x.Name);
repository.Projects.GetChannels(project).Items.ToList().ForEach(x => scopeNames[x.Id] = x.Name);
var allEnvironments = repository.Environments.FindAll();
var allRoles = repository.MachineRoles.GetAllRoleNames();
var deploymentSteps = repository.DeploymentProcesses.Get(project.DeploymentProcessId).Steps.ToList();
deploymentSteps.SelectMany(x => x.Actions).ToList().ForEach(x => scopeNames[x.Id] = x.Name);
//Get All Library Set Variables
var allVariables = repository.LibraryVariableSets.FindAll();
ExportVariableSets(parametersDictionary, deploymentSteps, repository, project, variablesList, scopeNames, allVariables, allEnvironments);
//var input = Console.ReadLine();
Environment.Exit(0);
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error {ex.Message}|{ex.StackTrace}");
Environment.Exit(1);
throw ex;
}
}
private static void ExportVariableSets(Dictionary<ParameterEnum, string> parametersDictionary, List<DeploymentStepResource> deploymentSteps, OctopusRepository repository, ProjectResource project, List<VariableViewModel> variablesList, Dictionary<string, string> scopeNames, List<LibraryVariableSetResource> allVariables, List<EnvironmentResource> allEnvironments)
{
var librarySets = new List<LibraryVariableSetResource>();
foreach (var item in project.IncludedLibraryVariableSetIds)
{
var list = allVariables.FindAll(p => p.Id == item);
librarySets.AddRange(list);
}
foreach (var libraryVariableSetResource in librarySets)
{
var variables = repository.VariableSets.Get(libraryVariableSetResource.VariableSetId);
var variableSetName = libraryVariableSetResource.Name;
foreach (var variable in variables.Variables)
{
variablesList.Add(new VariableViewModel(variable, variableSetName, scopeNames));
}
}
var dataTableVariable = new DataTable();
dataTableVariable.Columns.Add("VariableSet", typeof(string));
dataTableVariable.Columns.Add("Name", typeof(string));
dataTableVariable.Columns.Add("Value", typeof(string));
dataTableVariable.Columns.Add("Scope", typeof(string));
var dataTableDeployStep = new DataTable();
dataTableDeployStep.Columns.Add("Process", typeof(string));
dataTableDeployStep.Columns.Add("StepName", typeof(string));
dataTableDeployStep.Columns.Add("PackageId", typeof(string));
dataTableDeployStep.Columns.Add("InstalledPath", typeof(string));
dataTableDeployStep.Columns.Add("TargetRole", typeof(string));
dataTableDeployStep.Columns.Add("Environment", typeof(string));
dataTableDeployStep.Columns.Add("ScriptSyntax", typeof(string));
dataTableDeployStep.Columns.Add("ScriptSource", typeof(string));
dataTableDeployStep.Columns.Add("ScriptBody", typeof(string));
//Get All Project Variables for the Project
var projectSets = repository.VariableSets.Get(project.VariableSetId);
foreach (var variable in projectSets.Variables)
{
variablesList.Add(new VariableViewModel(variable, parametersDictionary[ParameterEnum.ProjectName], scopeNames));
}
foreach (var vm in variablesList)
{
dataTableVariable.Rows.Add(vm.VariableSetName, vm.Name, vm.Value, vm.Scope);
}
foreach (var item in deploymentSteps)
{
var process = item.Name;
var targetRole = item.Properties["Octopus.Action.TargetRoles"].Value;
foreach (var action in item.Actions)
{
var stepName = action.Name;
action.Properties.TryGetValue("Octopus.Action.Package.PackageId", out PropertyValueResource packageId);
action.Properties.TryGetValue("Octopus.Action.Package.CustomInstallationDirectory", out PropertyValueResource installedPath);
var environment = allEnvironments.FirstOrDefault(p => p.Id == action.Environments.FirstOrDefault())?.Name;
action.Properties.TryGetValue("Octopus.Action.Script.Syntax", out PropertyValueResource scriptSyntax);
action.Properties.TryGetValue("Octopus.Action.Script.ScriptSource", out PropertyValueResource scriptSource);
action.Properties.TryGetValue("Octopus.Action.Script.ScriptBody", out PropertyValueResource scriptBody);
dataTableDeployStep.Rows.Add(process, stepName, packageId?.Value, installedPath?.Value, targetRole, environment, scriptSyntax?.Value, scriptSource?.Value, scriptBody?.Value);
}
}
WriteExcelWithNPOI(dataTableVariable, dataTableDeployStep, "xlsx", parametersDictionary[ParameterEnum.ProjectName], parametersDictionary[ParameterEnum.OutputPath]);
dataTableVariable.Dispose();
dataTableDeployStep.Dispose();
}
public static void WriteExcelWithNPOI(DataTable dtVariables, DataTable dtProcessSteps, string extension, string fileName, string outputPath)
{
IWorkbook workbook;
Console.WriteLine($"Export data with {extension} file extension");
if (extension == "xlsx")
{
workbook = new XSSFWorkbook();
}
else if (extension == "xls")
{
workbook = new HSSFWorkbook();
}
else
{
throw new Exception("This format is not supported");
}
ISheet librarySetSheet = workbook.CreateSheet("Library Sets");
ISheet deploymentProcessSheet = workbook.CreateSheet("Deployment Process");
CreateDataSheet(dtVariables, librarySetSheet);
CreateDataSheet(dtProcessSteps, deploymentProcessSheet);
if (!Directory.Exists(outputPath))
{
Directory.CreateDirectory(outputPath);
}
var outputFolder = DateTime.Now.ToString("yyyyMMdd");
var targetPath = Path.Combine(outputPath, outputFolder);
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
var outputFileName = $"{fileName.Replace(" ", "_")}_{ DateTime.Now.ToString("yyyyMMdd_HHmmss")}.{ extension}";
var outputFileInfo = Path.Combine(targetPath, outputFileName);
Console.WriteLine($"Success to export data {outputFileInfo}");
using (var fs = File.Create(outputFileInfo))
{
workbook.Write(fs);
}
}
private static void CreateDataSheet(DataTable dt, ISheet sheet)
{
//make a header row
IRow rowHeader = sheet.CreateRow(0);
for (int j = 0; j < dt.Columns.Count; j++)
{
ICell cell = rowHeader.CreateCell(j);
string columnName = dt.Columns[j].ToString();
cell.SetCellValue(columnName);
}
//loops through data
for (int i = 0; i < dt.Rows.Count; i++)
{
IRow rowData = sheet.CreateRow(i + 1);
for (int j = 0; j < dt.Columns.Count; j++)
{
ICell cell = rowData.CreateCell(j);
string columnName = dt.Columns[j].ToString();
cell.SetCellValue(dt.Rows[i][columnName].ToString());
}
}
}
}
}