Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 22 additions & 28 deletions Tasks/DeleteFilesV1/deletefiles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import path = require('path');
import os = require('os');
import fs = require('fs');
import tl = require('azure-pipelines-task-lib/task');
import fastGlob = require('fast-glob');

tl.setResourcePath(path.join(__dirname, 'task.json'));

(() => {
Expand Down Expand Up @@ -30,15 +33,8 @@ tl.setResourcePath(path.join(__dirname, 'task.json'));
return;
}

// find all files
let foundPaths: string[] = tl.find(sourceFolder, {
allowBrokenSymbolicLinks: true,
followSpecifiedSymbolicLink: true,
followSymbolicLinks: true
});

// short-circuit if not exists
if (!foundPaths.length) {
if (!tl.exist(sourceFolder)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is an issue with task-lib we should push this fix in task-lib instead of fixing jus tthis task as task-lib is shared between multiple tasks and other tasks might be vuln to same bug...

tl.debug('source folder not found. nothing to delete.');
tl.setResult(tl.TaskResult.Succeeded, tl.loc("NoFiles"));
return;
Expand All @@ -55,9 +51,7 @@ tl.setResourcePath(path.join(__dirname, 'task.json'));
// a normalization function that could be trusted 100% to match the format produced by tl.find().
// For example if the input contains "\\\share", it would need to be normalized as "\\share". To
// avoid worrying about catching every normalization edge case, checking the item name suffices instead.
if (buildCleanup &&
foundPaths.some((itemPath: string) => path.basename(itemPath).toLowerCase() == '000admin')) {

if (buildCleanup && fastGlob.globSync('000admin.*', { cwd: sourceFolder, caseSensitiveMatch: false, }).length) {
tl.warning(tl.loc('SkippingSymbolStore', sourceFolder))
return;
}
Expand All @@ -73,7 +67,7 @@ tl.setResourcePath(path.join(__dirname, 'task.json'));
}

// apply the match patterns
let matches: string[] = matchPatterns(foundPaths, patterns, matchOptions);
let matches: string[] = matchPatterns(sourceFolder, patterns, matchOptions);

// sort by length (descending) so files are deleted before folders
matches = matches.sort((a: string, b: string) => {
Expand All @@ -99,9 +93,9 @@ tl.setResourcePath(path.join(__dirname, 'task.json'));
// if there wasn't an error, check if there's anything in the folder tree other than folders
// if not, delete the root as well
if (removeSourceFolder && !errorHappened) {
foundPaths = tl.find(sourceFolder);
const foundPaths = fs.readdirSync(sourceFolder);

if (foundPaths.length === 1) {
if (foundPaths.length === 0) {
try {
tl.rmRF(sourceFolder);
}
Expand Down Expand Up @@ -152,19 +146,19 @@ function joinPattern(sourcePath: string, pattern: string): string {
* @param {Object} options - Match options
* @return {string[]} Result matches
*/
function matchPatterns(paths: string[], patterns: string[], options: any): string[] {
const allMatches = tl.match(paths, patterns, null, options);

const hasExcludePatterns = patterns.find(pattern => {
const negateMarkIndex = pattern.indexOf('!');
return negateMarkIndex > -1 && getNegateMarksNumber(pattern, negateMarkIndex) % 2 === 1;
})
if(!hasExcludePatterns){
return allMatches;
}
function matchPatterns(sourceFolder: string, patterns: string[], options: any): string[] {
const isFilePattern = patterns.some(pattern => pattern.includes('.'))

const result = fastGlob.globSync(patterns, {
cwd: sourceFolder,
caseSensitiveMatch: !options.nocase,
dot: options.dot,
onlyFiles: isFilePattern,
absolute: true
});

const sourceFolderAbs = path.resolve(sourceFolder);
const resultsInSourceFolder = result.filter(item => path.resolve(item).startsWith(sourceFolderAbs));

const excludedPaths = paths.filter(path => !~allMatches.indexOf(path));
return allMatches.filter(match => {
return !excludedPaths.find(excludedPath => excludedPath.indexOf(match) === 0);
})
return resultsInSourceFolder;
}
228 changes: 227 additions & 1 deletion Tasks/DeleteFilesV1/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Tasks/DeleteFilesV1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"dependencies": {
"@types/mocha": "^5.2.7",
"@types/node": "^20.11.0",
"azure-pipelines-task-lib": "^4.16.0"
"azure-pipelines-task-lib": "^4.16.0",
"fast-glob": "^3.3.2"
},
"devDependencies": {
"typescript": "5.1.6"
Expand Down
Loading