Search in sources :

Example 1 with ProjectInfo

use of com.google.gerrit.extensions.common.ProjectInfo in project gerrit by GerritCodeReview.

the class ListProjects method display.

public SortedMap<String, ProjectInfo> display(@Nullable OutputStream displayOutputStream) throws BadRequestException, PermissionBackendException {
    if (groupUuid != null) {
        try {
            if (!groupControlFactory.controlFor(groupUuid).isVisible()) {
                return Collections.emptySortedMap();
            }
        } catch (NoSuchGroupException ex) {
            return Collections.emptySortedMap();
        }
    }
    PrintWriter stdout = null;
    if (displayOutputStream != null) {
        stdout = new PrintWriter(new BufferedWriter(new OutputStreamWriter(displayOutputStream, UTF_8)));
    }
    if (type == FilterType.PARENT_CANDIDATES) {
        // Historically, PARENT_CANDIDATES implied showDescription.
        showDescription = true;
    }
    int foundIndex = 0;
    int found = 0;
    TreeMap<String, ProjectInfo> output = new TreeMap<>();
    Map<String, String> hiddenNames = new HashMap<>();
    Map<Project.NameKey, Boolean> accessibleParents = new HashMap<>();
    PermissionBackend.WithUser perm = permissionBackend.user(currentUser);
    final TreeMap<Project.NameKey, ProjectNode> treeMap = new TreeMap<>();
    try {
        for (final Project.NameKey projectName : filter(perm)) {
            final ProjectState e = projectCache.get(projectName);
            if (e == null || (!all && e.getProject().getState() == HIDDEN)) {
                // If all wasn't selected, and its HIDDEN, pretend its not present.
                continue;
            }
            final ProjectControl pctl = e.controlFor(currentUser);
            if (groupUuid != null && !pctl.getLocalGroups().contains(GroupReference.forGroup(groupsCollection.parseId(groupUuid.get())))) {
                continue;
            }
            ProjectInfo info = new ProjectInfo();
            if (showTree && !format.isJson()) {
                treeMap.put(projectName, projectNodeFactory.create(pctl.getProject(), true));
                continue;
            }
            info.name = projectName.get();
            if (showTree && format.isJson()) {
                ProjectState parent = Iterables.getFirst(e.parents(), null);
                if (parent != null) {
                    if (isParentAccessible(accessibleParents, perm, parent)) {
                        info.parent = parent.getProject().getName();
                    } else {
                        info.parent = hiddenNames.get(parent.getProject().getName());
                        if (info.parent == null) {
                            info.parent = "?-" + (hiddenNames.size() + 1);
                            hiddenNames.put(parent.getProject().getName(), info.parent);
                        }
                    }
                }
            }
            if (showDescription) {
                info.description = Strings.emptyToNull(e.getProject().getDescription());
            }
            info.state = e.getProject().getState();
            try {
                if (!showBranch.isEmpty()) {
                    try (Repository git = repoManager.openRepository(projectName)) {
                        if (!type.matches(git)) {
                            continue;
                        }
                        List<Ref> refs = getBranchRefs(projectName, pctl);
                        if (!hasValidRef(refs)) {
                            continue;
                        }
                        for (int i = 0; i < showBranch.size(); i++) {
                            Ref ref = refs.get(i);
                            if (ref != null && ref.getObjectId() != null) {
                                if (info.branches == null) {
                                    info.branches = new LinkedHashMap<>();
                                }
                                info.branches.put(showBranch.get(i), ref.getObjectId().name());
                            }
                        }
                    }
                } else if (!showTree && type.useMatch()) {
                    try (Repository git = repoManager.openRepository(projectName)) {
                        if (!type.matches(git)) {
                            continue;
                        }
                    }
                }
            } catch (RepositoryNotFoundException err) {
                // If the Git repository is gone, the project doesn't actually exist anymore.
                continue;
            } catch (IOException err) {
                log.warn("Unexpected error reading " + projectName, err);
                continue;
            }
            if (type != FilterType.PARENT_CANDIDATES) {
                List<WebLinkInfo> links = webLinks.getProjectLinks(projectName.get());
                info.webLinks = links.isEmpty() ? null : links;
            }
            if (foundIndex++ < start) {
                continue;
            }
            if (limit > 0 && ++found > limit) {
                break;
            }
            if (stdout == null || format.isJson()) {
                output.put(info.name, info);
                continue;
            }
            if (!showBranch.isEmpty()) {
                for (String name : showBranch) {
                    String ref = info.branches != null ? info.branches.get(name) : null;
                    if (ref == null) {
                        // Print stub (forty '-' symbols)
                        ref = "----------------------------------------";
                    }
                    stdout.print(ref);
                    stdout.print(' ');
                }
            }
            stdout.print(info.name);
            if (info.description != null) {
                // We still want to list every project as one-liners, hence escaping \n.
                stdout.print(" - " + StringUtil.escapeString(info.description));
            }
            stdout.print('\n');
        }
        for (ProjectInfo info : output.values()) {
            info.id = Url.encode(info.name);
            info.name = null;
        }
        if (stdout == null) {
            return output;
        } else if (format.isJson()) {
            format.newGson().toJson(output, new TypeToken<Map<String, ProjectInfo>>() {
            }.getType(), stdout);
            stdout.print('\n');
        } else if (showTree && treeMap.size() > 0) {
            printProjectTree(stdout, treeMap);
        }
        return null;
    } finally {
        if (stdout != null) {
            stdout.flush();
        }
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) NoSuchGroupException(com.google.gerrit.common.errors.NoSuchGroupException) BufferedWriter(java.io.BufferedWriter) ProjectInfo(com.google.gerrit.extensions.common.ProjectInfo) PrintWriter(java.io.PrintWriter) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) IOException(java.io.IOException) TreeMap(java.util.TreeMap) Project(com.google.gerrit.reviewdb.client.Project) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) WebLinkInfo(com.google.gerrit.extensions.common.WebLinkInfo) OutputStreamWriter(java.io.OutputStreamWriter) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 2 with ProjectInfo

use of com.google.gerrit.extensions.common.ProjectInfo in project gerrit by GerritCodeReview.

the class GetChildProjectIT method getChildProject.

@Test
public void getChildProject() throws Exception {
    Project.NameKey child = projectOperations.newProject().create();
    ProjectInfo childInfo = gApi.projects().name(allProjects.get()).child(child.get()).get();
    assertProjectInfo(projectCache.get(child).get().getProject(), childInfo);
}
Also used : Project(com.google.gerrit.entities.Project) ProjectAssert.assertProjectInfo(com.google.gerrit.acceptance.rest.project.ProjectAssert.assertProjectInfo) ProjectInfo(com.google.gerrit.extensions.common.ProjectInfo) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 3 with ProjectInfo

use of com.google.gerrit.extensions.common.ProjectInfo in project gerrit by GerritCodeReview.

the class GetChildProjectIT method getGrandChildProjectWithRecursiveFlag.

@Test
public void getGrandChildProjectWithRecursiveFlag() throws Exception {
    Project.NameKey child = projectOperations.newProject().create();
    Project.NameKey grandChild = projectOperations.newProject().parent(child).create();
    ProjectInfo grandChildInfo = gApi.projects().name(allProjects.get()).child(grandChild.get()).get(true);
    assertProjectInfo(projectCache.get(grandChild).get().getProject(), grandChildInfo);
}
Also used : Project(com.google.gerrit.entities.Project) ProjectAssert.assertProjectInfo(com.google.gerrit.acceptance.rest.project.ProjectAssert.assertProjectInfo) ProjectInfo(com.google.gerrit.extensions.common.ProjectInfo) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 4 with ProjectInfo

use of com.google.gerrit.extensions.common.ProjectInfo in project gerrit by GerritCodeReview.

the class ListProjectsIT method listProjectsWithBranch.

@Test
public void listProjectsWithBranch() throws Exception {
    Map<String, ProjectInfo> result = gApi.projects().list().addShowBranch("master").getAsMap();
    assertThat(result).containsKey(project.get());
    ProjectInfo info = result.get(project.get());
    assertThat(info.branches).isNotNull();
    assertThat(info.branches).hasSize(1);
    assertThat(info.branches.get("master")).isNotNull();
}
Also used : ProjectInfo(com.google.gerrit.extensions.common.ProjectInfo) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 5 with ProjectInfo

use of com.google.gerrit.extensions.common.ProjectInfo in project gerrit by GerritCodeReview.

the class CreateProjectIT method createProjectWithGitSuffix.

@Test
public void createProjectWithGitSuffix() throws Exception {
    String newProjectName = name("newProject");
    ProjectInfo p = gApi.projects().create(newProjectName + ".git").get();
    assertThat(p.name).isEqualTo(newProjectName);
    Optional<ProjectState> projectState = projectCache.get(Project.nameKey(newProjectName));
    assertThat(projectState).isPresent();
    assertProjectInfo(projectState.get().getProject(), p);
    assertHead(newProjectName, "refs/heads/master");
}
Also used : ProjectAssert.assertProjectInfo(com.google.gerrit.acceptance.rest.project.ProjectAssert.assertProjectInfo) ProjectInfo(com.google.gerrit.extensions.common.ProjectInfo) ProjectState(com.google.gerrit.server.project.ProjectState) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

ProjectInfo (com.google.gerrit.extensions.common.ProjectInfo)38 Test (org.junit.Test)25 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)15 ProjectAssert.assertProjectInfo (com.google.gerrit.acceptance.rest.project.ProjectAssert.assertProjectInfo)10 ProjectState (com.google.gerrit.server.project.ProjectState)10 Project (com.google.gerrit.entities.Project)8 ProjectInput (com.google.gerrit.extensions.api.projects.ProjectInput)5 ImmutableList (com.google.common.collect.ImmutableList)3 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)3 Project (com.google.gerrit.reviewdb.client.Project)3 ProjectResource (com.google.gerrit.server.project.ProjectResource)3 ArrayList (java.util.ArrayList)3 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 RestResponse (com.google.gerrit.acceptance.RestResponse)2 AccountGroup (com.google.gerrit.entities.AccountGroup)2 ConfigInput (com.google.gerrit.extensions.api.projects.ConfigInput)2 LabelTypeInfo (com.google.gerrit.extensions.common.LabelTypeInfo)2 WebLinkInfo (com.google.gerrit.extensions.common.WebLinkInfo)2 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)2 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)2