Skip to content

Commit 24b998b

Browse files
committed
Merge pull request #461
2 parents a6f3e7d + 9a1bb09 commit 24b998b

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

src/main/java/org/kohsuke/github/GHIssue.java

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@
2424

2525
package org.kohsuke.github;
2626

27+
import static org.kohsuke.github.Previews.SQUIRREL_GIRL;
28+
2729
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
2830
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
29-
3031
import java.io.IOException;
3132
import java.net.URL;
33+
import java.util.ArrayList;
3234
import java.util.Arrays;
3335
import java.util.Collection;
3436
import java.util.Collections;
3537
import java.util.Date;
38+
import java.util.HashSet;
3639
import java.util.List;
3740
import java.util.Locale;
38-
39-
import static org.kohsuke.github.Previews.*;
41+
import java.util.Set;
4042

4143
/**
4244
* Represents an issue on GitHub.
@@ -216,6 +218,67 @@ public void setLabels(String... labels) throws IOException {
216218
editIssue("labels",labels);
217219
}
218220

221+
/**
222+
* Adds labels to the issue.
223+
*
224+
* @param names Names of the label
225+
*/
226+
public void addLabels(String... names) throws IOException {
227+
_addLabels(Arrays.asList(names));
228+
}
229+
230+
public void addLabels(GHLabel... labels) throws IOException {
231+
addLabels(Arrays.asList(labels));
232+
}
233+
234+
public void addLabels(Collection<GHLabel> labels) throws IOException {
235+
_addLabels(GHLabel.toNames(labels));
236+
}
237+
238+
private void _addLabels(Collection<String> names) throws IOException {
239+
List<String> newLabels = new ArrayList<String>();
240+
241+
for (GHLabel label : getLabels()) {
242+
newLabels.add(label.getName());
243+
}
244+
for (String name : names) {
245+
if (!newLabels.contains(name)) {
246+
newLabels.add(name);
247+
}
248+
}
249+
setLabels(newLabels.toArray(new String[0]));
250+
}
251+
252+
/**
253+
* Remove a given label by name from this issue.
254+
*/
255+
public void removeLabels(String... names) throws IOException {
256+
_removeLabels(Arrays.asList(names));
257+
}
258+
259+
/**
260+
* @see #removeLabels(String...)
261+
*/
262+
public void removeLabels(GHLabel... labels) throws IOException {
263+
removeLabels(Arrays.asList(labels));
264+
}
265+
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+
}
277+
}
278+
279+
setLabels(newLabels.toArray(new String[0]));
280+
}
281+
219282
/**
220283
* Obtains all the comments associated with this issue.
221284
*

src/main/java/org/kohsuke/github/GHLabel.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.kohsuke.github;
22

33
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.Collection;
6+
import java.util.List;
47

58
/**
69
* @author Kohsuke Kawaguchi
@@ -42,4 +45,12 @@ public void delete() throws IOException {
4245
public void setColor(String newColor) throws IOException {
4346
repo.root.retrieve().method("PATCH").with("name", name).with("color", newColor).to(url);
4447
}
48+
49+
/*package*/ static Collection<String> toNames(Collection<GHLabel> labels) {
50+
List<String> r = new ArrayList<String>();
51+
for (GHLabel l : labels) {
52+
r.add(l.getName());
53+
}
54+
return r;
55+
}
4556
}

0 commit comments

Comments
 (0)