1
- /* eslint-disable @typescript-eslint/no-use-before-define */
2
-
3
1
import { IBuildApi } from "azure-devops-node-api/BuildApi" ;
4
2
import {
5
3
BuildDefinition ,
@@ -20,6 +18,7 @@ import {
20
18
getRepositoryName ,
21
19
getRepositoryUrl ,
22
20
isGitHubUrl ,
21
+ validateRepoUrl
23
22
} from "../../lib/gitutils" ;
24
23
import {
25
24
createPipelineForDefinition ,
@@ -33,6 +32,8 @@ import {
33
32
validateOrgNameThrowable ,
34
33
validateProjectNameThrowable ,
35
34
} from "../../lib/validator" ;
35
+ import { build as buildError } from "../../lib/errorBuilder" ;
36
+ import { errorStatusCode } from "../../lib/errorStatusCode" ;
36
37
37
38
export interface CommandOptions {
38
39
orgName : string ;
@@ -52,13 +53,14 @@ export const fetchValues = async (
52
53
) : Promise < CommandOptions > => {
53
54
const { azure_devops } = Config ( ) ;
54
55
const gitOriginUrl = await getOriginUrl ( ) ;
56
+ const repoUrl = validateRepoUrl ( opts , gitOriginUrl ) ;
55
57
56
58
opts . orgName = opts . orgName || azure_devops ?. org || "" ;
57
59
opts . personalAccessToken =
58
60
opts . personalAccessToken || azure_devops ?. access_token || "" ;
59
61
opts . devopsProject = opts . devopsProject || azure_devops ?. project || "" ;
60
62
opts . pipelineName = opts . pipelineName || serviceName + "-pipeline" ;
61
- opts . repoName = getRepositoryName ( opts . repoUrl ) ;
63
+ opts . repoName = getRepositoryName ( repoUrl ) ;
62
64
opts . repoUrl = opts . repoUrl || getRepositoryUrl ( gitOriginUrl ) ;
63
65
opts . buildScriptUrl = opts . buildScriptUrl || BUILD_SCRIPT_URL ;
64
66
@@ -68,60 +70,23 @@ export const fetchValues = async (
68
70
return opts ;
69
71
} ;
70
72
71
- export const execute = async (
72
- serviceName : string ,
73
- opts : CommandOptions ,
74
- exitFn : ( status : number ) => Promise < void >
75
- ) : Promise < void > => {
76
- try {
77
- if ( ! opts . repoUrl ) {
78
- throw Error ( `Repo url not defined` ) ;
79
- }
80
- const gitUrlType = await isGitHubUrl ( opts . repoUrl ) ;
81
- if ( gitUrlType ) {
82
- throw Error (
83
- `GitHub repos are not supported. Repo url: ${ opts . repoUrl } is invalid`
84
- ) ;
85
- }
86
- await fetchValues ( serviceName , opts ) ;
87
- const accessOpts : AzureDevOpsOpts = {
88
- orgName : opts . orgName ,
89
- personalAccessToken : opts . personalAccessToken ,
90
- project : opts . devopsProject ,
91
- } ;
92
-
93
- // if a packages dir is supplied, its a mono-repo
94
- const pipelinesYamlPath = opts . packagesDir
95
- ? // if a packages dir is supplied, concat <packages-dir>/<service-name>
96
- path . join ( opts . packagesDir , serviceName , SERVICE_PIPELINE_FILENAME )
97
- : // if no packages dir, then just concat with the service directory.
98
- path . join ( serviceName , SERVICE_PIPELINE_FILENAME ) ;
99
-
100
- // By default the version descriptor is for the master branch
101
- await validateRepository (
102
- opts . devopsProject ,
103
- pipelinesYamlPath ,
104
- opts . yamlFileBranch ? opts . yamlFileBranch : "master" ,
105
- opts . repoName ,
106
- accessOpts
107
- ) ;
108
- await installBuildUpdatePipeline ( pipelinesYamlPath , opts ) ;
109
- await exitFn ( 0 ) ;
110
- } catch ( err ) {
111
- logger . error ( err ) ;
112
- await exitFn ( 1 ) ;
113
- }
73
+ /**
74
+ * Builds and returns variables required for the Build & Update service pipeline.
75
+ * @param buildScriptUrl Build Script URL
76
+ * @returns Object containing the necessary run-time variables for the Build & Update service pipeline.
77
+ */
78
+ export const requiredPipelineVariables = (
79
+ buildScriptUrl : string
80
+ ) : { [ key : string ] : BuildDefinitionVariable } => {
81
+ return {
82
+ BUILD_SCRIPT_URL : {
83
+ allowOverride : true ,
84
+ isSecret : false ,
85
+ value : buildScriptUrl ,
86
+ } ,
87
+ } ;
114
88
} ;
115
89
116
- export const commandDecorator = ( command : commander . Command ) : void => {
117
- buildCmd ( command , decorator ) . action (
118
- async ( serviceName : string , opts : CommandOptions ) => {
119
- await execute ( serviceName , opts , async ( status : number ) => {
120
- await exitCmd ( logger , process . exit , status ) ;
121
- } ) ;
122
- }
123
- ) ;
124
- } ;
125
90
126
91
/**
127
92
* Install a pipeline for the service in an azure devops org.
@@ -196,19 +161,57 @@ export const installBuildUpdatePipeline = async (
196
161
}
197
162
} ;
198
163
199
- /**
200
- * Builds and returns variables required for the Build & Update service pipeline.
201
- * @param buildScriptUrl Build Script URL
202
- * @returns Object containing the necessary run-time variables for the Build & Update service pipeline.
203
- */
204
- export const requiredPipelineVariables = (
205
- buildScriptUrl : string
206
- ) : { [ key : string ] : BuildDefinitionVariable } => {
207
- return {
208
- BUILD_SCRIPT_URL : {
209
- allowOverride : true ,
210
- isSecret : false ,
211
- value : buildScriptUrl ,
212
- } ,
213
- } ;
164
+ export const execute = async (
165
+ serviceName : string ,
166
+ opts : CommandOptions ,
167
+ exitFn : ( status : number ) => Promise < void >
168
+ ) : Promise < void > => {
169
+ try {
170
+ const gitOriginUrl = await getOriginUrl ( ) ;
171
+ const repoUrl = validateRepoUrl ( opts , gitOriginUrl ) ;
172
+ const gitUrlType = await isGitHubUrl ( repoUrl ) ;
173
+ if ( gitUrlType ) {
174
+ throw buildError ( errorStatusCode . VALIDATION_ERR , {
175
+ errorKey : "project-pipeline-err-github-repo" ,
176
+ values : [ repoUrl ] ,
177
+ } ) ;
178
+ }
179
+ await fetchValues ( serviceName , opts ) ;
180
+ const accessOpts : AzureDevOpsOpts = {
181
+ orgName : opts . orgName ,
182
+ personalAccessToken : opts . personalAccessToken ,
183
+ project : opts . devopsProject ,
184
+ } ;
185
+
186
+ // if a packages dir is supplied, its a mono-repo
187
+ const pipelinesYamlPath = opts . packagesDir
188
+ ? // if a packages dir is supplied, concat <packages-dir>/<service-name>
189
+ path . join ( opts . packagesDir , serviceName , SERVICE_PIPELINE_FILENAME )
190
+ : // if no packages dir, then just concat with the service directory.
191
+ path . join ( serviceName , SERVICE_PIPELINE_FILENAME ) ;
192
+
193
+ // By default the version descriptor is for the master branch
194
+ await validateRepository (
195
+ opts . devopsProject ,
196
+ pipelinesYamlPath ,
197
+ opts . yamlFileBranch ? opts . yamlFileBranch : "master" ,
198
+ opts . repoName ,
199
+ accessOpts
200
+ ) ;
201
+ await installBuildUpdatePipeline ( pipelinesYamlPath , opts ) ;
202
+ await exitFn ( 0 ) ;
203
+ } catch ( err ) {
204
+ logger . error ( err ) ;
205
+ await exitFn ( 1 ) ;
206
+ }
207
+ } ;
208
+
209
+ export const commandDecorator = ( command : commander . Command ) : void => {
210
+ buildCmd ( command , decorator ) . action (
211
+ async ( serviceName : string , opts : CommandOptions ) => {
212
+ await execute ( serviceName , opts , async ( status : number ) => {
213
+ await exitCmd ( logger , process . exit , status ) ;
214
+ } ) ;
215
+ }
216
+ ) ;
214
217
} ;
0 commit comments