Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Refactor GHContentSearchBuilder to allow a Fork type (#1154) #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions 24 src/main/java/org/kohsuke/github/GHContentSearchBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,34 @@ public GHContentSearchBuilder language(String v) {
* the v
* @return the gh content search builder
*/
// CS427 Issue Link: https://github.com/hub4j/github-api/issues/1154
@Deprecated
public GHContentSearchBuilder fork(String v) {
return q("fork:" + v);
}

/**
* Fork gh content search builder.
*
* @param fork
* search mode for forks
*
* @return the gh content search builder
*
* @see <a href=
* "https://docs.github.com/en/github/searching-for-information-on-github/searching-on-github/searching-in-forks">Searching
* in forks</a>
*/
// CS427 Issue Link: https://github.com/hub4j/github-api/issues/1154
public GHContentSearchBuilder fork(GHFork fork) {
if (GHFork.PARENT_ONLY.equals(fork)) {
this.terms.removeIf(term -> term.contains("fork:"));
return this;
}

return q("fork:" + fork);
}

/**
* Size gh content search builder.
*
Expand Down
39 changes: 39 additions & 0 deletions 39 src/main/java/org/kohsuke/github/GHFork.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.kohsuke.github;

/**
* The enum for Fork search mode
*/
// CS427 Issue Link: https://github.com/hub4j/github-api/issues/1154
public enum GHFork {

/**
* Search in the parent repository and in forks with more stars than the parent repository.
*
* Forks with the same or fewer stars than the parent repository are still ignored.
*/
PARENT_AND_FORKS("true"),

/**
* Search only in forks with more stars than the parent repository.
*
* The parent repository is ignored. If no forks have more stars than the parent, no results will be returned.
*/
FORKS_ONLY("only"),

/**
* (Default) Search only the parent repository.
*
* Forks are ignored.
*/
PARENT_ONLY("");

private String filterMode;
GHFork(final String mode) {
this.filterMode = mode;
}

@Override
public String toString() {
return filterMode;
}
}
43 changes: 41 additions & 2 deletions 43 src/main/java/org/kohsuke/github/GHRepositorySearchBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public GHRepositorySearchBuilder forks(String v) {
* in forks</a>
*
*/
// CS427 Issue Link: https://github.com/hub4j/github-api/issues/1154
@Deprecated
public GHRepositorySearchBuilder fork(Fork fork) {
if (Fork.PARENT_ONLY.equals(fork)) {
this.terms.removeIf(term -> term.contains("fork:"));
Expand All @@ -85,6 +87,40 @@ public GHRepositorySearchBuilder fork(Fork fork) {
return q("fork:" + fork);
}

/**
* Searching in forks
*
* The default search mode is {@link Fork#PARENT_ONLY}. In that mode, forks are not included in search results.
*
* <p>
* Passing {@link Fork#PARENT_AND_FORKS} or {@link Fork#FORKS_ONLY} will show results from forks, but only if they
* have more stars than the parent repository.
*
* <p>
* IMPORTANT: Regardless of this setting, no search results will ever be returned for forks with equal or fewer
* stars than the parent repository. Forks with less stars than the parent repository are not included in the index
* for code searching.
*
* @param fork
* search mode for forks
*
* @return the gh repository search builder
*
* @see <a href=
* "https://docs.github.com/en/github/searching-for-information-on-github/searching-on-github/searching-in-forks">Searching
* in forks</a>
*
*/
// CS427 Issue Link: https://github.com/hub4j/github-api/issues/1154
public GHRepositorySearchBuilder fork(GHFork fork) {
if (GHFork.PARENT_ONLY.equals(fork)) {
this.terms.removeIf(term -> term.contains("fork:"));
return this;
}

return q("fork:" + fork);
}

/**
* Search by repository visibility
*
Expand Down Expand Up @@ -217,8 +253,10 @@ public enum Sort {
}

/**
* The enum for Fork search mode
* The enum for Fork search mode. Note: Kept for backward compatibility. Use GHFork instead.
*/
// CS427 Issue Link: https://github.com/hub4j/github-api/issues/1154
@Deprecated
public enum Fork {

/**
Expand All @@ -243,10 +281,11 @@ public enum Fork {
PARENT_ONLY("");

private String filterMode;
Fork(String mode) {
Fork(final String mode) {
this.filterMode = mode;
}

@Override
public String toString() {
return filterMode;
}
Expand Down
12 changes: 7 additions & 5 deletions 12 src/test/java/org/kohsuke/github/GHRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,13 @@ public void listCommitCommentsNoComments() throws IOException {
assertThat("Commit has no comments", commitComments.isEmpty());
}

// CS427 Issue Link: https://github.com/hub4j/github-api/issues/1154
@Test
public void searchAllPublicAndForkedRepos() throws IOException {
PagedSearchIterable<GHRepository> list = gitHub.searchRepositories()
.user("t0m4uk1991")
.visibility(GHRepository.Visibility.PUBLIC)
.fork(GHRepositorySearchBuilder.Fork.PARENT_AND_FORKS)
.fork(GHFork.PARENT_AND_FORKS)
.list();
List<GHRepository> u = list.toList();
assertThat(u.size(), is(14));
Expand All @@ -437,7 +438,7 @@ public void searchForPublicForkedOnlyRepos() throws IOException {
PagedSearchIterable<GHRepository> list = gitHub.searchRepositories()
.user("t0m4uk1991")
.visibility(GHRepository.Visibility.PUBLIC)
.fork(GHRepositorySearchBuilder.Fork.FORKS_ONLY)
.fork(GHFork.FORKS_ONLY)
.list();
List<GHRepository> u = list.toList();
assertThat(u.size(), is(2));
Expand All @@ -464,18 +465,19 @@ public void ghRepositorySearchBuilderIgnoresUnknownVisibility() {
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("is:")).count(), is(1L));
}

// CS427 Issue Link: https://github.com/hub4j/github-api/issues/1154
@Test
public void ghRepositorySearchBuilderForkDefaultResetForksSearchTerms() {
GHRepositorySearchBuilder ghRepositorySearchBuilder = new GHRepositorySearchBuilder(gitHub);
ghRepositorySearchBuilder = ghRepositorySearchBuilder.fork(GHRepositorySearchBuilder.Fork.PARENT_AND_FORKS);
ghRepositorySearchBuilder = ghRepositorySearchBuilder.fork(GHFork.PARENT_AND_FORKS);
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:true")).count(), is(1L));
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(1L));

ghRepositorySearchBuilder = ghRepositorySearchBuilder.fork(GHRepositorySearchBuilder.Fork.FORKS_ONLY);
ghRepositorySearchBuilder = ghRepositorySearchBuilder.fork(GHFork.FORKS_ONLY);
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:only")).count(), is(1L));
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(2L));

ghRepositorySearchBuilder = ghRepositorySearchBuilder.fork(GHRepositorySearchBuilder.Fork.PARENT_ONLY);
ghRepositorySearchBuilder = ghRepositorySearchBuilder.fork(GHFork.PARENT_ONLY);
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(0L));
}

Expand Down
22 changes: 22 additions & 0 deletions 22 src/test/java/org/kohsuke/github/GitHubTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,28 @@ public void searchContent() throws Exception {

}

// CS427 Issue Link: https://github.com/hub4j/github-api/issues/1154
@Test
public void searchContentWithForks() {
final PagedSearchIterable<GHContent> results = gitHub.searchContent()
.q("addClass")
.language("js")
.sort(GHContentSearchBuilder.Sort.INDEXED)
.order(GHDirection.DESC)
.fork(GHFork.PARENT_ONLY)
.list();

final PagedSearchIterable<GHContent> resultsWithForks = gitHub.searchContent()
.q("addClass")
.language("js")
.sort(GHContentSearchBuilder.Sort.INDEXED)
.order(GHDirection.DESC)
.fork(GHFork.PARENT_AND_FORKS)
.list();

assertThat(results.getTotalCount(), lessThan(resultsWithForks.getTotalCount()));
}

@Test
public void testListMyAuthorizations() throws IOException {
PagedIterable<GHAuthorization> list = gitHub.listMyAuthorizations();
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.