Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit d90adfa

Browse files
committed
Implemented label CRUD operations on GHRepository
Fixes issue hub4j#105
1 parent 1dbcc4b commit d90adfa

File tree

6 files changed

+101
-21
lines changed

6 files changed

+101
-21
lines changed

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

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,17 @@ public class GHIssue extends GHObject {
5252
protected String closed_at;
5353
protected int comments;
5454
protected String body;
55-
protected List<Label> labels;
55+
protected List<GHLabel> labels;
5656
protected GHUser user;
5757
protected String title, html_url;
5858
protected GHIssue.PullRequest pull_request;
5959
protected GHMilestone milestone;
6060
protected GHUser closed_by;
6161

62-
public static class Label {
63-
private String url;
64-
private String name;
65-
private String color;
66-
67-
public String getUrl() {
68-
return url;
69-
}
70-
71-
public String getName() {
72-
return name;
73-
}
74-
75-
public String getColor() {
76-
return color;
77-
}
62+
/**
63+
* @deprecated use {@link GHLabel}
64+
*/
65+
public static class Label extends GHLabel {
7866
}
7967

8068
/*package*/ GHIssue wrap(GHRepository owner) {
@@ -134,9 +122,9 @@ public GHIssueState getState() {
134122
return Enum.valueOf(GHIssueState.class, state.toUpperCase(Locale.ENGLISH));
135123
}
136124

137-
public Collection<Label> getLabels() throws IOException {
125+
public Collection<GHLabel> getLabels() throws IOException {
138126
if(labels == null){
139-
return Collections.EMPTY_LIST;
127+
return Collections.emptyList();
140128
}
141129
return Collections.unmodifiableList(labels);
142130
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.kohsuke.github;
2+
3+
import java.io.IOException;
4+
5+
/**
6+
* @author Kohsuke Kawaguchi
7+
* @see GHIssue#getLabels()
8+
* @see GHRepository#listLabels()
9+
*/
10+
public class GHLabel {
11+
private String url, name, color;
12+
private GHRepository repo;
13+
14+
public String getUrl() {
15+
return url;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
/**
23+
* Color code without leading '#', such as 'f29513'
24+
*/
25+
public String getColor() {
26+
return color;
27+
}
28+
29+
/*package*/ GHLabel wrapUp(GHRepository repo) {
30+
this.repo = repo;
31+
return this;
32+
}
33+
34+
public void delete() throws IOException {
35+
repo.root.retrieve().method("DELETE").to(url);
36+
}
37+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public Date getMergedAt() {
127127
}
128128

129129
@Override
130-
public Collection<Label> getLabels() throws IOException {
130+
public Collection<GHLabel> getLabels() throws IOException {
131131
fetchIssue();
132132
return super.getLabels();
133133
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,36 @@ protected void wrapUp(GHEventInfo[] page) {
760760
};
761761
}
762762

763+
/**
764+
* Lists labels in this repository.
765+
*
766+
* https://developer.github.com/v3/issues/labels/#list-all-labels-for-this-repository
767+
*/
768+
public PagedIterable<GHLabel> listLabels() throws IOException {
769+
return new PagedIterable<GHLabel>() {
770+
public PagedIterator<GHLabel> iterator() {
771+
return new PagedIterator<GHLabel>(root.retrieve().asIterator(getApiTailUrl("labels"), GHLabel[].class)) {
772+
@Override
773+
protected void wrapUp(GHLabel[] page) {
774+
for (GHLabel c : page)
775+
c.wrapUp(GHRepository.this);
776+
}
777+
};
778+
}
779+
};
780+
}
781+
782+
public GHLabel getLabel(String name) throws IOException {
783+
return root.retrieve().to(getApiTailUrl("labels/"+name), GHLabel.class).wrapUp(this);
784+
}
785+
786+
public GHLabel createLabel(String name, String color) throws IOException {
787+
return root.retrieve().method("POST")
788+
.with("name",name)
789+
.with("color",color)
790+
.to(getApiTailUrl("labels"), GHLabel.class).wrapUp(this);
791+
}
792+
763793
/**
764794
*
765795
* See https://api.github.com/hooks for possible names and their configuration scheme.

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.net.URL;
1313
import java.util.*;
1414
import java.util.Map.Entry;
15+
import java.util.regex.Pattern;
1516

1617
/**
1718
* Unit test for simple App.
@@ -665,6 +666,30 @@ public void testReadme() throws IOException {
665666
assertEquals(readme.getContent(),"This is a markdown readme.\n");
666667
}
667668

669+
@Test
670+
public void testRepoLabel() throws IOException {
671+
GHRepository r = gitHub.getRepository("github-api-test-org/test-labels");
672+
List<GHLabel> lst = r.listLabels().asList();
673+
for (GHLabel l : lst) {
674+
System.out.println(l.getName());
675+
}
676+
assertTrue(lst.size() > 5);
677+
GHLabel e = r.getLabel("enhancement");
678+
assertEquals("enhancement",e.getName());
679+
assertNotNull(e.getUrl());
680+
assertTrue(Pattern.matches("[0-9a-fA-F]{6}",e.getColor()));
681+
682+
{// CRUD
683+
GHLabel t = r.createLabel("test", "123456");
684+
GHLabel t2 = r.getLabel("test");
685+
assertEquals(t.getName(), t2.getName());
686+
assertEquals(t.getColor(), "123456");
687+
assertEquals(t.getColor(), t2.getColor());
688+
assertEquals(t.getUrl(), t2.getUrl());
689+
t.delete();
690+
}
691+
}
692+
668693
private void kohsuke() {
669694
String login = getUser().getLogin();
670695
Assume.assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void setLabels() throws Exception {
2424
String label = rnd.next();
2525
p.setLabels(label);
2626

27-
Collection<GHIssue.Label> labels = getRepository().getPullRequest(p.getNumber()).getLabels();
27+
Collection<GHLabel> labels = getRepository().getPullRequest(p.getNumber()).getLabels();
2828
assertEquals(1, labels.size());
2929
assertEquals(label, labels.iterator().next().getName());
3030
}

0 commit comments

Comments
 (0)