Skip to content

Commit

Permalink
added global errors to logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Duda committed Jun 10, 2024
1 parent 62d05aa commit 3628003
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 41 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scope-tags",
"version": "0.3.3",
"version": "0.3.4",
"description": "Output human readable test scope report for QA",
"main": "dist/scope.js",
"types": "dist/scope.d.ts",
Expand Down Expand Up @@ -51,7 +51,7 @@
"@types/uuid": "^9.0.8",
"adf-validator": "^0.2.1",
"enquirer": "^2.4.1",
"html-creator": "^0.7.2",
"html-creator": "^0.7.3",
"nodegit": "^0.28.0-alpha.26",
"ts-morph": "^22.0.0",
"node-fetch": "^2.7.0",
Expand Down
23 changes: 19 additions & 4 deletions src/Commands/runReportForCommitListCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ export async function runReportForCommitListCommand(args: Array<string>, root: s
return;
}

try {
await generateReportForCommitList(root, buildDataFile, logFilePath);
} catch (e: any) {
Logger.pushGlobalError(e.stack);
}

saveLogs(root, logFilePath);
}

async function generateReportForCommitList(root: string, buildDataFile: string, logFilePath: string) {

const repository = new GitRepository(root);
const configFile = new ConfigFile(root);
const tagsDefinitionFile = new TagsDefinitionFile(root);
Expand Down Expand Up @@ -135,6 +146,13 @@ export async function runReportForCommitListCommand(args: Array<string>, root: s

Logger.setConfigurationProperty(ConfigurationProperty.POSTED_REPORTS, `${reportsPosted} of ${reportsGenerated} generated`);

console.log(`[Scope tags]: Posted comments: ${reportsPosted}`);
console.log(`[Scope tags]: Commits processed: ${totalCommitCount}`);
}

function saveLogs(root: string, logFilePath: string) {
const configFile = new ConfigFile(root);

if (logFilePath) {
if (fileExists(logFilePath)) {
console.log(`Deleting existing HTML logs from '${logFilePath}' to create new log file`);
Expand All @@ -144,7 +162,4 @@ export async function runReportForCommitListCommand(args: Array<string>, root: s
saveHTMLLogs(logFilePath, Logger.exportLogsToHTML(configFile));
console.log(`[Scope tags]: Saved HTML logs to '${logFilePath}'...'`);
}

console.log(`[Scope tags]: Posted comments: ${reportsPosted}`);
console.log(`[Scope tags]: Commits processed: ${totalCommitCount}`);
}
}
88 changes: 65 additions & 23 deletions src/HTMLCreator/HTMLCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ export class HTMLCreator {
attributes: {
id: "page-title",
},
content: 'Scope tags report logs'
content: "Scope tags report logs"
},
{
type: "hr"
}
]
}]).withBoilerplate();

this._html.document.addElementToType('head', {
this._html.document.addElementToType("head", {
type: "link",
attributes: {
rel: "stylesheet",
Expand All @@ -57,7 +57,7 @@ export class HTMLCreator {
});

// Add style overrides
this._html.document.addElementToType('head', {
this._html.document.addElementToType("head", {
type: "style",
content: CSSOverrides,
});
Expand Down Expand Up @@ -196,22 +196,30 @@ export class HTMLCreator {

issue.errors.forEach(errorMessage => {
this._appendErrorMessage(issue.key, errorMessage);
})
});

}
this._html.document.addElementToId(issue.key, { type: "hr" });
});
}

private _appendErrorMessage(key: string, errorMessage: string) {
this._html.document.addElementToId(key, {
type: "pre",
content: [{
type: "code",
content: errorMessage
}
]
});
private _appendErrorMessage(elementId: string, errorMessage: string) {
const sanitizedErrorMessage = errorMessage.replaceAll("<", "[").replaceAll(">", "]");

this._html.document.addElementToId(elementId,
{
type: "div",
content: [
{
type: "pre",
content: [{
type: "code",
content: sanitizedErrorMessage
}
]
}
]
});
}

public appendInstructions() {
Expand Down Expand Up @@ -242,7 +250,7 @@ export class HTMLCreator {
title: "Some extra data!!",
class: "addotional-data-on-hover"
},
content: `additional data on hover`
content: "additional data on hover"
},
{
type: "span",
Expand Down Expand Up @@ -376,7 +384,7 @@ export class HTMLCreator {
title: this._getRelevancyDescriptions(),
class: "addotional-data-on-hover-light"
},
content: `Relevancy`
content: "Relevancy"
}
]
},
Expand All @@ -400,8 +408,9 @@ export class HTMLCreator {
]
});
}

private _getRelevancyDescriptions() {
return [...RelevancyDescriptions.entries()].map(([relevancy, description]) => `${relevancy} - ${description.message}`).join('\n');
return [...RelevancyDescriptions.entries()].map(([relevancy, description]) => `${relevancy} - ${description.message}`).join("\n");
}

private _renderReferencedFiles(fileReferences: FileReference[]): any {
Expand All @@ -415,21 +424,21 @@ export class HTMLCreator {
title: this._renderTagIdentifiersToString(fileReference.tagIdentifiers),
class: fileReference.tagIdentifiers.length ? "addotional-data-on-hover" : undefined
},
content: `${fileReference.fileInfo.filename + (fileReference.fileInfo.unused ? '- unused' : '')}`
content: `${fileReference.fileInfo.filename + (fileReference.fileInfo.unused ? "- unused" : "")}`
}));
}

private _renderLinedAddedRemoved(linesAdded: number, linesRemoved: number) {
if (linesAdded === 0 && linesRemoved === 0) {
return '-';
return "-";
}

let linesAddedRemovedText = "";

if (linesAdded > 0) {
linesAddedRemovedText += `++${linesAdded}`;
if (linesRemoved > 0) {
linesAddedRemovedText += ` `;
linesAddedRemovedText += " ";
}
}

Expand All @@ -443,15 +452,48 @@ export class HTMLCreator {
private _renderTagIdentifiers(tagIdentifiers: TagIdentifier[]) {
return [{
type: "p",
content: tagIdentifiers.map(tagIdentifier => `<strong>${tagIdentifier.module}</strong> / <strong>${tagIdentifier.tag}</strong>`).join('<br>')
}]
content: tagIdentifiers.map(tagIdentifier => `<strong>${tagIdentifier.module}</strong> / <strong>${tagIdentifier.tag}</strong>`).join("<br>")
}];
}

private _renderTagIdentifiersToString(tagIdentifiers: TagIdentifier[]): string {
if (!tagIdentifiers.length) {
return 'Untagged'
return "Untagged";
}

return tagIdentifiers.map(tagIdentifier => `- '${tagIdentifier.tag}' from module '${tagIdentifier.module}'`).join('\n');
return tagIdentifiers.map(tagIdentifier => `- '${tagIdentifier.tag}' from module '${tagIdentifier.module}'`).join("\n");
}


public appendGlobalErrors(errors: string[]) {
if (!errors.length) {
return;
}

const divId = "errors-global";

this._html.document.addElementToType("main", {
type: "div",
attributes: {
id: divId
},
content: [
{
type: "h2",
content: "Errors"
},
{
type: "hr"
}
]
});

errors.forEach(error => {
this._appendErrorMessage(divId, error);
});

this._html.document.addElementToId(divId, {
type: "hr"
});
}
}
27 changes: 18 additions & 9 deletions src/Logger/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export class Logger {
private static _issues: IssueLog[] = [];
private static _DETACHED_ID = "__detached__";

private static _globalErrors: string[] = [];

private static _relevancyManager = new RelevancyManager();

private constructor() { }
Expand All @@ -85,18 +87,24 @@ export class Logger {
} as unknown as IssueLog));
}

static pushErrorMessage(issue: string, error: any) {
let matchingIssue = Logger._issues.find(issueLog => issueLog.key === issue);
static pushAjvErrorMessage(error: any, issue?: string) {
const justText = error.toString().replace(
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, "");

if (issue) {
const matchingIssue = Logger._issues.find(issueLog => issueLog.key === issue);

if (!matchingIssue) {
matchingIssue = this._getDetachedIssueLogs();
return;
if (matchingIssue) {
matchingIssue.errors.push(justText);
return;
}
}

const justText = error.toString().replace(
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
this._globalErrors.push(justText);
}

matchingIssue.errors.push(justText);
static pushGlobalError(errorMessage: string) {
this._globalErrors.push(errorMessage);
}

static pushFileInfo(fileData: FileData, fileInfo: FileInfo) {
Expand All @@ -122,7 +130,7 @@ export class Logger {
databaseContent: fileInfo.tagIdentifiers,
referencedFiles: fileInfo.usedIn,
ignored: fileInfo.ignored,
}
};

matchingCommitFileLogs.push(newFileLog);
}
Expand Down Expand Up @@ -155,6 +163,7 @@ export class Logger {
// ... add HTML content here

htmlCreator.appendConfiguration(Logger._configuration);
htmlCreator.appendGlobalErrors(Logger._globalErrors);
htmlCreator.appendIssueTableOfContents(Logger._issues);
htmlCreator.appendIssueLogs(Logger._issues, configFile.getViewIssueUrl(), configFile.getRepositoryURL(), configFile.getSeeCommitURL());
htmlCreator.appendInstructions();
Expand Down
2 changes: 1 addition & 1 deletion src/Report/ADFValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class ADFValidator {
console.log(errors);

if (issue) {
Logger.pushErrorMessage(issue, errors);
Logger.pushAjvErrorMessage(errors, issue);
}
console.log(`[ADFValidator] Error while validating ${commentJSON}`);
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/Report/BuildIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class BuildIntegration {
public getUniqueIssues(): Array<string> {
const commits = this._getCommits();
if (!commits || !commits.length) {
throw new Error("Commit list cannot be empty!");
return [];
}

const allIssues = commits.map(commit => commit.issue);
Expand Down Expand Up @@ -93,7 +93,7 @@ export class BuildIntegration {
process.stdout.write(`[BuildIntegration] Response '${content}', OK: ${rawResponse.ok}`);
return rawResponse.ok;
} catch (error) {
process.stdout.write(`[BuildIntegration] Could not parse response.`);
process.stdout.write("[BuildIntegration] Could not parse response.");
return false;
}
}
Expand Down

0 comments on commit 3628003

Please sign in to comment.