Skip to content

Commit 521fea9

Browse files
Marquis WongMarquis Wong
authored andcommitted
Add "local" connector type
Runs analysis tools on the HEAD of the local git repository and reports to the log. Allows someone to check for any comments before pushing to Gerrit/Gitlab.
1 parent 01f0303 commit 521fea9

File tree

10 files changed

+176
-4
lines changed

10 files changed

+176
-4
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ dependencies {
7272
compile 'commons-cli:commons-cli:1.2'
7373
compile 'com.jayway.jsonpath:json-path:2.4.0'
7474
compile 'commons-io:commons-io:2.4'
75+
compile 'org.eclipse.jgit:org.eclipse.jgit:5.5.1.201910021850-r'
7576

7677
compile 'com.urswolfer.gerrit.client.rest:gerrit-rest-java-client:0.8.8'
7778

src/main/java/pl/touk/sputnik/configuration/GeneralOption.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public enum GeneralOption implements ConfigurationOption {
1919
MESSAGE_PROBLEM_FORMAT("message.problemFormat", "Sputnik problem format. {0}: reporter, {1}: message", "There is a problem with {0}: {1}"),
2020
MESSAGE_SCORE_PASSING_COMMENT("message.scorePassingComment", "Comment when no errors are found", "Perfect!"),
2121

22-
CONNECTOR_TYPE("connector.type", "Connector: <stash|gerrit|github|saas>", ConnectorType.GERRIT.getName()),
22+
CONNECTOR_TYPE("connector.type", "Connector: <stash|gerrit|github|saas|local>", ConnectorType.GERRIT.getName()),
2323
HOST("connector.host", "Connector server host", "localhost"),
2424
PORT("connector.port", "Connector server port", "80"),
2525
PATH("connector.path", "Connector server path", ""),

src/main/java/pl/touk/sputnik/connector/ConnectorFacadeFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pl.touk.sputnik.configuration.GeneralOptionNotSupportedException;
66
import pl.touk.sputnik.connector.gerrit.GerritFacadeBuilder;
77
import pl.touk.sputnik.connector.github.GithubFacadeBuilder;
8+
import pl.touk.sputnik.connector.local.LocalFacadeBuilder;
89
import pl.touk.sputnik.connector.saas.SaasFacadeBuilder;
910
import pl.touk.sputnik.connector.stash.StashFacadeBuilder;
1011

@@ -15,6 +16,7 @@ public class ConnectorFacadeFactory {
1516
StashFacadeBuilder stashFacadeBuilder = new StashFacadeBuilder();
1617
GithubFacadeBuilder githubFacadeBuilder = new GithubFacadeBuilder();
1718
SaasFacadeBuilder saasFacadeBuilder = new SaasFacadeBuilder();
19+
LocalFacadeBuilder localFacadeBuilder = new LocalFacadeBuilder();
1820

1921
@NotNull
2022
public ConnectorFacade build(@NotNull ConnectorType type, Configuration configuration) {
@@ -27,6 +29,9 @@ public ConnectorFacade build(@NotNull ConnectorType type, Configuration configur
2729
return githubFacadeBuilder.build(configuration);
2830
case SAAS:
2931
return saasFacadeBuilder.build(configuration);
32+
case LOCAL:
33+
return localFacadeBuilder.build();
34+
3035
default:
3136
throw new GeneralOptionNotSupportedException("Connector " + type.getName() + " is not supported");
3237
}

src/main/java/pl/touk/sputnik/connector/ConnectorType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public enum ConnectorType {
1313
GERRIT("gerrit"),
1414
STASH("stash"),
1515
GITHUB("github"),
16-
SAAS("saas");
16+
SAAS("saas"),
17+
LOCAL("local");
1718

1819
/** Name used in configuration file. */
1920
@Getter
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package pl.touk.sputnik.connector;
22

33
public enum Connectors {
4-
STASH, GERRIT, GITHUB, SAAS
4+
STASH, GERRIT, GITHUB, SAAS, LOCAL
55
}

src/main/java/pl/touk/sputnik/connector/gerrit/GerritFacade.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,5 @@ public void validate(Configuration configuration) throws GeneralOptionNotSupport
8585
public void setReview(@NotNull Review review) {
8686
publish(review);
8787
}
88+
8889
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package pl.touk.sputnik.connector.local;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.eclipse.jgit.api.Git;
5+
import org.eclipse.jgit.api.errors.GitAPIException;
6+
import org.eclipse.jgit.diff.DiffEntry;
7+
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
8+
import org.jetbrains.annotations.NotNull;
9+
import pl.touk.sputnik.configuration.Configuration;
10+
import pl.touk.sputnik.configuration.GeneralOption;
11+
import pl.touk.sputnik.configuration.GeneralOptionNotSupportedException;
12+
import pl.touk.sputnik.connector.ConnectorFacade;
13+
import pl.touk.sputnik.connector.ConnectorValidator;
14+
import pl.touk.sputnik.connector.Connectors;
15+
import pl.touk.sputnik.connector.ReviewPublisher;
16+
import pl.touk.sputnik.review.Comment;
17+
import pl.touk.sputnik.review.Review;
18+
import pl.touk.sputnik.review.ReviewFile;
19+
20+
import java.util.List;
21+
22+
import static java.util.stream.Collectors.toList;
23+
24+
@Slf4j
25+
public class LocalFacade implements ConnectorFacade, ConnectorValidator, ReviewPublisher {
26+
private final Git git;
27+
28+
public LocalFacade(Git git) {
29+
this.git = git;
30+
}
31+
32+
@NotNull
33+
@Override
34+
public List<ReviewFile> listFiles() {
35+
try {
36+
List<DiffEntry> diffs = git.diff().call();
37+
return diffs.stream()
38+
.filter(this::isNotDeleted)
39+
.map(DiffEntry::getNewPath)
40+
.map(ReviewFile::new)
41+
.collect(toList());
42+
} catch (GitAPIException e) {
43+
throw new RuntimeException("Error when listing files", e);
44+
}
45+
}
46+
47+
private boolean isNotDeleted(DiffEntry aDiffEntry) {
48+
return aDiffEntry.getChangeType() != ChangeType.DELETE;
49+
}
50+
51+
@Override
52+
public void publish(@NotNull Review review) {
53+
for (ReviewFile file : review.getFiles()) {
54+
if (file.getComments().isEmpty()) {
55+
continue;
56+
}
57+
58+
log.warn("{} comments on {}", file.getComments().size(), file.getReviewFilename());
59+
for (Comment comment : file.getComments()) {
60+
log.warn("Line {}: {}", comment.getLine(), comment.getMessage());
61+
}
62+
log.warn("");
63+
}
64+
for (String message : review.getMessages()) {
65+
log.warn(message);
66+
}
67+
}
68+
69+
@Override
70+
public Connectors name() {
71+
return Connectors.LOCAL;
72+
}
73+
74+
@Override
75+
public void validate(Configuration configuration) throws GeneralOptionNotSupportedException {
76+
boolean commentOnlyChangedLines = Boolean.parseBoolean(configuration
77+
.getProperty(GeneralOption.COMMENT_ONLY_CHANGED_LINES));
78+
79+
if (commentOnlyChangedLines) {
80+
throw new GeneralOptionNotSupportedException("This connector does not support "
81+
+ GeneralOption.COMMENT_ONLY_CHANGED_LINES.getKey());
82+
}
83+
}
84+
85+
@Override
86+
public void setReview(@NotNull Review review) {
87+
publish(review);
88+
}
89+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package pl.touk.sputnik.connector.local;
2+
3+
import org.eclipse.jgit.api.Git;
4+
import org.eclipse.jgit.lib.Repository;
5+
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
6+
7+
import java.io.IOException;
8+
9+
public class LocalFacadeBuilder {
10+
public LocalFacade build() {
11+
try (Repository repository = new FileRepositoryBuilder().readEnvironment().findGitDir().build()) {
12+
try (Git git = new Git(repository)) {
13+
return new LocalFacade(git);
14+
}
15+
} catch (IOException e) {
16+
throw new RuntimeException("Error getting git repository", e);
17+
}
18+
}
19+
}

src/main/java/pl/touk/sputnik/review/ReviewFile.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,4 @@ public String getSourceDir() {
3939
private String createJavaClassName() {
4040
return StringUtils.substringBeforeLast(ENTRY_PATTERN.matcher(reviewFilename).replaceFirst(""), DOT).replace('/', '.');
4141
}
42-
4342
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package pl.touk.sputnik.connector.local;
2+
3+
import com.google.common.collect.ImmutableList;
4+
import org.eclipse.jgit.api.Git;
5+
import org.eclipse.jgit.api.errors.GitAPIException;
6+
import org.eclipse.jgit.diff.DiffEntry;
7+
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.ExtendWith;
11+
import org.mockito.Mock;
12+
import org.mockito.junit.jupiter.MockitoExtension;
13+
import pl.touk.sputnik.review.ReviewFile;
14+
15+
import java.util.List;
16+
17+
import static org.assertj.core.api.Assertions.assertThat;
18+
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
19+
import static org.mockito.Mockito.when;
20+
21+
@ExtendWith(MockitoExtension.class)
22+
class LocalFacadeTest {
23+
private LocalFacade localFacade;
24+
@Mock(answer = RETURNS_DEEP_STUBS)
25+
private Git git;
26+
@Mock
27+
private DiffEntry modifiedFile, deletedFile, newFile;
28+
29+
@BeforeEach
30+
void setUp() throws GitAPIException {
31+
localFacade = new LocalFacade(git);
32+
33+
when(modifiedFile.getNewPath()).thenReturn("/path/to/modifiedFile");
34+
when(modifiedFile.getChangeType()).thenReturn(ChangeType.MODIFY);
35+
36+
when(newFile.getNewPath()).thenReturn("/path/to/newFile");
37+
when(newFile.getChangeType()).thenReturn(ChangeType.ADD);
38+
39+
when(deletedFile.getChangeType()).thenReturn(ChangeType.DELETE);
40+
41+
when(git.diff().call()).thenReturn(ImmutableList.of(modifiedFile, deletedFile, newFile));
42+
}
43+
44+
@Test
45+
void shouldParseListFilesResponse() {
46+
List<ReviewFile> reviewFiles = localFacade.listFiles();
47+
assertThat(reviewFiles).isNotEmpty();
48+
}
49+
50+
@Test
51+
void shouldNotListDeletedFiles() {
52+
List<ReviewFile> reviewFiles = localFacade.listFiles();
53+
assertThat(reviewFiles)
54+
.hasSize(2)
55+
.extracting(ReviewFile::getReviewFilename).containsExactly(modifiedFile.getNewPath(), newFile.getNewPath());
56+
}
57+
}

0 commit comments

Comments
 (0)