|
30 | 30 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
31 | 31 | import java.io.IOException; |
32 | 32 | import java.net.URL; |
| 33 | +import java.util.ArrayList; |
33 | 34 | import java.util.Arrays; |
34 | 35 | import java.util.Collection; |
35 | 36 | import java.util.Collections; |
@@ -218,70 +219,64 @@ public void setLabels(String... labels) throws IOException { |
218 | 219 | } |
219 | 220 |
|
220 | 221 | /** |
221 | | - * Adds a label to the issue. If it does not exist in the {@link GHRepository}, it will be |
222 | | - * created with the specified color. If either {@code name} or {@code color} is null, it will |
223 | | - * be a <a href="https://en.wikipedia.org/wiki/NOP">NOP</a>. |
| 222 | + * Adds labels to the issue. |
224 | 223 | * |
225 | | - * @param name Name of the label |
226 | | - * @param color Hex code of the label, without # |
| 224 | + * @param names Names of the label |
227 | 225 | */ |
228 | | - public void addLabel(String name, String color) throws IOException { |
229 | | - if (name != null && color != null) { |
230 | | - // All labels that we have seen |
231 | | - Set<String> repoLabels = new HashSet<String>(); |
232 | | - Set<String> seenLabels = new HashSet<String>(); |
233 | | - |
234 | | - // Check if label already exists |
235 | | - for (GHLabel repoLabel : getRepository().listLabels().asSet()) { |
236 | | - repoLabels.add(repoLabel.getName()); |
237 | | - } |
| 226 | + public void addLabels(String... names) throws IOException { |
| 227 | + _addLabels(Arrays.asList(names)); |
| 228 | + } |
238 | 229 |
|
239 | | - // Label does not exist |
240 | | - if (!repoLabels.contains(name)) { |
241 | | - GHLabel newLabel = getRepository().createLabel(name, color); |
242 | | - repoLabels.add(newLabel.getName()); |
243 | | - seenLabels.add(newLabel.getName()); |
244 | | - } |
| 230 | + public void addLabels(GHLabel... labels) throws IOException { |
| 231 | + addLabels(Arrays.asList(labels)); |
| 232 | + } |
245 | 233 |
|
246 | | - // See if label exists on issue already |
247 | | - boolean foundInIssue = false; |
| 234 | + public void addLabels(Collection<GHLabel> labels) throws IOException { |
| 235 | + _addLabels(GHLabel.toNames(labels)); |
| 236 | + } |
248 | 237 |
|
249 | | - for (GHLabel existingIssueLabel : getLabels()) { |
250 | | - if (existingIssueLabel.getName().equalsIgnoreCase(name)) { |
251 | | - foundInIssue = true; |
252 | | - } |
| 238 | + private void _addLabels(Collection<String> names) throws IOException { |
| 239 | + List<String> newLabels = new ArrayList<String>(); |
253 | 240 |
|
254 | | - seenLabels.add(existingIssueLabel.getName()); |
| 241 | + for (GHLabel label : getLabels()) { |
| 242 | + newLabels.add(label.getName()); |
255 | 243 | } |
256 | | - |
257 | | - // If the label doesn't exist in the issue, add it |
258 | | - if (!foundInIssue) { |
259 | | - seenLabels.add(name); |
260 | | - setLabels(seenLabels.toArray(new String[0])); |
| 244 | + for (String name : names) { |
| 245 | + if (!newLabels.contains(name)) { |
| 246 | + newLabels.add(name); |
| 247 | + } |
261 | 248 | } |
262 | | - } |
| 249 | + setLabels(newLabels.toArray(new String[0])); |
263 | 250 | } |
264 | 251 |
|
265 | 252 | /** |
266 | | - * @see #removeLabel(String) |
| 253 | + * Remove a given label by name from this issue. |
267 | 254 | */ |
268 | | - public void removeLabel(Label label) throws IOException { |
269 | | - removeLabel(label.getName()); |
| 255 | + public void removeLabels(String... names) throws IOException { |
| 256 | + _removeLabels(Arrays.asList(names)); |
270 | 257 | } |
271 | 258 |
|
272 | 259 | /** |
273 | | - * Remove a given label by name from this issue. |
| 260 | + * @see #removeLabels(String...) |
274 | 261 | */ |
275 | | - public void removeLabel(String name) throws IOException { |
276 | | - Set<String> newLabels = new HashSet<String>(); |
| 262 | + public void removeLabels(GHLabel... labels) throws IOException { |
| 263 | + removeLabels(Arrays.asList(labels)); |
| 264 | + } |
277 | 265 |
|
278 | | - for (Label existingLabel : labels) { |
279 | | - if (!existingLabel.getName().equalsIgnoreCase(name)) { |
280 | | - newLabels.add(existingLabel.getName()); |
| 266 | + public void removeLabels(Collection<GHLabel> labels) throws IOException { |
| 267 | + _removeLabels(GHLabel.toNames(labels)); |
| 268 | + } |
| 269 | + |
| 270 | + private void _removeLabels(Collection<String> names) throws IOException { |
| 271 | + List<String> newLabels = new ArrayList<String>(); |
| 272 | + |
| 273 | + for (GHLabel l : getLabels()) { |
| 274 | + if (!names.contains(l.getName())) { |
| 275 | + newLabels.add(l.getName()); |
| 276 | + } |
281 | 277 | } |
282 | | - } |
283 | 278 |
|
284 | | - setLabels(newLabels.toArray(new String[0])); |
| 279 | + setLabels(newLabels.toArray(new String[0])); |
285 | 280 | } |
286 | 281 |
|
287 | 282 | /** |
|
0 commit comments