Search in sources :

Example 1 with CreateProjectArgs

use of com.google.gerrit.server.project.CreateProjectArgs in project gerrit by GerritCodeReview.

the class ProjectOperationsImpl method createNewProject.

private Project.NameKey createNewProject(TestProjectCreation projectCreation) throws Exception {
    String name = projectCreation.name().orElse(RandomStringUtils.randomAlphabetic(8));
    CreateProjectArgs args = new CreateProjectArgs();
    args.setProjectName(name);
    args.permissionsOnly = projectCreation.permissionOnly().orElse(false);
    args.branch = projectCreation.branches().stream().map(RefNames::fullName).collect(toImmutableList());
    args.createEmptyCommit = projectCreation.createEmptyCommit().orElse(true);
    projectCreation.parent().ifPresent(p -> args.newParent = p);
    // ProjectCreator wants non-null owner IDs.
    args.ownerIds = new ArrayList<>(projectCreation.owners());
    projectCreation.submitType().ifPresent(st -> args.submitType = st);
    projectCreator.createProject(args);
    return Project.nameKey(name);
}
Also used : RefNames(com.google.gerrit.entities.RefNames) CreateProjectArgs(com.google.gerrit.server.project.CreateProjectArgs)

Example 2 with CreateProjectArgs

use of com.google.gerrit.server.project.CreateProjectArgs in project gerrit by GerritCodeReview.

the class CreateProject method apply.

@Override
public Response<ProjectInfo> apply(TopLevelResource resource, IdString id, ProjectInput input) throws RestApiException, IOException, ConfigInvalidException, PermissionBackendException {
    String name = id.get();
    if (input == null) {
        input = new ProjectInput();
    }
    if (input.name != null && !name.equals(input.name)) {
        throw new BadRequestException("name must match URL");
    }
    CreateProjectArgs args = new CreateProjectArgs();
    args.setProjectName(ProjectUtil.sanitizeProjectName(name));
    String parentName = MoreObjects.firstNonNull(Strings.emptyToNull(input.parent), allProjects.get());
    args.newParent = projectsCollection.get().parse(parentName, false).getNameKey();
    if (args.newParent.equals(allUsers)) {
        throw new ResourceConflictException(String.format("Cannot inherit from '%s' project", allUsers.get()));
    }
    args.createEmptyCommit = input.createEmptyCommit;
    args.permissionsOnly = input.permissionsOnly;
    args.projectDescription = Strings.emptyToNull(input.description);
    args.submitType = input.submitType;
    args.branch = normalizeBranchNames(input.branches);
    if (input.owners == null || input.owners.isEmpty()) {
        args.ownerIds = new ArrayList<>(projectOwnerGroups.create(args.getProject()).get());
    } else {
        args.ownerIds = Lists.newArrayListWithCapacity(input.owners.size());
        for (String owner : input.owners) {
            args.ownerIds.add(groupResolver.get().parse(owner).getGroupUUID());
        }
    }
    args.contributorAgreements = MoreObjects.firstNonNull(input.useContributorAgreements, InheritableBoolean.INHERIT);
    args.signedOffBy = MoreObjects.firstNonNull(input.useSignedOffBy, InheritableBoolean.INHERIT);
    args.contentMerge = input.submitType == SubmitType.FAST_FORWARD_ONLY ? InheritableBoolean.FALSE : MoreObjects.firstNonNull(input.useContentMerge, InheritableBoolean.INHERIT);
    args.newChangeForAllNotInTarget = MoreObjects.firstNonNull(input.createNewChangeForAllNotInTarget, InheritableBoolean.INHERIT);
    args.changeIdRequired = MoreObjects.firstNonNull(input.requireChangeId, InheritableBoolean.INHERIT);
    args.rejectEmptyCommit = MoreObjects.firstNonNull(input.rejectEmptyCommit, InheritableBoolean.INHERIT);
    args.enableSignedPush = MoreObjects.firstNonNull(input.enableSignedPush, InheritableBoolean.INHERIT);
    args.requireSignedPush = MoreObjects.firstNonNull(input.requireSignedPush, InheritableBoolean.INHERIT);
    try {
        args.maxObjectSizeLimit = ProjectConfig.validMaxObjectSizeLimit(input.maxObjectSizeLimit);
    } catch (ConfigInvalidException e) {
        throw new BadRequestException(e.getMessage());
    }
    Lock nameLock = lockManager.call(lockManager -> lockManager.getLock(args.getProject()));
    nameLock.lock();
    try {
        try {
            projectCreationValidationListeners.runEach(l -> l.validateNewProject(args), ValidationException.class);
        } catch (ValidationException e) {
            throw new ResourceConflictException(e.getMessage(), e);
        }
        ProjectState projectState = projectCreator.createProject(args);
        requireNonNull(projectState, () -> String.format("failed to create project %s", args.getProject().get()));
        if (input.pluginConfigValues != null) {
            ConfigInput in = new ConfigInput();
            in.pluginConfigValues = input.pluginConfigValues;
            in.description = args.projectDescription;
            putConfig.get().apply(projectState, in);
        }
        return Response.created(json.format(projectState));
    } finally {
        nameLock.unlock();
    }
}
Also used : ProjectInput(com.google.gerrit.extensions.api.projects.ProjectInput) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ValidationException(com.google.gerrit.server.validators.ValidationException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ConfigInput(com.google.gerrit.extensions.api.projects.ConfigInput) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ProjectState(com.google.gerrit.server.project.ProjectState) IdString(com.google.gerrit.extensions.restapi.IdString) CreateProjectArgs(com.google.gerrit.server.project.CreateProjectArgs) Lock(java.util.concurrent.locks.Lock)

Example 3 with CreateProjectArgs

use of com.google.gerrit.server.project.CreateProjectArgs in project gerrit by GerritCodeReview.

the class CancellationIT method handleServerDeadlineExceeded.

@Test
public void handleServerDeadlineExceeded() throws Exception {
    ProjectCreationValidationListener projectCreationListener = new ProjectCreationValidationListener() {

        @Override
        public void validateNewProject(CreateProjectArgs args) throws ValidationException {
            // Simulate an exceeded deadline by throwing RequestCancelledException.
            throw new RequestCancelledException(RequestStateProvider.Reason.SERVER_DEADLINE_EXCEEDED, /* cancellationMessage= */
            null);
        }
    };
    try (Registration registration = extensionRegistry.newRegistration().add(projectCreationListener)) {
        RestResponse response = adminRestSession.put("/projects/" + name("new"));
        assertThat(response.getStatusCode()).isEqualTo(SC_REQUEST_TIMEOUT);
        assertThat(response.getEntityContent()).isEqualTo("Server Deadline Exceeded");
    }
}
Also used : ProjectCreationValidationListener(com.google.gerrit.server.validators.ProjectCreationValidationListener) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) RestResponse(com.google.gerrit.acceptance.RestResponse) RequestCancelledException(com.google.gerrit.server.cancellation.RequestCancelledException) CreateProjectArgs(com.google.gerrit.server.project.CreateProjectArgs) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 4 with CreateProjectArgs

use of com.google.gerrit.server.project.CreateProjectArgs in project gerrit by GerritCodeReview.

the class CancellationIT method exceededDeadlineForOneRequestDoesntAbortFollowUpRequest.

@Test
@GerritConfig(name = "deadline.default.timeout", value = "500ms")
public void exceededDeadlineForOneRequestDoesntAbortFollowUpRequest() throws Exception {
    ProjectCreationValidationListener projectCreationValidationListener = new ProjectCreationValidationListener() {

        @Override
        public void validateNewProject(CreateProjectArgs args) throws ValidationException {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException("interrupted during sleep", e);
            }
        }
    };
    try (Registration registration = extensionRegistry.newRegistration().add(projectCreationValidationListener)) {
        RestResponse response = adminRestSession.putWithHeaders("/projects/" + name("new"));
        assertThat(response.getStatusCode()).isEqualTo(SC_REQUEST_TIMEOUT);
        assertThat(response.getEntityContent()).isEqualTo("Server Deadline Exceeded\n\ndefault.timeout=500ms");
    }
    // verify that the exceeded deadline for the previous request, isn't applied to a new request
    RestResponse response = adminRestSession.putWithHeaders("/projects/" + name("new2"));
    response.assertCreated();
}
Also used : ProjectCreationValidationListener(com.google.gerrit.server.validators.ProjectCreationValidationListener) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) RestResponse(com.google.gerrit.acceptance.RestResponse) CreateProjectArgs(com.google.gerrit.server.project.CreateProjectArgs) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 5 with CreateProjectArgs

use of com.google.gerrit.server.project.CreateProjectArgs in project gerrit by GerritCodeReview.

the class SshCancellationIT method handleWrappedRequestCancelledException.

@Test
public void handleWrappedRequestCancelledException() throws Exception {
    ProjectCreationValidationListener projectCreationListener = new ProjectCreationValidationListener() {

        @Override
        public void validateNewProject(CreateProjectArgs args) throws ValidationException {
            throw new RuntimeException(new RequestCancelledException(RequestStateProvider.Reason.SERVER_DEADLINE_EXCEEDED, "deadline = 10m"));
        }
    };
    try (Registration registration = extensionRegistry.newRegistration().add(projectCreationListener)) {
        adminSshSession.exec("gerrit create-project " + name("new"));
        adminSshSession.assertFailure("Server Deadline Exceeded (deadline = 10m)");
    }
}
Also used : ProjectCreationValidationListener(com.google.gerrit.server.validators.ProjectCreationValidationListener) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) RequestCancelledException(com.google.gerrit.server.cancellation.RequestCancelledException) CreateProjectArgs(com.google.gerrit.server.project.CreateProjectArgs) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

CreateProjectArgs (com.google.gerrit.server.project.CreateProjectArgs)13 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)11 Registration (com.google.gerrit.acceptance.ExtensionRegistry.Registration)11 ProjectCreationValidationListener (com.google.gerrit.server.validators.ProjectCreationValidationListener)11 Test (org.junit.Test)11 RequestCancelledException (com.google.gerrit.server.cancellation.RequestCancelledException)10 RestResponse (com.google.gerrit.acceptance.RestResponse)6 GerritConfig (com.google.gerrit.acceptance.config.GerritConfig)1 RefNames (com.google.gerrit.entities.RefNames)1 ConfigInput (com.google.gerrit.extensions.api.projects.ConfigInput)1 ProjectInput (com.google.gerrit.extensions.api.projects.ProjectInput)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 IdString (com.google.gerrit.extensions.restapi.IdString)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 ProjectState (com.google.gerrit.server.project.ProjectState)1 ValidationException (com.google.gerrit.server.validators.ValidationException)1 Lock (java.util.concurrent.locks.Lock)1 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)1