Line data Source code
1 : // Copyright (C) 2015 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 : 19 : import com.google.gerrit.extensions.api.projects.TagApi; 20 : import com.google.gerrit.extensions.api.projects.TagInfo; 21 : import com.google.gerrit.extensions.api.projects.TagInput; 22 : import com.google.gerrit.extensions.common.Input; 23 : import com.google.gerrit.extensions.restapi.IdString; 24 : import com.google.gerrit.extensions.restapi.RestApiException; 25 : import com.google.gerrit.server.permissions.PermissionBackendException; 26 : import com.google.gerrit.server.project.ProjectResource; 27 : import com.google.gerrit.server.project.TagResource; 28 : import com.google.gerrit.server.restapi.project.CreateTag; 29 : import com.google.gerrit.server.restapi.project.DeleteTag; 30 : import com.google.gerrit.server.restapi.project.ListTags; 31 : import com.google.gerrit.server.restapi.project.TagsCollection; 32 : import com.google.inject.Inject; 33 : import com.google.inject.assistedinject.Assisted; 34 : import java.io.IOException; 35 : 36 : public class TagApiImpl implements TagApi { 37 : interface Factory { 38 : TagApiImpl create(ProjectResource project, String ref); 39 : } 40 : 41 : private final ListTags listTags; 42 : private final CreateTag createTag; 43 : private final DeleteTag deleteTag; 44 : private final TagsCollection tags; 45 : private final String ref; 46 : private final ProjectResource project; 47 : 48 : @Inject 49 : TagApiImpl( 50 : ListTags listTags, 51 : CreateTag createTag, 52 : DeleteTag deleteTag, 53 : TagsCollection tags, 54 : @Assisted ProjectResource project, 55 7 : @Assisted String ref) { 56 7 : this.listTags = listTags; 57 7 : this.createTag = createTag; 58 7 : this.deleteTag = deleteTag; 59 7 : this.tags = tags; 60 7 : this.project = project; 61 7 : this.ref = ref; 62 7 : } 63 : 64 : @Override 65 : public TagApi create(TagInput input) throws RestApiException { 66 : try { 67 7 : createTag.apply(project, IdString.fromDecoded(ref), input); 68 7 : return this; 69 1 : } catch (Exception e) { 70 1 : throw asRestApiException("Cannot create tag", e); 71 : } 72 : } 73 : 74 : @Override 75 : public TagInfo get() throws RestApiException { 76 : try { 77 3 : return listTags.get(project, IdString.fromDecoded(ref)); 78 2 : } catch (Exception e) { 79 2 : throw asRestApiException("Cannot get tag", e); 80 : } 81 : } 82 : 83 : @Override 84 : public void delete() throws RestApiException { 85 : try { 86 1 : deleteTag.apply(resource(), new Input()); 87 1 : } catch (Exception e) { 88 1 : throw asRestApiException("Cannot delete tag", e); 89 1 : } 90 1 : } 91 : 92 : private TagResource resource() throws RestApiException, IOException, PermissionBackendException { 93 1 : return tags.parse(project, IdString.fromDecoded(ref)); 94 : } 95 : }