Skip to content

Commit

Permalink
Add --author option to content-generator commands
Browse files Browse the repository at this point in the history
* Applies to all commands that perform commits.

* Defaults to the `USER` env. var. (which was already the case in the
  `generate` command).
  • Loading branch information
dimas-b committed Jul 13, 2023
1 parent 8657c62 commit 8ef2348
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 24 deletions.
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,37 @@
/*
* 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 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 withStandardCommitMetaFields(CommitMeta meta) {
if (meta.getAuthor() != null || author == null || author.isEmpty()) {
return meta;
}

return CommitMeta.builder().from(meta).author(author).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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,19 +194,19 @@ 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(
missingNamespaces.stream()
.map(ContentKey::toString)
.collect(Collectors.joining(", ", "Create namespaces ", ""))))
withStandardCommitMetaFields(
CommitMeta.fromMessage(
missingNamespaces.stream()
.map(ContentKey::toString)
.collect(Collectors.joining(", ", "Create namespaces ", "")))))
.commit();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,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 +60,7 @@ public void execute() throws NessieNotFoundException, NessieConflictException {

Branch head =
api.commitMultipleOperations()
.commitMeta(CommitMeta.fromMessage(message))
.commitMeta(withStandardCommitMetaFields(CommitMeta.fromMessage(message)))
.branch((Branch) refInfo)
.operation(Operation.Delete.of(contentKey))
.commit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,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 +213,13 @@ 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());
withStandardCommitMetaFields(
CommitMeta.builder()
.message(
String.format(
"Commit #%d of %d on %s", commitNum, numCommits, branchName))
.authorTime(Instant.now())
.build()));

// 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 @@ -44,7 +44,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 +188,9 @@ 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(withStandardCommitMetaFields(CommitMeta.fromMessage(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

0 comments on commit 8ef2348

Please sign in to comment.