Implemented label CRUD operations on GHRepository

Fixes issue #105
This commit is contained in:
Kohsuke Kawaguchi
2015-02-15 06:55:35 -08:00
parent 1dbcc4b776
commit d90adfa98e
6 changed files with 101 additions and 21 deletions

View File

@@ -52,29 +52,17 @@ public class GHIssue extends GHObject {
protected String closed_at;
protected int comments;
protected String body;
protected List<Label> labels;
protected List<GHLabel> labels;
protected GHUser user;
protected String title, html_url;
protected GHIssue.PullRequest pull_request;
protected GHMilestone milestone;
protected GHUser closed_by;
public static class Label {
private String url;
private String name;
private String color;
public String getUrl() {
return url;
}
public String getName() {
return name;
}
public String getColor() {
return color;
}
/**
* @deprecated use {@link GHLabel}
*/
public static class Label extends GHLabel {
}
/*package*/ GHIssue wrap(GHRepository owner) {
@@ -134,9 +122,9 @@ public class GHIssue extends GHObject {
return Enum.valueOf(GHIssueState.class, state.toUpperCase(Locale.ENGLISH));
}
public Collection<Label> getLabels() throws IOException {
public Collection<GHLabel> getLabels() throws IOException {
if(labels == null){
return Collections.EMPTY_LIST;
return Collections.emptyList();
}
return Collections.unmodifiableList(labels);
}

View File

@@ -0,0 +1,37 @@
package org.kohsuke.github;
import java.io.IOException;
/**
* @author Kohsuke Kawaguchi
* @see GHIssue#getLabels()
* @see GHRepository#listLabels()
*/
public class GHLabel {
private String url, name, color;
private GHRepository repo;
public String getUrl() {
return url;
}
public String getName() {
return name;
}
/**
* Color code without leading '#', such as 'f29513'
*/
public String getColor() {
return color;
}
/*package*/ GHLabel wrapUp(GHRepository repo) {
this.repo = repo;
return this;
}
public void delete() throws IOException {
repo.root.retrieve().method("DELETE").to(url);
}
}

View File

@@ -127,7 +127,7 @@ public class GHPullRequest extends GHIssue {
}
@Override
public Collection<Label> getLabels() throws IOException {
public Collection<GHLabel> getLabels() throws IOException {
fetchIssue();
return super.getLabels();
}

View File

@@ -760,6 +760,36 @@ public class GHRepository extends GHObject {
};
}
/**
* Lists labels in this repository.
*
* https://developer.github.com/v3/issues/labels/#list-all-labels-for-this-repository
*/
public PagedIterable<GHLabel> listLabels() throws IOException {
return new PagedIterable<GHLabel>() {
public PagedIterator<GHLabel> iterator() {
return new PagedIterator<GHLabel>(root.retrieve().asIterator(getApiTailUrl("labels"), GHLabel[].class)) {
@Override
protected void wrapUp(GHLabel[] page) {
for (GHLabel c : page)
c.wrapUp(GHRepository.this);
}
};
}
};
}
public GHLabel getLabel(String name) throws IOException {
return root.retrieve().to(getApiTailUrl("labels/"+name), GHLabel.class).wrapUp(this);
}
public GHLabel createLabel(String name, String color) throws IOException {
return root.retrieve().method("POST")
.with("name",name)
.with("color",color)
.to(getApiTailUrl("labels"), GHLabel.class).wrapUp(this);
}
/**
*
* See https://api.github.com/hooks for possible names and their configuration scheme.

View File

@@ -12,6 +12,7 @@ import java.io.IOException;
import java.net.URL;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Pattern;
/**
* Unit test for simple App.
@@ -665,6 +666,30 @@ public class AppTest extends AbstractGitHubApiTestBase {
assertEquals(readme.getContent(),"This is a markdown readme.\n");
}
@Test
public void testRepoLabel() throws IOException {
GHRepository r = gitHub.getRepository("github-api-test-org/test-labels");
List<GHLabel> lst = r.listLabels().asList();
for (GHLabel l : lst) {
System.out.println(l.getName());
}
assertTrue(lst.size() > 5);
GHLabel e = r.getLabel("enhancement");
assertEquals("enhancement",e.getName());
assertNotNull(e.getUrl());
assertTrue(Pattern.matches("[0-9a-fA-F]{6}",e.getColor()));
{// CRUD
GHLabel t = r.createLabel("test", "123456");
GHLabel t2 = r.getLabel("test");
assertEquals(t.getName(), t2.getName());
assertEquals(t.getColor(), "123456");
assertEquals(t.getColor(), t2.getColor());
assertEquals(t.getUrl(), t2.getUrl());
t.delete();
}
}
private void kohsuke() {
String login = getUser().getLogin();
Assume.assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));

View File

@@ -24,7 +24,7 @@ public class PullRequestTest extends AbstractGitHubApiTestBase {
String label = rnd.next();
p.setLabels(label);
Collection<GHIssue.Label> labels = getRepository().getPullRequest(p.getNumber()).getLabels();
Collection<GHLabel> labels = getRepository().getPullRequest(p.getNumber()).getLabels();
assertEquals(1, labels.size());
assertEquals(label, labels.iterator().next().getName());
}