1 /** 2 * Copyright (c) 2013-2023, jcabi.com 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 1) Redistributions of source code must retain the above 8 * copyright notice, this list of conditions and the following 9 * disclaimer. 2) Redistributions in binary form must reproduce the above 10 * copyright notice, this list of conditions and the following 11 * disclaimer in the documentation and/or other materials provided 12 * with the distribution. 3) Neither the name of the jcabi.com nor 13 * the names of its contributors may be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 19 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 28 * OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 package com.jcabi.github; 31 32 import com.jcabi.aspects.Immutable; 33 import com.jcabi.aspects.Loggable; 34 import java.io.IOException; 35 import lombok.EqualsAndHashCode; 36 import lombok.ToString; 37 38 /** 39 * Github labels. 40 * 41 * @author Yegor Bugayenko (yegor256@gmail.com) 42 * @version $Id: fdb9432c497397b4cb99053e811540a08a579d7f $ 43 * @since 0.1 44 * @see <a href="https://developer.github.com/v3/issues/labels/">Labels API</a> 45 */ 46 @Immutable 47 @SuppressWarnings("PMD.TooManyMethods") 48 public interface Labels { 49 50 /** 51 * The repo we're in. 52 * @return Repo 53 */ 54 Repo repo(); 55 56 /** 57 * Create new label. 58 * @param name The name of it 59 * @param color Color of it 60 * @return The label created 61 * @throws IOException If there is any I/O problem 62 * @see <a href="https://developer.github.com/v3/issues/labels/#create-a-label">Create a Label</a> 63 */ 64 Label create( 65 String name, String color 66 ) throws IOException; 67 68 /** 69 * Get a label by name. 70 * @param name The name of it 71 * @return The label 72 * @see <a href="https://developer.github.com/v3/issues/labels/#get-a-single-label">Get a single label</a> 73 */ 74 Label get(String name); 75 76 /** 77 * Iterate them all. 78 * @return Iterator of labels 79 * @see <a href="https://developer.github.com/v3/issues/labels/#list-labels-on-an-issue">List Labels on an Issue</a> 80 */ 81 Iterable<Label> iterate(); 82 83 /** 84 * Delete label by name. 85 * @param name Name of the label to remove 86 * @throws IOException If there is any I/O problem 87 * @see <a href="https://developer.github.com/v3/issues/labels/#delete-a-label">Delete a Label</a> 88 */ 89 void delete(String name) 90 throws IOException; 91 92 /** 93 * Smart Labels with extra features. 94 */ 95 @Immutable 96 @ToString 97 @Loggable(Loggable.DEBUG) 98 @EqualsAndHashCode(of = "labels") 99 final class Smart implements Labels { 100 /** 101 * Encapsulated labels. 102 */ 103 private final transient Labels labels; 104 /** 105 * Public ctor. 106 * @param lbl Labels 107 */ 108 public Smart( 109 final Labels lbl 110 ) { 111 this.labels = lbl; 112 } 113 /** 114 * Label exists? 115 * @param name Name of the label 116 * @return TRUE if it exists 117 */ 118 public boolean contains( 119 final String name 120 ) { 121 boolean contains = false; 122 for (final Label label : this.labels.iterate()) { 123 if (label.name().equals(name)) { 124 contains = true; 125 break; 126 } 127 } 128 return contains; 129 } 130 /** 131 * Create or get label. 132 * @param name Name of the label 133 * @return Label found or created 134 * @throws IOException If there is any I/O problem 135 */ 136 public Label createOrGet( 137 final String name 138 ) throws IOException { 139 return this.createOrGet(name, "c0c0c0"); 140 } 141 /** 142 * Create or get label (with this explicit color). 143 * @param name Name of the label 144 * @param color Color to set (or modify) 145 * @return Label found or created 146 * @throws IOException If there is any I/O problem 147 * @since 0.7 148 */ 149 public Label createOrGet( 150 final String name, final String color 151 ) throws IOException { 152 final Label.Smart label; 153 if (this.contains(name)) { 154 label = new Label.Smart(this.labels.get(name)); 155 if (!label.color().equals(color)) { 156 label.color(color); 157 } 158 } else { 159 label = new Label.Smart(this.labels.create(name, color)); 160 } 161 return label; 162 } 163 @Override 164 public Repo repo() { 165 return this.labels.repo(); 166 } 167 @Override 168 public Label create( 169 final String name, final String color 170 ) throws IOException { 171 return this.labels.create(name, color); 172 } 173 @Override 174 public Label get( 175 final String name 176 ) { 177 return this.labels.get(name); 178 } 179 @Override 180 public Iterable<Label> iterate() { 181 return this.labels.iterate(); 182 } 183 @Override 184 public void delete( 185 final String name 186 ) throws IOException { 187 this.labels.delete(name); 188 } 189 } 190 191 }