Skip to content

Commit 4828993

Browse files
committed
Add shared query qualifier method
1 parent e92fb3a commit 4828993

File tree

5 files changed

+124
-27
lines changed

5 files changed

+124
-27
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,23 @@ public class GHContentSearchBuilder extends GHSearchBuilder<GHContent> {
1212
}
1313

1414
/**
15-
* Search terms.
15+
* {@inheritDoc}
1616
*/
17+
@Override
1718
public GHContentSearchBuilder q(String term) {
1819
super.q(term);
1920
return this;
2021
}
2122

23+
/**
24+
* {@inheritDoc}
25+
*/
26+
@Override
27+
GHContentSearchBuilder q(String qualifier, String value) {
28+
super.q(qualifier, value);
29+
return this;
30+
}
31+
2232
/**
2333
* In gh content search builder.
2434
*
@@ -47,10 +57,11 @@ public GHContentSearchBuilder language(String v) {
4757
* @param v
4858
* the v
4959
* @return the gh content search builder
60+
* @deprecated use {@link #fork(GHFork)}.
5061
*/
5162
@Deprecated
5263
public GHContentSearchBuilder fork(String v) {
53-
return q("fork:" + v);
64+
return q("fork", v);
5465
}
5566

5667
/**
@@ -66,12 +77,7 @@ public GHContentSearchBuilder fork(String v) {
6677
* in forks</a>
6778
*/
6879
public GHContentSearchBuilder fork(GHFork fork) {
69-
if (GHFork.PARENT_ONLY.equals(fork)) {
70-
this.terms.removeIf(term -> term.contains("fork:"));
71-
return this;
72-
}
73-
74-
return q("fork:" + fork);
80+
return q("fork", fork.toString());
7581
}
7682

7783
/**

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,23 @@ public class GHRepositorySearchBuilder extends GHSearchBuilder<GHRepository> {
1212
}
1313

1414
/**
15-
* Search terms.
15+
* {@inheritDoc}
1616
*/
17+
@Override
1718
public GHRepositorySearchBuilder q(String term) {
1819
super.q(term);
1920
return this;
2021
}
2122

23+
/**
24+
* {@inheritDoc}
25+
*/
26+
@Override
27+
GHRepositorySearchBuilder q(String qualifier, String value) {
28+
super.q(qualifier, value);
29+
return this;
30+
}
31+
2232
/**
2333
* In gh repository search builder.
2434
*
@@ -47,9 +57,11 @@ public GHRepositorySearchBuilder size(String v) {
4757
* @param v
4858
* the v
4959
* @return the gh repository search builder
60+
* @deprecated use {@link #fork(GHFork)} instead.
5061
*/
62+
@Deprecated
5163
public GHRepositorySearchBuilder forks(String v) {
52-
return q("forks:" + v);
64+
return q("fork", v);
5365
}
5466

5567
/**
@@ -74,16 +86,11 @@ public GHRepositorySearchBuilder forks(String v) {
7486
* @see <a href=
7587
* "https://docs.github.com/en/github/searching-for-information-on-github/searching-on-github/searching-in-forks">Searching
7688
* in forks</a>
77-
*
89+
* @deprecated use {@link #fork(GHFork)} instead.
7890
*/
7991
@Deprecated
8092
public GHRepositorySearchBuilder fork(Fork fork) {
81-
if (Fork.PARENT_ONLY.equals(fork)) {
82-
this.terms.removeIf(term -> term.contains("fork:"));
83-
return this;
84-
}
85-
86-
return q("fork:" + fork);
93+
return q("fork", fork.toString());
8794
}
8895

8996
/**
@@ -111,12 +118,7 @@ public GHRepositorySearchBuilder fork(Fork fork) {
111118
*
112119
*/
113120
public GHRepositorySearchBuilder fork(GHFork fork) {
114-
if (GHFork.PARENT_ONLY.equals(fork)) {
115-
this.terms.removeIf(term -> term.contains("fork:"));
116-
return this;
117-
}
118-
119-
return q("fork:" + fork);
121+
return q("fork", fork.toString());
120122
}
121123

122124
/**
@@ -251,7 +253,9 @@ public enum Sort {
251253
}
252254

253255
/**
254-
* The enum for Fork search mode. Note: Kept for backward compatibility. Use GHFork instead.
256+
* The enum for Fork search mode.
257+
*
258+
* @deprecated Kept for backward compatibility. Use {@link GHFork} instead.
255259
*/
256260
@Deprecated
257261
public enum Fork {

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import java.util.ArrayList;
66
import java.util.List;
77

8+
import javax.annotation.CheckForNull;
9+
import javax.annotation.Nonnull;
10+
811
/**
912
* Base class for various search builders.
1013
*
@@ -39,6 +42,30 @@ public GHQueryBuilder<T> q(String term) {
3942
return this;
4043
}
4144

45+
/**
46+
* Add a search term with qualifier.
47+
*
48+
* If {@code value} is empty or {@code null}, all terms with the current qualifier will be removed.
49+
*
50+
* @param qualifier
51+
* the qualifier for this term
52+
* @param value
53+
* the value for this term. If empty or null, all terms with the current qualifier will be removed.
54+
* @return the gh query builder
55+
*/
56+
GHQueryBuilder<T> q(@Nonnull final String qualifier, @CheckForNull final String value) {
57+
if (StringUtils.isEmpty(qualifier)) {
58+
throw new IllegalArgumentException("qualifier cannot be null or empty");
59+
}
60+
if (StringUtils.isEmpty(value)) {
61+
final String removeQualifier = qualifier + ":";
62+
terms.removeIf(term -> term.startsWith(removeQualifier));
63+
} else {
64+
terms.add(qualifier + ":" + value);
65+
}
66+
return this;
67+
}
68+
4269
/**
4370
* Performs the search.
4471
*/

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ public void ghRepositorySearchBuilderIgnoresUnknownVisibility() {
467467
@Test
468468
public void ghRepositorySearchBuilderForkDefaultResetForksSearchTerms() {
469469
GHRepositorySearchBuilder ghRepositorySearchBuilder = new GHRepositorySearchBuilder(gitHub);
470+
470471
ghRepositorySearchBuilder = ghRepositorySearchBuilder.fork(GHFork.PARENT_AND_FORKS);
471472
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:true")).count(), is(1L));
472473
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(1L));
@@ -479,6 +480,36 @@ public void ghRepositorySearchBuilderForkDefaultResetForksSearchTerms() {
479480
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(0L));
480481
}
481482

483+
@Test
484+
public void ghRepositorySearchBuilderForkDeprecatedEnum() {
485+
GHRepositorySearchBuilder ghRepositorySearchBuilder = new GHRepositorySearchBuilder(gitHub);
486+
ghRepositorySearchBuilder = ghRepositorySearchBuilder.fork(GHRepositorySearchBuilder.Fork.PARENT_AND_FORKS);
487+
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:true")).count(), is(1L));
488+
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(1L));
489+
490+
ghRepositorySearchBuilder = ghRepositorySearchBuilder.fork(GHRepositorySearchBuilder.Fork.FORKS_ONLY);
491+
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:only")).count(), is(1L));
492+
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(2L));
493+
494+
ghRepositorySearchBuilder = ghRepositorySearchBuilder.fork(GHRepositorySearchBuilder.Fork.PARENT_ONLY);
495+
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(0L));
496+
}
497+
498+
@Test
499+
public void ghRepositorySearchBuilderForkDeprecatedString() {
500+
GHRepositorySearchBuilder ghRepositorySearchBuilder = new GHRepositorySearchBuilder(gitHub);
501+
ghRepositorySearchBuilder = ghRepositorySearchBuilder.forks(GHFork.PARENT_AND_FORKS.toString());
502+
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:true")).count(), is(1L));
503+
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(1L));
504+
505+
ghRepositorySearchBuilder = ghRepositorySearchBuilder.forks(GHFork.FORKS_ONLY.toString());
506+
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:only")).count(), is(1L));
507+
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(2L));
508+
509+
ghRepositorySearchBuilder = ghRepositorySearchBuilder.forks(null);
510+
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(0L));
511+
}
512+
482513
@Test
483514
public void listCommitCommentsSomeComments() throws IOException {
484515
List<GHCommitComment> commitComments = getRepository()

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.kohsuke.github;
22

33
import com.google.common.collect.Iterables;
4+
import org.junit.Assert;
45
import org.junit.Test;
56
import org.kohsuke.github.example.dataobject.ReadOnlyObjects;
67

@@ -138,20 +139,25 @@ public void searchContent() throws Exception {
138139
assertThat(c3.getPath(), not(equalTo(c2.getPath())));
139140
assertThat(r3.getTotalCount(), equalTo(r2.getTotalCount()));
140141

141-
PagedSearchIterable<GHContent> r4 = gitHub.searchContent()
142+
GHContentSearchBuilder searchBuilder = gitHub.searchContent()
142143
.q("addClass")
143144
.in("file")
144145
.language("js")
145146
.repo("jquery/jquery")
146147
.sort(GHContentSearchBuilder.Sort.INDEXED)
147-
.order(GHDirection.DESC)
148-
.list();
148+
.order(GHDirection.DESC);
149+
150+
PagedSearchIterable<GHContent> r4 = searchBuilder.list();
149151

150152
GHContent c4 = r4.iterator().next();
151153
assertThat(c4.getPath(), not(equalTo(c2.getPath())));
152154
assertThat(c4.getPath(), not(equalTo(c3.getPath())));
153155
assertThat(r4.getTotalCount(), equalTo(r2.getTotalCount()));
154156

157+
// Verify qualifier not allowed to be empty
158+
IllegalArgumentException e = Assert.assertThrows(IllegalArgumentException.class,
159+
() -> searchBuilder.q("", "not valid"));
160+
assertThat(e.getMessage(), equalTo("qualifier cannot be null or empty"));
155161
}
156162

157163
@Test
@@ -173,6 +179,29 @@ public void searchContentWithForks() {
173179
.list();
174180

175181
assertThat(results.getTotalCount(), lessThan(resultsWithForks.getTotalCount()));
182+
183+
// Do not record these.
184+
// This will verify that the queries for the deprecated path are identical to the ones above.
185+
if (!mockGitHub.isTakeSnapshot()) {
186+
final PagedSearchIterable<GHContent> resultsDeprecated = gitHub.searchContent()
187+
.q("addClass")
188+
.language("js")
189+
.sort(GHContentSearchBuilder.Sort.INDEXED)
190+
.order(GHDirection.DESC)
191+
.fork(GHFork.PARENT_ONLY.toString())
192+
.list();
193+
194+
final PagedSearchIterable<GHContent> resultsWithForksDeprecated = gitHub.searchContent()
195+
.q("addClass")
196+
.language("js")
197+
.sort(GHContentSearchBuilder.Sort.INDEXED)
198+
.order(GHDirection.DESC)
199+
.fork(GHFork.PARENT_AND_FORKS.toString())
200+
.list();
201+
202+
assertThat(resultsDeprecated.getTotalCount(), equalTo(results.getTotalCount()));
203+
assertThat(resultsWithForksDeprecated.getTotalCount(), equalTo(resultsWithForks.getTotalCount()));
204+
}
176205
}
177206

178207
@Test

0 commit comments

Comments
 (0)