Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plagiarism check: Fix display of escaped characters #6123

Merged
merged 15 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ spotless {
java {
target project.fileTree(project.rootDir) {
include "**/*.java"
exclude "**/src/main/java/de/tum/in/www1/artemis/service/connectors/BambooService.java", "**/src/test/resources/test-data/repository-export/EncodingISO_8559_1.java", "**/node_modules/**", "**/out/**", "**/repos/**", "**/build/**", "**/src/main/generated/**", "**/src/main/resources/templates/**"
exclude "**/src/main/java/de/tum/in/www1/artemis/service/connectors/BambooService.java", "**/src/test/resources/test-data/repository-export/EncodingISO_8559_1.java", "**/node_modules/**", "**/out/**", "**/repos/**", "**/repos-download/**", "**/build/**", "**/src/main/generated/**", "**/src/main/resources/templates/**"
}
importOrderFile "artemis-spotless.importorder"
eclipse("4.19.0").configFile "artemis-spotless-style.xml"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import javax.persistence.Entity;

import org.apache.commons.lang3.ArrayUtils;

import de.jplag.JPlagResult;
import de.tum.in.www1.artemis.domain.Exercise;
import de.tum.in.www1.artemis.domain.plagiarism.PlagiarismComparison;
Expand All @@ -30,11 +28,7 @@ public void convertJPlagResult(JPlagResult result, Exercise exercise) {
this.comparisons.add(comparison);
}
this.duration = result.getDuration();
// NOTE: there seems to be an issue in JPlag 4.0 that the similarity distribution is reversed, either in the implementation or in the documentation.
// we use it like this: 0: [0% - 10%), 1: [10% - 20%), 2: [20% - 30%), ..., 9: [90% - 100%] so we reverse it
var similarityDistribution = result.getSimilarityDistribution();
ArrayUtils.reverse(similarityDistribution);
this.setSimilarityDistribution(similarityDistribution);
this.setSimilarityDistribution(result.getSimilarityDistribution());
this.setExercise(exercise);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,11 @@ private List<Repository> downloadRepositories(ProgrammingExercise programmingExe
// Used for sending progress notifications
var topic = plagiarismWebsocketService.getProgrammingExercisePlagiarismCheckTopic(programmingExercise.getId());

int maxRepositories = participations.size() + 1;
List<Repository> downloadedRepositories = new ArrayList<>();
participations.parallelStream().forEach(participation -> {
try {
var progressMessage = "Downloading repositories: " + (downloadedRepositories.size() + 1) + "/" + participations.size();
var progressMessage = "Downloading repositories: " + (downloadedRepositories.size() + 1) + "/" + maxRepositories;
plagiarismWebsocketService.notifyInstructorAboutPlagiarismState(topic, PlagiarismCheckState.RUNNING, List.of(progressMessage));

Repository repo = gitService.getOrCheckoutRepositoryForJPlag(participation, targetPath);
Expand All @@ -363,6 +364,9 @@ private List<Repository> downloadRepositories(ProgrammingExercise programmingExe

// clone the template repo
try {
var progressMessage = "Downloading repositories: " + maxRepositories + "/" + maxRepositories;
plagiarismWebsocketService.notifyInstructorAboutPlagiarismState(topic, PlagiarismCheckState.RUNNING, List.of(progressMessage));

Repository templateRepo = gitService.getOrCheckoutRepository(programmingExercise.getTemplateParticipation(), targetPath);
gitService.resetToOriginHead(templateRepo); // start with clean state
downloadedRepositories.add(templateRepo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class TextSubmissionViewerComponent implements OnChanges {
this.repositoryService.getFile(file, domain).subscribe({
next: ({ fileContent }) => {
this.loading = false;
this.fileContent = this.insertMatchTokens(escape(fileContent));
this.fileContent = this.insertMatchTokens(fileContent);
},
error: () => {
this.loading = false;
Expand Down Expand Up @@ -206,14 +206,24 @@ export class TextSubmissionViewerComponent implements OnChanges {
if (position < 0) {
position = 0;
}
return [text.slice(0, position), token, text.slice(position)].join('');
return escape(text.slice(0, position)) + token + escape(text.slice(position));
}

insertMatchTokens(fileContent: string) {
insertMatchTokens(fileContent: string): string {
const matches = this.getMatchesForCurrentFile().sort((m1, m2) => m1.from.line - m2.from.line);
if (!matches.length) {
return escape(fileContent);
}

const rows = fileContent.split('\n');
const matches = this.getMatchesForCurrentFile();
const offsets = new Array(rows.length).fill(0);

for (let i = 0; i < matches[0].from.line - 1; i++) {
if (rows[i]) {
rows[i] = escape(rows[i]);
}
}

matches.forEach((match) => {
if (!match.from) {
captureException(new Error('"from" is not defined in insertMatchTokens'));
Expand All @@ -234,6 +244,12 @@ export class TextSubmissionViewerComponent implements OnChanges {
offsets[idxLineFrom] += this.tokenStart.length;
}

for (let i = idxLineFrom + 1; i < idxLineTo; i++) {
if (rows[i]) {
rows[i] = escape(rows[i]);
}
}

const idxColumnTo = match.to.column + match.to.length - 1 + offsets[idxLineTo];

if (rows[idxLineTo]) {
Expand All @@ -242,6 +258,12 @@ export class TextSubmissionViewerComponent implements OnChanges {
}
});

for (let i = matches.last()!.to.line; i < rows.length; i++) {
if (rows[i]) {
rows[i] = escape(rows[i]);
}
}

return rows.join('\n');
}
}