Merge changes I860e2c4c,I0333f8cf,I524ee5fe

* changes:
  Add serializer for StoredCommentLinkInfo
  Add serializer for SubscribeSection
  Add serializer for ConfiguredMimeType
This commit is contained in:
Edwin Kempin
2020-07-20 06:24:50 +00:00
committed by Gerrit Code Review
15 changed files with 347 additions and 13 deletions

View File

@@ -16,6 +16,7 @@ java_library(
"//lib/auto:auto-value",
"//lib/auto:auto-value-annotations",
"//lib/errorprone:annotations",
"//lib/flogger:api",
"//proto:cache_java_proto",
"//proto:entities_java_proto",
],

View File

@@ -12,11 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.project;
package com.google.gerrit.entities;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.flogger.FluentLogger;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
@@ -34,7 +35,7 @@ public abstract class ConfiguredMimeTypes {
protected abstract ImmutableList<TypeMatcher> matchers();
static ConfiguredMimeTypes create(String projectName, Config rc) {
public static ConfiguredMimeTypes create(String projectName, Config rc) {
Set<String> types = rc.getSubsections(MIMETYPE);
ImmutableList.Builder<TypeMatcher> matchers = ImmutableList.builder();
if (!types.isEmpty()) {
@@ -67,21 +68,31 @@ public abstract class ConfiguredMimeTypes {
return null;
}
protected abstract static class TypeMatcher {
public abstract static class TypeMatcher {
private final String type;
private final String pattern;
private TypeMatcher(String type) {
private TypeMatcher(String type, String pattern) {
this.type = type;
this.pattern = pattern;
}
public String getPattern() {
return pattern;
}
public String getType() {
return type;
}
protected abstract boolean matches(String path);
}
protected static class FnType extends TypeMatcher {
public static class FnType extends TypeMatcher {
private final FileNameMatcher matcher;
private FnType(String type, String pattern) throws InvalidPatternException {
super(type);
public FnType(String type, String pattern) throws InvalidPatternException {
super(type, pattern);
this.matcher = new FileNameMatcher(pattern, null);
}
@@ -91,13 +102,28 @@ public abstract class ConfiguredMimeTypes {
m.append(input);
return m.isMatch();
}
@Override
public boolean equals(Object o) {
if (!(o instanceof FnType)) {
return false;
}
FnType other = (FnType) o;
return Objects.equals(other.getType(), getType())
&& Objects.equals(other.getPattern(), getPattern());
}
@Override
public int hashCode() {
return Objects.hash(getType(), getPattern());
}
}
protected static class ReType extends TypeMatcher {
public static class ReType extends TypeMatcher {
private final Pattern re;
private ReType(String type, String pattern) throws PatternSyntaxException {
super(type);
public ReType(String type, String pattern) throws PatternSyntaxException {
super(type, pattern);
this.re = Pattern.compile(pattern);
}
@@ -105,5 +131,20 @@ public abstract class ConfiguredMimeTypes {
protected boolean matches(String input) {
return re.matcher(input).matches();
}
@Override
public boolean equals(Object o) {
if (!(o instanceof ReType)) {
return false;
}
ReType other = (ReType) o;
return Objects.equals(other.getType(), getType())
&& Objects.equals(other.getPattern(), getPattern());
}
@Override
public int hashCode() {
return Objects.hash(getType(), getPattern());
}
}
}

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.project;
package com.google.gerrit.entities;
import static com.google.common.base.Preconditions.checkArgument;
@@ -68,7 +68,7 @@ public abstract class StoredCommentLinkInfo {
}
/** Creates and returns a new {@link StoredCommentLinkInfo} instance with the same values. */
static StoredCommentLinkInfo fromInfo(CommentLinkInfo src, boolean enabled) {
public static StoredCommentLinkInfo fromInfo(CommentLinkInfo src, boolean enabled) {
return builder(src.name)
.setMatch(src.match)
.setLink(src.link)
@@ -79,7 +79,7 @@ public abstract class StoredCommentLinkInfo {
}
/** Returns an {@link CommentLinkInfo} instance with the same values. */
CommentLinkInfo toInfo() {
public CommentLinkInfo toInfo() {
CommentLinkInfo info = new CommentLinkInfo();
info.name = getName();
info.match = getMatch();

View File

@@ -0,0 +1,40 @@
// Copyright (C) 2020 The Android Open Source Project
//
// 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 com.google.gerrit.server.cache.serialize.entities;
import com.google.gerrit.entities.ConfiguredMimeTypes;
import com.google.gerrit.server.cache.proto.Cache;
import java.util.regex.PatternSyntaxException;
import org.eclipse.jgit.errors.InvalidPatternException;
public class ConfiguredMimeTypeSerializer {
public static ConfiguredMimeTypes.TypeMatcher deserialize(Cache.ConfiguredMimeTypeProto proto) {
try {
return proto.getIsRegularExpression()
? new ConfiguredMimeTypes.ReType(proto.getType(), proto.getPattern())
: new ConfiguredMimeTypes.FnType(proto.getType(), proto.getPattern());
} catch (PatternSyntaxException | InvalidPatternException e) {
throw new IllegalStateException(e);
}
}
public static Cache.ConfiguredMimeTypeProto serialize(ConfiguredMimeTypes.TypeMatcher value) {
return Cache.ConfiguredMimeTypeProto.newBuilder()
.setType(value.getType())
.setPattern(value.getPattern())
.setIsRegularExpression(value instanceof ConfiguredMimeTypes.ReType)
.build();
}
}

View File

@@ -0,0 +1,47 @@
// Copyright (C) 2020 The Android Open Source Project
//
// 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 com.google.gerrit.server.cache.serialize.entities;
import static com.google.common.base.Strings.emptyToNull;
import static com.google.common.base.Strings.nullToEmpty;
import com.google.gerrit.entities.StoredCommentLinkInfo;
import com.google.gerrit.server.cache.proto.Cache;
/** Helper to (de)serialize values for caches. */
public class StoredCommentLinkInfoSerializer {
public static StoredCommentLinkInfo deserialize(Cache.StoredCommentLinkInfoProto proto) {
return StoredCommentLinkInfo.builder(proto.getName())
.setMatch(emptyToNull(proto.getMatch()))
.setLink(emptyToNull(proto.getLink()))
.setHtml(emptyToNull(proto.getHtml()))
.setEnabled(proto.getEnabled())
.setOverrideOnly(proto.getOverrideOnly())
.build();
}
public static Cache.StoredCommentLinkInfoProto serialize(StoredCommentLinkInfo autoValue) {
return Cache.StoredCommentLinkInfoProto.newBuilder()
.setName(autoValue.getName())
.setMatch(nullToEmpty(autoValue.getMatch()))
.setLink(nullToEmpty(autoValue.getLink()))
.setHtml(nullToEmpty(autoValue.getHtml()))
.setEnabled(autoValue.getEnabled())
.setOverrideOnly(autoValue.getOverrideOnly())
.build();
}
private StoredCommentLinkInfoSerializer() {}
}

View File

@@ -0,0 +1,40 @@
// Copyright (C) 2020 The Android Open Source Project
//
// 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 com.google.gerrit.server.cache.serialize.entities;
import com.google.gerrit.common.data.SubscribeSection;
import com.google.gerrit.entities.Project;
import com.google.gerrit.server.cache.proto.Cache;
/** Helper to (de)serialize values for caches. */
public class SubscribeSectionSerializer {
public static SubscribeSection deserialize(Cache.SubscribeSectionProto proto) {
SubscribeSection.Builder builder =
SubscribeSection.builder(Project.nameKey(proto.getProjectName()));
proto.getMatchingRefSpecsList().forEach(rs -> builder.addMatchingRefSpec(rs));
proto.getMultiMatchRefSpecsList().forEach(rs -> builder.addMultiMatchRefSpec(rs));
return builder.build();
}
public static Cache.SubscribeSectionProto serialize(SubscribeSection autoValue) {
Cache.SubscribeSectionProto.Builder builder =
Cache.SubscribeSectionProto.newBuilder().setProjectName(autoValue.project().get());
autoValue.multiMatchRefSpecsAsString().forEach(rs -> builder.addMultiMatchRefSpecs(rs));
autoValue.matchingRefSpecsAsString().forEach(rs -> builder.addMatchingRefSpecs(rs));
return builder.build();
}
private SubscribeSectionSerializer() {}
}

View File

@@ -25,9 +25,11 @@ import com.google.gerrit.common.data.SubscribeSection;
import com.google.gerrit.entities.AccountGroup;
import com.google.gerrit.entities.BranchNameKey;
import com.google.gerrit.entities.BranchOrderSection;
import com.google.gerrit.entities.ConfiguredMimeTypes;
import com.google.gerrit.entities.GroupReference;
import com.google.gerrit.entities.NotifyConfig;
import com.google.gerrit.entities.Project;
import com.google.gerrit.entities.StoredCommentLinkInfo;
import java.util.Collection;
import java.util.List;
import java.util.Map;

View File

@@ -18,6 +18,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.entities.StoredCommentLinkInfo;
import com.google.gerrit.extensions.api.projects.CommentLinkInfo;
import com.google.gerrit.server.config.ConfigUpdatedEvent;
import com.google.gerrit.server.config.ConfigUpdatedEvent.ConfigUpdateEntry;

View File

@@ -47,6 +47,7 @@ import com.google.gerrit.entities.AccountGroup;
import com.google.gerrit.entities.Address;
import com.google.gerrit.entities.BooleanProjectConfig;
import com.google.gerrit.entities.BranchOrderSection;
import com.google.gerrit.entities.ConfiguredMimeTypes;
import com.google.gerrit.entities.GroupDescription;
import com.google.gerrit.entities.GroupReference;
import com.google.gerrit.entities.LabelValue;
@@ -54,6 +55,7 @@ import com.google.gerrit.entities.NotifyConfig;
import com.google.gerrit.entities.NotifyConfig.NotifyType;
import com.google.gerrit.entities.Project;
import com.google.gerrit.entities.RefNames;
import com.google.gerrit.entities.StoredCommentLinkInfo;
import com.google.gerrit.exceptions.InvalidNameException;
import com.google.gerrit.extensions.client.InheritableBoolean;
import com.google.gerrit.extensions.client.ProjectState;

View File

@@ -33,6 +33,7 @@ import com.google.gerrit.entities.BranchNameKey;
import com.google.gerrit.entities.BranchOrderSection;
import com.google.gerrit.entities.GroupReference;
import com.google.gerrit.entities.Project;
import com.google.gerrit.entities.StoredCommentLinkInfo;
import com.google.gerrit.extensions.api.projects.CommentLinkInfo;
import com.google.gerrit.extensions.client.SubmitType;
import com.google.gerrit.extensions.restapi.ResourceConflictException;

View File

@@ -0,0 +1,36 @@
// Copyright (C) 2020 The Android Open Source Project
//
// 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 com.google.gerrit.server.cache.serialize.entities;
import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.server.cache.serialize.entities.ConfiguredMimeTypeSerializer.deserialize;
import static com.google.gerrit.server.cache.serialize.entities.ConfiguredMimeTypeSerializer.serialize;
import com.google.gerrit.entities.ConfiguredMimeTypes;
import org.junit.Test;
public class ConfiguredMimeTypeSerializerTest {
@Test
public void reType_roundTrip() {
ConfiguredMimeTypes.ReType value = new ConfiguredMimeTypes.ReType("type", "pattern");
assertThat(deserialize(serialize(value))).isEqualTo(value);
}
@Test
public void fnType_roundTrip() throws Exception {
ConfiguredMimeTypes.FnType value = new ConfiguredMimeTypes.FnType("type", "pattern");
assertThat(deserialize(serialize(value))).isEqualTo(value);
}
}

View File

@@ -0,0 +1,58 @@
// Copyright (C) 2020 The Android Open Source Project
//
// 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 com.google.gerrit.server.cache.serialize.entities;
import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.server.cache.serialize.entities.StoredCommentLinkInfoSerializer.deserialize;
import static com.google.gerrit.server.cache.serialize.entities.StoredCommentLinkInfoSerializer.serialize;
import com.google.gerrit.entities.StoredCommentLinkInfo;
import org.junit.Test;
public class StoredCommentLinkInfoSerializerTest {
@Test
public void htmlOnly_roundTrip() {
StoredCommentLinkInfo autoValue =
StoredCommentLinkInfo.builder("name")
.setEnabled(true)
.setHtml("<p>html")
.setMatch("*")
.build();
assertThat(deserialize(serialize(autoValue))).isEqualTo(autoValue);
}
@Test
public void linkOnly_roundTrip() {
StoredCommentLinkInfo autoValue =
StoredCommentLinkInfo.builder("name")
.setEnabled(true)
.setLink("<p>html")
.setMatch("*")
.build();
assertThat(deserialize(serialize(autoValue))).isEqualTo(autoValue);
}
@Test
public void overrideOnly_roundTrip() {
StoredCommentLinkInfo autoValue =
StoredCommentLinkInfo.builder("name")
.setEnabled(true)
.setOverrideOnly(true)
.setLink("<p>html")
.setMatch("*")
.build();
assertThat(deserialize(serialize(autoValue))).isEqualTo(autoValue);
}
}

View File

@@ -0,0 +1,37 @@
// Copyright (C) 2020 The Android Open Source Project
//
// 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 com.google.gerrit.server.cache.serialize.entities;
import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.server.cache.serialize.entities.SubscribeSectionSerializer.deserialize;
import static com.google.gerrit.server.cache.serialize.entities.SubscribeSectionSerializer.serialize;
import com.google.gerrit.common.data.SubscribeSection;
import com.google.gerrit.entities.Project;
import org.junit.Test;
public class SubscribeSectionSerializerTest {
@Test
public void roundTrip() {
SubscribeSection autoValue =
SubscribeSection.builder(Project.nameKey("project"))
.addMultiMatchRefSpec("multi")
.addMultiMatchRefSpec("multi2")
.addMatchingRefSpec("matching1")
.addMatchingRefSpec("matching2")
.build();
assertThat(deserialize(serialize(autoValue))).isEqualTo(autoValue);
}
}

View File

@@ -30,6 +30,7 @@ import com.google.gerrit.entities.BranchOrderSection;
import com.google.gerrit.entities.GroupReference;
import com.google.gerrit.entities.Project;
import com.google.gerrit.entities.RefNames;
import com.google.gerrit.entities.StoredCommentLinkInfo;
import com.google.gerrit.extensions.client.InheritableBoolean;
import com.google.gerrit.server.config.AllProjectsName;
import com.google.gerrit.server.config.PluginConfig;

View File

@@ -436,3 +436,30 @@ message LabelTypeProto {
bool can_override = 17;
repeated string ref_patterns = 18;
}
// Serialized form of com.google.gerrit.server.project.ConfiguredMimeTypes.
// Next ID: 4
message ConfiguredMimeTypeProto {
string type = 1;
string pattern = 2;
bool is_regular_expression = 3;
}
// Serialized form of com.google.gerrit.common.data.SubscribeSection.
// Next ID: 4
message SubscribeSectionProto {
string project_name = 1;
repeated string multi_match_ref_specs = 2;
repeated string matching_ref_specs = 3;
}
// Serialized form of com.google.gerrit.entities.StoredCommentLinkInfo.
// Next ID: 7
message StoredCommentLinkInfoProto {
string name = 1;
string match = 2;
string link = 3;
string html = 4;
bool enabled = 5;
bool override_only = 6;
}