ETag computation needs to honor changes in other changes

If `submitWholetopic` is enabled a change may stay unchanged,
but a change in the same topic can influence submittability.

Change-Id: I5c958369e9deec9546bc691629f14266d6755721
This commit is contained in:
Stefan Beller
2015-02-17 10:10:38 -08:00
parent ea34214362
commit add7038b5e
3 changed files with 60 additions and 13 deletions

View File

@@ -57,17 +57,15 @@ public class ChangeResource implements RestResource, HasETag {
return getControl().getNotes();
}
@Override
public String getETag() {
CurrentUser user = control.getCurrentUser();
Hasher h = Hashing.md5().newHasher()
.putLong(getChange().getLastUpdatedOn().getTime())
// This includes all information relevant for ETag computation
// unrelated to the UI.
public void prepareETag(Hasher h, CurrentUser user) {
h.putLong(getChange().getLastUpdatedOn().getTime())
.putInt(getChange().getRowVersion())
.putBoolean(user.getStarredChanges().contains(getChange().getId()))
.putInt(user.isIdentifiedUser()
? ((IdentifiedUser) user).getAccountId().get()
: 0);
byte[] buf = new byte[20];
ObjectId noteId;
try {
@@ -82,6 +80,14 @@ public class ChangeResource implements RestResource, HasETag {
for (ProjectState p : control.getProjectControl().getProjectState().tree()) {
hashObjectId(h, p.getConfig().getRevision(), buf);
}
}
@Override
public String getETag() {
CurrentUser user = control.getCurrentUser();
Hasher h = Hashing.md5().newHasher()
.putBoolean(user.getStarredChanges().contains(getChange().getId()));
prepareETag(h, user);
return h.hash().toString();
}

View File

@@ -14,22 +14,59 @@
package com.google.gerrit.server.change;
import com.google.common.base.Strings;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
import com.google.gerrit.extensions.restapi.ETagView;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.query.change.ChangeData;
import com.google.gerrit.server.query.change.InternalChangeQuery;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.OrmRuntimeException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
@Singleton
public class GetRevisionActions implements RestReadView<RevisionResource> {
private final ActionJson delegate;
import org.eclipse.jgit.lib.Config;
@Singleton
public class GetRevisionActions implements ETagView<RevisionResource> {
private final ActionJson delegate;
private final Provider<InternalChangeQuery> queryProvider;
private final Config config;
@Inject
GetRevisionActions(ActionJson delegate) {
GetRevisionActions(
ActionJson delegate,
Provider<InternalChangeQuery> queryProvider,
@GerritServerConfig Config config) {
this.delegate = delegate;
this.queryProvider = queryProvider;
this.config = config;
}
@Override
public Object apply(RevisionResource rsrc) {
return Response.withMustRevalidate(delegate.format(rsrc));
}
@Override
public String getETag(RevisionResource rsrc) {
String topic = rsrc.getChange().getTopic();
if (!Submit.wholeTopicEnabled(config)
|| Strings.isNullOrEmpty(topic)) {
return rsrc.getETag();
}
Hasher h = Hashing.md5().newHasher();
CurrentUser user = rsrc.getControl().getCurrentUser();
try {
for (ChangeData c : queryProvider.get().byTopicOpen(topic)) {
new ChangeResource(c.changeControl()).prepareETag(h, user);
}
} catch (OrmException e){
throw new OrmRuntimeException(e);
}
return h.hash().toString();
}
}

View File

@@ -169,7 +169,7 @@ public class Submit implements RestModifyView<RevisionResource, SubmitInput>,
this.titlePattern = new ParameterizedString(MoreObjects.firstNonNull(
cfg.getString("change", null, "submitTooltip"),
DEFAULT_TOOLTIP));
submitWholeTopic = cfg.getBoolean("change", null, "submitWholeTopic" , false);
submitWholeTopic = wholeTopicEnabled(cfg);
this.submitTopicLabel = MoreObjects.firstNonNull(
Strings.emptyToNull(cfg.getString("change", null, "submitTopicLabel")),
"Submit whole topic");
@@ -654,6 +654,10 @@ public class Submit implements RestModifyView<RevisionResource, SubmitInput>,
return new RevisionResource(changes.parse(target), rsrc.getPatchSet());
}
static boolean wholeTopicEnabled(Config config) {
return config.getBoolean("change", null, "submitWholeTopic" , false);
}
public static class CurrentRevision implements
RestModifyView<ChangeResource, SubmitInput> {
private final Provider<ReviewDb> dbProvider;