Add REST endpoint to add new label definitions
Bug: Issue 11522 Signed-off-by: Edwin Kempin <ekempin@google.com> Change-Id: I09b8642d266feeeef0c5ae2bdeb106615e4f5bfe
This commit is contained in:
@@ -19,33 +19,70 @@ import static com.google.gerrit.server.api.ApiUtil.asRestApiException;
|
||||
import com.google.gerrit.extensions.api.projects.LabelApi;
|
||||
import com.google.gerrit.extensions.common.LabelDefinitionInfo;
|
||||
import com.google.gerrit.extensions.common.LabelDefinitionInput;
|
||||
import com.google.gerrit.extensions.restapi.IdString;
|
||||
import com.google.gerrit.extensions.restapi.RestApiException;
|
||||
import com.google.gerrit.server.permissions.PermissionBackendException;
|
||||
import com.google.gerrit.server.project.LabelResource;
|
||||
import com.google.gerrit.server.project.ProjectCache;
|
||||
import com.google.gerrit.server.project.ProjectResource;
|
||||
import com.google.gerrit.server.restapi.project.CreateLabel;
|
||||
import com.google.gerrit.server.restapi.project.GetLabel;
|
||||
import com.google.gerrit.server.restapi.project.LabelsCollection;
|
||||
import com.google.gerrit.server.restapi.project.SetLabel;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.assistedinject.Assisted;
|
||||
|
||||
public class LabelApiImpl implements LabelApi {
|
||||
interface Factory {
|
||||
LabelApiImpl create(LabelResource rsrc);
|
||||
LabelApiImpl create(ProjectResource project, String label);
|
||||
}
|
||||
|
||||
private final LabelsCollection labels;
|
||||
private final CreateLabel createLabel;
|
||||
private final GetLabel getLabel;
|
||||
private final SetLabel setLabel;
|
||||
private final LabelResource rsrc;
|
||||
private final ProjectCache projectCache;
|
||||
private final String label;
|
||||
|
||||
private ProjectResource project;
|
||||
|
||||
@Inject
|
||||
LabelApiImpl(GetLabel getLabel, SetLabel setLabel, @Assisted LabelResource rsrc) {
|
||||
LabelApiImpl(
|
||||
LabelsCollection labels,
|
||||
CreateLabel createLabel,
|
||||
GetLabel getLabel,
|
||||
SetLabel setLabel,
|
||||
ProjectCache projectCache,
|
||||
@Assisted ProjectResource project,
|
||||
@Assisted String label) {
|
||||
this.labels = labels;
|
||||
this.createLabel = createLabel;
|
||||
this.getLabel = getLabel;
|
||||
this.setLabel = setLabel;
|
||||
this.rsrc = rsrc;
|
||||
this.projectCache = projectCache;
|
||||
this.project = project;
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LabelApi create(LabelDefinitionInput input) throws RestApiException {
|
||||
try {
|
||||
createLabel.apply(project, IdString.fromDecoded(label), input);
|
||||
|
||||
// recreate project resource because project state was updated by creating the new label and
|
||||
// needs to be reloaded
|
||||
project =
|
||||
new ProjectResource(projectCache.checkedGet(project.getNameKey()), project.getUser());
|
||||
return this;
|
||||
} catch (Exception e) {
|
||||
throw asRestApiException("Cannot create branch", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LabelDefinitionInfo get() throws RestApiException {
|
||||
try {
|
||||
return getLabel.apply(rsrc).value();
|
||||
return getLabel.apply(resource()).value();
|
||||
} catch (Exception e) {
|
||||
throw asRestApiException("Cannot get label", e);
|
||||
}
|
||||
@@ -54,9 +91,13 @@ public class LabelApiImpl implements LabelApi {
|
||||
@Override
|
||||
public LabelDefinitionInfo update(LabelDefinitionInput input) throws RestApiException {
|
||||
try {
|
||||
return setLabel.apply(rsrc, input).value();
|
||||
return setLabel.apply(resource(), input).value();
|
||||
} catch (Exception e) {
|
||||
throw asRestApiException("Cannot update label", e);
|
||||
}
|
||||
}
|
||||
|
||||
private LabelResource resource() throws RestApiException, PermissionBackendException {
|
||||
return labels.parse(project, IdString.fromDecoded(label));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +73,6 @@ import com.google.gerrit.server.restapi.project.GetHead;
|
||||
import com.google.gerrit.server.restapi.project.GetParent;
|
||||
import com.google.gerrit.server.restapi.project.Index;
|
||||
import com.google.gerrit.server.restapi.project.IndexChanges;
|
||||
import com.google.gerrit.server.restapi.project.LabelsCollection;
|
||||
import com.google.gerrit.server.restapi.project.ListBranches;
|
||||
import com.google.gerrit.server.restapi.project.ListDashboards;
|
||||
import com.google.gerrit.server.restapi.project.ListLabels;
|
||||
@@ -132,7 +131,6 @@ public class ProjectApiImpl implements ProjectApi {
|
||||
private final Index index;
|
||||
private final IndexChanges indexChanges;
|
||||
private final Provider<ListLabels> listLabels;
|
||||
private final LabelsCollection labels;
|
||||
private final LabelApiImpl.Factory labelApi;
|
||||
|
||||
@AssistedInject
|
||||
@@ -171,7 +169,6 @@ public class ProjectApiImpl implements ProjectApi {
|
||||
IndexChanges indexChanges,
|
||||
Provider<ListLabels> listLabels,
|
||||
LabelApiImpl.Factory labelApi,
|
||||
LabelsCollection labels,
|
||||
@Assisted ProjectResource project) {
|
||||
this(
|
||||
permissionBackend,
|
||||
@@ -209,7 +206,6 @@ public class ProjectApiImpl implements ProjectApi {
|
||||
indexChanges,
|
||||
listLabels,
|
||||
labelApi,
|
||||
labels,
|
||||
null);
|
||||
}
|
||||
|
||||
@@ -249,7 +245,6 @@ public class ProjectApiImpl implements ProjectApi {
|
||||
IndexChanges indexChanges,
|
||||
Provider<ListLabels> listLabels,
|
||||
LabelApiImpl.Factory labelApi,
|
||||
LabelsCollection labels,
|
||||
@Assisted String name) {
|
||||
this(
|
||||
permissionBackend,
|
||||
@@ -287,7 +282,6 @@ public class ProjectApiImpl implements ProjectApi {
|
||||
indexChanges,
|
||||
listLabels,
|
||||
labelApi,
|
||||
labels,
|
||||
name);
|
||||
}
|
||||
|
||||
@@ -327,7 +321,6 @@ public class ProjectApiImpl implements ProjectApi {
|
||||
IndexChanges indexChanges,
|
||||
Provider<ListLabels> listLabels,
|
||||
LabelApiImpl.Factory labelApi,
|
||||
LabelsCollection labels,
|
||||
String name) {
|
||||
this.permissionBackend = permissionBackend;
|
||||
this.createProject = createProject;
|
||||
@@ -365,7 +358,6 @@ public class ProjectApiImpl implements ProjectApi {
|
||||
this.indexChanges = indexChanges;
|
||||
this.listLabels = listLabels;
|
||||
this.labelApi = labelApi;
|
||||
this.labels = labels;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -715,7 +707,7 @@ public class ProjectApiImpl implements ProjectApi {
|
||||
@Override
|
||||
public LabelApi label(String labelName) throws RestApiException {
|
||||
try {
|
||||
return labelApi.create(labels.parse(checkExists(), IdString.fromDecoded(labelName)));
|
||||
return labelApi.create(checkExists(), labelName);
|
||||
} catch (Exception e) {
|
||||
throw asRestApiException("Cannot parse label", e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user