Skip to content

Commit

Permalink
#46 Parsing author and timestamp for Todo.
Browse files Browse the repository at this point in the history
  • Loading branch information
criske committed Jan 7, 2021
1 parent 59099e1 commit ad27930
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 11 deletions.
50 changes: 48 additions & 2 deletions src/main/java/com/selfxdsd/todocli/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ public final class Todo {
*/
private String path;


/**
* The author of todo.
*/
private String author;

/**
* Timestamp of creation.
*/
private String timestamp;

/**
* Creates a new Todo object.
*
Expand Down Expand Up @@ -211,6 +222,38 @@ public void setPath(final String path) {
}
}

/**
* Set the todo author.
* @param author Author.
*/
public void setAuthor(final String author){
this.author = author;
}

/**
* Get the todo author.
* @return String
*/
public String getAuthor(){
return this.author;
}

/**
* Set the todo creation timestamp.
* @param timestamp Timestamp.
*/
public void setTimestamp(final String timestamp){
this.timestamp = timestamp;
}

/**
* Get the todo timestamp.
* @return String.
*/
public String getTimestamp(){
return this.timestamp;
}


/**
* Compares this to the given, other, Todo object.
Expand Down Expand Up @@ -267,12 +310,15 @@ public String toString() {

return String.format(
"TODO [%s, TicketID: %s, Estimated Time: %s, "
+ "Body: '%s', Path: '%s']",
+ "Body: '%s', Path: '%s', Author: '%s', "
+ "Timestamp: '%s'.]",
linesPart,
this.ticketID,
this.estimatedTime,
this.body,
this.path
this.path,
this.author,
this.timestamp
);
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/selfxdsd/todocli/TodoBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ class TodoBuilder {
*/
private String path;


/**
* The author of todo.
*/
private String author;

/**
* Timestamp of creation.
*/
private String timestamp;

/**
* The first line of the Todo.
*
Expand Down Expand Up @@ -130,6 +141,26 @@ public TodoBuilder setPath(final String path) {
return this;
}

/**
* Set the todo author.
* @param author Author.
* @return Builder.
*/
public TodoBuilder setAuthor(final String author){
this.author = author;
return this;
}

/**
* Set the todo creation timestamp.
* @param timestamp Timestamp.
* @return Builder.
*/
public TodoBuilder setTimestamp(final String timestamp){
this.timestamp = timestamp;
return this;
}

/**
* Creates a new Todo.
*
Expand All @@ -144,6 +175,8 @@ public Todo build() {
this.body
);
todo.setPath(this.path);
todo.setAuthor(this.author);
todo.setTimestamp(this.timestamp);
return todo;
}
}
41 changes: 32 additions & 9 deletions src/main/java/com/selfxdsd/todocli/TodoParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
package com.selfxdsd.todocli;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
Expand All @@ -38,11 +38,20 @@
*/
public final class TodoParser {

/**
* Git blame header pattern that should be at the start of each line.
*/
private static final String GIT_BLAME_PATTERN =
".*\\((.+)\\s+(\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}\\s\\+\\d{4})"
+ "\\s+\\d+\\)\\s";

/**
* TODO Pattern.
*/
private static final Pattern TODO_PATTERN = Pattern.compile(
"^\\W*(@todo|TODO|@fixme|FIXME)\\s*(#\\d+:\\d+(m|min|mins))\\b(.*)$"
"^" + GIT_BLAME_PATTERN
+ "\\W*(@todo|TODO|@fixme|FIXME)\\s*(#\\d+:\\d+(m|min|mins))\\b(.*)"
+ "$"
);

/**
Expand All @@ -53,9 +62,7 @@ public final class TodoParser {
*/
public List<Todo> parse(final String path) throws IOException {
final List<Todo> todos = new ArrayList<>();
try (final BufferedReader reader = new BufferedReader(
new FileReader(path)
)) {
try (final BufferedReader reader = this.readFileWithBlame(path)) {
final StringBuilder bodyBuilder = new StringBuilder();
final TodoBuilder todoBuilder = new TodoBuilder().setPath(path);
int lineIndex = -1;
Expand Down Expand Up @@ -118,10 +125,12 @@ private int checkForTodo(final StringBuilder bodyBuilder,
final Matcher matcher = TODO_PATTERN.matcher(line);
final int todoPosition;
if (matcher.find()) {
todoPosition = matcher.start(1);
todoBuilder.setStart(lineIndex + 1);
bodyBuilder.append(matcher.group(4));
this.addHeader(todoBuilder, matcher.group(2));
todoPosition = matcher.start(3);
todoBuilder.setAuthor(matcher.group(1).trim())
.setTimestamp(matcher.group(2))
.setStart(lineIndex + 1);
bodyBuilder.append(matcher.group(6));
this.addHeader(todoBuilder, matcher.group(4));
} else {
todoPosition = -1;
}
Expand Down Expand Up @@ -165,4 +174,18 @@ private void addHeader(final TodoBuilder todoBuilder,
);
}

/**
* Reads file together with its git blame.
* @param path File path.
* @return Reader.
* @throws IOException If something goes wrong.
*/
private BufferedReader readFileWithBlame(final String path)
throws IOException {
final String command = "git blame " + path;
final Process process = Runtime.getRuntime().exec(command);
return new BufferedReader(
new InputStreamReader(process.getInputStream())
);
}
}
9 changes: 9 additions & 0 deletions src/test/java/com/selfxdsd/todocli/TodoParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,15 @@ public void shouldAllowPyFilesToBeParsedForTodos() throws IOException {
"This is a multiline todo for python.",
todos.get(0).getBody()
);
assertEquals(
"cristianpela",
todos.get(0).getAuthor()
);
assertEquals(
"2020-12-15 14:39:07 +0200",
todos.get(0).getTimestamp()
);

assertEquals(
"This is another multiline todo for python.",
todos.get(1).getBody()
Expand Down

1 comment on commit ad27930

@charlesmike
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@criske I've opened the Issues [#48, #49, #50, #51, #52, #53, #54, #55, #56, #57, #58, #59, #60, #61, #62, #63, #64, #65, #66, #67, #68, #69, #70, #71] for the newly added to-dos.

The to-dos may have been added in an earlier commit, but I've found them just now.

Please sign in to comment.