Search in sources :

Example 41 with Project

use of com.google.gerrit.entities.Project in project gerrit by GerritCodeReview.

the class SetParentCommand method run.

@Override
protected void run() throws Failure {
    enableGracefulStop();
    if (oldParent == null && children.isEmpty()) {
        throw die("child projects have to be specified as " + "arguments or the --children-of option has to be set");
    }
    if (oldParent == null && !excludedChildren.isEmpty()) {
        throw die("--exclude can only be used together with --children-of");
    }
    final StringBuilder err = new StringBuilder();
    if (newParent != null) {
        newParentKey = newParent.getProject().getNameKey();
    }
    final List<Project.NameKey> childProjects = children.stream().map(ProjectState::getNameKey).collect(toList());
    if (oldParent != null) {
        try {
            childProjects.addAll(getChildrenForReparenting(oldParent));
        } catch (PermissionBackendException e) {
            throw new Failure(1, "permissions unavailable", e);
        } catch (Exception e) {
            throw new Failure(1, "failure in request", e);
        }
    }
    for (Project.NameKey nameKey : childProjects) {
        final String name = nameKey.get();
        ProjectState project = projectCache.get(nameKey).orElseThrow(illegalState(nameKey));
        try {
            setParent.apply(new ProjectResource(project, user), parentInput(newParentKey.get()));
        } catch (AuthException e) {
            err.append("error: insuffient access rights to change parent of '").append(name).append("'\n");
        } catch (ResourceConflictException | ResourceNotFoundException | BadRequestException e) {
            err.append("error: ").append(e.getMessage()).append("'\n");
        } catch (UnprocessableEntityException | IOException e) {
            throw new Failure(1, "failure in request", e);
        } catch (PermissionBackendException e) {
            throw new Failure(1, "permissions unavailable", e);
        }
    }
    if (err.length() > 0) {
        while (err.charAt(err.length() - 1) == '\n') {
            err.setLength(err.length() - 1);
        }
        throw die(err.toString());
    }
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) AuthException(com.google.gerrit.extensions.restapi.AuthException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IOException(java.io.IOException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AuthException(com.google.gerrit.extensions.restapi.AuthException) IOException(java.io.IOException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Project(com.google.gerrit.entities.Project) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ProjectState(com.google.gerrit.server.project.ProjectState) ProjectResource(com.google.gerrit.server.project.ProjectResource) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 42 with Project

use of com.google.gerrit.entities.Project in project gerrit by GerritCodeReview.

the class CreateProjectCommand method run.

@Override
protected void run() throws Failure {
    enableGracefulStop();
    try {
        if (!suggestParent) {
            if (projectName == null) {
                throw die("Project name is required.");
            }
            ProjectInput input = new ProjectInput();
            input.name = projectName;
            if (ownerIds != null) {
                input.owners = Lists.transform(ownerIds, AccountGroup.UUID::get);
            }
            if (newParent != null) {
                input.parent = newParent.getName();
            }
            input.permissionsOnly = permissionsOnly;
            input.description = projectDescription;
            input.submitType = submitType;
            input.useContributorAgreements = contributorAgreements;
            input.useSignedOffBy = signedOffBy;
            input.useContentMerge = contentMerge;
            input.requireChangeId = requireChangeID;
            input.createNewChangeForAllNotInTarget = createNewChangeForAllNotInTarget;
            input.branches = branch;
            input.createEmptyCommit = createEmptyCommit;
            input.maxObjectSizeLimit = maxObjectSizeLimit;
            input.rejectEmptyCommit = rejectEmptyCommit;
            if (pluginConfigValues != null) {
                input.pluginConfigValues = parsePluginConfigValues(pluginConfigValues);
            }
            gApi.projects().create(input);
        } else {
            for (Project.NameKey parent : suggestParentCandidates.getNameKeys()) {
                stdout.print(parent.get() + '\n');
            }
        }
    } catch (RestApiException err) {
        throw die(err);
    } catch (PermissionBackendException err) {
        throw new Failure(1, "permissions unavailable", err);
    }
}
Also used : Project(com.google.gerrit.entities.Project) ProjectInput(com.google.gerrit.extensions.api.projects.ProjectInput) AccountGroup(com.google.gerrit.entities.AccountGroup) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException)

Example 43 with Project

use of com.google.gerrit.entities.Project in project gerrit by GerritCodeReview.

the class RestApiServletIT method xGerritUpdatedRefSetMultipleHeadersForSubmitTopic.

@Test
@GerritConfig(name = "change.submitWholeTopic", value = "true")
public void xGerritUpdatedRefSetMultipleHeadersForSubmitTopic() throws Exception {
    String secondProject = "secondProject";
    projectOperations.newProject().name(secondProject).create();
    TestRepository<InMemoryRepository> secondRepo = cloneProject(Project.nameKey("secondProject"), admin);
    String topic = "topic";
    String branch = "refs/heads/master";
    Result change1 = createChange(testRepo, branch, "first change", "a.txt", "message", topic);
    Result change2 = createChange(secondRepo, branch, "second change", "b.txt", "message", topic);
    String metaRef1 = RefNames.changeMetaRef(change1.getChange().getId());
    String metaRef2 = RefNames.changeMetaRef(change2.getChange().getId());
    gApi.changes().id(change1.getChangeId()).current().review(ReviewInput.approve());
    gApi.changes().id(change2.getChangeId()).current().review(ReviewInput.approve());
    Project.NameKey project1 = change1.getChange().project();
    Project.NameKey project2 = change2.getChange().project();
    try (Repository repository1 = repoManager.openRepository(project1);
        Repository repository2 = repoManager.openRepository(project2)) {
        ObjectId originalFirstMetaRefSha1 = getMetaRefSha1(change1);
        ObjectId originalSecondMetaRefSha1 = getMetaRefSha1(change2);
        ObjectId originalDestinationBranchSha1Project1 = repository1.resolve(change1.getChange().change().getDest().branch());
        ObjectId originalDestinationBranchSha1Project2 = repository2.resolve(change2.getChange().change().getDest().branch());
        RestResponse response = adminRestSession.postWithHeaders("/changes/" + change2.getChangeId() + "/submit", /* content = */
        null, X_GERRIT_UPDATED_REF_ENABLED_HEADER);
        response.assertOK();
        assertThat(gApi.changes().id(change1.getChangeId()).get().status).isEqualTo(ChangeStatus.MERGED);
        ObjectId firstMetaRefSha1 = getMetaRefSha1(change1);
        ObjectId secondMetaRefSha1 = getMetaRefSha1(change2);
        List<String> headers = response.getHeaders(X_GERRIT_UPDATED_REF);
        String branchSha1Project1 = repository1.getRefDatabase().exactRef(change1.getChange().change().getDest().branch()).getObjectId().name();
        String branchSha1Project2 = repository2.getRefDatabase().exactRef(change2.getChange().change().getDest().branch()).getObjectId().name();
        // During submit, all relevant meta refs of the latest patchset are updated + the destination
        // branch/es.
        // TODO(paiking): This doesn't work well for torn submissions: If the changes are in
        // different projects in the same topic, and we tried to submit those changes together, it's
        // possible that the first submission only submitted one of the changes, and then the retry
        // submitted the other change. If that happens, when the user retries, they will not get the
        // meta ref updates for the change that got submitted on the previous submission attempt.
        // Ideally, submit should be idempotent and always return all meta refs on all submission
        // attempts.
        assertThat(headers).containsExactly(String.format("%s~%s~%s~%s", Url.encode(project1.get()), Url.encode(metaRef1), originalFirstMetaRefSha1.getName(), firstMetaRefSha1.getName()), String.format("%s~%s~%s~%s", Url.encode(project2.get()), Url.encode(metaRef2), originalSecondMetaRefSha1.getName(), secondMetaRefSha1.getName()), String.format("%s~%s~%s~%s", Url.encode(project1.get()), Url.encode(branch), originalDestinationBranchSha1Project1.getName(), branchSha1Project1), String.format("%s~%s~%s~%s", Url.encode(project2.get()), Url.encode(branch), originalDestinationBranchSha1Project2.getName(), branchSha1Project2));
    }
}
Also used : Project(com.google.gerrit.entities.Project) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) ObjectId(org.eclipse.jgit.lib.ObjectId) RestResponse(com.google.gerrit.acceptance.RestResponse) Result(com.google.gerrit.acceptance.PushOneCommit.Result) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 44 with Project

use of com.google.gerrit.entities.Project in project gerrit by GerritCodeReview.

the class ChangeOperationsImplTest method createdChangeHasSpecifiedTargetBranch.

@Test
public void createdChangeHasSpecifiedTargetBranch() throws Exception {
    Project.NameKey project = projectOperations.newProject().branches("test-branch").create();
    Change.Id changeId = changeOperations.newChange().project(project).branch("test-branch").create();
    ChangeInfo change = getChangeFromServer(changeId);
    assertThat(change.branch).isEqualTo("test-branch");
}
Also used : Project(com.google.gerrit.entities.Project) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) Change(com.google.gerrit.entities.Change) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 45 with Project

use of com.google.gerrit.entities.Project in project gerrit by GerritCodeReview.

the class ChangeOperationsImplTest method createdChangeUsesSpecifiedBranchTipAsParent.

@Test
public void createdChangeUsesSpecifiedBranchTipAsParent() throws Exception {
    Project.NameKey project = projectOperations.newProject().branches("test-branch").create();
    Change.Id changeId = changeOperations.newChange().project(project).childOf().tipOfBranch("refs/heads/test-branch").create();
    ChangeInfo change = getChangeFromServer(changeId);
    CommitInfo currentPatchsetCommit = change.revisions.get(change.currentRevision).commit;
    ObjectId parentCommitId = projectOperations.project(project).getHead("test-branch").getId();
    assertThat(currentPatchsetCommit).parents().onlyElement().commit().isEqualTo(parentCommitId.name());
}
Also used : Project(com.google.gerrit.entities.Project) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.entities.Change) CommitInfo(com.google.gerrit.extensions.common.CommitInfo) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Aggregations

Project (com.google.gerrit.entities.Project)184 Test (org.junit.Test)109 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)67 Change (com.google.gerrit.entities.Change)43 Repository (org.eclipse.jgit.lib.Repository)34 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)33 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)32 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)31 BranchNameKey (com.google.gerrit.entities.BranchNameKey)30 Config (org.eclipse.jgit.lib.Config)26 ObjectId (org.eclipse.jgit.lib.ObjectId)26 IOException (java.io.IOException)25 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)24 ProjectState (com.google.gerrit.server.project.ProjectState)23 Inject (com.google.inject.Inject)23 List (java.util.List)23 AuthException (com.google.gerrit.extensions.restapi.AuthException)22 ChangeData (com.google.gerrit.server.query.change.ChangeData)22 RevCommit (org.eclipse.jgit.revwalk.RevCommit)22 PatchSet (com.google.gerrit.entities.PatchSet)20