summaryrefslogtreecommitdiffstats
path: root/gerrit-server/src/main/java/com/google/gerrit/server/project/ListChildProjects.java
blob: 5a6aa11b25e737289b421d9a28787431abc9e42d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (C) 2013 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.project;

import static java.util.stream.Collectors.toList;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.google.gerrit.extensions.common.ProjectInfo;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.config.AllProjectsName;
import com.google.gerrit.server.permissions.PermissionBackend;
import com.google.gerrit.server.permissions.PermissionBackendException;
import com.google.gerrit.server.permissions.ProjectPermission;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.kohsuke.args4j.Option;

public class ListChildProjects implements RestReadView<ProjectResource> {

  @Option(name = "--recursive", usage = "to list child projects recursively")
  private boolean recursive;

  private final ProjectCache projectCache;
  private final PermissionBackend permissionBackend;
  private final Provider<CurrentUser> user;
  private final AllProjectsName allProjects;
  private final ProjectJson json;

  @Inject
  ListChildProjects(
      ProjectCache projectCache,
      PermissionBackend permissionBackend,
      Provider<CurrentUser> user,
      AllProjectsName allProjectsName,
      ProjectJson json) {
    this.projectCache = projectCache;
    this.permissionBackend = permissionBackend;
    this.user = user;
    this.allProjects = allProjectsName;
    this.json = json;
  }

  public void setRecursive(boolean recursive) {
    this.recursive = recursive;
  }

  @Override
  public List<ProjectInfo> apply(ProjectResource rsrc) throws PermissionBackendException {
    if (recursive) {
      return recursiveChildProjects(rsrc.getNameKey());
    }
    return directChildProjects(rsrc.getNameKey());
  }

  private List<ProjectInfo> directChildProjects(Project.NameKey parent)
      throws PermissionBackendException {
    Map<Project.NameKey, Project> children = new HashMap<>();
    for (Project.NameKey name : projectCache.all()) {
      ProjectState c = projectCache.get(name);
      if (c != null && parent.equals(c.getProject().getParent(allProjects))) {
        children.put(c.getNameKey(), c.getProject());
      }
    }
    return permissionBackend.user(user).filter(ProjectPermission.ACCESS, children.keySet()).stream()
        .sorted()
        .map((p) -> json.format(children.get(p)))
        .collect(toList());
  }

  private List<ProjectInfo> recursiveChildProjects(Project.NameKey parent)
      throws PermissionBackendException {
    Map<Project.NameKey, Project> projects = readAllProjects();
    Multimap<Project.NameKey, Project.NameKey> children = parentToChildren(projects);
    PermissionBackend.WithUser perm = permissionBackend.user(user);

    List<ProjectInfo> results = new ArrayList<>();
    depthFirstFormat(results, perm, projects, children, parent);
    return results;
  }

  private Map<Project.NameKey, Project> readAllProjects() {
    Map<Project.NameKey, Project> projects = new HashMap<>();
    for (Project.NameKey name : projectCache.all()) {
      ProjectState c = projectCache.get(name);
      if (c != null) {
        projects.put(c.getNameKey(), c.getProject());
      }
    }
    return projects;
  }

  /** Map of parent project to direct child. */
  private Multimap<Project.NameKey, Project.NameKey> parentToChildren(
      Map<Project.NameKey, Project> projects) {
    Multimap<Project.NameKey, Project.NameKey> m = ArrayListMultimap.create();
    for (Map.Entry<Project.NameKey, Project> e : projects.entrySet()) {
      if (!allProjects.equals(e.getKey())) {
        m.put(e.getValue().getParent(allProjects), e.getKey());
      }
    }
    return m;
  }

  private void depthFirstFormat(
      List<ProjectInfo> results,
      PermissionBackend.WithUser perm,
      Map<Project.NameKey, Project> projects,
      Multimap<Project.NameKey, Project.NameKey> children,
      Project.NameKey parent)
      throws PermissionBackendException {
    List<Project.NameKey> canSee =
        perm.filter(ProjectPermission.ACCESS, children.get(parent)).stream()
            .sorted()
            .collect(toList());
    children.removeAll(parent); // removing all entries prevents cycles.

    for (Project.NameKey c : canSee) {
      results.add(json.format(projects.get(c)));
      depthFirstFormat(results, perm, projects, children, c);
    }
  }
}