Skip to content

Commit

Permalink
[#1659] Separate backend and frontend responsibilites (#1691)
Browse files Browse the repository at this point in the history
The backend modifies files generated by the frontend. This breaks the
separation of responsibilities for generated report files, which makes 
testing more challenging.

Let's stop the backend manually modifying report titles in frontend
generated files, and have the frontend update the report title itself. 
The default report title generated by the frontend is now also 
'RepoSense Report' instead of 'reposense' so that there is no delay in
updating the report title in the default case. The backend will proceed
to generate report files in the absence of `templateZip.zip`. Let's
also add a test to check that report titles are correctly updated.
  • Loading branch information
gok99 authored Mar 13, 2022
1 parent 731a268 commit e55de43
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ task systemtest(dependsOn: 'zipReport', type: Test) {
task startServerInBackground(dependsOn: 'classes', type: com.github.psxpaul.task.JavaExecFork) {
main = mainClassName
classpath = sourceSets.main.runtimeClasspath
args = ['--repo', 'https://github.com/reposense/RepoSense.git', 'https://github.com/reposense/testrepo-Empty.git', '--since', 'd1', '--view']
args = ['--config', './frontend/cypress/config', '--since', 'd1', '--view']
String versionJvmArgs = '-Dversion=' + getRepoSenseVersion()
jvmArgs = [ versionJvmArgs ]
waitForPort = 9000
Expand Down
3 changes: 3 additions & 0 deletions frontend/cypress/config/repo-config.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Repository's Location,Branch,File formats,Ignore Glob List,Ignore standalone config,Ignore Commits List,Ignore Authors List,Shallow Cloning,Find Previous Authors
https://github.com/reposense/RepoSense.git,master,,,,,,,
https://github.com/reposense/testrepo-Empty.git,master,,,,,,,
3 changes: 3 additions & 0 deletions frontend/cypress/config/report-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"title": "RepoSense Test Report"
}
5 changes: 5 additions & 0 deletions frontend/cypress/tests/general/general.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('general', () => {
it('correctly replaces report title', () => {
cy.title().should('eq', 'RepoSense Test Report');
});
});
1 change: 1 addition & 0 deletions frontend/src/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ window.api = {
window.repoSenseVersion = data.repoSenseVersion;
window.isSinceDateProvided = data.isSinceDateProvided;
window.isUntilDateProvided = data.isUntilDateProvided;
document.title = data.reportTitle || document.title;

const errorMessages = {};
Object.entries(data.errorSet).forEach(([repoName, message]) => {
Expand Down
6 changes: 6 additions & 0 deletions frontend/vue.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
const StyleLintPlugin = require('stylelint-webpack-plugin');

module.exports = {
pages: {
index: {
entry: 'src/main.js',
title: 'RepoSense Report',
},
},
publicPath: './',
outputDir: 'build/',
configureWebpack: {
Expand Down
33 changes: 10 additions & 23 deletions src/main/java/reposense/report/ReportGenerator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package reposense.report;

import static org.apache.commons.text.StringEscapeUtils.escapeHtml4;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
Expand Down Expand Up @@ -65,8 +63,6 @@ public class ReportGenerator {

// zip file which contains all the report template files
private static final String TEMPLATE_FILE = "/templateZip.zip";
private static final String INDEX_PAGE_TEMPLATE = "index.html";
private static final String INDEX_PAGE_DEFAULT_TITLE = "<title>reposense</title>";

private static final String MESSAGE_INVALID_CONFIG_JSON = "%s Ignoring the config provided by %s (%s).";
private static final String MESSAGE_ERROR_CREATING_DIRECTORY =
Expand All @@ -82,6 +78,8 @@ public class ReportGenerator {
private static final String MESSAGE_COMPLETE_ANALYSIS = "Analysis of %s (%s) completed!";
private static final String MESSAGE_REPORT_GENERATED = "The report is generated at %s";
private static final String MESSAGE_BRANCH_DOES_NOT_EXIST = "Branch %s does not exist in %s! Analysis terminated.";
private static final String MESSAGE_MISSING_TEMPLATE =
"Unable to find template file. Proceeding to generate report...";

private static final String LOG_ERROR_CLONING = "Failed to clone from %s";
private static final String LOG_ERROR_EXPANDING_COMMIT = "Cannot expand %s, it shall remain unexpanded";
Expand Down Expand Up @@ -124,7 +122,7 @@ public static List<Path> generateReposReport(List<RepoConfiguration> configs, St
LocalDateTime untilDate, boolean isSinceDateProvided, boolean isUntilDateProvided, int numCloningThreads,
int numAnalysisThreads, Supplier<String> reportGenerationTimeProvider, ZoneId zoneId,
boolean shouldFreshClone) throws IOException {
prepareTemplateFile(reportConfig, outputPath);
prepareTemplateFile(outputPath);
if (Files.exists(Paths.get(assetsPath))) {
FileUtil.copyDirectoryContents(assetsPath, outputPath, assetsFilesWhiteList);
}
Expand Down Expand Up @@ -153,26 +151,15 @@ public static List<Path> generateReposReport(List<RepoConfiguration> configs, St

/**
* Copies the template file to the specified {@code outputPath} for the repo report to be generated.
* @throws IOException if template resource is not found.
* @throws IOException if I/O error encountered while copying template file.
*/
private static void prepareTemplateFile(ReportConfiguration config, String outputPath) throws IOException {
private static void prepareTemplateFile(String outputPath) throws IOException {
InputStream is = RepoSense.class.getResourceAsStream(TEMPLATE_FILE);
FileUtil.copyTemplate(is, outputPath);
setReportConfiguration(config, outputPath);
}

private static void setReportConfiguration(ReportConfiguration config, String outputPath) throws IOException {
setLandingPageTitle(outputPath, config.getTitle());
}

/**
* Set title of template file located at {@code filePath} to {@code pageTitle}
*/
private static void setLandingPageTitle(String filePath, String pageTitle) throws IOException {
Path indexPagePath = Paths.get(filePath, INDEX_PAGE_TEMPLATE);
String line = new String(Files.readAllBytes(indexPagePath));
String newLine = line.replaceAll(INDEX_PAGE_DEFAULT_TITLE, "<title>" + escapeHtml4(pageTitle) + "</title>");
Files.write(indexPagePath, newLine.getBytes());
if (is != null) {
FileUtil.copyTemplate(is, outputPath);
} else {
logger.warning(MESSAGE_MISSING_TEMPLATE);
}
}

/**
Expand Down

0 comments on commit e55de43

Please sign in to comment.