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

Add --author option to content-generator commands #7232

Merged
merged 1 commit into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ void deleteContentWithMessage() throws NessieNotFoundException {
"delete",
"--uri",
NESSIE_API_URI,
"--author",
"Test Author 123 <test@example.com>",
"--message",
"test-message-123",
"--branch",
Expand All @@ -92,6 +94,7 @@ void deleteContentWithMessage() throws NessieNotFoundException {
CommitMeta commitMeta =
api.getCommitLog().refName(branch.getName()).get().getLogEntries().get(0).getCommitMeta();
assertThat(commitMeta.getMessage()).contains("test-message-123");
assertThat(commitMeta.getAuthor()).isEqualTo("Test Author 123 <test@example.com>");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.InstanceOfAssertFactories.type;
import static org.projectnessie.model.Content.Type.DELTA_LAKE_TABLE;
import static org.projectnessie.model.Content.Type.ICEBERG_TABLE;
import static org.projectnessie.model.Content.Type.ICEBERG_VIEW;
Expand All @@ -26,7 +27,9 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.projectnessie.client.api.NessieApiV2;
import org.projectnessie.model.CommitMeta;
import org.projectnessie.model.Content;
import org.projectnessie.model.LogResponse;
import org.projectnessie.tools.contentgenerator.RunContentGenerator.ProcessResult;

class ITGenerateContent extends AbstractContentGeneratorTest {
Expand All @@ -47,6 +50,8 @@ void basicGenerateContentTest(Content.Type contentType) throws Exception {
ProcessResult proc =
runGeneratorCmd(
"generate",
"--author",
"Test Author 123",
"-n",
Integer.toString(numCommits),
"-u",
Expand All @@ -57,7 +62,12 @@ void basicGenerateContentTest(Content.Type contentType) throws Exception {

assertThat(proc).extracting(ProcessResult::getExitCode).isEqualTo(0);
// 1 commit to create the namespaces
assertThat(api.getCommitLog().refName(testCaseBranch).stream()).hasSize(numCommits + 1);
assertThat(api.getCommitLog().refName(testCaseBranch).stream())
.hasSize(numCommits + 1)
.first(type(LogResponse.LogEntry.class))
.extracting(LogResponse.LogEntry::getCommitMeta)
.extracting(CommitMeta::getAuthor)
.isEqualTo("Test Author 123");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ void refreshOneKey() throws NessieNotFoundException, NessieConflictException {

assertThat(
runMain(
"--author",
"Test Author 123",
"--key",
key1.getElements().get(0),
"--key",
Expand All @@ -128,8 +130,9 @@ void refreshOneKey() throws NessieNotFoundException, NessieConflictException {
.first()
.extracting(
logEntry -> logEntry.getCommitMeta().getMessage(),
logEntry -> logEntry.getCommitMeta().getAuthor(),
logEntry -> Objects.requireNonNull(logEntry.getOperations()).get(0).getKey())
.containsExactly("Refresh 1 key(s)", key1);
.containsExactly("Refresh 1 key(s)", "Test Author 123", key1);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2023 Dremio
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.projectnessie.tools.contentgenerator.cli;

import org.projectnessie.model.CommitMeta;
import org.projectnessie.model.ImmutableCommitMeta;
import picocli.CommandLine;

public abstract class CommittingCommand extends AbstractCommand {

@CommandLine.Option(
names = {"--author"},
defaultValue = "${USER}",
description =
"Commit author for changes made by this command (optional, defaults to the USER env. var.)")
private String author;

protected CommitMeta commitMetaFromMessage(String message) {
ImmutableCommitMeta.Builder builder = CommitMeta.builder().message(message);

if (author != null && !author.isEmpty()) {
builder.author(author);
}

return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.projectnessie.error.NessieConflictException;
import org.projectnessie.error.NessieNotFoundException;
import org.projectnessie.model.Branch;
import org.projectnessie.model.CommitMeta;
import org.projectnessie.model.ContentKey;
import org.projectnessie.model.Namespace;
import org.projectnessie.model.Operation.Put;
Expand All @@ -49,7 +48,7 @@
name = "create-missing-namespaces",
mixinStandardHelpOptions = true,
description = "Creates missing namespaces for content keys at branch HEADs.")
public class CreateMissingNamespaces extends AbstractCommand {
public class CreateMissingNamespaces extends CommittingCommand {

@Option(
names = {"-r", "--branch"},
Expand Down Expand Up @@ -194,16 +193,15 @@ static Stream<Branch> branchesStream(NessieApiV2 api, Predicate<String> includeB
}

@VisibleForTesting
static void commitCreateNamespaces(
NessieApiV2 api, Branch branch, List<ContentKey> missingNamespaces)
void commitCreateNamespaces(NessieApiV2 api, Branch branch, List<ContentKey> missingNamespaces)
throws NessieNotFoundException, NessieConflictException {
CommitMultipleOperationsBuilder commit = api.commitMultipleOperations().branch(branch);
for (ContentKey nsKey : missingNamespaces) {
commit.operation(Put.of(nsKey, Namespace.of(nsKey)));
}
commit
.commitMeta(
CommitMeta.fromMessage(
commitMetaFromMessage(
missingNamespaces.stream()
.map(ContentKey::toString)
.collect(Collectors.joining(", ", "Create namespaces ", ""))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.projectnessie.error.NessieConflictException;
import org.projectnessie.error.NessieNotFoundException;
import org.projectnessie.model.Branch;
import org.projectnessie.model.CommitMeta;
import org.projectnessie.model.ContentKey;
import org.projectnessie.model.Operation;
import org.projectnessie.model.Reference;
Expand All @@ -29,7 +28,7 @@

/** Deletes content objects. */
@Command(name = "delete", mixinStandardHelpOptions = true, description = "Delete content objects")
public class DeleteContent extends AbstractCommand {
public class DeleteContent extends CommittingCommand {

@Option(
names = {"-r", "--branch"},
Expand Down Expand Up @@ -60,7 +59,7 @@ public void execute() throws NessieNotFoundException, NessieConflictException {

Branch head =
api.commitMultipleOperations()
.commitMeta(CommitMeta.fromMessage(message))
.commitMeta(commitMetaFromMessage(message))
.branch((Branch) refInfo)
.operation(Operation.Delete.of(contentKey))
.commit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static org.projectnessie.tools.contentgenerator.keygen.KeyGenerator.newKeyGenerator;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
Expand All @@ -43,7 +42,6 @@
import org.projectnessie.error.NessieConflictException;
import org.projectnessie.error.NessieReferenceNotFoundException;
import org.projectnessie.model.Branch;
import org.projectnessie.model.CommitMeta;
import org.projectnessie.model.Content;
import org.projectnessie.model.ContentKey;
import org.projectnessie.model.GetMultipleContentsResponse;
Expand All @@ -61,7 +59,7 @@
import picocli.CommandLine.ParameterException;

@Command(name = "generate", mixinStandardHelpOptions = true, description = "Generate commits")
public class GenerateContent extends AbstractCommand {
public class GenerateContent extends CommittingCommand {

@Option(
names = {"-b", "--num-branches"},
Expand Down Expand Up @@ -213,13 +211,9 @@ public void execute() throws BaseNessieClientServerException {
api.commitMultipleOperations()
.branch(commitToBranch)
.commitMeta(
CommitMeta.builder()
.message(
String.format(
"Commit #%d of %d on %s", commitNum, numCommits, branchName))
.author(System.getProperty("user.name"))
.authorTime(Instant.now())
.build());
commitMetaFromMessage(
String.format(
"Commit #%d of %d on %s", commitNum, numCommits, branchName)));

// Collect the namespaces that we do not (yet) know whether those exist.
Set<ContentKey> namespacesToCheck = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.projectnessie.client.api.NessieApiV2;
import org.projectnessie.error.BaseNessieClientServerException;
import org.projectnessie.model.Branch;
import org.projectnessie.model.CommitMeta;
import org.projectnessie.model.Content;
import org.projectnessie.model.ContentKey;
import org.projectnessie.model.Operation;
Expand All @@ -44,7 +43,7 @@
name = "content-refresh",
mixinStandardHelpOptions = true,
description = "Get and Put content objects without changes to refresh their storage model")
public class RefreshContent extends AbstractCommand {
public class RefreshContent extends CommittingCommand {

@Option(
names = {"--input"},
Expand Down Expand Up @@ -188,7 +187,7 @@ private void commitSameContent(
String msg = message == null ? ("Refresh " + contentMap.size() + " key(s)") : message;

CommitMultipleOperationsBuilder request =
api.commitMultipleOperations().branch(branch).commitMeta(CommitMeta.fromMessage(msg));
api.commitMultipleOperations().branch(branch).commitMeta(commitMetaFromMessage(msg));

for (Map.Entry<ContentKey, Content> entry : contentMap.entrySet()) {
Content content = entry.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.tuple;
import static org.assertj.core.api.InstanceOfAssertFactories.type;
import static org.projectnessie.jaxrs.ext.NessieJaxRsExtension.jaxRsExtension;
import static org.projectnessie.model.CommitMeta.fromMessage;
import static org.projectnessie.model.Content.Type.NAMESPACE;
import static org.projectnessie.tools.contentgenerator.RunContentGenerator.runGeneratorCmd;
import static org.projectnessie.tools.contentgenerator.cli.CreateMissingNamespaces.branchesStream;
import static org.projectnessie.tools.contentgenerator.cli.CreateMissingNamespaces.collectMissingNamespaceKeys;
import static org.projectnessie.tools.contentgenerator.cli.CreateMissingNamespaces.commitCreateNamespaces;

import java.net.URI;
import org.assertj.core.api.SoftAssertions;
Expand All @@ -40,9 +40,11 @@
import org.projectnessie.client.ext.NessieClientUri;
import org.projectnessie.jaxrs.ext.NessieJaxRsExtension;
import org.projectnessie.model.Branch;
import org.projectnessie.model.CommitMeta;
import org.projectnessie.model.ContentKey;
import org.projectnessie.model.EntriesResponse;
import org.projectnessie.model.IcebergTable;
import org.projectnessie.model.LogResponse;
import org.projectnessie.model.Namespace;
import org.projectnessie.model.Operation.Put;
import org.projectnessie.model.Reference;
Expand All @@ -67,6 +69,7 @@ public class TestCreateMissingNamespaces {

private NessieApiV2 nessieApi;
private URI uri;
private final CreateMissingNamespaces cmd = new CreateMissingNamespaces();

@BeforeEach
public void setUp(NessieClientFactory clientFactory, @NessieClientUri URI uri) {
Expand All @@ -84,7 +87,13 @@ public void roundtrip() throws Exception {
prepareRoundtrip();

ProcessResult result =
runGeneratorCmd("create-missing-namespaces", "--verbose", "--uri", uri.toString());
runGeneratorCmd(
"create-missing-namespaces",
"--author",
"Author 123",
"--verbose",
"--uri",
uri.toString());

soft.assertThat(result)
.extracting(
Expand Down Expand Up @@ -112,6 +121,13 @@ public void roundtrip() throws Exception {
" all namespaces present.",
"Successfully processed 4 branches, created 5 namespaces."),
singletonList(""));

soft.assertThat(nessieApi.getCommitLog().refName("branch1").stream())
.isNotEmpty()
.first(type(LogResponse.LogEntry.class))
.extracting(LogResponse.LogEntry::getCommitMeta)
.extracting(CommitMeta::getAuthor)
.isEqualTo("Author 123");
}

@Test
Expand Down Expand Up @@ -312,7 +328,7 @@ public void testCollectMissingNamespaceKeys() throws Exception {
public void testCommitCreateNamespaces() throws Exception {
Branch defaultBranch = nessieApi.getDefaultBranch();

commitCreateNamespaces(
cmd.commitCreateNamespaces(
nessieApi,
defaultBranch,
asList(
Expand Down