Convert remaining uses of MetaDataUpdate to try-with-resource

These were not causing a "should be managed in try-with-resource"
warning from Eclipse, and were found by manual inspection.

Change-Id: I30c09f329c458117ece5878eeaba9c905eee06b2
This commit is contained in:
David Pursehouse
2015-12-18 14:26:45 +09:00
parent 55a35ca60a
commit eafb9bcd5a
7 changed files with 139 additions and 134 deletions

View File

@@ -519,20 +519,22 @@ public abstract class AbstractDaemonTest {
protected void setUseContributorAgreements(InheritableBoolean value) protected void setUseContributorAgreements(InheritableBoolean value)
throws Exception { throws Exception {
MetaDataUpdate md = metaDataUpdateFactory.create(project); try (MetaDataUpdate md = metaDataUpdateFactory.create(project)) {
ProjectConfig config = ProjectConfig.read(md); ProjectConfig config = ProjectConfig.read(md);
config.getProject().setUseContributorAgreements(value); config.getProject().setUseContributorAgreements(value);
config.commit(md); config.commit(md);
projectCache.evict(config.getProject()); projectCache.evict(config.getProject());
}
} }
protected void setUseSignedOffBy(InheritableBoolean value) protected void setUseSignedOffBy(InheritableBoolean value)
throws Exception { throws Exception {
MetaDataUpdate md = metaDataUpdateFactory.create(project); try (MetaDataUpdate md = metaDataUpdateFactory.create(project)) {
ProjectConfig config = ProjectConfig.read(md); ProjectConfig config = ProjectConfig.read(md);
config.getProject().setUseSignedOffBy(value); config.getProject().setUseSignedOffBy(value);
config.commit(md); config.commit(md);
projectCache.evict(config.getProject()); projectCache.evict(config.getProject());
}
} }
protected void deny(String permission, AccountGroup.UUID id, String ref) protected void deny(String permission, AccountGroup.UUID id, String ref)
@@ -566,17 +568,18 @@ public abstract class AbstractDaemonTest {
protected void grant(String permission, Project.NameKey project, String ref, protected void grant(String permission, Project.NameKey project, String ref,
boolean force) throws RepositoryNotFoundException, IOException, boolean force) throws RepositoryNotFoundException, IOException,
ConfigInvalidException { ConfigInvalidException {
MetaDataUpdate md = metaDataUpdateFactory.create(project); try (MetaDataUpdate md = metaDataUpdateFactory.create(project)) {
md.setMessage(String.format("Grant %s on %s", permission, ref)); md.setMessage(String.format("Grant %s on %s", permission, ref));
ProjectConfig config = ProjectConfig.read(md); ProjectConfig config = ProjectConfig.read(md);
AccessSection s = config.getAccessSection(ref, true); AccessSection s = config.getAccessSection(ref, true);
Permission p = s.getPermission(permission, true); Permission p = s.getPermission(permission, true);
AccountGroup adminGroup = groupCache.get(new AccountGroup.NameKey("Administrators")); AccountGroup adminGroup = groupCache.get(new AccountGroup.NameKey("Administrators"));
PermissionRule rule = new PermissionRule(config.resolve(adminGroup)); PermissionRule rule = new PermissionRule(config.resolve(adminGroup));
rule.setForce(force); rule.setForce(force);
p.add(rule); p.add(rule);
config.commit(md); config.commit(md);
projectCache.evict(config.getProject()); projectCache.evict(config.getProject());
}
} }
protected void blockRead(String ref) throws Exception { protected void blockRead(String ref) throws Exception {

View File

@@ -30,12 +30,9 @@ import com.google.gerrit.server.git.MetaDataUpdate;
import com.google.gerrit.server.git.ProjectConfig; import com.google.gerrit.server.git.ProjectConfig;
import com.google.gerrit.server.group.SystemGroupBackend; import com.google.gerrit.server.group.SystemGroupBackend;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.IOException;
public class ChangeOwnerIT extends AbstractDaemonTest { public class ChangeOwnerIT extends AbstractDaemonTest {
private TestAccount user2; private TestAccount user2;
@@ -80,20 +77,20 @@ public class ChangeOwnerIT extends AbstractDaemonTest {
approve(a, changeId); approve(a, changeId);
} }
private void grantApproveToChangeOwner() throws IOException, private void grantApproveToChangeOwner() throws Exception {
ConfigInvalidException { try (MetaDataUpdate md = metaDataUpdateFactory.create(project)) {
MetaDataUpdate md = metaDataUpdateFactory.create(project); md.setMessage(String.format("Grant approve to change owner"));
md.setMessage(String.format("Grant approve to change owner")); ProjectConfig config = ProjectConfig.read(md);
ProjectConfig config = ProjectConfig.read(md); AccessSection s = config.getAccessSection("refs/heads/*", true);
AccessSection s = config.getAccessSection("refs/heads/*", true); Permission p = s.getPermission(LABEL + "Code-Review", true);
Permission p = s.getPermission(LABEL + "Code-Review", true); PermissionRule rule = new PermissionRule(config
PermissionRule rule = new PermissionRule(config .resolve(SystemGroupBackend.getGroup(SystemGroupBackend.CHANGE_OWNER)));
.resolve(SystemGroupBackend.getGroup(SystemGroupBackend.CHANGE_OWNER))); rule.setMin(-2);
rule.setMin(-2); rule.setMax(+2);
rule.setMax(+2); p.add(rule);
p.add(rule); config.commit(md);
config.commit(md); projectCache.evict(config.getProject());
projectCache.evict(config.getProject()); }
} }
private String createMyChange() throws Exception { private String createMyChange() throws Exception {

View File

@@ -117,12 +117,13 @@ public abstract class AbstractChangeUpdate extends VersionedMetaData {
throws IOException { throws IOException {
if (migration.writeChanges()) { if (migration.writeChanges()) {
load(); load();
MetaDataUpdate md = try (MetaDataUpdate md =
updateFactory.create(getProjectName(), updateFactory.create(getProjectName(),
repoManager.openMetadataRepository(getProjectName()), getUser(), repoManager.openMetadataRepository(getProjectName()), getUser(),
bru); bru)) {
md.setAllowEmpty(true); md.setAllowEmpty(true);
return super.openUpdate(md); return super.openUpdate(md);
}
} }
return new BatchMetaDataUpdate() { return new BatchMetaDataUpdate() {
@Override @Override

View File

@@ -114,68 +114,69 @@ public class AllProjectsCreator {
private void initAllProjects(Repository git) private void initAllProjects(Repository git)
throws IOException, ConfigInvalidException { throws IOException, ConfigInvalidException {
MetaDataUpdate md = new MetaDataUpdate( try (MetaDataUpdate md = new MetaDataUpdate(
GitReferenceUpdated.DISABLED, GitReferenceUpdated.DISABLED,
allProjectsName, allProjectsName,
git); git)) {
md.getCommitBuilder().setAuthor(serverUser); md.getCommitBuilder().setAuthor(serverUser);
md.getCommitBuilder().setCommitter(serverUser); md.getCommitBuilder().setCommitter(serverUser);
md.setMessage(MoreObjects.firstNonNull( md.setMessage(MoreObjects.firstNonNull(
Strings.emptyToNull(message), Strings.emptyToNull(message),
"Initialized Gerrit Code Review " + Version.getVersion())); "Initialized Gerrit Code Review " + Version.getVersion()));
ProjectConfig config = ProjectConfig.read(md); ProjectConfig config = ProjectConfig.read(md);
Project p = config.getProject(); Project p = config.getProject();
p.setDescription("Access inherited by all other projects."); p.setDescription("Access inherited by all other projects.");
p.setRequireChangeID(InheritableBoolean.TRUE); p.setRequireChangeID(InheritableBoolean.TRUE);
p.setUseContentMerge(InheritableBoolean.TRUE); p.setUseContentMerge(InheritableBoolean.TRUE);
p.setUseContributorAgreements(InheritableBoolean.FALSE); p.setUseContributorAgreements(InheritableBoolean.FALSE);
p.setUseSignedOffBy(InheritableBoolean.FALSE); p.setUseSignedOffBy(InheritableBoolean.FALSE);
p.setEnableSignedPush(InheritableBoolean.FALSE); p.setEnableSignedPush(InheritableBoolean.FALSE);
AccessSection cap = config.getAccessSection(AccessSection.GLOBAL_CAPABILITIES, true); AccessSection cap = config.getAccessSection(AccessSection.GLOBAL_CAPABILITIES, true);
AccessSection all = config.getAccessSection(AccessSection.ALL, true); AccessSection all = config.getAccessSection(AccessSection.ALL, true);
AccessSection heads = config.getAccessSection(AccessSection.HEADS, true); AccessSection heads = config.getAccessSection(AccessSection.HEADS, true);
AccessSection tags = config.getAccessSection("refs/tags/*", true); AccessSection tags = config.getAccessSection("refs/tags/*", true);
AccessSection meta = config.getAccessSection(RefNames.REFS_CONFIG, true); AccessSection meta = config.getAccessSection(RefNames.REFS_CONFIG, true);
AccessSection magic = config.getAccessSection("refs/for/" + AccessSection.ALL, true); AccessSection magic = config.getAccessSection("refs/for/" + AccessSection.ALL, true);
grant(config, cap, GlobalCapability.ADMINISTRATE_SERVER, admin); grant(config, cap, GlobalCapability.ADMINISTRATE_SERVER, admin);
grant(config, all, Permission.READ, admin, anonymous); grant(config, all, Permission.READ, admin, anonymous);
if (batch != null) { if (batch != null) {
Permission priority = cap.getPermission(GlobalCapability.PRIORITY, true); Permission priority = cap.getPermission(GlobalCapability.PRIORITY, true);
PermissionRule r = rule(config, batch); PermissionRule r = rule(config, batch);
r.setAction(Action.BATCH); r.setAction(Action.BATCH);
priority.add(r); priority.add(r);
Permission stream = cap.getPermission(GlobalCapability.STREAM_EVENTS, true); Permission stream = cap.getPermission(GlobalCapability.STREAM_EVENTS, true);
stream.add(rule(config, batch)); stream.add(rule(config, batch));
}
LabelType cr = initCodeReviewLabel(config);
grant(config, heads, cr, -1, 1, registered);
grant(config, heads, cr, -2, 2, admin, owners);
grant(config, heads, Permission.CREATE, admin, owners);
grant(config, heads, Permission.PUSH, admin, owners);
grant(config, heads, Permission.SUBMIT, admin, owners);
grant(config, heads, Permission.FORGE_AUTHOR, registered);
grant(config, heads, Permission.FORGE_COMMITTER, admin, owners);
grant(config, heads, Permission.EDIT_TOPIC_NAME, true, admin, owners);
grant(config, tags, Permission.PUSH_TAG, admin, owners);
grant(config, tags, Permission.PUSH_SIGNED_TAG, admin, owners);
grant(config, magic, Permission.PUSH, registered);
grant(config, magic, Permission.PUSH_MERGE, registered);
meta.getPermission(Permission.READ, true).setExclusiveGroup(true);
grant(config, meta, Permission.READ, admin, owners);
grant(config, meta, cr, -2, 2, admin, owners);
grant(config, meta, Permission.PUSH, admin, owners);
grant(config, meta, Permission.SUBMIT, admin, owners);
config.commitToNewRef(md, RefNames.REFS_CONFIG);
} }
LabelType cr = initCodeReviewLabel(config);
grant(config, heads, cr, -1, 1, registered);
grant(config, heads, cr, -2, 2, admin, owners);
grant(config, heads, Permission.CREATE, admin, owners);
grant(config, heads, Permission.PUSH, admin, owners);
grant(config, heads, Permission.SUBMIT, admin, owners);
grant(config, heads, Permission.FORGE_AUTHOR, registered);
grant(config, heads, Permission.FORGE_COMMITTER, admin, owners);
grant(config, heads, Permission.EDIT_TOPIC_NAME, true, admin, owners);
grant(config, tags, Permission.PUSH_TAG, admin, owners);
grant(config, tags, Permission.PUSH_SIGNED_TAG, admin, owners);
grant(config, magic, Permission.PUSH, registered);
grant(config, magic, Permission.PUSH_MERGE, registered);
meta.getPermission(Permission.READ, true).setExclusiveGroup(true);
grant(config, meta, Permission.READ, admin, owners);
grant(config, meta, cr, -2, 2, admin, owners);
grant(config, meta, Permission.PUSH, admin, owners);
grant(config, meta, Permission.SUBMIT, admin, owners);
config.commitToNewRef(md, RefNames.REFS_CONFIG);
} }
public static LabelType initCodeReviewLabel(ProjectConfig c) { public static LabelType initCodeReviewLabel(ProjectConfig c) {

View File

@@ -75,29 +75,30 @@ public class AllUsersCreator {
private void initAllUsers(Repository git) private void initAllUsers(Repository git)
throws IOException, ConfigInvalidException { throws IOException, ConfigInvalidException {
MetaDataUpdate md = new MetaDataUpdate( try (MetaDataUpdate md = new MetaDataUpdate(
GitReferenceUpdated.DISABLED, GitReferenceUpdated.DISABLED,
allUsersName, allUsersName,
git); git)) {
md.getCommitBuilder().setAuthor(serverUser); md.getCommitBuilder().setAuthor(serverUser);
md.getCommitBuilder().setCommitter(serverUser); md.getCommitBuilder().setCommitter(serverUser);
md.setMessage("Initialized Gerrit Code Review " + Version.getVersion()); md.setMessage("Initialized Gerrit Code Review " + Version.getVersion());
ProjectConfig config = ProjectConfig.read(md); ProjectConfig config = ProjectConfig.read(md);
Project project = config.getProject(); Project project = config.getProject();
project.setDescription("Individual user settings and preferences."); project.setDescription("Individual user settings and preferences.");
AccessSection all = config.getAccessSection(RefNames.REFS_USERS + "*", true); AccessSection all = config.getAccessSection(RefNames.REFS_USERS + "*", true);
all.getPermission(Permission.READ, true).setExclusiveGroup(true); all.getPermission(Permission.READ, true).setExclusiveGroup(true);
AccessSection defaults = config.getAccessSection(RefNames.REFS_USERS_DEFAULT, true); AccessSection defaults = config.getAccessSection(RefNames.REFS_USERS_DEFAULT, true);
defaults.getPermission(Permission.READ, true).setExclusiveGroup(true); defaults.getPermission(Permission.READ, true).setExclusiveGroup(true);
grant(config, defaults, Permission.READ, admin); grant(config, defaults, Permission.READ, admin);
defaults.getPermission(Permission.PUSH, true).setExclusiveGroup(true); defaults.getPermission(Permission.PUSH, true).setExclusiveGroup(true);
grant(config, defaults, Permission.PUSH, admin); grant(config, defaults, Permission.PUSH, admin);
defaults.getPermission(Permission.CREATE, true).setExclusiveGroup(true); defaults.getPermission(Permission.CREATE, true).setExclusiveGroup(true);
grant(config, defaults, Permission.CREATE, admin); grant(config, defaults, Permission.CREATE, admin);
config.commit(md); config.commit(md);
}
} }
} }

View File

@@ -196,10 +196,11 @@ public class LabelNormalizerTest {
} }
private void save(ProjectConfig pc) throws Exception { private void save(ProjectConfig pc) throws Exception {
MetaDataUpdate md = try(MetaDataUpdate md =
metaDataUpdateFactory.create(pc.getProject().getNameKey(), user); metaDataUpdateFactory.create(pc.getProject().getNameKey(), user)) {
pc.commit(md); pc.commit(md);
projectCache.evict(pc.getProject().getNameKey()); projectCache.evict(pc.getProject().getNameKey());
}
} }
private PatchSetApproval psa(Account.Id accountId, String label, int value) { private PatchSetApproval psa(Account.Id accountId, String label, int value) {

View File

@@ -249,17 +249,18 @@ public class ProjectConfigTest extends LocalDiskRepositoryTestCase {
private RevCommit commit(ProjectConfig cfg) throws IOException, private RevCommit commit(ProjectConfig cfg) throws IOException,
MissingObjectException, IncorrectObjectTypeException { MissingObjectException, IncorrectObjectTypeException {
MetaDataUpdate md = new MetaDataUpdate( try (MetaDataUpdate md = new MetaDataUpdate(
GitReferenceUpdated.DISABLED, GitReferenceUpdated.DISABLED,
cfg.getProject().getNameKey(), cfg.getProject().getNameKey(),
db); db)) {
util.tick(5); util.tick(5);
util.setAuthorAndCommitter(md.getCommitBuilder()); util.setAuthorAndCommitter(md.getCommitBuilder());
md.setMessage("Edit\n"); md.setMessage("Edit\n");
cfg.commit(md); cfg.commit(md);
Ref ref = db.getRef(RefNames.REFS_CONFIG); Ref ref = db.getRef(RefNames.REFS_CONFIG);
return util.getRevWalk().parseCommit(ref.getObjectId()); return util.getRevWalk().parseCommit(ref.getObjectId());
}
} }
private void update(RevCommit rev) throws Exception { private void update(RevCommit rev) throws Exception {