1
1
const util = require ( './ci-util' ) ;
2
2
const fs = require ( 'fs' ) ;
3
3
const path = require ( 'path' ) ;
4
- var crypto = require ( 'crypto ' ) ;
4
+ const semver = require ( 'semver ' ) ;
5
5
6
6
const fileToJson = util . fileToJson ;
7
7
const buildTasksPath = util . buildTasksPath ;
8
- const GITHUB_LINK = 'https://github.com/microsoft/azure-pipelines-tasks/blob/master/docs/migrateNode16.md' ;
8
+ const logToPipeline = util . logToPipeline ;
9
+ const GITHUB_LINK = 'https://github.com/microsoft/azure-pipelines-tasks/blob/master/docs/validation-errors.md' ;
9
10
10
11
/**
11
12
* Function walking through directories and looking for
@@ -37,17 +38,18 @@ function findLib(dirPath, libRegExp) {
37
38
38
39
/**
39
40
* Function iterates over the given array to find
40
- * which tasks have multiple task lib packages
41
- * @param {Array } scanningTask
41
+ * which tasks have package in node_modules
42
+ * @param {Array } scanningTask - array of tasks
43
+ * @param {RegExp } regExp - regular expression to find package
44
+ * @param {Boolean } includeAll - flag to include all founded packages
42
45
* @returns Array<Object>
43
46
*/
44
- function findWithFsFromPaths ( scanningTask ) {
47
+ function findPackageUsingRegExp ( scanningTask , regExp , includeAll = false ) {
45
48
const foundedTasks = [ ] ;
46
49
for ( let task of scanningTask ) {
47
50
const taskPath = task . taskPath
48
- const reg = new RegExp ( 'azure-pipelines-task-lib' )
49
- const result = findLib ( path . join ( taskPath , 'node_modules' ) , reg ) ;
50
- if ( result . length > 1 ) {
51
+ const result = findLib ( path . join ( taskPath , 'node_modules' ) , regExp ) ;
52
+ if ( ( ! includeAll && result . length > 1 ) || includeAll ) {
51
53
const foundedPaths = result . map ( ( path ) => path . replace ( buildTasksPath , '' ) ) ;
52
54
foundedTasks . push ( {
53
55
task : task . taskName ,
@@ -75,11 +77,10 @@ function isNodeHandlerExists(taskDefinition) {
75
77
}
76
78
77
79
/**
78
- * Function looking for multiple azure-pipelines-task-lib versions
79
- * in builded tasks, in case if package found multiple times throw error
80
- * Note: now function compares only for tasks which have Node10 and Node16 in their task.json
80
+ * Function to get all built tasks
81
+ * @returns {Array<Tasks> } - array of tasks with path and versions
81
82
*/
82
- function findNonUniqueTaskLib ( ) {
83
+ function getBuiltTasks ( ) {
83
84
const taskPaths = fs . readdirSync ( buildTasksPath , { encoding : 'utf-8' } )
84
85
const scanningTasks = [ ] ;
85
86
for ( let taskName of taskPaths ) {
@@ -105,33 +106,82 @@ function findNonUniqueTaskLib() {
105
106
} ) ;
106
107
}
107
108
108
- const haveDependencies = findWithFsFromPaths ( scanningTasks ) ;
109
+ return scanningTasks ;
110
+ }
111
+
112
+ /**
113
+ * Function looking for multiple azure-pipelines-task-lib versions
114
+ * in builded tasks, in case if package found multiple times throw error
115
+ * Note: now function compares only for tasks which have Node10 and Node16 in their task.json
116
+ */
117
+ function findNonUniqueTaskLib ( ) {
118
+ const taskLibSection = "#findnonuniquetasklib-section"
119
+ const scanningTasks = getBuiltTasks ( ) ;
120
+ const reg = new RegExp ( 'azure-pipelines-task-lib' )
121
+ const haveDependencies = findPackageUsingRegExp ( scanningTasks , reg , false ) ;
109
122
if ( haveDependencies . length > 0 ) {
110
- console . log ( `##vso[task.logissue type=error;sourcepath=ci/check-tasks.js;linenumber=109;]The following tasks have duplicate azure-pipelines-task-lib:
111
- ${ JSON . stringify ( haveDependencies , null , 2 ) }
112
- Please examine the following link: ${ GITHUB_LINK } ` ) ;
123
+ logToPipeline ( 'error' , `The following tasks have duplicate azure-pipelines-task-lib:\n${ JSON . stringify ( haveDependencies , null , 2 ) } ` ) ;
124
+ logToPipeline ( 'error' , `Please examine the following link: ${ GITHUB_LINK + taskLibSection } ` ) ;
113
125
process . exit ( 1 ) ;
114
126
}
115
127
116
- console . log ( ' No duplicates found. ') ;
128
+ logToPipeline ( 'info' , ' No duplicates found') ;
117
129
return null ;
118
130
}
119
131
120
132
function analyzePowershellTasks ( ) {
121
133
let output = '' ;
122
134
if ( process . platform !== 'win32' ) {
123
- console . log ( 'The powershell check is only supported on Windows. Skipping...' ) ;
135
+ logToPipeline ( 'info' , 'The powershell check is only supported on Windows. Skipping...' ) ;
124
136
return ;
125
137
}
126
138
127
139
try {
128
140
const pwshScriptPath = path . join ( __dirname , 'check-powershell-syntax.ps1' ) ;
129
141
output = util . run ( `powershell -NoLogo -Sta -NoProfile -NonInteractive -ExecutionPolicy Unrestricted ${ pwshScriptPath } ${ buildTasksPath } ` , true ) ;
130
142
} catch ( e ) {
131
- console . log ( `##vso[task.logissue type=error;sourcepath=ci/check-tasks.js;linenumber=123;]Please check the tasks, seems like they have invalid PowerShell syntax.` )
143
+ logToPipeline ( 'error' , 'Please check the tasks, seems like they have invalid PowerShell syntax.' ) ;
144
+ process . exit ( 1 ) ;
145
+ }
146
+ }
147
+
148
+ function findIncompatibleAgentBase ( ) {
149
+ const minAgentBaseVersion = '6.0.2' ;
150
+ const agentBaseSection = "#findincompatibleagentbase-section"
151
+ const scanningTasks = getBuiltTasks ( ) ;
152
+ const reg = new RegExp ( 'agent-base' )
153
+ const agentBaseTasks = findPackageUsingRegExp ( scanningTasks , reg , true ) ;
154
+ const errors = [ ] ;
155
+
156
+ for ( const { task, locations } of agentBaseTasks ) {
157
+ if ( ! locations . length ) continue ;
158
+
159
+ for ( const agentBasePath of locations ) {
160
+ const packagePath = path . join ( buildTasksPath , agentBasePath , 'package.json' ) ;
161
+ if ( ! fs . existsSync ( packagePath ) ) {
162
+ logToPipeline ( 'warning' , `The following task has no package.json file: ${ task } ` ) ;
163
+ continue ;
164
+ }
165
+
166
+ const agentBaseVersion = fileToJson ( packagePath ) . version ;
167
+ if ( semver . lt ( agentBaseVersion , minAgentBaseVersion ) ) {
168
+ errors . push ( { task, agentBasePath, agentBaseVersion } ) ;
169
+ }
170
+ }
171
+ }
172
+
173
+ if ( errors . length ) {
174
+ logToPipeline ( 'warning' , `The following tasks have incompatible agent-base versions, please use agent-base >= ${ minAgentBaseVersion } :\n${ JSON . stringify ( errors , null , 2 ) } ` ) ;
175
+ logToPipeline ( 'error' , `Please examine the following link: ${ GITHUB_LINK + agentBaseSection } ` ) ;
132
176
process . exit ( 1 ) ;
133
177
}
134
178
}
135
179
180
+
181
+
182
+ // logToPipeline("section", "Start findNonUniqueTaskLib")
136
183
// findNonUniqueTaskLib();
137
- analyzePowershellTasks ( ) ;
184
+ // logToPipeline("section", "Start analyzePowershellTasks")
185
+ // analyzePowershellTasks();
186
+ logToPipeline ( "section" , "Start findIncompatibleAgentBase" )
187
+ findIncompatibleAgentBase ( ) ;
0 commit comments