Search in sources :

Example 11 with BatchLabelInput

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

the class PostLabelsIT method createLabels.

@Test
public void createLabels() throws Exception {
    LabelDefinitionInput fooInput = new LabelDefinitionInput();
    fooInput.name = "Foo";
    fooInput.values = ImmutableMap.of("+1", "Looks Good", " 0", "Don't Know", "-1", "Looks Bad");
    LabelDefinitionInput barInput = new LabelDefinitionInput();
    barInput.name = "Bar";
    barInput.values = ImmutableMap.of("+1", "Looks Good", " 0", "Don't Know", "-1", "Looks Bad");
    BatchLabelInput input = new BatchLabelInput();
    input.create = ImmutableList.of(fooInput, barInput);
    gApi.projects().name(allProjects.get()).labels(input);
    assertThat(gApi.projects().name(allProjects.get()).label("Foo").get()).isNotNull();
    assertThat(gApi.projects().name(allProjects.get()).label("Bar").get()).isNotNull();
}
Also used : LabelDefinitionInput(com.google.gerrit.extensions.common.LabelDefinitionInput) BatchLabelInput(com.google.gerrit.extensions.common.BatchLabelInput) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 12 with BatchLabelInput

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

the class PostLabelsIT method cannotSetCommitMessageOnLabelDefinitionInputForCreate.

@Test
public void cannotSetCommitMessageOnLabelDefinitionInputForCreate() throws Exception {
    LabelDefinitionInput labelInput = new LabelDefinitionInput();
    labelInput.name = "Foo";
    labelInput.commitMessage = "Create Label Foo";
    BatchLabelInput input = new BatchLabelInput();
    input.create = ImmutableList.of(labelInput);
    BadRequestException thrown = assertThrows(BadRequestException.class, () -> gApi.projects().name(allProjects.get()).labels(input));
    assertThat(thrown).hasMessageThat().contains("commit message on label definition input not supported");
}
Also used : BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) LabelDefinitionInput(com.google.gerrit.extensions.common.LabelDefinitionInput) BatchLabelInput(com.google.gerrit.extensions.common.BatchLabelInput) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 13 with BatchLabelInput

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

the class PostLabels method apply.

@Override
public Response<?> apply(ProjectResource rsrc, BatchLabelInput input) throws AuthException, UnprocessableEntityException, PermissionBackendException, IOException, ConfigInvalidException, BadRequestException, ResourceConflictException {
    if (!user.get().isIdentifiedUser()) {
        throw new AuthException("Authentication required");
    }
    permissionBackend.currentUser().project(rsrc.getNameKey()).check(ProjectPermission.WRITE_CONFIG);
    if (input == null) {
        input = new BatchLabelInput();
    }
    try (MetaDataUpdate md = updateFactory.create(rsrc.getNameKey())) {
        boolean dirty = false;
        ProjectConfig config = projectConfigFactory.read(md);
        if (input.delete != null && !input.delete.isEmpty()) {
            for (String labelName : input.delete) {
                if (!deleteLabel.deleteLabel(config, labelName.trim())) {
                    throw new UnprocessableEntityException(String.format("label %s not found", labelName));
                }
            }
            dirty = true;
        }
        if (input.create != null && !input.create.isEmpty()) {
            for (LabelDefinitionInput labelInput : input.create) {
                if (labelInput.name == null || labelInput.name.trim().isEmpty()) {
                    throw new BadRequestException("label name is required for new label");
                }
                if (labelInput.commitMessage != null) {
                    throw new BadRequestException("commit message on label definition input not supported");
                }
                createLabel.createLabel(config, labelInput.name.trim(), labelInput);
            }
            dirty = true;
        }
        if (input.update != null && !input.update.isEmpty()) {
            for (Map.Entry<String, LabelDefinitionInput> e : input.update.entrySet()) {
                LabelType labelType = config.getLabelSections().get(e.getKey().trim());
                if (labelType == null) {
                    throw new UnprocessableEntityException(String.format("label %s not found", e.getKey()));
                }
                if (e.getValue().commitMessage != null) {
                    throw new BadRequestException("commit message on label definition input not supported");
                }
                setLabel.updateLabel(config, labelType, e.getValue());
            }
            dirty = true;
        }
        if (input.commitMessage != null) {
            md.setMessage(Strings.emptyToNull(input.commitMessage.trim()));
        } else {
            md.setMessage("Update labels");
        }
        if (dirty) {
            config.commit(md);
            projectCache.evictAndReindex(rsrc.getProjectState().getProject());
        }
    }
    return Response.ok("");
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) LabelType(com.google.gerrit.entities.LabelType) AuthException(com.google.gerrit.extensions.restapi.AuthException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) BatchLabelInput(com.google.gerrit.extensions.common.BatchLabelInput) LabelDefinitionInput(com.google.gerrit.extensions.common.LabelDefinitionInput) Map(java.util.Map) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 14 with BatchLabelInput

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

the class PostLabelsIT method deleteNonExistingLabel.

@Test
public void deleteNonExistingLabel() throws Exception {
    BatchLabelInput input = new BatchLabelInput();
    input.delete = ImmutableList.of("Foo");
    UnprocessableEntityException thrown = assertThrows(UnprocessableEntityException.class, () -> gApi.projects().name(allProjects.get()).labels(input));
    assertThat(thrown).hasMessageThat().contains("label Foo not found");
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) BatchLabelInput(com.google.gerrit.extensions.common.BatchLabelInput) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 15 with BatchLabelInput

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

the class PostLabelsIT method noOpUpdate.

@Test
public void noOpUpdate() throws Exception {
    RevCommit refsMetaConfigHead = projectOperations.project(allProjects).getHead(RefNames.REFS_CONFIG);
    gApi.projects().name(allProjects.get()).labels(new BatchLabelInput());
    assertThat(projectOperations.project(allProjects).getHead(RefNames.REFS_CONFIG)).isEqualTo(refsMetaConfigHead);
}
Also used : BatchLabelInput(com.google.gerrit.extensions.common.BatchLabelInput) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Aggregations

BatchLabelInput (com.google.gerrit.extensions.common.BatchLabelInput)27 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)26 Test (org.junit.Test)26 LabelDefinitionInput (com.google.gerrit.extensions.common.LabelDefinitionInput)17 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)5 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)4 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)4 LabelDefinitionInfo (com.google.gerrit.extensions.common.LabelDefinitionInfo)3 AuthException (com.google.gerrit.extensions.restapi.AuthException)3 LabelType (com.google.gerrit.entities.LabelType)1 MetaDataUpdate (com.google.gerrit.server.git.meta.MetaDataUpdate)1 ProjectConfig (com.google.gerrit.server.project.ProjectConfig)1 Map (java.util.Map)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1