-
Notifications
You must be signed in to change notification settings - Fork 119
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
Improve Gerrit connector usability #230
Changes from 4 commits
89b8a36
87d0476
e1e1984
448173c
d635644
7ee15f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package pl.touk.sputnik.connector.gerrit; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import pl.touk.sputnik.configuration.Configuration; | ||
import pl.touk.sputnik.configuration.GeneralOption; | ||
|
||
@Data | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class GerritOptions { | ||
/** | ||
* Indicates whether to use Gerrit's internal password token. | ||
*/ | ||
private final boolean useHttpPassword; | ||
/** | ||
* Indicates whether to avoid publishing the same comment again when the review is retriggered | ||
* for the same revision. | ||
*/ | ||
private final boolean omitDuplicateComments; | ||
|
||
static GerritOptions from(Configuration configuration) { | ||
return new GerritOptions( | ||
Boolean.parseBoolean(configuration.getProperty(GeneralOption.GERRIT_USE_HTTP_PASSWORD)), | ||
Boolean.parseBoolean(configuration.getProperty(GeneralOption.GERRIT_OMIT_DUPLICATE_COMMENTS))); | ||
} | ||
|
||
@VisibleForTesting | ||
static GerritOptions empty() { | ||
return new GerritOptions(false, false); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package pl.touk.sputnik.connector.gerrit; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import pl.touk.sputnik.configuration.CliOption; | ||
import pl.touk.sputnik.configuration.Configuration; | ||
import pl.touk.sputnik.configuration.ConfigurationOption; | ||
import pl.touk.sputnik.configuration.GeneralOption; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class GerritFacadeBuilderTest { | ||
private static final String CHANGE_ID_WITH_SLASH = "project/subproject~branch/subbranch~changeId"; | ||
private static final String REVISION_ID = "changeId"; | ||
|
||
@Mock | ||
private Configuration configuration; | ||
|
||
private GerritFacadeBuilder gerritFacadeBuilder; | ||
|
||
@BeforeEach | ||
void setup() { | ||
when(configuration.getProperty(any())) | ||
.then(invocation -> ((ConfigurationOption)invocation.getArgument(0)).getDefaultValue()); | ||
configure(CliOption.CHANGE_ID, CHANGE_ID_WITH_SLASH); | ||
configure(CliOption.REVISION_ID, REVISION_ID); | ||
|
||
gerritFacadeBuilder = new GerritFacadeBuilder(); | ||
} | ||
|
||
@Test | ||
void build_shouldEscapeChangeIdWithSlash() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to fit project style please skip build_ prefix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Roger that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
GerritFacade connector = gerritFacadeBuilder.build(configuration); | ||
|
||
assertEquals("project%2Fsubproject~branch%2Fsubbranch~changeId", connector.gerritPatchset.getChangeId()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use assertj assertions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Roger that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
assertEquals(REVISION_ID, connector.gerritPatchset.getRevisionId()); | ||
} | ||
|
||
@Test | ||
void build_optionsPassed() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldBuildOptions maybe? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Roger that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
configure(GeneralOption.GERRIT_USE_HTTP_PASSWORD, "true"); | ||
configure(GeneralOption.GERRIT_OMIT_DUPLICATE_COMMENTS, "false"); | ||
|
||
GerritFacade connector = gerritFacadeBuilder.build(configuration); | ||
|
||
assertTrue(connector.options.isUseHttpPassword()); | ||
assertFalse(connector.options.isOmitDuplicateComments()); | ||
} | ||
|
||
public void configure(ConfigurationOption option, String value) { | ||
when(configuration.getProperty(option)).thenReturn(value); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,27 +5,41 @@ | |
import com.google.gerrit.extensions.api.GerritApi; | ||
import com.google.gerrit.extensions.api.changes.ChangeApi; | ||
import com.google.gerrit.extensions.api.changes.Changes; | ||
import com.google.gerrit.extensions.api.changes.ReviewInput; | ||
import com.google.gerrit.extensions.api.changes.RevisionApi; | ||
import com.google.gerrit.extensions.common.FileInfo; | ||
import com.google.gerrit.extensions.restapi.RestApiException; | ||
import com.google.gson.Gson; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParser; | ||
import com.urswolfer.gerrit.client.rest.http.changes.FileInfoParser; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import pl.touk.sputnik.configuration.Configuration; | ||
import pl.touk.sputnik.review.Review; | ||
import pl.touk.sputnik.review.ReviewFile; | ||
import pl.touk.sputnik.review.ReviewFormatter; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertSame; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.argThat; | ||
import static org.mockito.Mockito.lenient; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.Mockito.withSettings; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class GerritFacadeTest { | ||
|
@@ -36,13 +50,10 @@ class GerritFacadeTest { | |
|
||
@Mock | ||
private GerritApi gerritApi; | ||
|
||
private GerritFacade gerritFacade; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
gerritFacade = new GerritFacade(gerritApi, null); | ||
} | ||
@Mock | ||
private GerritOptions gerritOptions; | ||
@Mock | ||
private Configuration configuration; | ||
|
||
@Test | ||
void shouldParseListFilesResponse() throws IOException, RestApiException { | ||
|
@@ -56,7 +67,38 @@ void shouldNotListDeletedFiles() throws IOException, RestApiException { | |
assertThat(reviewFiles).hasSize(1); | ||
} | ||
|
||
@Test | ||
void publish() throws IOException, RestApiException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use should... naming style There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Roger that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
when(gerritOptions.isOmitDuplicateComments()).thenReturn(true); | ||
Review review = new Review(new ArrayList<>(), new ReviewFormatter(configuration)); | ||
ArgumentCaptor<ReviewInput> reviewInputCaptor = ArgumentCaptor.forClass(ReviewInput.class); | ||
|
||
createGerritFacade().publish(review); | ||
|
||
verify(gerritApi.changes().id(CHANGE_ID).revision(REVISION_ID)).review(reviewInputCaptor.capture()); | ||
ReviewInput reviewInput = reviewInputCaptor.getValue(); | ||
assertTrue(reviewInput.omitDuplicateComments); | ||
assertEquals(TAG, reviewInput.tag); | ||
} | ||
|
||
@Test | ||
void getRevision() throws IOException, RestApiException { | ||
GerritFacade gerritFacade = createGerritFacade(); | ||
|
||
assertSame(gerritApi.changes().id(CHANGE_ID).revision(REVISION_ID), gerritFacade.getRevision()); | ||
} | ||
|
||
@Test | ||
void setReview_doesPublish() throws IOException, RestApiException { | ||
Review review = new Review(new ArrayList<>(), new ReviewFormatter(configuration)); | ||
|
||
createGerritFacade().setReview(review); | ||
|
||
verify(gerritApi.changes().id(CHANGE_ID).revision(REVISION_ID)).review(any()); | ||
} | ||
|
||
private GerritFacade createGerritFacade() throws IOException, RestApiException { | ||
@SuppressWarnings("UnstableApiUsage") | ||
String listFilesJson = Resources.toString(Resources.getResource("json/gerrit-listfiles.json"), Charsets.UTF_8); | ||
JsonElement jsonElement = new JsonParser().parse(listFilesJson); | ||
Map<String, FileInfo> fileInfoMap = new FileInfoParser(new Gson()).parseFileInfos(jsonElement); | ||
|
@@ -65,10 +107,9 @@ private GerritFacade createGerritFacade() throws IOException, RestApiException { | |
when(gerritApi.changes()).thenReturn(changes); | ||
ChangeApi changeApi = mock(ChangeApi.class); | ||
when(changes.id(CHANGE_ID)).thenReturn(changeApi); | ||
RevisionApi revisionApi = mock(RevisionApi.class); | ||
RevisionApi revisionApi = mock(RevisionApi.class, withSettings().lenient()); | ||
when(changeApi.revision(REVISION_ID)).thenReturn(revisionApi); | ||
when(revisionApi.files()).thenReturn(fileInfoMap); | ||
return new GerritFacade(gerritApi, new GerritPatchset(CHANGE_ID, REVISION_ID, TAG)); | ||
return new GerritFacade(gerritApi, new GerritPatchset(CHANGE_ID, REVISION_ID, TAG), gerritOptions); | ||
} | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just hope you're right and it works :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add a comment with a reference to
ChangesRestClient.id where they do it in a very similar way with the 2 and 3 argument methods.