Skip to content

Commit

Permalink
Merge pull request #49 from MaibornWolff/Gitlogparser-author-toggle
Browse files Browse the repository at this point in the history
Added possibility to toggle the "authors" Attribute of SCMLogParser
  • Loading branch information
ukinimod authored Nov 1, 2017
2 parents 0258dcc + 8f31987 commit 9be7c1d
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 23 deletions.
4 changes: 2 additions & 2 deletions analysis/import/SCMLogParser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Generates visualisation data from repository (Git or SVN) logs. It supports the
| `weeks_with_commits` | weeks with commits |
| `number_of_authors` | number of authors with commits |

Additionally it saves the names of authors.
Additionally it saves the names of authors when the --a flag is set.

## Usage

Expand All @@ -21,6 +21,6 @@ The generated logs must be in UTF-8 encoding.

### Executing the SCMLogParser

> `ccsh scmlogparser <log file> [--git|--svn] [<output file>]`
> `ccsh scmlogparser <log file> [--git|--svn] [--a] [<output file>]`
The result is written as JSON to standard out or into the specified output file.
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,25 @@ private ProjectConverter() {
// utility class
}

public static Project convert(String projectName, List<VersionControlledFile> versionControlledFiles) {
public static Project convert(String projectName, List<VersionControlledFile> versionControlledFiles, boolean containsAuthors) {
Project project = new Project(projectName);
versionControlledFiles.forEach(vcFile -> ProjectConverter.addVersionControlledFile(project, vcFile));
versionControlledFiles.forEach(vcFile -> ProjectConverter.addVersionControlledFile(project, vcFile, containsAuthors));
return project;
}

private static void addVersionControlledFile(Project project, VersionControlledFile versionControlledFile) {
Map<String, Object> attributes = extractAttributes(versionControlledFile);
private static void addVersionControlledFile(Project project, VersionControlledFile versionControlledFile, boolean containsAuthors) {
Map<String, Object> attributes = extractAttributes(versionControlledFile, containsAuthors);
Node newNode = new Node(extractFilenamePart(versionControlledFile), NodeType.File, attributes, "", Collections.emptyList());
NodeInserter.insertByPath(project, PathFactory.fromFileSystemPath(extractPathPart(versionControlledFile)), newNode);
}

private static Map<String, Object> extractAttributes(VersionControlledFile versionControlledFile) {
private static Map<String, Object> extractAttributes(VersionControlledFile versionControlledFile, boolean containsAuthors) {
HashMap<String, Object> attributes = new HashMap<>();
attributes.put("number_of_commits", versionControlledFile.getNumberOfOccurrencesInCommits());
attributes.put("weeks_with_commits", versionControlledFile.getNumberOfWeeksWithCommits());
attributes.put("authors", versionControlledFile.getAuthors());
if (containsAuthors) {
attributes.put("authors", versionControlledFile.getAuthors());
}
attributes.put("number_of_authors", versionControlledFile.getNumberOfAuthors());
return attributes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,24 @@ public static void main(String[] args) throws IOException {
String pathToLog = args[0];
String gitOrSvn = args[1];

Project project = parseDataFromLog(pathToLog, gitOrSvn);
String outputFile = null;

boolean containsAuthors = false;

if (args.length >= 3) {
ProjectSerializer.serializeProjectAndWriteToFile(project, args[2]);
if ("--a".equals(args[2])) {
containsAuthors = true;
if (args.length >= 4) {
outputFile = args[3];
}
} else {
outputFile = args[2];
}
}

Project project = parseDataFromLog(pathToLog, gitOrSvn, containsAuthors);
if (outputFile != null) {
ProjectSerializer.serializeProjectAndWriteToFile(project, outputFile);
} else {
ProjectSerializer.serializeProject(project, new OutputStreamWriter(System.out));
}
Expand All @@ -36,7 +51,7 @@ public static void main(String[] args) throws IOException {
}
}

private static Project parseDataFromLog(String pathToLog, String gitOrSvn) throws IOException {
private static Project parseDataFromLog(String pathToLog, String gitOrSvn, boolean containsAuthors) throws IOException {
LogParserStrategy parserStrategy = null;
switch (gitOrSvn) {
case "--git":
Expand All @@ -49,7 +64,7 @@ private static Project parseDataFromLog(String pathToLog, String gitOrSvn) throw
showErrorAndTerminate();
}
Stream<String> lines = Files.lines(Paths.get(pathToLog));
return new LogParser(parserStrategy).parse(lines);
return new LogParser(parserStrategy, containsAuthors).parse(lines);
}

private static void showErrorAndTerminate() {
Expand All @@ -58,8 +73,9 @@ private static void showErrorAndTerminate() {
}

private static void showHelpAndTerminate() {
System.out.println("Please use the following syntax\n\"SCMLogParser-x.x.jar <pathToLogFile> --git/--svn\" [<pathToOutputfile>]\n" +
System.out.println("Please use the following syntax\n\"SCMLogParser-x.x.jar <pathToLogFile> --git/--svn [--a] [<pathToOutputfile>]\"\n" +
"The log file must have been created by using \"svn log --verbose\" or \"git log --name-status\"\n" +
"Set the --a parameter to add an array of authors to every file of your output\n" +
"If no output file was specified, the output will be piped to standard out");
System.exit(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
public class LogParser {

private final LogParserStrategy parserStrategy;
private final boolean containsAuthors;

public LogParser(LogParserStrategy parserStrategy) {
public LogParser(LogParserStrategy parserStrategy, boolean containsAuthors) {
this.parserStrategy = parserStrategy;
this.containsAuthors = containsAuthors;
}

public Project parse(Stream<String> lines) {
Expand All @@ -29,7 +31,7 @@ List<VersionControlledFile> parseLoglines(Stream<String> logLines) {
}

private Project convertToProject(List<VersionControlledFile> versionControlledFiles) {
return ProjectConverter.convert("SCMLogParser", versionControlledFiles);
return ProjectConverter.convert("SCMLogParser", versionControlledFiles, containsAuthors);
}

Commit parseCommit(List<String> commitLines) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package de.maibornwolff.codecharta.importer.scmlogparser;

import de.maibornwolff.codecharta.model.Project;
import de.maibornwolff.codecharta.model.input.Commit;
import de.maibornwolff.codecharta.model.input.VersionControlledFile;
import org.junit.Test;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -15,12 +19,40 @@ public void canCreateAnEmptyProject() throws Exception {
String projectname = "Projectname";

// when
Project project = ProjectConverter.convert(projectname, Collections.emptyList());
Project project = ProjectConverter.convert(projectname, Collections.emptyList(), true);

//then
assertThat(project.getNodes()).hasSize(0);
assertThat(project.getProjectName()).isEqualTo(projectname);
}

@Test
public void canConvertProjectWithAuthors() {
//given
String projectname = "ProjectWithAuthors";
VersionControlledFile file1 = new VersionControlledFile("File 1");
file1.registerCommit(new Commit("Author", Arrays.asList("File 1, File 2"), LocalDateTime.now()));

//when
Project project = ProjectConverter.convert(projectname, Arrays.asList(file1), true);

//then
assertThat(project.getRootNode().getChildren().get(0).getAttributes().containsKey("authors")).isTrue();
}

@Test
public void canConvertProjectWithoutAuthors() {
//given
String projectname = "ProjectWithoutAuthors";
VersionControlledFile file1 = new VersionControlledFile("File 1");
file1.registerCommit(new Commit("Author", Arrays.asList("File 1, File 2"), LocalDateTime.now()));

//when
Project project = ProjectConverter.convert(projectname, Arrays.asList(file1), false);

//then
assertThat(project.getRootNode().getChildren().get(0).getAttributes().containsKey("authors")).isFalse();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class LogParserTest {
@Test
public void logParserSVNGoldenMasterTest() throws Exception {
// given
LogParser svnLogParser = new LogParser(new SVNLogParserStrategy());
LogParser svnLogParser = new LogParser(new SVNLogParserStrategy(), true);
InputStreamReader ccjsonReader = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(EXPECTED_SVN_CC_JSON));
Project expectedProject = ProjectDeserializer.deserializeProject(ccjsonReader);
URL resource = this.getClass().getClassLoader().getResource(SVN_LOG);
Expand All @@ -54,7 +54,7 @@ public void logParserSVNGoldenMasterTest() throws Exception {
@Test
public void logParserGitGoldenMasterTest() throws Exception {
// given
LogParser gitLogParser = new LogParser(new GitLogParserStrategy());
LogParser gitLogParser = new LogParser(new GitLogParserStrategy(), true);
InputStreamReader ccjsonReader = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(EXPECTED_GIT_CC_JSON));
Project expectedProject = ProjectDeserializer.deserializeProject(ccjsonReader);
URL resource = this.getClass().getClassLoader().getResource(GIT_LOG);
Expand Down Expand Up @@ -83,7 +83,7 @@ public void parseCommitRaisesExceptionWhenAuthorIsMissing() {
when(parserStrategy.parseAuthor(any())).thenReturn(Optional.empty());
when(parserStrategy.parseDate(any())).thenReturn(Optional.of(LocalDateTime.now()));
when(parserStrategy.parseFilenames(any())).thenReturn(Collections.emptyList());
LogParser logParser = new LogParser(parserStrategy);
LogParser logParser = new LogParser(parserStrategy, true);

// when & then
assertThatThrownBy(() -> logParser.parseCommit(Collections.emptyList())).isInstanceOf(RuntimeException.class);
Expand All @@ -96,7 +96,7 @@ public void parseCommitRaisesExceptionWhenCommitDateIsMissing() {
when(parserStrategy.parseAuthor(any())).thenReturn(Optional.of("An Author"));
when(parserStrategy.parseDate(any())).thenReturn(Optional.empty());
when(parserStrategy.parseFilenames(any())).thenReturn(Collections.emptyList());
LogParser logParser = new LogParser(parserStrategy);
LogParser logParser = new LogParser(parserStrategy, true);

// when & then
assertThatThrownBy(() -> logParser.parseCommit(Collections.emptyList())).isInstanceOf(RuntimeException.class);
Expand All @@ -113,7 +113,7 @@ public void parseCommit() {
when(parserStrategy.parseAuthor(input)).thenReturn(Optional.of(author));
when(parserStrategy.parseDate(input)).thenReturn(Optional.of(commitDate));
when(parserStrategy.parseFilenames(input)).thenReturn(filenames);
LogParser logParser = new LogParser(parserStrategy);
LogParser logParser = new LogParser(parserStrategy, true);

// when
Commit commit = logParser.parseCommit(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public abstract class ParserStrategyContractTest {

@Test
public void parsesCommit() {
LogParser logParser = new LogParser(getLogParserStrategy());
LogParser logParser = new LogParser(getLogParserStrategy(), true);
Commit commit = logParser.parseCommit(getFullCommit());
assertThat(commit)
.extracting(Commit::getAuthor, Commit::getFilenames, Commit::getCommitDate)
Expand Down Expand Up @@ -70,7 +70,7 @@ public void canProvidesAnAproppriateLogLineCollectorToSeparateCommits() throws E
@Test
public void accumulatesCommitFiles() {
Stream<String> logLines = Stream.concat(getFullCommit().stream(), getFullCommit().stream());
List<VersionControlledFile> files = new LogParser(getLogParserStrategy()).parseLoglines(logLines);
List<VersionControlledFile> files = new LogParser(getLogParserStrategy(), true).parseLoglines(logLines);
assertThat(files)
.extracting(VersionControlledFile::getFilename, VersionControlledFile::getNumberOfOccurrencesInCommits, VersionControlledFile::getAuthors)
.containsExactlyInAnyOrder(
Expand Down

0 comments on commit 9be7c1d

Please sign in to comment.