Skip to content
Snippets Groups Projects
Commit da76a3b3 authored by michael.minelli's avatar michael.minelli
Browse files

Sonar => Resolve issues

parent 9e3f29d2
No related branches found
No related tags found
1 merge request!2Add missing header status code (linked to backend issue #19)
...@@ -8,26 +8,26 @@ import zlib from 'zlib'; ...@@ -8,26 +8,26 @@ import zlib from 'zlib';
class ArchiveHelper { class ArchiveHelper {
private async explore(absoluteBasePath: string, rootPath: string, pack: tar.Pack) { private async explore(absoluteBasePath: string, rootPath: string, pack: tar.Pack) {
for ( let file of await fs.promises.readdir(rootPath) ) { for ( const file of await fs.promises.readdir(rootPath) ) {
if ( file === 'output.tar' ) { let filename = file;
continue; if ( filename !== 'output.tar' ) {
} filename = path.join(rootPath, filename);
file = path.join(rootPath, file); const stat = await fs.promises.stat(filename);
const stat = await fs.promises.stat(file);
if ( stat.isDirectory() ) { if ( stat.isDirectory() ) {
await this.explore(absoluteBasePath, file, pack); await this.explore(absoluteBasePath, filename, pack);
continue; } else {
}
const entry = pack.entry({ const entry = pack.entry({
name: file.replace(absoluteBasePath, ''), name: filename.replace(absoluteBasePath, ''),
size: stat.size size: stat.size
}, (err) => { }, err => {
if ( err ) { if ( err ) {
throw err; throw err;
} }
}); });
const stream = fs.createReadStream(file); const readStream = fs.createReadStream(filename);
stream.pipe(entry); readStream.pipe(entry);
}
}
} }
} }
...@@ -55,7 +55,7 @@ class ArchiveHelper { ...@@ -55,7 +55,7 @@ class ArchiveHelper {
await this.compress(folderPath, tarDataStream); await this.compress(folderPath, tarDataStream);
data = await (new Promise((resolve) => { data = await (new Promise(resolve => {
tarDataStream.on('close', () => { tarDataStream.on('close', () => {
resolve(data); resolve(data);
}); });
......
...@@ -8,10 +8,9 @@ import Json5FileValidator from '../Json5FileValidator'; ...@@ -8,10 +8,9 @@ import Json5FileValidator from '../Json5FileValidator';
class SharedAssignmentHelper { class SharedAssignmentHelper {
validateDescriptionFile(filePathOrStr: string, isFile: boolean = true, version: number = 1): { content: AssignmentFile | undefined, isValid: boolean, error: string | null } { validateDescriptionFile(filePathOrStr: string, isFile: boolean = true, version: number = 1): { content: AssignmentFile | undefined, isValid: boolean, error: string | null } {
switch ( version ) { if ( version === 1 ) {
case 1:
return Json5FileValidator.validateFile(AssignmentFile, filePathOrStr, isFile); return Json5FileValidator.validateFile(AssignmentFile, filePathOrStr, isFile);
default: } else {
return { return {
content: undefined, content: undefined,
isValid: false, isValid: false,
...@@ -24,7 +23,7 @@ class SharedAssignmentHelper { ...@@ -24,7 +23,7 @@ class SharedAssignmentHelper {
const pipelines = await SharedGitlabManager.getRepositoryPipelines(repositoryId, 'main'); const pipelines = await SharedGitlabManager.getRepositoryPipelines(repositoryId, 'main');
if ( pipelines.length > 0 ) { if ( pipelines.length > 0 ) {
const lastPipeline = pipelines[0]; const lastPipeline = pipelines[0];
if ( lastPipeline.status != GitlabPipelineStatus.SUCCESS ) { if ( lastPipeline.status !== GitlabPipelineStatus.SUCCESS ) {
return { return {
isPublishable: false, isPublishable: false,
lastPipeline : lastPipeline, lastPipeline : lastPipeline,
......
class LazyVal<T> { class LazyVal<T> {
private val: T | undefined = undefined; private val: T | undefined = undefined;
private valLoader: () => Promise<T> | T;
constructor(private valLoader: () => Promise<T> | T) {} constructor(valLoader: () => Promise<T> | T) {
this.valLoader = valLoader;
}
get value(): Promise<T> { get value(): Promise<T> {
return new Promise<T>((resolve) => { return new Promise<T>(resolve => {
if ( this.val === undefined ) { if ( this.val === undefined ) {
Promise.resolve(this.valLoader()).then((value: T) => { Promise.resolve(this.valLoader()).then((value: T) => {
this.val = value; this.val = value;
......
...@@ -14,8 +14,9 @@ class Toolbox { ...@@ -14,8 +14,9 @@ class Toolbox {
const files = await fs.readdir(dirPath); const files = await fs.readdir(dirPath);
await Promise.all(files.map(async file => { await Promise.all(files.map(async file => {
if ( (await fs.stat(dirPath + '/' + file)).isDirectory() ) { const filePath = path.join(dirPath, file);
arrayOfFiles = await this.getAllFiles(dirPath + '/' + file, arrayOfFiles); if ( (await fs.stat(filePath)).isDirectory() ) {
arrayOfFiles = await this.getAllFiles(filePath, arrayOfFiles);
} else { } else {
arrayOfFiles.push(path.join(dirPath, file)); arrayOfFiles.push(path.join(dirPath, file));
} }
......
...@@ -64,9 +64,7 @@ function registerStringCapitalizeName() { ...@@ -64,9 +64,7 @@ function registerStringCapitalizeName() {
function registerStringConvertWithEnvVars() { function registerStringConvertWithEnvVars() {
String.prototype.convertWithEnvVars = function (this: string): string { String.prototype.convertWithEnvVars = function (this: string): string {
return this.replace(/\${?([a-zA-Z0-9_]+)}?/g, (_match: string, p1: string) => { return this.replace(/\${?([a-zA-Z0-9_]+)}?/g, (_match: string, p1: string) => process.env[p1] || '');
return process.env[p1] || '';
});
}; };
} }
......
...@@ -51,33 +51,37 @@ class RecursiveFilesStats { ...@@ -51,33 +51,37 @@ class RecursiveFilesStats {
return this.getFiles(`${ path.resolve(rootPath) }/`, rootPath, options, [], callback); return this.getFiles(`${ path.resolve(rootPath) }/`, rootPath, options, [], callback);
} }
private async getFiles(absoluteBasePath: string, rootPath: string, options: RecursiveReaddirFilesOptions = {}, files: IFileDirStat[] = [], callback?: Callback): Promise<IFileDirStat[]> { private async getFilesDirsStat(rootPath: string, options: RecursiveReaddirFilesOptions): Promise<Array<IFileDirStat>> {
const {
ignored, include, exclude, filter
} = options;
const filesData = await fs.promises.readdir(rootPath); const filesData = await fs.promises.readdir(rootPath);
const fileDir: IFileDirStat[] = filesData.map((file) => ({ return filesData.map(file => ({
name: file, path: path.join(rootPath, file) name: file,
})).filter((item) => { path: path.join(rootPath, file)
if ( include && include.test(item.path) ) { })).filter(item => {
if ( options.include && options.include.test(item.path) ) {
return true; return true;
} }
if ( exclude && exclude.test(item.path) ) { if ( options.exclude && options.exclude.test(item.path) ) {
return false; return false;
} }
if ( ignored ) { if ( options.ignored ) {
return !ignored.test(item.path); return !options.ignored.test(item.path);
} }
return true; return true;
}); });
}
private async getFiles(absoluteBasePath: string, rootPath: string, options: RecursiveReaddirFilesOptions = {}, files: IFileDirStat[] = [], callback?: Callback): Promise<IFileDirStat[]> {
const fileDir: Array<IFileDirStat> = await this.getFilesDirsStat(rootPath, options);
if ( callback ) { if ( callback ) {
fileDir.map(async (item: IFileDirStat) => { fileDir.forEach(item => {
const stat = await this.getStat(item.path, absoluteBasePath, options); this.getStat(item.path, absoluteBasePath, options).then(stat => {
if ( stat.isDirectory!() ) { if ( stat.isDirectory!() ) {
await this.getFiles(absoluteBasePath, item.path, options, [], callback); this.getFiles(absoluteBasePath, item.path, options, [], callback).then();
} }
callback(item.path, stat); callback(item.path, stat);
}); });
});
} else { } else {
await Promise.all(fileDir.map(async (item: IFileDirStat) => { await Promise.all(fileDir.map(async (item: IFileDirStat) => {
const stat = await this.getStat(item.path, absoluteBasePath, options); const stat = await this.getStat(item.path, absoluteBasePath, options);
...@@ -89,9 +93,9 @@ class RecursiveFilesStats { ...@@ -89,9 +93,9 @@ class RecursiveFilesStats {
} }
})); }));
} }
return files.filter((item) => { return files.filter(item => {
if ( filter && typeof filter === 'function' ) { if ( options.filter && typeof options.filter === 'function' ) {
return filter(item); return options.filter(item);
} }
return true; return true;
}); });
...@@ -124,10 +128,7 @@ class RecursiveFilesStats { ...@@ -124,10 +128,7 @@ class RecursiveFilesStats {
delete stat.ctimeMs; delete stat.ctimeMs;
delete stat.birthtimeMs; delete stat.birthtimeMs;
delete stat.atime; delete stat.atime;
//delete stat.mtime;
delete stat.ctime; delete stat.ctime;
//delete stat.birthtime;
//delete stat.mode;
} }
return stat; return stat;
......
...@@ -23,7 +23,11 @@ winston.addColors(colors); ...@@ -23,7 +23,11 @@ winston.addColors(colors);
const format = winston.format.combine(winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), winston.format(info => ({ const format = winston.format.combine(winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), winston.format(info => ({
...info, ...info,
level: info.level.toUpperCase() level: info.level.toUpperCase()
}))(), SharedConfig.production ? winston.format.uncolorize() : winston.format.colorize({ all: true }), winston.format.prettyPrint(), winston.format.errors({ stack: true }), winston.format.align(), winston.format.printf((info) => `[${ info.timestamp }] (${ process.pid }) ${ info.level } ${ info.message } ${ info.metadata ? `\n${ JSON.stringify(info.metadata) }` : '' } ${ info.stack ? `\n${ info.stack }` : '' } `)); }))(), SharedConfig.production ? winston.format.uncolorize() : winston.format.colorize({ all: true }), winston.format.prettyPrint(), winston.format.errors({ stack: true }), winston.format.align(), winston.format.printf(info => {
const metadata = info.metadata ? `\n${ JSON.stringify(info.metadata) }` : '';
const stack = info.stack ? `\n${ info.stack }` : '';
return `[${ info.timestamp }] (${ process.pid }) ${ info.level } ${ info.message } ${ metadata } ${ stack } `;
}));
const commonTransportOptions = { const commonTransportOptions = {
handleRejections: true, handleRejections: true,
...@@ -54,11 +58,11 @@ if ( SharedConfig.production ) { ...@@ -54,11 +58,11 @@ if ( SharedConfig.production ) {
}) ]); }) ]);
} }
const logger = winston.createLogger({ const WinstonLogger = winston.createLogger({
levels, levels,
format, format,
transports, transports,
exitOnError: false exitOnError: false
}); });
export default logger; export default WinstonLogger;
const projectIdRoute = '/projects/{{id}}';
enum GitlabRoute { enum GitlabRoute {
NOTIFICATION_SETTINGS = '/notification_settings', NOTIFICATION_SETTINGS = '/notification_settings',
PROFILE_GET = '/user', PROFILE_GET = '/user',
USERS_GET = '/users', USERS_GET = '/users',
REPOSITORY_GET = '/projects/{{id}}', REPOSITORY_GET = '/projects/{{id}}',
REPOSITORY_CREATE = '/projects', // eslint-disable-next-line @typescript-eslint/no-duplicate-enum-values REPOSITORY_CREATE = '/projects', // eslint-disable-next-line @typescript-eslint/no-duplicate-enum-values
REPOSITORY_DELETE = '/projects/{{id}}', // eslint-disable-next-line @typescript-eslint/no-duplicate-enum-values REPOSITORY_DELETE = projectIdRoute, // eslint-disable-next-line @typescript-eslint/no-duplicate-enum-values
REPOSITORY_EDIT = '/projects/{{id}}', REPOSITORY_EDIT = projectIdRoute,
REPOSITORY_FORK = '/projects/{{id}}/fork', REPOSITORY_FORK = '/projects/{{id}}/fork',
REPOSITORY_MEMBER_ADD = '/projects/{{id}}/members', REPOSITORY_MEMBER_ADD = '/projects/{{id}}/members',
REPOSITORY_MEMBERS_GET = '/projects/{{id}}/members/all', REPOSITORY_MEMBERS_GET = '/projects/{{id}}/members/all',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment