|
1 |
| -/* eslint-disable @typescript-eslint/no-use-before-define */ |
2 |
| - |
3 | 1 | import { VariableGroup } from "azure-devops-node-api/interfaces/ReleaseInterfaces";
|
4 | 2 | import commander from "commander";
|
5 | 3 | import path from "path";
|
@@ -55,118 +53,6 @@ export const validateValues = (projectName: string, orgName: string): void => {
|
55 | 53 | validateOrgNameThrowable(orgName);
|
56 | 54 | };
|
57 | 55 |
|
58 |
| -/** |
59 |
| - * Executes the command. |
60 |
| - * |
61 |
| - * @param variableGroupName Variable Group Name |
62 |
| - * @param opts Option object from command |
63 |
| - */ |
64 |
| -export const execute = async ( |
65 |
| - variableGroupName: string, |
66 |
| - opts: CommandOptions, |
67 |
| - exitFn: (status: number) => Promise<void> |
68 |
| -): Promise<void> => { |
69 |
| - if (!hasValue(variableGroupName)) { |
70 |
| - await exitFn(1); |
71 |
| - return; |
72 |
| - } |
73 |
| - |
74 |
| - try { |
75 |
| - const projectPath = process.cwd(); |
76 |
| - logger.verbose(`project path: ${projectPath}`); |
77 |
| - |
78 |
| - checkDependencies(projectPath); |
79 |
| - |
80 |
| - const { azure_devops } = Config(); |
81 |
| - |
82 |
| - const { |
83 |
| - registryName, |
84 |
| - servicePrincipalId, |
85 |
| - servicePrincipalPassword, |
86 |
| - tenant, |
87 |
| - hldRepoUrl = azure_devops?.hld_repository, |
88 |
| - orgName = azure_devops?.org, |
89 |
| - personalAccessToken = azure_devops?.access_token, |
90 |
| - devopsProject = azure_devops?.project, |
91 |
| - } = opts; |
92 |
| - |
93 |
| - const accessOpts: AzureDevOpsOpts = { |
94 |
| - orgName, |
95 |
| - personalAccessToken, |
96 |
| - project: devopsProject, |
97 |
| - }; |
98 |
| - |
99 |
| - logger.debug(`access options: ${JSON.stringify(accessOpts)}`); |
100 |
| - |
101 |
| - const errors = validateForRequiredValues(decorator, { |
102 |
| - devopsProject, |
103 |
| - hldRepoUrl, |
104 |
| - orgName, |
105 |
| - personalAccessToken, |
106 |
| - registryName, |
107 |
| - servicePrincipalId, |
108 |
| - servicePrincipalPassword, |
109 |
| - tenant, |
110 |
| - }); |
111 |
| - |
112 |
| - if (errors.length !== 0) { |
113 |
| - await exitFn(1); |
114 |
| - return; |
115 |
| - } |
116 |
| - |
117 |
| - // validateForRequiredValues assure that devopsProject |
118 |
| - // and orgName are not empty string |
119 |
| - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
120 |
| - validateValues(devopsProject!, orgName!); |
121 |
| - |
122 |
| - const variableGroup = await create( |
123 |
| - variableGroupName, |
124 |
| - registryName, |
125 |
| - hldRepoUrl, |
126 |
| - servicePrincipalId, |
127 |
| - servicePrincipalPassword, |
128 |
| - tenant, |
129 |
| - accessOpts |
130 |
| - ); |
131 |
| - |
132 |
| - // set the variable group name |
133 |
| - // variableGroup.name is set at this point that's it should have value |
134 |
| - // and not empty string or undefined. having || "" is just to avoid |
135 |
| - // eslint error |
136 |
| - setVariableGroupInBedrockFile(projectPath, variableGroup.name || ""); |
137 |
| - |
138 |
| - // update hld-lifecycle.yaml with variable groups in bedrock.yaml |
139 |
| - updateLifeCyclePipeline(projectPath); |
140 |
| - |
141 |
| - // print newly created variable group |
142 |
| - echo(JSON.stringify(variableGroup, null, 2)); |
143 |
| - |
144 |
| - logger.info( |
145 |
| - "Successfully created a variable group in Azure DevOps project!" |
146 |
| - ); |
147 |
| - await exitFn(0); |
148 |
| - } catch (err) { |
149 |
| - logger.error(`Error occurred while creating variable group`); |
150 |
| - logger.error(err); |
151 |
| - await exitFn(1); |
152 |
| - } |
153 |
| -}; |
154 |
| - |
155 |
| -/** |
156 |
| - * Adds the create command to the variable-group command object |
157 |
| - * |
158 |
| - * @param command Commander command object to decorate |
159 |
| - */ |
160 |
| -export const commandDecorator = (command: commander.Command): void => { |
161 |
| - buildCmd(command, decorator).action( |
162 |
| - async (variableGroupName: string, opts: CommandOptions) => { |
163 |
| - await execute(variableGroupName, opts, async (status: number) => { |
164 |
| - await exitCmd(logger, process.exit, status); |
165 |
| - }); |
166 |
| - } |
167 |
| - ); |
168 |
| -}; |
169 |
| - |
170 | 56 | /**
|
171 | 57 | * Creates a Azure DevOps variable group
|
172 | 58 | *
|
@@ -319,3 +205,115 @@ export const updateLifeCyclePipeline = (rootProjectPath: string): void => {
|
319 | 205 | // Write out
|
320 | 206 | write(pipelineFile, absProjectRoot, fileName);
|
321 | 207 | };
|
| 208 | + |
| 209 | +/** |
| 210 | + * Executes the command. |
| 211 | + * |
| 212 | + * @param variableGroupName Variable Group Name |
| 213 | + * @param opts Option object from command |
| 214 | + */ |
| 215 | +export const execute = async ( |
| 216 | + variableGroupName: string, |
| 217 | + opts: CommandOptions, |
| 218 | + exitFn: (status: number) => Promise<void> |
| 219 | +): Promise<void> => { |
| 220 | + if (!hasValue(variableGroupName)) { |
| 221 | + await exitFn(1); |
| 222 | + return; |
| 223 | + } |
| 224 | + |
| 225 | + try { |
| 226 | + const projectPath = process.cwd(); |
| 227 | + logger.verbose(`project path: ${projectPath}`); |
| 228 | + |
| 229 | + checkDependencies(projectPath); |
| 230 | + |
| 231 | + const { azure_devops } = Config(); |
| 232 | + |
| 233 | + const { |
| 234 | + registryName, |
| 235 | + servicePrincipalId, |
| 236 | + servicePrincipalPassword, |
| 237 | + tenant, |
| 238 | + hldRepoUrl = azure_devops?.hld_repository, |
| 239 | + orgName = azure_devops?.org, |
| 240 | + personalAccessToken = azure_devops?.access_token, |
| 241 | + devopsProject = azure_devops?.project, |
| 242 | + } = opts; |
| 243 | + |
| 244 | + const accessOpts: AzureDevOpsOpts = { |
| 245 | + orgName, |
| 246 | + personalAccessToken, |
| 247 | + project: devopsProject, |
| 248 | + }; |
| 249 | + |
| 250 | + logger.debug(`access options: ${JSON.stringify(accessOpts)}`); |
| 251 | + |
| 252 | + const errors = validateForRequiredValues(decorator, { |
| 253 | + devopsProject, |
| 254 | + hldRepoUrl, |
| 255 | + orgName, |
| 256 | + personalAccessToken, |
| 257 | + registryName, |
| 258 | + servicePrincipalId, |
| 259 | + servicePrincipalPassword, |
| 260 | + tenant, |
| 261 | + }); |
| 262 | + |
| 263 | + if (errors.length !== 0) { |
| 264 | + await exitFn(1); |
| 265 | + return; |
| 266 | + } |
| 267 | + |
| 268 | + // validateForRequiredValues assure that devopsProject |
| 269 | + // and orgName are not empty string |
| 270 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 271 | + validateValues(devopsProject!, orgName!); |
| 272 | + |
| 273 | + const variableGroup = await create( |
| 274 | + variableGroupName, |
| 275 | + registryName, |
| 276 | + hldRepoUrl, |
| 277 | + servicePrincipalId, |
| 278 | + servicePrincipalPassword, |
| 279 | + tenant, |
| 280 | + accessOpts |
| 281 | + ); |
| 282 | + |
| 283 | + // set the variable group name |
| 284 | + // variableGroup.name is set at this point that's it should have value |
| 285 | + // and not empty string or undefined. having || "" is just to avoid |
| 286 | + // eslint error |
| 287 | + setVariableGroupInBedrockFile(projectPath, variableGroup.name || ""); |
| 288 | + |
| 289 | + // update hld-lifecycle.yaml with variable groups in bedrock.yaml |
| 290 | + updateLifeCyclePipeline(projectPath); |
| 291 | + |
| 292 | + // print newly created variable group |
| 293 | + echo(JSON.stringify(variableGroup, null, 2)); |
| 294 | + |
| 295 | + logger.info( |
| 296 | + "Successfully created a variable group in Azure DevOps project!" |
| 297 | + ); |
| 298 | + await exitFn(0); |
| 299 | + } catch (err) { |
| 300 | + logger.error(`Error occurred while creating variable group`); |
| 301 | + logger.error(err); |
| 302 | + await exitFn(1); |
| 303 | + } |
| 304 | +}; |
| 305 | + |
| 306 | +/** |
| 307 | + * Adds the create command to the variable-group command object |
| 308 | + * |
| 309 | + * @param command Commander command object to decorate |
| 310 | + */ |
| 311 | +export const commandDecorator = (command: commander.Command): void => { |
| 312 | + buildCmd(command, decorator).action( |
| 313 | + async (variableGroupName: string, opts: CommandOptions) => { |
| 314 | + await execute(variableGroupName, opts, async (status: number) => { |
| 315 | + await exitCmd(logger, process.exit, status); |
| 316 | + }); |
| 317 | + } |
| 318 | + ); |
| 319 | +}; |
0 commit comments