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

View File

@@ -14,22 +14,59 @@
package com.google.gerrit.server.change; 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.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.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton; import com.google.inject.Singleton;
@Singleton import org.eclipse.jgit.lib.Config;
public class GetRevisionActions implements RestReadView<RevisionResource> {
private final ActionJson delegate;
@Singleton
public class GetRevisionActions implements ETagView<RevisionResource> {
private final ActionJson delegate;
private final Provider<InternalChangeQuery> queryProvider;
private final Config config;
@Inject @Inject
GetRevisionActions(ActionJson delegate) { GetRevisionActions(
ActionJson delegate,
Provider<InternalChangeQuery> queryProvider,
@GerritServerConfig Config config) {
this.delegate = delegate; this.delegate = delegate;
this.queryProvider = queryProvider;
this.config = config;
} }
@Override @Override
public Object apply(RevisionResource rsrc) { public Object apply(RevisionResource rsrc) {
return Response.withMustRevalidate(delegate.format(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( this.titlePattern = new ParameterizedString(MoreObjects.firstNonNull(
cfg.getString("change", null, "submitTooltip"), cfg.getString("change", null, "submitTooltip"),
DEFAULT_TOOLTIP)); DEFAULT_TOOLTIP));
submitWholeTopic = cfg.getBoolean("change", null, "submitWholeTopic" , false); submitWholeTopic = wholeTopicEnabled(cfg);
this.submitTopicLabel = MoreObjects.firstNonNull( this.submitTopicLabel = MoreObjects.firstNonNull(
Strings.emptyToNull(cfg.getString("change", null, "submitTopicLabel")), Strings.emptyToNull(cfg.getString("change", null, "submitTopicLabel")),
"Submit whole topic"); "Submit whole topic");
@@ -654,6 +654,10 @@ public class Submit implements RestModifyView<RevisionResource, SubmitInput>,
return new RevisionResource(changes.parse(target), rsrc.getPatchSet()); 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 public static class CurrentRevision implements
RestModifyView<ChangeResource, SubmitInput> { RestModifyView<ChangeResource, SubmitInput> {
private final Provider<ReviewDb> dbProvider; private final Provider<ReviewDb> dbProvider;