Add #getName() and #getNameKey() to ProjectState

This commit adds #getName() and #getNameKey() to ProjectState and
migrates callers to spare calling #getProject() in many places.

Change-Id: I124d370b47723ff3fc7d5bc51a697890a16ab01c
This commit is contained in:
Patrick Hiesel 2017-09-07 16:07:34 +02:00
parent d46fb98646
commit 55326bd493
29 changed files with 61 additions and 63 deletions

View File

@ -184,7 +184,7 @@ public class ApprovalCopier {
ChangeKind kind =
changeKindCache.getChangeKind(
project.getProject().getNameKey(),
project.getNameKey(),
rw,
repoConfig,
ObjectId.fromString(priorPs.getRevision().get()),

View File

@ -135,7 +135,7 @@ public class ReviewerRecommender {
.getProvider()
.get()
.suggestReviewers(
projectState.getProject().getNameKey(),
projectState.getNameKey(),
changeNotes.getChangeId(),
query,
reviewerScores.keySet()));
@ -239,8 +239,7 @@ public class ReviewerRecommender {
List<Predicate<ChangeData>> predicates = new ArrayList<>();
for (Account.Id id : candidates) {
try {
Predicate<ChangeData> projectQuery =
changeQueryBuilder.project(projectState.getProject().getName());
Predicate<ChangeData> projectQuery = changeQueryBuilder.project(projectState.getName());
// Get all labels for this project and create a compound OR query to
// fetch all changes where users have applied one of these labels

View File

@ -76,7 +76,7 @@ public class CherryPickCommit
input.message = message.isEmpty() ? commit.getFullMessage() : message;
String destination = Strings.nullToEmpty(input.destination).trim();
input.parent = input.parent == null ? 1 : input.parent;
Project.NameKey projectName = rsrc.getProjectState().getProject().getNameKey();
Project.NameKey projectName = rsrc.getProjectState().getNameKey();
if (destination.isEmpty()) {
throw new BadRequestException("destination must be non-empty");
@ -99,7 +99,7 @@ public class CherryPickCommit
projectName,
commit,
input,
new Branch.NameKey(rsrc.getProjectState().getProject().getNameKey(), refName));
new Branch.NameKey(rsrc.getProjectState().getNameKey(), refName));
return json.noOptions().format(projectName, cherryPickedChangeId);
} catch (InvalidChangeOperationException e) {
throw new BadRequestException(e.getMessage());

View File

@ -315,6 +315,6 @@ public class FileContentUtil {
private Repository openRepository(ProjectState project)
throws RepositoryNotFoundException, IOException {
return repoManager.openRepository(project.getProject().getNameKey());
return repoManager.openRepository(project.getNameKey());
}
}

View File

@ -218,7 +218,7 @@ public class GetDiff implements RestReadView<FileResource> {
List<DiffWebLinkInfo> links =
webLinks.getDiffLinks(
state.getProject().getName(),
state.getName(),
resource.getPatchKey().getParentKey().getParentKey().get(),
basePatchSet != null ? basePatchSet.getId().get() : null,
revA,

View File

@ -95,7 +95,7 @@ public class MergeOpRepoManager implements AutoCloseable {
}
Project.NameKey getProjectName() {
return project.getProject().getNameKey();
return project.getNameKey();
}
public CodeReviewRevWalk getCodeReviewRevWalk() {

View File

@ -188,7 +188,7 @@ public class VisibleRefFilter extends AbstractAdvertiseRefsHook {
if (!deferredTags.isEmpty() && (!result.isEmpty() || filterTagsSeparately)) {
TagMatcher tags =
tagCache
.get(projectState.getProject().getNameKey())
.get(projectState.getNameKey())
.matcher(
tagCache,
git,
@ -268,7 +268,7 @@ public class VisibleRefFilter extends AbstractAdvertiseRefsHook {
}
private Map<Change.Id, Branch.NameKey> visibleChangesBySearch() {
Project.NameKey project = projectState.getProject().getNameKey();
Project.NameKey project = projectState.getNameKey();
try {
Map<Change.Id, Branch.NameKey> visibleChanges = new HashMap<>();
for (ChangeData cd : changeCache.getChangeData(db.get(), project)) {
@ -286,7 +286,7 @@ public class VisibleRefFilter extends AbstractAdvertiseRefsHook {
}
private Map<Change.Id, Branch.NameKey> visibleChangesByScan() {
Project.NameKey p = projectState.getProject().getNameKey();
Project.NameKey p = projectState.getNameKey();
Stream<ChangeNotesResult> s;
try {
s = changeNotesFactory.scan(git, db.get(), p);

View File

@ -526,7 +526,7 @@ abstract class SubmitStrategyOp implements BatchUpdateOp {
try (Repository git = args.repoManager.openRepository(getProject())) {
git.setGitwebDescription(p.getProject().getDescription());
} catch (IOException e) {
log.error("cannot update description of " + p.getProject().getName(), e);
log.error("cannot update description of " + p.getName(), e);
}
}
}

View File

@ -145,7 +145,7 @@ public class MergeValidators {
if (RefNames.REFS_CONFIG.equals(destBranch.get())) {
final Project.NameKey newParent;
try {
ProjectConfig cfg = new ProjectConfig(destProject.getProject().getNameKey());
ProjectConfig cfg = new ProjectConfig(destProject.getNameKey());
cfg.load(repo, commit);
newParent = cfg.getProject().getParent(allProjectsName);
final Project.NameKey oldParent = destProject.getProject().getParent(allProjectsName);
@ -256,7 +256,7 @@ public class MergeValidators {
IdentifiedUser caller)
throws MergeValidationException {
Account.Id accountId = Account.Id.fromRef(destBranch.get());
if (!allUsersName.equals(destProject.getProject().getNameKey()) || accountId == null) {
if (!allUsersName.equals(destProject.getNameKey()) || accountId == null) {
return;
}

View File

@ -258,9 +258,7 @@ public class CommentSender extends ReplyToChangeSender {
log.warn(
String.format(
"Cannot load %s from %s in %s",
c.key.filename,
patchList.getNewId().name(),
projectState.getProject().getName()),
c.key.filename, patchList.getNewId().name(), projectState.getName()),
e);
currentGroup.fileData = null;
}
@ -586,7 +584,7 @@ public class CommentSender extends ReplyToChangeSender {
private Repository getRepository() {
try {
return args.server.openRepository(projectState.getProject().getNameKey());
return args.server.openRepository(projectState.getNameKey());
} catch (IOException e) {
return null;
}

View File

@ -103,7 +103,7 @@ public class ProjectWatch {
} catch (QueryParseException e) {
log.warn(
"Project {} has invalid notify {} filter \"{}\": {}",
state.getProject().getName(),
state.getName(),
nc.getName(),
nc.getFilter(),
e.getMessage());

View File

@ -41,6 +41,6 @@ public class ChildProjectResource implements RestResource {
public boolean isDirectChild() {
ProjectState firstParent = Iterables.getFirst(child.parents(), null);
return firstParent != null && parent.getNameKey().equals(firstParent.getProject().getNameKey());
return firstParent != null && parent.getNameKey().equals(firstParent.getNameKey());
}
}

View File

@ -38,7 +38,7 @@ class CommitIncludedIn implements RestReadView<CommitResource> {
public IncludedInInfo apply(CommitResource rsrc)
throws RestApiException, OrmException, IOException {
RevCommit commit = rsrc.getCommit();
Project.NameKey project = rsrc.getProjectState().getProject().getNameKey();
Project.NameKey project = rsrc.getProjectState().getNameKey();
return includedIn.apply(project, commit.getId().getName());
}
}

View File

@ -101,7 +101,7 @@ public class CommitsCollection implements ChildCollection<ProjectResource, Commi
/** @return true if {@code commit} is visible to the caller. */
public boolean canRead(ProjectState state, Repository repo, RevCommit commit) {
Project.NameKey project = state.getProject().getNameKey();
Project.NameKey project = state.getNameKey();
// Look for changes associated with the commit.
try {
@ -126,7 +126,7 @@ public class CommitsCollection implements ChildCollection<ProjectResource, Commi
log.error(
String.format(
"Cannot verify permissions to commit object %s in repository %s",
commit.name(), state.getProject().getNameKey()),
commit.name(), state.getNameKey()),
e);
return false;
}

View File

@ -159,7 +159,7 @@ public class ConfigInfoImpl extends ConfigInfo {
p.type = configEntry.getType();
p.permittedValues = configEntry.getPermittedValues();
p.editable = configEntry.isEditable(project) ? true : null;
if (configEntry.isInheritable() && !allProjects.equals(project.getProject().getNameKey())) {
if (configEntry.isInheritable() && !allProjects.equals(project.getNameKey())) {
PluginConfig cfgWithInheritance =
cfgFactory.getFromProjectConfigWithInheritance(project, e.getPluginName());
p.inheritable = true;

View File

@ -34,7 +34,7 @@ public class FileResource implements RestResource {
public static FileResource create(
GitRepositoryManager repoManager, ProjectState projectState, ObjectId rev, String path)
throws ResourceNotFoundException, IOException {
try (Repository repo = repoManager.openRepository(projectState.getProject().getNameKey());
try (Repository repo = repoManager.openRepository(projectState.getNameKey());
RevWalk rw = new RevWalk(repo)) {
RevTree tree = rw.parseTree(rev);
if (TreeWalk.forPath(repo, path, tree) != null) {

View File

@ -39,6 +39,6 @@ public class GetChildProject implements RestReadView<ChildProjectResource> {
if (recursive || rsrc.isDirectChild()) {
return json.format(rsrc.getChild().getProject());
}
throw new ResourceNotFoundException(rsrc.getChild().getProject().getName());
throw new ResourceNotFoundException(rsrc.getChild().getName());
}
}

View File

@ -77,7 +77,7 @@ public class ListChildProjects implements RestReadView<ProjectResource> {
for (Project.NameKey name : projectCache.all()) {
ProjectState c = projectCache.get(name);
if (c != null && parent.equals(c.getProject().getParent(allProjects))) {
children.put(c.getProject().getNameKey(), c.getProject());
children.put(c.getNameKey(), c.getProject());
}
}
return permissionBackend
@ -105,7 +105,7 @@ public class ListChildProjects implements RestReadView<ProjectResource> {
for (Project.NameKey name : projectCache.all()) {
ProjectState c = projectCache.get(name);
if (c != null) {
projects.put(c.getProject().getNameKey(), c.getProject());
projects.put(c.getNameKey(), c.getProject());
}
}
return projects;

View File

@ -93,7 +93,7 @@ class ListDashboards implements RestReadView<ProjectResource> {
private Collection<ProjectState> tree(ProjectResource rsrc) throws PermissionBackendException {
Map<Project.NameKey, ProjectState> tree = new LinkedHashMap<>();
for (ProjectState ps : rsrc.getProjectState().tree()) {
tree.put(ps.getProject().getNameKey(), ps);
tree.put(ps.getNameKey(), ps);
}
tree.keySet()
.retainAll(permissionBackend.user(user).filter(ProjectPermission.ACCESS, tree.keySet()));
@ -102,10 +102,8 @@ class ListDashboards implements RestReadView<ProjectResource> {
private List<DashboardInfo> scan(ProjectState state, String project, boolean setDefault)
throws ResourceNotFoundException, IOException, PermissionBackendException {
Project.NameKey projectName = state.getProject().getNameKey();
PermissionBackend.ForProject perm =
permissionBackend.user(user).project(state.getProject().getNameKey());
try (Repository git = gitManager.openRepository(projectName);
PermissionBackend.ForProject perm = permissionBackend.user(user).project(state.getNameKey());
try (Repository git = gitManager.openRepository(state.getNameKey());
RevWalk rw = new RevWalk(git)) {
List<DashboardInfo> all = new ArrayList<>();
for (Ref ref : git.getRefDatabase().getRefs(REFS_DASHBOARDS).values()) {

View File

@ -373,12 +373,12 @@ public class ListProjects implements RestReadView<TopLevelResource> {
ProjectState parent = Iterables.getFirst(e.parents(), null);
if (parent != null) {
if (isParentAccessible(accessibleParents, perm, parent)) {
info.parent = parent.getProject().getName();
info.parent = parent.getName();
} else {
info.parent = hiddenNames.get(parent.getProject().getName());
info.parent = hiddenNames.get(parent.getName());
if (info.parent == null) {
info.parent = "?-" + (hiddenNames.size() + 1);
hiddenNames.put(parent.getProject().getName(), info.parent);
hiddenNames.put(parent.getName(), info.parent);
}
}
}
@ -506,8 +506,7 @@ public class ListProjects implements RestReadView<TopLevelResource> {
} else {
log.warn(
String.format(
"parent project %s of project %s not found",
parent.get(), ps.getProject().getName()));
"parent project %s of project %s not found", parent.get(), ps.getName()));
}
}
}
@ -518,7 +517,7 @@ public class ListProjects implements RestReadView<TopLevelResource> {
private boolean isParentAccessible(
Map<Project.NameKey, Boolean> checked, PermissionBackend.WithUser perm, ProjectState p)
throws PermissionBackendException {
Project.NameKey name = p.getProject().getNameKey();
Project.NameKey name = p.getNameKey();
Boolean b = checked.get(name);
if (b == null) {
try {

View File

@ -274,8 +274,7 @@ public class ProjectControl {
if (!canPerformOnAnyRef(Permission.PUSH)
&& !canPerformOnAnyRef(Permission.CREATE_TAG)
&& !isOwner()) {
String pName = state.getProject().getName();
return new Capable("Upload denied for project '" + pName + "'");
return new Capable("Upload denied for project '" + state.getName() + "'");
}
if (state.isUseContributorAgreements()) {
return verifyActiveContributorAgreement();

View File

@ -44,7 +44,7 @@ class ProjectHierarchyIterator implements Iterator<ProjectState> {
allProjectsName = all;
seen = Sets.newLinkedHashSet();
seen.add(firstResult.getProject().getNameKey());
seen.add(firstResult.getNameKey());
next = firstResult;
}

View File

@ -180,7 +180,7 @@ public class ProjectState {
}
private boolean isRevisionOutOfDate() {
try (Repository git = gitMgr.openRepository(getProject().getNameKey())) {
try (Repository git = gitMgr.openRepository(getNameKey())) {
Ref ref = git.getRefDatabase().exactRef(RefNames.REFS_CONFIG);
if (ref == null || ref.getObjectId() == null) {
return true;
@ -203,7 +203,7 @@ public class ProjectState {
public PrologEnvironment newPrologEnvironment() throws CompileException {
PrologMachineCopy pmc = rulesMachine;
if (pmc == null) {
pmc = rulesCache.loadMachine(getProject().getNameKey(), config.getRulesId());
pmc = rulesCache.loadMachine(getNameKey(), config.getRulesId());
rulesMachine = pmc;
}
return envFactory.create(pmc);
@ -226,6 +226,14 @@ public class ProjectState {
return config.getProject();
}
public Project.NameKey getNameKey() {
return getProject().getNameKey();
}
public String getName() {
return getNameKey().get();
}
public ProjectConfig getConfig() {
return config;
}
@ -236,10 +244,10 @@ public class ProjectState {
}
ProjectLevelConfig cfg = new ProjectLevelConfig(fileName, this);
try (Repository git = gitMgr.openRepository(getProject().getNameKey())) {
try (Repository git = gitMgr.openRepository(getNameKey())) {
cfg.load(git);
} catch (IOException | ConfigInvalidException e) {
log.warn("Failed to load " + fileName + " for " + getProject().getName(), e);
log.warn("Failed to load " + fileName + " for " + getName(), e);
}
configs.put(fileName, cfg);
@ -268,7 +276,7 @@ public class ProjectState {
section.setPermissions(copy);
}
SectionMatcher matcher = SectionMatcher.wrap(getProject().getNameKey(), section);
SectionMatcher matcher = SectionMatcher.wrap(getNameKey(), section);
if (matcher != null) {
sm.add(matcher);
}

View File

@ -105,7 +105,7 @@ public class PutConfig implements RestModifyView<ProjectResource, ConfigInput> {
public ConfigInfo apply(ProjectState projectState, ConfigInput input)
throws ResourceNotFoundException, BadRequestException, ResourceConflictException {
Project.NameKey projectName = projectState.getProject().getNameKey();
Project.NameKey projectName = projectState.getNameKey();
if (input == null) {
throw new BadRequestException("config is required");
}
@ -309,7 +309,7 @@ public class PutConfig implements RestModifyView<ProjectResource, ConfigInput> {
throw new BadRequestException(
String.format(
"Not allowed to set parameter '%s' of plugin '%s' on project '%s'.",
parameterName, pluginName, projectState.getProject().getName()));
parameterName, pluginName, projectState.getName()));
}
}

View File

@ -77,8 +77,7 @@ public class SetParent implements RestModifyView<ProjectResource, Input> {
IdentifiedUser user = rsrc.getUser().asIdentifiedUser();
String parentName =
MoreObjects.firstNonNull(Strings.emptyToNull(input.parent), allProjects.get());
validateParentUpdate(
rsrc.getProjectState().getProject().getNameKey(), user, parentName, checkIfAdmin);
validateParentUpdate(rsrc.getProjectState().getNameKey(), user, parentName, checkIfAdmin);
try (MetaDataUpdate md = updateFactory.create(rsrc.getNameKey())) {
ProjectConfig config = ProjectConfig.read(md);
Project project = config.getProject();
@ -128,11 +127,11 @@ public class SetParent implements RestModifyView<ProjectResource, Input> {
if (Iterables.tryFind(
parent.tree(),
p -> {
return p.getProject().getNameKey().equals(project);
return p.getNameKey().equals(project);
})
.isPresent()) {
throw new ResourceConflictException(
"cycle exists between " + project.get() + " and " + parent.getProject().getName());
"cycle exists between " + project.get() + " and " + parent.getName());
}
}
}

View File

@ -600,8 +600,7 @@ public class SubmitRuleEvaluator {
try {
parentEnv = parentState.newPrologEnvironment();
} catch (CompileException err) {
throw new RuleEvalException(
"Cannot consult rules.pl for " + parentState.getProject().getName(), err);
throw new RuleEvalException("Cannot consult rules.pl for " + parentState.getName(), err);
}
parentEnv.copyStoredValues(childEnv);
@ -619,12 +618,12 @@ public class SubmitRuleEvaluator {
throw new RuleEvalException(
String.format(
"%s on change %d of %s",
err.getMessage(), cd.getId().get(), parentState.getProject().getName()));
err.getMessage(), cd.getId().get(), parentState.getName()));
} catch (RuntimeException err) {
throw new RuleEvalException(
String.format(
"Exception calling %s on change %d of %s",
filterRule, cd.getId().get(), parentState.getProject().getName()),
filterRule, cd.getId().get(), parentState.getName()),
err);
} finally {
reductionsConsumed += env.getReductions();
@ -690,6 +689,6 @@ public class SubmitRuleEvaluator {
}
private String getProjectName() {
return control.getProjectControl().getProjectState().getProject().getName();
return control.getProjectControl().getProjectState().getName();
}
}

View File

@ -56,7 +56,7 @@ public class ParentProjectPredicate extends OrPredicate<ChangeData> {
}
List<Predicate<ChangeData>> r = new ArrayList<>();
r.add(new ProjectPredicate(projectState.getProject().getName()));
r.add(new ProjectPredicate(projectState.getName()));
try {
ProjectResource proj = new ProjectResource(projectState.controlFor(self.get()));
ListChildProjects children = listChildProjects.get();

View File

@ -69,8 +69,7 @@ public abstract class AbstractGitCommand extends BaseCommand {
@Override
public Project.NameKey getProjectName() {
Project project = projectControl.getProjectState().getProject();
return project.getNameKey();
return projectControl.getProjectState().getNameKey();
}
});
} finally {

View File

@ -226,6 +226,6 @@ final class AdminSetParent extends SshCommand {
if (ps == null) {
return Collections.emptySet();
}
return ps.parents().transform(s -> s.getProject().getNameKey()).toSet();
return ps.parents().transform(s -> s.getNameKey()).toSet();
}
}