Skip to content

Commit a59ee6a

Browse files
committed
Add removeLabel() that throws when label missing
1 parent 1fefc77 commit a59ee6a

40 files changed

Lines changed: 769 additions & 543 deletions

File tree

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

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public void assignTo(GHUser user) throws IOException {
312312
}
313313

314314
/**
315-
* Sets labels.
315+
* Sets labels on the target to a specific list.
316316
*
317317
* @param labels
318318
* the labels
@@ -326,6 +326,8 @@ public void setLabels(String... labels) throws IOException {
326326
/**
327327
* Adds labels to the issue.
328328
*
329+
* Labels that are already present on the target are ignored.
330+
*
329331
* @param names
330332
* Names of the label
331333
* @throws IOException
@@ -338,6 +340,8 @@ public void addLabels(String... names) throws IOException {
338340
/**
339341
* Add labels.
340342
*
343+
* Labels that are already present on the target are ignored.
344+
*
341345
* @param labels
342346
* the labels
343347
* @throws IOException
@@ -350,6 +354,8 @@ public void addLabels(GHLabel... labels) throws IOException {
350354
/**
351355
* Add labels.
352356
*
357+
* Labels that are already present on the target are ignored.
358+
*
353359
* @param labels
354360
* the labels
355361
* @throws IOException
@@ -364,7 +370,23 @@ private void _addLabels(Collection<String> names) throws IOException {
364370
}
365371

366372
/**
367-
* Remove a given label by name from this issue.
373+
* Remove a single label.
374+
*
375+
* Attempting to remove a label that is not present throws {@link GHFileNotFoundException}.
376+
*
377+
* @param name
378+
* the name
379+
* @throws IOException
380+
* the io exception, throws {@link GHFileNotFoundException} if label was not present.
381+
*/
382+
public void removeLabel(String name) throws IOException {
383+
root.createRequest().method("DELETE").withUrlPath(getIssuesApiRoute() + "/labels", name).send();
384+
}
385+
386+
/**
387+
* Remove a collection of labels.
388+
*
389+
* Attempting to remove labels that are not present on the target are ignored.
368390
*
369391
* @param names
370392
* the names
@@ -376,7 +398,9 @@ public void removeLabels(String... names) throws IOException {
376398
}
377399

378400
/**
379-
* Remove labels.
401+
* Remove a collection of labels.
402+
*
403+
* Attempting to remove labels that are not present on the target are ignored.
380404
*
381405
* @param labels
382406
* the labels
@@ -389,7 +413,9 @@ public void removeLabels(GHLabel... labels) throws IOException {
389413
}
390414

391415
/**
392-
* Remove labels.
416+
* Remove a collection of labels.
417+
*
418+
* Attempting to remove labels that are not present on the target are ignored.
393419
*
394420
* @param labels
395421
* the labels
@@ -402,7 +428,11 @@ public void removeLabels(Collection<GHLabel> labels) throws IOException {
402428

403429
private void _removeLabels(Collection<String> names) throws IOException {
404430
for (String name : names) {
405-
root.createRequest().method("DELETE").withUrlPath(getIssuesApiRoute() + "/labels", name).send();
431+
try {
432+
removeLabel(name);
433+
} catch (GHFileNotFoundException e) {
434+
// when trying to remove multiple labels, we ignore already removed
435+
}
406436
}
407437
}
408438

src/test/java/org/kohsuke/github/GHPullRequestTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,14 +430,21 @@ public void addLabels() throws Exception {
430430
String addedLabel3 = "addLabels_label_name_3";
431431

432432
p.addLabels(addedLabel1);
433+
434+
int requestCount = mockGitHub.getRequestCount();
433435
p.addLabels(addedLabel2, addedLabel3);
436+
// multiple labels can be added with one api call
437+
assertThat(mockGitHub.getRequestCount(), equalTo(requestCount + 1));
434438

435439
Collection<GHLabel> labels = getRepository().getPullRequest(p.getNumber()).getLabels();
436440
assertEquals(3, labels.size());
437441
assertThat(labels,
438442
containsInAnyOrder(hasProperty("name", equalTo(addedLabel1)),
439443
hasProperty("name", equalTo(addedLabel2)),
440444
hasProperty("name", equalTo(addedLabel3))));
445+
446+
// Adding a label which is already present does not throw an error
447+
p.addLabels(addedLabel1);
441448
}
442449

443450
@Test
@@ -474,11 +481,26 @@ public void removeLabels() throws Exception {
474481
Collection<GHLabel> labels = getRepository().getPullRequest(p.getNumber()).getLabels();
475482
assertEquals(3, labels.size());
476483

484+
int requestCount = mockGitHub.getRequestCount();
477485
p.removeLabels(label2, label3);
486+
// each label deleted is a separate api call
487+
assertThat(mockGitHub.getRequestCount(), equalTo(requestCount + 2));
478488

479489
labels = getRepository().getPullRequest(p.getNumber()).getLabels();
480490
assertEquals(1, labels.size());
481491
assertEquals(label1, labels.iterator().next().getName());
492+
493+
// Removing some labels that are not present does not throw
494+
// This is consistent with earlier behavior and with addLabels()
495+
p.removeLabels(label3);
496+
497+
// Calling removeLabel() on label that is not present will throw
498+
try {
499+
p.removeLabel(label3);
500+
fail("Expected GHFileNotFoundException");
501+
} catch (GHFileNotFoundException e) {
502+
assertThat(e.getMessage(), containsString("Label does not exist"));
503+
}
482504
}
483505

484506
@Test

src/test/resources/org/kohsuke/github/GHPullRequestTest/wiremock/addLabels/__files/repos_hub4j-test-org_github-api-3.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"deployments_url": "https://api.github.com/repos/hub4j-test-org/github-api/deployments",
6767
"created_at": "2019-09-06T23:26:04Z",
6868
"updated_at": "2021-01-22T03:50:37Z",
69-
"pushed_at": "2021-03-10T12:52:04Z",
69+
"pushed_at": "2021-03-13T01:53:25Z",
7070
"git_url": "git://github.com/hub4j-test-org/github-api.git",
7171
"ssh_url": "git@github.com:hub4j-test-org/github-api.git",
7272
"clone_url": "https://github.com/hub4j-test-org/github-api.git",
@@ -195,7 +195,7 @@
195195
"deployments_url": "https://api.github.com/repos/hub4j/github-api/deployments",
196196
"created_at": "2010-04-19T04:13:03Z",
197197
"updated_at": "2021-03-10T10:27:30Z",
198-
"pushed_at": "2021-03-10T02:45:39Z",
198+
"pushed_at": "2021-03-10T13:03:30Z",
199199
"git_url": "git://github.com/hub4j/github-api.git",
200200
"ssh_url": "git@github.com:hub4j/github-api.git",
201201
"clone_url": "https://github.com/hub4j/github-api.git",
@@ -210,7 +210,7 @@
210210
"has_downloads": true,
211211
"has_wiki": true,
212212
"has_pages": true,
213-
"forks_count": 529,
213+
"forks_count": 530,
214214
"mirror_url": null,
215215
"archived": false,
216216
"disabled": false,
@@ -222,7 +222,7 @@
222222
"url": "https://api.github.com/licenses/mit",
223223
"node_id": "MDc6TGljZW5zZTEz"
224224
},
225-
"forks": 529,
225+
"forks": 530,
226226
"open_issues": 77,
227227
"watchers": 740,
228228
"default_branch": "master"
@@ -295,7 +295,7 @@
295295
"deployments_url": "https://api.github.com/repos/hub4j/github-api/deployments",
296296
"created_at": "2010-04-19T04:13:03Z",
297297
"updated_at": "2021-03-10T10:27:30Z",
298-
"pushed_at": "2021-03-10T02:45:39Z",
298+
"pushed_at": "2021-03-10T13:03:30Z",
299299
"git_url": "git://github.com/hub4j/github-api.git",
300300
"ssh_url": "git@github.com:hub4j/github-api.git",
301301
"clone_url": "https://github.com/hub4j/github-api.git",
@@ -310,7 +310,7 @@
310310
"has_downloads": true,
311311
"has_wiki": true,
312312
"has_pages": true,
313-
"forks_count": 529,
313+
"forks_count": 530,
314314
"mirror_url": null,
315315
"archived": false,
316316
"disabled": false,
@@ -322,11 +322,11 @@
322322
"url": "https://api.github.com/licenses/mit",
323323
"node_id": "MDc6TGljZW5zZTEz"
324324
},
325-
"forks": 529,
325+
"forks": 530,
326326
"open_issues": 77,
327327
"watchers": 740,
328328
"default_branch": "master"
329329
},
330-
"network_count": 529,
330+
"network_count": 530,
331331
"subscribers_count": 0
332332
}

src/test/resources/org/kohsuke/github/GHPullRequestTest/wiremock/addLabels/__files/repos_hub4j-test-org_github-api-7.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"deployments_url": "https://api.github.com/repos/hub4j-test-org/github-api/deployments",
6767
"created_at": "2019-09-06T23:26:04Z",
6868
"updated_at": "2021-01-22T03:50:37Z",
69-
"pushed_at": "2021-03-10T12:52:16Z",
69+
"pushed_at": "2021-03-13T01:53:58Z",
7070
"git_url": "git://github.com/hub4j-test-org/github-api.git",
7171
"ssh_url": "git@github.com:hub4j-test-org/github-api.git",
7272
"clone_url": "https://github.com/hub4j-test-org/github-api.git",
@@ -195,7 +195,7 @@
195195
"deployments_url": "https://api.github.com/repos/hub4j/github-api/deployments",
196196
"created_at": "2010-04-19T04:13:03Z",
197197
"updated_at": "2021-03-10T10:27:30Z",
198-
"pushed_at": "2021-03-10T02:45:39Z",
198+
"pushed_at": "2021-03-10T13:03:30Z",
199199
"git_url": "git://github.com/hub4j/github-api.git",
200200
"ssh_url": "git@github.com:hub4j/github-api.git",
201201
"clone_url": "https://github.com/hub4j/github-api.git",
@@ -210,7 +210,7 @@
210210
"has_downloads": true,
211211
"has_wiki": true,
212212
"has_pages": true,
213-
"forks_count": 529,
213+
"forks_count": 530,
214214
"mirror_url": null,
215215
"archived": false,
216216
"disabled": false,
@@ -222,7 +222,7 @@
222222
"url": "https://api.github.com/licenses/mit",
223223
"node_id": "MDc6TGljZW5zZTEz"
224224
},
225-
"forks": 529,
225+
"forks": 530,
226226
"open_issues": 77,
227227
"watchers": 740,
228228
"default_branch": "master"
@@ -295,7 +295,7 @@
295295
"deployments_url": "https://api.github.com/repos/hub4j/github-api/deployments",
296296
"created_at": "2010-04-19T04:13:03Z",
297297
"updated_at": "2021-03-10T10:27:30Z",
298-
"pushed_at": "2021-03-10T02:45:39Z",
298+
"pushed_at": "2021-03-10T13:03:30Z",
299299
"git_url": "git://github.com/hub4j/github-api.git",
300300
"ssh_url": "git@github.com:hub4j/github-api.git",
301301
"clone_url": "https://github.com/hub4j/github-api.git",
@@ -310,7 +310,7 @@
310310
"has_downloads": true,
311311
"has_wiki": true,
312312
"has_pages": true,
313-
"forks_count": 529,
313+
"forks_count": 530,
314314
"mirror_url": null,
315315
"archived": false,
316316
"disabled": false,
@@ -322,11 +322,11 @@
322322
"url": "https://api.github.com/licenses/mit",
323323
"node_id": "MDc6TGljZW5zZTEz"
324324
},
325-
"forks": 529,
325+
"forks": 530,
326326
"open_issues": 77,
327327
"watchers": 740,
328328
"default_branch": "master"
329329
},
330-
"network_count": 529,
330+
"network_count": 530,
331331
"subscribers_count": 0
332332
}

src/test/resources/org/kohsuke/github/GHPullRequestTest/wiremock/addLabels/__files/repos_hub4j-test-org_github-api_issues_416_labels-6.json renamed to src/test/resources/org/kohsuke/github/GHPullRequestTest/wiremock/addLabels/__files/repos_hub4j-test-org_github-api_issues_427_labels-6.json

File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[
2+
{
3+
"id": 2806272360,
4+
"node_id": "MDU6TGFiZWwyODA2MjcyMzYw",
5+
"url": "https://api.github.com/repos/hub4j-test-org/github-api/labels/addLabels_label_name_1",
6+
"name": "addLabels_label_name_1",
7+
"color": "ededed",
8+
"default": false,
9+
"description": null
10+
},
11+
{
12+
"id": 2806272397,
13+
"node_id": "MDU6TGFiZWwyODA2MjcyMzk3",
14+
"url": "https://api.github.com/repos/hub4j-test-org/github-api/labels/addLabels_label_name_2",
15+
"name": "addLabels_label_name_2",
16+
"color": "ededed",
17+
"default": false,
18+
"description": null
19+
},
20+
{
21+
"id": 2809132787,
22+
"node_id": "MDU6TGFiZWwyODA5MTMyNzg3",
23+
"url": "https://api.github.com/repos/hub4j-test-org/github-api/labels/addLabels_label_name_3",
24+
"name": "addLabels_label_name_3",
25+
"color": "ededed",
26+
"default": false,
27+
"description": null
28+
}
29+
]

0 commit comments

Comments
 (0)