Skip to content

Commit 7f15f12

Browse files
committed
completed code to create a new issue
1 parent e53e62b commit 7f15f12

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed
Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,59 @@
11
package org.kohsuke.github;
22

33
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.List;
46

57
/**
68
* @author Kohsuke Kawaguchi
79
*/
810
public class GHIssueBuilder {
911
private final GHRepository repo;
1012
private final Requester builder;
13+
private List<String> labels = new ArrayList<String>();
1114

1215
GHIssueBuilder(GHRepository repo, String title) {
1316
this.repo = repo;
1417
this.builder = new Requester(repo.root);
1518
builder.with("title",title);
1619
}
1720

21+
/**
22+
* Sets the main text of an issue, which is arbitrary multi-line text.
23+
*/
1824
public GHIssueBuilder body(String str) {
1925
builder.with("body",str);
2026
return this;
2127
}
2228

29+
public GHIssueBuilder assignee(GHUser user) {
30+
if (user!=null)
31+
builder.with("assignee",user.getLogin());
32+
return this;
33+
}
34+
35+
public GHIssueBuilder assignee(String user) {
36+
if (user!=null)
37+
builder.with("assignee",user);
38+
return this;
39+
}
40+
41+
public GHIssueBuilder milestone(GHMilestone milestone) {
42+
if (milestone!=null)
43+
builder.with("milestone",milestone.getNumber());
44+
return this;
45+
}
46+
47+
public GHIssueBuilder label(String label) {
48+
if (label!=null)
49+
labels.add(label);
50+
return this;
51+
}
52+
2353
/**
2454
* Creates a new issue.
2555
*/
2656
public GHIssue create() throws IOException {
27-
return builder.to("/repos/"+repo.getOwnerName()+'/'+repo.getName()+"/issues",GHIssue.class).wrap(repo);
57+
return builder.with("labels",labels).to(repo.getApiTailUrl("issues"),GHIssue.class).wrap(repo);
2858
}
2959
}

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -616,24 +616,42 @@ public boolean remove(Object url) {
616616
*/
617617
public Map<String,GHBranch> getBranches() throws IOException {
618618
Map<String,GHBranch> r = new TreeMap<String,GHBranch>();
619-
for (GHBranch p : root.retrieve().to("/repos/" + owner.login + "/" + name + "/branches", GHBranch[].class)) {
619+
for (GHBranch p : root.retrieve().to(getApiTailUrl("branches"), GHBranch[].class)) {
620620
p.wrap(this);
621621
r.put(p.getName(),p);
622622
}
623623
return r;
624624
}
625625

626+
/**
627+
* @deprecated
628+
* Use {@link #listMilestones(GHIssueState)}
629+
*/
626630
public Map<Integer, GHMilestone> getMilestones() throws IOException {
627631
Map<Integer,GHMilestone> milestones = new TreeMap<Integer, GHMilestone>();
628-
GHMilestone[] ms = root.retrieve().to("/repos/" + owner.login + "/" + name + "/milestones", GHMilestone[].class);
629-
for (GHMilestone m : ms) {
630-
m.owner = this;
631-
m.root = root;
632+
for (GHMilestone m : listMilestones(GHIssueState.OPEN)) {
632633
milestones.put(m.getNumber(), m);
633634
}
634635
return milestones;
635636
}
636637

638+
/**
639+
* Lists up all the milestones in this repository.
640+
*/
641+
public PagedIterable<GHMilestone> listMilestones(final GHIssueState state) {
642+
return new PagedIterable<GHMilestone>() {
643+
public PagedIterator<GHMilestone> iterator() {
644+
return new PagedIterator<GHMilestone>(root.retrieve().asIterator(getApiTailUrl("milestones?state="+state.toString().toLowerCase(Locale.ENGLISH)), GHMilestone[].class)) {
645+
@Override
646+
protected void wrapUp(GHMilestone[] page) {
647+
for (GHMilestone c : page)
648+
c.wrap(GHRepository.this);
649+
}
650+
};
651+
}
652+
};
653+
}
654+
637655
public GHMilestone getMilestone(int number) throws IOException {
638656
GHMilestone m = milestones.get(number);
639657
if (m == null) {
@@ -669,4 +687,8 @@ public boolean equals(Object obj) {
669687
}
670688
return false;
671689
}
690+
691+
String getApiTailUrl(String tail) {
692+
return "/repos/" + owner.login + "/" + name +'/'+tail;
693+
}
672694
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.kohsuke.github.GHIssueComment;
1515
import org.kohsuke.github.GHIssueState;
1616
import org.kohsuke.github.GHKey;
17+
import org.kohsuke.github.GHMilestone;
1718
import org.kohsuke.github.GHMyself;
1819
import org.kohsuke.github.GHOrganization;
1920
import org.kohsuke.github.GHOrganization.Permission;
@@ -62,7 +63,10 @@ public void testIssueWithNoComment() throws IOException {
6263
}
6364

6465
public void testCreateIssue() throws IOException {
65-
GHIssue o = GitHub.connect().getRepository("kohsuke/test").createIssue("testing").body("this is body").create();
66+
GHUser u = GitHub.connect().getUser("kohsuke");
67+
GHRepository r = u.getRepository("test");
68+
GHMilestone someMilestone = r.listMilestones(GHIssueState.CLOSED).iterator().next();
69+
GHIssue o = r.createIssue("testing").body("this is body").assignee(u).label("bug").label("question").milestone(someMilestone).create();
6670
System.out.println(o.getUrl());
6771
o.close();
6872
}

0 commit comments

Comments
 (0)