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();
}
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");
}
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("");
}
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");
}
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);
}
Aggregations