Skip to content

Commit

Permalink
Implemented label CRUD operations on GHRepository
Browse files Browse the repository at this point in the history
Fixes issue #105
  • Loading branch information
kohsuke committed Feb 15, 2015
1 parent 1dbcc4b commit d90adfa
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 21 deletions.
26 changes: 7 additions & 19 deletions src/main/java/org/kohsuke/github/GHIssue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

This comment has been minimized.

Copy link
@KostyaSha

KostyaSha Mar 2, 2015

Contributor

Maybe GHLabel extends Label? :)

This comment has been minimized.

Copy link
@KostyaSha

KostyaSha Mar 2, 2015

Contributor

Ah, no, then something other is wrong...

}

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

public Collection<Label> getLabels() throws IOException {
public Collection<GHLabel> getLabels() throws IOException {

This comment has been minimized.

Copy link
@KostyaSha

KostyaSha Mar 2, 2015

Contributor

This breaks backward compatibility of other plugins that use this method because they expect Label and not GHLabel

This comment has been minimized.

Copy link
@kohsuke

kohsuke Mar 2, 2015

Author Collaborator

oh crap, I think line 55 needs to remain List<Label>, right?

This comment has been minimized.

Copy link
@KostyaSha

KostyaSha Mar 2, 2015

Contributor

maybe keep public Collection<Label> getLabels() throws IOException { but deprecate and wrap to
public Collection<GHLabel> getLabels() ?

This comment has been minimized.

Copy link
@kohsuke

kohsuke Mar 2, 2015

Author Collaborator

I claim the fix in 1156689 would do.

This comment has been minimized.

Copy link
@KostyaSha

KostyaSha via email Mar 2, 2015

Contributor

This comment has been minimized.

Copy link
@kohsuke

kohsuke Mar 2, 2015

Author Collaborator

As I noted elsewhere, I'm willing to break the source compatibility for cases like this.

This comment has been minimized.

Copy link
@KostyaSha

KostyaSha Mar 2, 2015

Contributor

Seems you already fixed it.

if(labels == null){
return Collections.EMPTY_LIST;
return Collections.emptyList();
}
return Collections.unmodifiableList(labels);
}
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/org/kohsuke/github/GHLabel.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/kohsuke/github/GHPullRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public Date getMergedAt() {
}

@Override
public Collection<Label> getLabels() throws IOException {
public Collection<GHLabel> getLabels() throws IOException {

This comment has been minimized.

Copy link
@KostyaSha

KostyaSha Feb 15, 2015

Contributor

Not sure if it fixed after all changes, but getLabels() doesn't return any labels for PR. While getIssue for the same number do. Do you really need to have labels in PR object?

fetchIssue();
return super.getLabels();
}
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,36 @@ protected void wrapUp(GHEventInfo[] page) {
};
}

/**
* 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.
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/org/kohsuke/github/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.net.URL;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Pattern;

/**
* Unit test for simple App.
Expand Down Expand Up @@ -665,6 +666,30 @@ public void testReadme() throws IOException {
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"));
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/kohsuke/github/PullRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void setLabels() throws Exception {
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());
}
Expand Down

0 comments on commit d90adfa

Please sign in to comment.