Skip to content

Commit

Permalink
fixes for module grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Duda committed Feb 23, 2024
1 parent 169913f commit 1bdb0be
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 76 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ dist/
.scope/
coverage/
importMap.json
# commitfile.txt
# logs.html
commitfile.txt
logs.html
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scope-tags",
"version": "0.1.16",
"version": "0.1.18",
"description": "Output human readable test scope report for QA",
"main": "dist/scope.js",
"types": "dist/scope.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/runAddCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ export function runAddCommand(args: Array<string>, root: string) {
await repository.amendMostRecentCommit(fileTagsDatabase.getPath(), newCommitMessage);

}); // TODO: Save already tagged files
}).catch(e => console.log("Canceled")); // TODO: Save already tagged files
}).catch(e => console.log("Canceled")); // TODO: Save already tagged files - ???
}
5 changes: 4 additions & 1 deletion src/Commands/runReportForCommitCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ConfigFile } from "../Scope/ConfigFile";
import { FileTagsDatabase } from "../Scope/FileTagsDatabase";
import { TagsDefinitionFile } from "../Scope/TagsDefinitionFile";
import { fileExists } from "../FileSystem/fileSystemUtils";
import { RelevancyManager } from "../Relevancy/RelevancyManager";

export function runReportForCommitCommand(args: Array<string>, root: string) {
// Checks if all files from the commit are present in database (or excluded)
Expand Down Expand Up @@ -44,9 +45,11 @@ export function runReportForCommitCommand(args: Array<string>, root: string) {
})

const generator = new ReportGenerator(repository, tagsDefinitionFile, fileTagsDatabase, referenceFinders);
const relevancyManager = new RelevancyManager();

repository.getCommitByHash(hash).then(async (commit: Commit) => {
const report = await generator.generateReportForCommit(commit, projects[0].name);
const relevancyMap = relevancyManager.loadRelevancyMapFromCommits([commit]);
const report = await generator.generateReportForCommit(commit, projects[0].name, relevancyMap);
generator.getReportAsJiraComment(report, true);
});
}
25 changes: 15 additions & 10 deletions src/Commands/runReportForCommitListCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ export function runReportForCommitListCommand(args: Array<string>, root: string)
console.log(`[Scope tags]: Generating report for issue '${issue}'...'`);
const report = await generator.generateReportForCommits(commits, projects[0].name, buildTag, false, relevancyMap);

if (generator.isReportEmpty(report)) {
console.log(`[Scope tags]: Report ommited because no tags for modified files were found'`);
return;
}

const commentReportJSON = generator.getReportAsJiraComment(report, false);

console.log(`[Scope tags]: Posting report as comment for issue '${issue}'...'`);
Expand All @@ -98,19 +103,19 @@ export function runReportForCommitListCommand(args: Array<string>, root: string)
report: commentReportJSON,
hostname: os.hostname(),
});

if (logFilePath) {
if (fileExists(logFilePath)) {
console.log(`Deleting existing HTML logs from '${logFilePath}' to create new log file`);
removeFile(logFilePath);
}

saveHTMLLogs(logFilePath, Logger.exportLogsToHTML(configFile));
console.log(`[Scope tags]: Saved HTML logs to '${logFilePath}'...'`);
}
});
});

if (logFilePath) {
if (fileExists(logFilePath)) {
console.log(`Deleting existing HTML logs from '${logFilePath}' to create new log file`);
removeFile(logFilePath);
}

saveHTMLLogs(logFilePath, Logger.exportLogsToHTML(configFile));
console.log(`[Scope tags]: Saved HTML logs to '${logFilePath}'...'`);
}

console.log(`[Scope tags]: Posted comments: ${uniqueIssues.length}`);
console.log(`[Scope tags]: Commits processed: ${totalCommitCount}`);
}
5 changes: 4 additions & 1 deletion src/Commands/runTagCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export function runTagCommitCommand(args: Array<string>, root: string) {
const fileData = repository.convertFilesToFileData(filesToTag);

fileTagger.start(fileData).then(() => {
console.log("All files tagged");
console.log("All files tagged. Saving to database...");
fileTagsDatabase.save();
});
} else {
// Ask the user if they want to re-tag selected files
Expand All @@ -57,6 +58,8 @@ export function runTagCommitCommand(args: Array<string>, root: string) {
const fileData = repository.convertFilesToFileData(selectedFiles.map(selectedFile => selectedFile.path));

await fileTagger.start(fileData, false);
console.log("All files tagged. Saving to database...");
fileTagsDatabase.save();
});
}
}
2 changes: 1 addition & 1 deletion src/HTMLCreator/HTMLCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export class HTMLCreator {
},
{
type: "td",
content: entry.relevancy
content: entry.relevancy || "-"
},
{
type: "td",
Expand Down
4 changes: 2 additions & 2 deletions src/Logger/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export type FileLog = {
changeType: string,
linesAdded: number,
linesRemoved: number,
relevancy: string,
relevancy: Relevancy | null,
databaseContent: TagIdentifier[],
referencedFiles: FileReference[],
}
Expand Down Expand Up @@ -91,7 +91,7 @@ export class Logger {
changeType: Utils.getEnumKeyByEnumValue(GitDeltaType, fileData.change) || `= ${fileData.change} (unknown)`,
linesAdded: fileInfo.linesAdded,
linesRemoved: 0,
relevancy: Utils.getEnumKeyByEnumValue(Relevancy, fileInfo.relevancy) || `-`,
relevancy: fileInfo.relevancy,
databaseContent: fileInfo.tagIdentifiers,
referencedFiles: fileInfo.usedIn,
}
Expand Down
4 changes: 4 additions & 0 deletions src/References/TSReferenceFinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export class TSReferenceFinder implements IReferenceFinder {

const exportedDeclarations = sourceFile.getExportedDeclarations();

console.log(exportedDeclarations);

for (const declaration of exportedDeclarations.values()) {
let referencedSymbols: Array<ReferencedSymbol> = [];

Expand Down Expand Up @@ -70,6 +72,8 @@ export class TSReferenceFinder implements IReferenceFinder {

const referenceFilePath = path.resolve(reference.getSourceFile().getFilePath());

console.log(referenceFilePath);

const referencedFileInfo: ReferencedFileInfo = {
filename: referenceFilePath,
unused: isUnused
Expand Down
21 changes: 14 additions & 7 deletions src/Report/JiraBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type TagInfo = {

export type ModuleInfo = {
module: string,
count: number
tags: Array<string>
}

export type LinesInfo = {
Expand Down Expand Up @@ -66,8 +66,8 @@ export class JiraBuilder {
const headers = [
{ name: "Affected tags", width: 50 },
{ name: "Lines", width: 20 },
{ name: "Referenced modules (+ count)" },
{ name: "Referenced tags (+ modules)" },
{ name: "Referenced modules" },
{ name: "Referenced tags" },
];
return tableRow(headers.map(header => {
return header.width
Expand All @@ -80,16 +80,23 @@ export class JiraBuilder {
return tableRow([
tableHeader({})(p(entry.affectedTags.join('\n'))),
tableHeader({})(p(`++ ${entry.lines.added}\n-- ${entry.lines.removed}`)),
tableHeader({})(this._parseUniqueModules(entry.uniqueModules)),
tableHeader({})(...this._referencedModulesAsNestedExpands(entry.uniqueModules)),
tableHeader({})(...this._referencedTagsAsNestedExpands(entry.referencedTags, entry.unusedReferences)),
]);
}

private _parseUniqueModules(uniqueModules: ModuleInfo[]) {
private _referencedModulesAsNestedExpands(
uniqueModules: ModuleInfo[],
): any[] {
if (!uniqueModules.length) {
return p("-");
return [p("-")];
}
return p(uniqueModules.map(unique => `(${unique.count}) ${unique.module}`).join('\n'));

return uniqueModules.map(uniqueModule => {
return nestedExpand({ title: uniqueModule.module })(
p(uniqueModule.tags.join('\n'))
);
});
}

private _referencedTagsAsNestedExpands(
Expand Down
93 changes: 43 additions & 50 deletions src/Report/ReportGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,15 @@ export type Report = {
};

export class ReportGenerator {

private _repository: GitRepository;
private _tagsDefinitionFile: TagsDefinitionFile;
private _fileTagsDatabase: FileTagsDatabase;

private _referenceFinders: Array<IReferenceFinder>;

constructor(
repository: GitRepository,
tagsDefinitionFile: TagsDefinitionFile,
fileTagsDatabase: FileTagsDatabase,
referenceFinders: Array<IReferenceFinder>
) {
this._repository = repository;
this._tagsDefinitionFile = tagsDefinitionFile;
this._fileTagsDatabase = fileTagsDatabase;
this._referenceFinders = referenceFinders;
}

public async generateReportForCommit(commit: Commit, projectName: string): Promise<Report> {
return this.generateReportForCommits([commit], projectName, "-", true);
private _repository: GitRepository,
private _tagsDefinitionFile: TagsDefinitionFile,
private _fileTagsDatabase: FileTagsDatabase,
private _referenceFinders: Array<IReferenceFinder>
) { }

public async generateReportForCommit(commit: Commit, projectName: string, relevancyMap: RelevancyMap): Promise<Report> {
return this.generateReportForCommits([commit], projectName, "-", true, relevancyMap);
}

public async generateReportForCommits(
Expand All @@ -75,7 +63,8 @@ export class ReportGenerator {
const affectedModules = this._getAffectedModules(fileInfoArray);

if (printDebugInfo) {
console.log()
console.log(fileInfoArray);
console.log(affectedModules);
}

const report: Report = {
Expand Down Expand Up @@ -148,7 +137,7 @@ export class ReportGenerator {
}
private _getUsedIn(fileData: FileData, relevancy: Relevancy | null) {
// TODO: Add some logging explainig why we do that

console.log(relevancy);
if (relevancy === Relevancy.HIGH) {
return this._getFileReferences(fileData.newPath);
} else {
Expand Down Expand Up @@ -218,30 +207,6 @@ export class ReportGenerator {
};
}

public getReportAsJiraComment(report: Report, printToConsole = false): string {
const finalReportTableData: Array<ReportTableRow> = report.allModules.map(moduleReport => {
const modulesAndTagsInfo = this._getReferencedTags(moduleReport);

return {
affectedTags: this._getAffectedTags(moduleReport),
lines: this._calculateLines(moduleReport),
lastChange: report.date,
uniqueModules: modulesAndTagsInfo.uniqueModules,
referencedTags: modulesAndTagsInfo.tagInfo,
unusedReferences: modulesAndTagsInfo.unusedReferences
};
});

const jiraBuilder = new JiraBuilder();
return jiraBuilder.parseReport(
finalReportTableData,
report.date,
report.projectName,
report.jobName,
printToConsole
);
}

private _getAffectedTags(moduleReport: ModuleReport): Array<string> {
const allTags: Array<Tag["name"]> = [];
const currentModule = this._tagsDefinitionFile.getModuleByName(moduleReport.module);
Expand Down Expand Up @@ -294,12 +259,12 @@ export class ReportGenerator {

const infoInUniqueModules = uniqueModules.find(info => info.module === identifier.module)

if (infoInUniqueModules) {
infoInUniqueModules.count++;
if (infoInUniqueModules && !infoInUniqueModules.tags.includes(identifier.tag)) {
infoInUniqueModules.tags.push(identifier.tag);
} else {
uniqueModules.push({
module: identifier.module,
count: 1,
tags: [],
});
}
}
Expand All @@ -312,9 +277,37 @@ export class ReportGenerator {
}
})
return {
uniqueModules: uniqueModules.sort((uniqueA, uniqueB) => uniqueB.count - uniqueA.count),
uniqueModules: uniqueModules.sort((uniqueA, uniqueB) => uniqueB.tags.length - uniqueA.tags.length),
tagInfo: tagInfo.sort((tagInfoA, tagInfoB) => tagInfoB.modules.length - tagInfoA.modules.length),
unusedReferences: unusedReferences
}
};

public getReportAsJiraComment(report: Report, printToConsole = false): string {
const finalReportTableData: Array<ReportTableRow> = report.allModules.map(moduleReport => {
const modulesAndTagsInfo = this._getReferencedTags(moduleReport);

return {
affectedTags: this._getAffectedTags(moduleReport),
lines: this._calculateLines(moduleReport),
lastChange: report.date,
uniqueModules: modulesAndTagsInfo.uniqueModules,
referencedTags: modulesAndTagsInfo.tagInfo,
unusedReferences: modulesAndTagsInfo.unusedReferences
};
});

const jiraBuilder = new JiraBuilder();
return jiraBuilder.parseReport(
finalReportTableData,
report.date,
report.projectName,
report.jobName,
printToConsole
);
}

public isReportEmpty(report: Report): boolean {
return report.allModules.some(moduleReport => moduleReport.files.some(file => file.tagIdentifiers.length));
}
}

0 comments on commit 1bdb0be

Please sign in to comment.