Skip to content

Commit 9a1bb09

Browse files
committed
Massaging the changes.
In particular, avoid the kind of addLabel() method that has lots of side effect and do multiple things.
1 parent 1c4b716 commit 9a1bb09

File tree

2 files changed

+51
-45
lines changed

2 files changed

+51
-45
lines changed

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

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
3131
import java.io.IOException;
3232
import java.net.URL;
33+
import java.util.ArrayList;
3334
import java.util.Arrays;
3435
import java.util.Collection;
3536
import java.util.Collections;
@@ -218,70 +219,64 @@ public void setLabels(String... labels) throws IOException {
218219
}
219220

220221
/**
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.
224223
*
225-
* @param name Name of the label
226-
* @param color Hex code of the label, without #
224+
* @param names Names of the label
227225
*/
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+
}
238229

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+
}
245233

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+
}
248237

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>();
253240

254-
seenLabels.add(existingIssueLabel.getName());
241+
for (GHLabel label : getLabels()) {
242+
newLabels.add(label.getName());
255243
}
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+
}
261248
}
262-
}
249+
setLabels(newLabels.toArray(new String[0]));
263250
}
264251

265252
/**
266-
* @see #removeLabel(String)
253+
* Remove a given label by name from this issue.
267254
*/
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));
270257
}
271258

272259
/**
273-
* Remove a given label by name from this issue.
260+
* @see #removeLabels(String...)
274261
*/
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+
}
277265

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+
}
281277
}
282-
}
283278

284-
setLabels(newLabels.toArray(new String[0]));
279+
setLabels(newLabels.toArray(new String[0]));
285280
}
286281

287282
/**

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)