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

Extract CommitMetaUpdater class, fix "set authors on merge" #7039

Merged
merged 1 commit into from
Jun 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 @@ -16,7 +16,6 @@
package org.projectnessie.services.impl;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Collections.singletonList;
import static org.projectnessie.model.Validation.HASH_OR_RELATIVE_COMMIT_SPEC_MESSAGE;
import static org.projectnessie.model.Validation.HASH_OR_RELATIVE_COMMIT_SPEC_PATTERN;
import static org.projectnessie.services.cel.CELUtil.CONTAINER;
Expand All @@ -31,9 +30,7 @@
import com.google.common.collect.ImmutableMap;
import java.security.Principal;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
Expand All @@ -47,7 +44,6 @@
import org.projectnessie.error.NessieReferenceNotFoundException;
import org.projectnessie.model.CommitMeta;
import org.projectnessie.model.ContentKey;
import org.projectnessie.model.ImmutableCommitMeta;
import org.projectnessie.services.authz.Authorizer;
import org.projectnessie.services.authz.BatchAccessChecker;
import org.projectnessie.services.authz.ServerAccessContext;
Expand Down Expand Up @@ -205,80 +201,8 @@ protected ServerAccessContext createAccessContext() {
protected MetadataRewriter<CommitMeta> commitMetaUpdate(
@Nullable @jakarta.annotation.Nullable CommitMeta commitMeta,
IntFunction<String> squashMessage) {
return new MetadataRewriter<CommitMeta>() {
// Used for setting contextual commit properties during new and merge/transplant commits.
// WARNING: ONLY SET PROPERTIES, WHICH APPLY COMMONLY TO ALL COMMIT TYPES.
private final Principal principal = getPrincipal();
private final String committer = principal == null ? "" : principal.getName();
private final Instant now = Instant.now();

private CommitMeta buildCommitMeta(
ImmutableCommitMeta.Builder metadata, Supplier<String> defaultMessage) {

ImmutableCommitMeta pre = metadata.message("").build();

if (commitMeta != null && !commitMeta.getAllAuthors().isEmpty()) {
metadata.allAuthors(commitMeta.getAllAuthors());
} else if (pre.getAllAuthors().isEmpty()) {
metadata.allAuthors(singletonList(committer));
}

if (commitMeta != null && !commitMeta.getAllSignedOffBy().isEmpty()) {
metadata.allSignedOffBy(commitMeta.getAllSignedOffBy());
}

if (commitMeta != null && commitMeta.getAuthorTime() != null) {
metadata.authorTime(commitMeta.getAuthorTime());
} else if (pre.getAuthorTime() == null) {
metadata.authorTime(now);
}

if (commitMeta != null && !commitMeta.getAllProperties().isEmpty()) {
metadata.allProperties(commitMeta.getAllProperties());
}

if (commitMeta != null && !commitMeta.getMessage().isEmpty()) {
metadata.message(commitMeta.getMessage());
} else {
metadata.message(defaultMessage.get());
}

return metadata.committer(committer).commitTime(now).build();
}

@Override
public CommitMeta rewriteSingle(CommitMeta metadata) {
return buildCommitMeta(CommitMeta.builder().from(metadata), metadata::getMessage);
}

@Override
public CommitMeta squash(List<CommitMeta> metadata) {
Optional<String> msg = Optional.ofNullable(squashMessage.apply(metadata.size()));

if (metadata.size() == 1 && !msg.isPresent()) {
return rewriteSingle(metadata.get(0));
}

Map<String, String> newProperties = new HashMap<>();
for (CommitMeta commitMeta : metadata) {
newProperties.putAll(commitMeta.getProperties());
}

return buildCommitMeta(
CommitMeta.builder().properties(newProperties),
() ->
msg.orElseGet(
() -> {
StringBuilder newMessage = new StringBuilder();
for (CommitMeta commitMeta : metadata) {
if (newMessage.length() > 0) {
newMessage.append("\n---------------------------------------------\n");
}
newMessage.append(commitMeta.getMessage());
}
return newMessage.toString();
}));
}
};
Principal principal = getPrincipal();
String committer = principal == null ? "" : principal.getName();
return new CommitMetaUpdater(committer, Instant.now(), commitMeta, squashMessage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* 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.services.impl;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;

import java.time.Instant;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.IntFunction;
import java.util.function.Supplier;
import org.projectnessie.model.CommitMeta;
import org.projectnessie.model.ImmutableCommitMeta;
import org.projectnessie.versioned.MetadataRewriter;

public class CommitMetaUpdater implements MetadataRewriter<CommitMeta> {
private final String committer;
private final Instant now;
private final CommitMeta commitMetaOverride;
private final IntFunction<String> squashMessage;

public CommitMetaUpdater(
String committer,
Instant now,
CommitMeta commitMetaOverride,
IntFunction<String> squashMessage) {
this.committer = committer;
this.now = now;
this.commitMetaOverride = commitMetaOverride;
this.squashMessage = squashMessage;
}

private CommitMeta buildCommitMeta(
ImmutableCommitMeta.Builder metaBuilder, Supplier<String> defaultMessage) {

CommitMeta pre = metaBuilder.message("").build();

if (hasAuthors(commitMetaOverride)) {
metaBuilder.allAuthors(emptyList());
copyAuthors(commitMetaOverride, metaBuilder::addAllAuthors);
} else if (!hasAuthors(pre)) {
metaBuilder.allAuthors(singletonList(committer != null ? committer : ""));
}

if (commitMetaOverride != null && !commitMetaOverride.getAllSignedOffBy().isEmpty()) {
metaBuilder.allSignedOffBy(commitMetaOverride.getAllSignedOffBy());
}

if (commitMetaOverride != null && commitMetaOverride.getAuthorTime() != null) {
metaBuilder.authorTime(commitMetaOverride.getAuthorTime());
} else if (pre.getAuthorTime() == null) {
metaBuilder.authorTime(now);
}

if (commitMetaOverride != null && !commitMetaOverride.getAllProperties().isEmpty()) {
metaBuilder.allProperties(commitMetaOverride.getAllProperties());
}

if (commitMetaOverride != null && !commitMetaOverride.getMessage().isEmpty()) {
metaBuilder.message(commitMetaOverride.getMessage());
} else {
metaBuilder.message(defaultMessage.get());
}

if (committer != null) {
metaBuilder.committer(committer);
}

return metaBuilder.commitTime(now).build();
}

@Override
public CommitMeta rewriteSingle(CommitMeta metadata) {
return buildCommitMeta(CommitMeta.builder().from(metadata), metadata::getMessage);
}

@Override
public CommitMeta squash(List<CommitMeta> metadata) {
Optional<String> msg = Optional.ofNullable(squashMessage.apply(metadata.size()));

if (metadata.size() == 1 && !msg.isPresent()) {
return rewriteSingle(metadata.get(0));
}

Map<String, String> newProperties = new HashMap<>();
Set<String> authors = new LinkedHashSet<>();
Instant authorTime = null;
for (CommitMeta commitMeta : metadata) {
newProperties.putAll(commitMeta.getProperties());
copyAuthors(commitMeta, authors::add);

Instant metaAuthorTime = commitMeta.getAuthorTime();
if (authorTime == null
|| (metaAuthorTime != null && authorTime.compareTo(metaAuthorTime) < 0)) {
authorTime = commitMeta.getAuthorTime();
}
}

ImmutableCommitMeta.Builder newMetaBuilder =
CommitMeta.builder().properties(newProperties).allAuthors(authors).authorTime(authorTime);

return buildCommitMeta(
newMetaBuilder,
() ->
msg.orElseGet(
() -> {
StringBuilder newMessage = new StringBuilder();
for (CommitMeta commitMeta : metadata) {
if (newMessage.length() > 0) {
newMessage.append("\n---------------------------------------------\n");
}
newMessage.append(commitMeta.getMessage());
}
return newMessage.toString();
}));
}

private static void copyAuthors(CommitMeta commitMeta, Consumer<String> addAuthor) {
for (String author : commitMeta.getAllAuthors()) {
if (author != null && !author.isEmpty()) {
addAuthor.accept(author);
}
}
}

private static boolean hasAuthors(CommitMeta commitMeta) {
if (commitMeta != null) {
for (String author : commitMeta.getAllAuthors()) {
if (author != null && !author.isEmpty()) {
return true;
}
}
}
return false;
}
}