Line data Source code
1 : // Copyright (C) 2019 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.api.projects; 16 : 17 : import static com.google.gerrit.server.api.ApiUtil.asRestApiException; 18 : import static com.google.gerrit.server.project.ProjectCache.illegalState; 19 : 20 : import com.google.gerrit.common.Nullable; 21 : import com.google.gerrit.extensions.api.projects.LabelApi; 22 : import com.google.gerrit.extensions.common.InputWithCommitMessage; 23 : import com.google.gerrit.extensions.common.LabelDefinitionInfo; 24 : import com.google.gerrit.extensions.common.LabelDefinitionInput; 25 : import com.google.gerrit.extensions.restapi.IdString; 26 : import com.google.gerrit.extensions.restapi.RestApiException; 27 : import com.google.gerrit.server.permissions.PermissionBackendException; 28 : import com.google.gerrit.server.project.LabelResource; 29 : import com.google.gerrit.server.project.ProjectCache; 30 : import com.google.gerrit.server.project.ProjectResource; 31 : import com.google.gerrit.server.restapi.project.CreateLabel; 32 : import com.google.gerrit.server.restapi.project.DeleteLabel; 33 : import com.google.gerrit.server.restapi.project.GetLabel; 34 : import com.google.gerrit.server.restapi.project.LabelsCollection; 35 : import com.google.gerrit.server.restapi.project.SetLabel; 36 : import com.google.inject.Inject; 37 : import com.google.inject.assistedinject.Assisted; 38 : 39 : public class LabelApiImpl implements LabelApi { 40 : interface Factory { 41 : LabelApiImpl create(ProjectResource project, String label); 42 : } 43 : 44 : private final LabelsCollection labels; 45 : private final CreateLabel createLabel; 46 : private final GetLabel getLabel; 47 : private final SetLabel setLabel; 48 : private final DeleteLabel deleteLabel; 49 : private final ProjectCache projectCache; 50 : private final String label; 51 : 52 : private ProjectResource project; 53 : 54 : @Inject 55 : LabelApiImpl( 56 : LabelsCollection labels, 57 : CreateLabel createLabel, 58 : GetLabel getLabel, 59 : SetLabel setLabel, 60 : DeleteLabel deleteLabel, 61 : ProjectCache projectCache, 62 : @Assisted ProjectResource project, 63 7 : @Assisted String label) { 64 7 : this.labels = labels; 65 7 : this.createLabel = createLabel; 66 7 : this.getLabel = getLabel; 67 7 : this.setLabel = setLabel; 68 7 : this.deleteLabel = deleteLabel; 69 7 : this.projectCache = projectCache; 70 7 : this.project = project; 71 7 : this.label = label; 72 7 : } 73 : 74 : @Override 75 : public LabelApi create(LabelDefinitionInput input) throws RestApiException { 76 : try { 77 3 : createLabel.apply(project, IdString.fromDecoded(label), input); 78 : 79 : // recreate project resource because project state was updated by creating the new label and 80 : // needs to be reloaded 81 3 : project = 82 : new ProjectResource( 83 : projectCache 84 3 : .get(project.getNameKey()) 85 3 : .orElseThrow(illegalState(project.getNameKey())), 86 3 : project.getUser()); 87 3 : return this; 88 2 : } catch (Exception e) { 89 2 : throw asRestApiException("Cannot create branch", e); 90 : } 91 : } 92 : 93 : @Override 94 : public LabelDefinitionInfo get() throws RestApiException { 95 : try { 96 6 : return getLabel.apply(resource()).value(); 97 4 : } catch (Exception e) { 98 4 : throw asRestApiException("Cannot get label", e); 99 : } 100 : } 101 : 102 : @Override 103 : public LabelDefinitionInfo update(LabelDefinitionInput input) throws RestApiException { 104 : try { 105 1 : return setLabel.apply(resource(), input).value(); 106 1 : } catch (Exception e) { 107 1 : throw asRestApiException("Cannot update label", e); 108 : } 109 : } 110 : 111 : @Override 112 : public void delete(@Nullable String commitMessage) throws RestApiException { 113 : try { 114 1 : deleteLabel.apply(resource(), new InputWithCommitMessage(commitMessage)); 115 1 : } catch (Exception e) { 116 1 : throw asRestApiException("Cannot delete label", e); 117 1 : } 118 1 : } 119 : 120 : private LabelResource resource() throws RestApiException, PermissionBackendException { 121 7 : return labels.parse(project, IdString.fromDecoded(label)); 122 : } 123 : }