Skip to content

Commit b888639

Browse files
feat: limit searching issues based on type (#2186)
* feat: limit searching issues based on type * Update src/test/java/org/kohsuke/github/AppTest.java Co-authored-by: Liam Newman <bitwiseman@gmail.com> * chore: sanitize search terms before updating search query * chore: adjust tests * fix: update test snapshots --------- Co-authored-by: Liam Newman <bitwiseman@gmail.com>
1 parent f6e6a77 commit b888639

14 files changed

Lines changed: 10413 additions & 0 deletions

File tree

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ public GHIssueSearchBuilder isClosed() {
5757
return q("is:closed");
5858
}
5959

60+
/**
61+
* Filters results to only include issues (excludes pull requests).
62+
*
63+
* @return the gh issue search builder
64+
*/
65+
public GHIssueSearchBuilder isIssue() {
66+
terms.removeIf("is:pr"::equals);
67+
return q("is:issue");
68+
}
69+
6070
/**
6171
* Is merged gh issue search builder.
6272
*
@@ -75,6 +85,16 @@ public GHIssueSearchBuilder isOpen() {
7585
return q("is:open");
7686
}
7787

88+
/**
89+
* Filters results to only include pull requests (excludes issues).
90+
*
91+
* @return the gh issue search builder
92+
*/
93+
public GHIssueSearchBuilder isPullRequest() {
94+
terms.removeIf("is:issue"::equals);
95+
return q("is:pr");
96+
}
97+
7898
/**
7999
* Mentions gh issue search builder.
80100
*
@@ -121,6 +141,19 @@ public GHIssueSearchBuilder q(String term) {
121141
return this;
122142
}
123143

144+
/**
145+
* Filters results to a specific repository.
146+
*
147+
* @param owner
148+
* the repository owner
149+
* @param name
150+
* the repository name
151+
* @return the gh issue search builder
152+
*/
153+
public GHIssueSearchBuilder repo(String owner, String name) {
154+
return q("repo:" + owner + "/" + name);
155+
}
156+
124157
/**
125158
* Sort gh issue search builder.
126159
*

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,52 @@ public void testIssueSearch() {
855855
}
856856
}
857857

858+
/**
859+
* Test issue search with isIssue filter to exclude pull requests.
860+
*/
861+
@Test
862+
public void testIssueSearchIssuesOnly() {
863+
PagedSearchIterable<GHIssue> r = gitHub.searchIssues()
864+
.repo("hub4j", "github-api")
865+
.isPullRequest()
866+
.isIssue()
867+
.isClosed()
868+
.sort(GHIssueSearchBuilder.Sort.CREATED)
869+
.list();
870+
assertThat(r.getTotalCount(), greaterThan(0));
871+
int count = 0;
872+
for (GHIssue issue : r) {
873+
assertThat(issue.getTitle(), notNullValue());
874+
assertThat(issue.getPullRequest(), nullValue());
875+
if (++count >= 3) {
876+
break;
877+
}
878+
}
879+
}
880+
881+
/**
882+
* Test issue search with isPullRequest filter to only return pull requests.
883+
*/
884+
@Test
885+
public void testIssueSearchPullRequestsOnly() {
886+
PagedSearchIterable<GHIssue> r = gitHub.searchIssues()
887+
.repo("hub4j", "github-api")
888+
.isIssue()
889+
.isPullRequest()
890+
.isClosed()
891+
.sort(GHIssueSearchBuilder.Sort.CREATED)
892+
.list();
893+
assertThat(r.getTotalCount(), greaterThan(0));
894+
int count = 0;
895+
for (GHIssue issue : r) {
896+
assertThat(issue.getTitle(), notNullValue());
897+
assertThat(issue.getPullRequest(), notNullValue());
898+
if (++count >= 3) {
899+
break;
900+
}
901+
}
902+
}
903+
858904
/**
859905
* Test issue with comment.
860906
*
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"login": "Anonycoders",
3+
"id": 40047636,
4+
"node_id": "MDQ6VXNlcjQwMDQ3NjM2",
5+
"avatar_url": "https://avatars.githubusercontent.com/u/40047636?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/Anonycoders",
8+
"html_url": "https://github.com/Anonycoders",
9+
"followers_url": "https://api.github.com/users/Anonycoders/followers",
10+
"following_url": "https://api.github.com/users/Anonycoders/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/Anonycoders/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/Anonycoders/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/Anonycoders/subscriptions",
14+
"organizations_url": "https://api.github.com/users/Anonycoders/orgs",
15+
"repos_url": "https://api.github.com/users/Anonycoders/repos",
16+
"events_url": "https://api.github.com/users/Anonycoders/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/Anonycoders/received_events",
18+
"type": "User",
19+
"user_view_type": "public",
20+
"site_admin": false,
21+
"name": "Sorena Sarabadani",
22+
"company": "@Adevinta",
23+
"blog": "",
24+
"location": "Berlin, Germany",
25+
"email": "sorena.sarabadani@gmail.com",
26+
"hireable": null,
27+
"bio": "Ex-Shopifyer - Adevinta/Kleinanzeigen",
28+
"twitter_username": "sorena_s",
29+
"notification_email": "sorena.sarabadani@gmail.com",
30+
"public_repos": 12,
31+
"public_gists": 0,
32+
"followers": 38,
33+
"following": 4,
34+
"created_at": "2018-06-08T02:07:15Z",
35+
"updated_at": "2026-01-24T22:07:12Z"
36+
}

0 commit comments

Comments
 (0)