diff --git a/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMNavigator.java b/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMNavigator.java index e6fee5bac..f08408661 100644 --- a/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMNavigator.java +++ b/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMNavigator.java @@ -986,9 +986,15 @@ public void visitSources(SCMSourceObserver observer) throws IOException, Interru String.format("Looking up repositories of myself %s", repoOwner))); final Iterable repositories; if (!gitHubSCMNavigatorContext.getTopics().isEmpty()) { - repositories = - getGhRepositoriesBasedOnTopics( - listener, github, gitHubSCMNavigatorContext.getTopics()); + listener + .getLogger() + .println( + GitHubConsoleNote.create( + System.currentTimeMillis(), + String.format( + "Looking up repositories for topics: '%s'", + gitHubSCMNavigatorContext.getTopics()))); + repositories = searchRepositories(github, gitHubSCMNavigatorContext); } else { repositories = myself.listRepositories(100); } @@ -1094,9 +1100,15 @@ public void visitSources(SCMSourceObserver observer) throws IOException, Interru .listRepositories() .withPageSize(100); } else if (!gitHubSCMNavigatorContext.getTopics().isEmpty()) { - repositories = - getGhRepositoriesBasedOnTopics( - listener, github, gitHubSCMNavigatorContext.getTopics()); + listener + .getLogger() + .println( + GitHubConsoleNote.create( + System.currentTimeMillis(), + String.format( + "Looking up repositories for topics: '%s'", + gitHubSCMNavigatorContext.getTopics()))); + repositories = searchRepositories(github, gitHubSCMNavigatorContext); } else { repositories = org.listRepositories(100); } @@ -1247,17 +1259,14 @@ public void visitSources(SCMSourceObserver observer) throws IOException, Interru } } - private Iterable getGhRepositoriesBasedOnTopics( - final TaskListener listener, final GitHub github, final List topics) { - listener - .getLogger() - .println( - GitHubConsoleNote.create( - System.currentTimeMillis(), - String.format("Looking up repositories for topics: '%s'", topics))); + private Iterable searchRepositories( + final GitHub github, final GitHubSCMNavigatorContext context) { final GHRepositorySearchBuilder ghRepositorySearchBuilder = github.searchRepositories(); - topics.forEach(ghRepositorySearchBuilder::topic); - ghRepositorySearchBuilder.q("org:" + getRepoOwner()); + context.getTopics().forEach(ghRepositorySearchBuilder::topic); + ghRepositorySearchBuilder.org(getRepoOwner()); + if (!context.isExcludeForkedRepositories()) { + ghRepositorySearchBuilder.q("fork:true"); + } return ghRepositorySearchBuilder.list().withPageSize(100); } diff --git a/src/test/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMNavigatorTest.java b/src/test/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMNavigatorTest.java index 5687da1d9..4d7aefa4b 100644 --- a/src/test/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMNavigatorTest.java +++ b/src/test/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMNavigatorTest.java @@ -45,6 +45,7 @@ import hudson.util.ListBoxModel; import hudson.util.LogTaskListener; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashSet; @@ -200,6 +201,21 @@ public void fetchRepos_BelongingToAuthenticatedUser_FilteredByTopic() throws Exc assertEquals(projectNames, Collections.singleton("yolo")); } + @Test + public void fetchRepos_BelongingToAuthenticatedUser_FilteredByTopic_ExcludeForks() + throws Exception { + setCredentials(Collections.singletonList(credentials)); + navigator = navigatorForRepoOwner("stephenc", credentials.getId()); + navigator.setTraits( + Arrays.asList(new TopicsTrait("api"), new ExcludeForkedRepositoriesTrait())); + final Set projectNames = new HashSet<>(); + final SCMSourceObserver observer = getObserver(projectNames); + + navigator.visitSources(observer); + + assertEquals(Collections.singleton("yolo-archived"), projectNames); + } + @Test public void fetchRepos_BelongingToAuthenticatedUser_FilteredByTopic_RemovesAll() throws Exception { diff --git a/src/test/resources/api/__files/body-search-user-repos-O8W78-no-forks.json b/src/test/resources/api/__files/body-search-user-repos-O8W78-no-forks.json new file mode 100644 index 000000000..b5e5e7b8c --- /dev/null +++ b/src/test/resources/api/__files/body-search-user-repos-O8W78-no-forks.json @@ -0,0 +1,100 @@ +{ + "total_count": 1, + "incomplete_results": false, + "items": [ + { + "id": 43041241, + "name": "yolo-archived", + "full_name": "stephenc/yolo-archived", + "owner": { + "login": "stephenc", + "id": 209336, + "avatar_url": "https://avatars.githubusercontent.com/u/209336?v=3", + "gravatar_id": "", + "url": "https://api.github.com/users/stephenc", + "html_url": "https://github.com/stephenc", + "followers_url": "https://api.github.com/users/stephenc/followers", + "following_url": "https://api.github.com/users/stephenc/following{/other_user}", + "gists_url": "https://api.github.com/users/stephenc/gists{/gist_id}", + "starred_url": "https://api.github.com/users/stephenc/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/stephenc/subscriptions", + "organizations_url": "https://api.github.com/users/stephenc/orgs", + "repos_url": "https://api.github.com/users/stephenc/repos", + "events_url": "https://api.github.com/users/stephenc/events{/privacy}", + "received_events_url": "https://api.github.com/users/stephenc/received_events", + "type": "User", + "site_admin": false + }, + "private": false, + "html_url": "https://github.com/stephenc/yolo-archived", + "description": "It lived once, and then it was archived", + "fork": false, + "url": "https://api.github.com/repos/stephenc/yolo-archived", + "forks_url": "https://api.github.com/repos/stephenc/yolo-archived/forks", + "keys_url": "https://api.github.com/repos/stephenc/yolo-archived/keys{/key_id}", + "collaborators_url": "https://api.github.com/repos/stephenc/yolo-archived/collaborators{/collaborator}", + "teams_url": "https://api.github.com/repos/stephenc/yolo-archived/teams", + "hooks_url": "https://api.github.com/repos/stephenc/yolo-archived/hooks", + "issue_events_url": "https://api.github.com/repos/stephenc/yolo-archived/issues/events{/number}", + "events_url": "https://api.github.com/repos/stephenc/yolo-archived/events", + "assignees_url": "https://api.github.com/repos/stephenc/yolo-archived/assignees{/user}", + "branches_url": "https://api.github.com/repos/stephenc/yolo-archived/branches{/branch}", + "tags_url": "https://api.github.com/repos/stephenc/yolo-archived/tags", + "blobs_url": "https://api.github.com/repos/stephenc/yolo-archived/git/blobs{/sha}", + "git_tags_url": "https://api.github.com/repos/stephenc/yolo-archived/git/tags{/sha}", + "git_refs_url": "https://api.github.com/repos/stephenc/yolo-archived/git/refs{/sha}", + "trees_url": "https://api.github.com/repos/stephenc/yolo-archived/git/trees{/sha}", + "statuses_url": "https://api.github.com/repos/stephenc/yolo-archived/statuses/{sha}", + "languages_url": "https://api.github.com/repos/stephenc/yolo-archived/languages", + "stargazers_url": "https://api.github.com/repos/stephenc/yolo-archived/stargazers", + "contributors_url": "https://api.github.com/repos/stephenc/yolo-archived/contributors", + "subscribers_url": "https://api.github.com/repos/stephenc/yolo-archived/subscribers", + "subscription_url": "https://api.github.com/repos/stephenc/yolo-archived/subscription", + "commits_url": "https://api.github.com/repos/stephenc/yolo-archived/commits{/sha}", + "git_commits_url": "https://api.github.com/repos/stephenc/yolo-archived/git/commits{/sha}", + "comments_url": "https://api.github.com/repos/stephenc/yolo-archived/comments{/number}", + "issue_comment_url": "https://api.github.com/repos/stephenc/yolo-archived/issues/comments{/number}", + "contents_url": "https://api.github.com/repos/stephenc/yolo-archived/contents/{+path}", + "compare_url": "https://api.github.com/repos/stephenc/yolo-archived/compare/{base}...{head}", + "merges_url": "https://api.github.com/repos/stephenc/yolo-archived/merges", + "archive_url": "https://api.github.com/repos/stephenc/yolo-archived/{archive_format}{/ref}", + "downloads_url": "https://api.github.com/repos/stephenc/yolo-archived/downloads", + "issues_url": "https://api.github.com/repos/stephenc/yolo-archived/issues{/number}", + "pulls_url": "https://api.github.com/repos/stephenc/yolo-archived/pulls{/number}", + "milestones_url": "https://api.github.com/repos/stephenc/yolo-archived/milestones{/number}", + "notifications_url": "https://api.github.com/repos/stephenc/yolo-archived/notifications{?since,all,participating}", + "labels_url": "https://api.github.com/repos/stephenc/yolo-archived/labels{/name}", + "releases_url": "https://api.github.com/repos/stephenc/yolo-archived/releases{/id}", + "deployments_url": "https://api.github.com/repos/stephenc/yolo-archived/deployments", + "created_at": "2015-09-24T02:58:30Z", + "updated_at": "2016-12-07T23:55:35Z", + "pushed_at": "2016-12-01T16:07:01Z", + "git_url": "git://github.com/stephenc/yolo-archived.git", + "ssh_url": "git@github.com:stephenc/yolo-archived.git", + "clone_url": "https://github.com/stephenc/yolo-archived.git", + "svn_url": "https://github.com/stephenc/yolo-archived", + "homepage": "http://yolo-archived.example.com", + "size": 3, + "stargazers_count": 0, + "watchers_count": 0, + "language": null, + "has_issues": true, + "has_downloads": true, + "archived": true, + "has_wiki": true, + "has_pages": false, + "forks_count": 3, + "mirror_url": null, + "open_issues_count": 1, + "forks": 3, + "open_issues": 1, + "watchers": 0, + "default_branch": "master", + "permissions": { + "admin": false, + "push": false, + "pull": true + } + } + ] +} diff --git a/src/test/resources/api/mappings/mapping-search-repositories-based-on-topics-O8W78.json b/src/test/resources/api/mappings/mapping-search-repositories-based-on-topics-O8W78.json index ad4eca204..9059cfc3e 100644 --- a/src/test/resources/api/mappings/mapping-search-repositories-based-on-topics-O8W78.json +++ b/src/test/resources/api/mappings/mapping-search-repositories-based-on-topics-O8W78.json @@ -8,7 +8,7 @@ }, "queryParameters" : { "q" : { - "equalTo" : "topic:cool topic:great topic:was-awesome org:stephenc" + "equalTo" : "topic:cool topic:great topic:was-awesome org:stephenc fork:true" } } }, @@ -35,4 +35,4 @@ "X-GitHub-Request-Id" : "C1F9:C8CD:5FFC805:7325FB9:5E340656" } } -} \ No newline at end of file +} diff --git a/src/test/resources/api/mappings/mapping-search-repositories-based-on-topics-api-O8W78-exclude-forks.json b/src/test/resources/api/mappings/mapping-search-repositories-based-on-topics-api-O8W78-exclude-forks.json new file mode 100644 index 000000000..c7e0aa617 --- /dev/null +++ b/src/test/resources/api/mappings/mapping-search-repositories-based-on-topics-api-O8W78-exclude-forks.json @@ -0,0 +1,38 @@ +{ + "request" : { + "urlPath" : "/search/repositories", + "method" : "GET", + "basicAuth": { + "username": "git-user", + "password": "git-secret" + }, + "queryParameters" : { + "q" : { + "equalTo" : "topic:api org:stephenc" + } + } + }, + "response" : { + "status" : 200, + "bodyFileName" : "body-search-user-repos-O8W78-no-forks.json", + "headers" : { + "Date" : "Fri, 31 Jan 2020 10:49:58 GMT", + "Content-Type" : "application/json; charset=utf-8", + "Server" : "GitHub.com", + "Status" : "200 OK", + "X-GitHub-Media-Type" : "github.v3; format=json", + "X-RateLimit-Limit" : "60", + "X-RateLimit-Remaining" : "54", + "X-RateLimit-Reset" : "1580469786", + "Access-Control-Expose-Headers" : "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type", + "Access-Control-Allow-Origin" : "*", + "Strict-Transport-Security" : "max-age=31536000; includeSubdomains; preload", + "X-Frame-Options" : "deny", + "X-Content-Type-Options" : "nosniff", + "X-XSS-Protection" : "1; mode=block", + "Referrer-Policy" : "origin-when-cross-origin, strict-origin-when-cross-origin", + "Content-Security-Policy" : "default-src 'none'", + "X-GitHub-Request-Id" : "C1F9:C8CD:5FFC805:7325FB9:5E340656" + } + } +} diff --git a/src/test/resources/api/mappings/mapping-search-repositories-based-on-topics-awesome-O8W78.json b/src/test/resources/api/mappings/mapping-search-repositories-based-on-topics-awesome-O8W78.json index 0371fb6ad..0a945764c 100644 --- a/src/test/resources/api/mappings/mapping-search-repositories-based-on-topics-awesome-O8W78.json +++ b/src/test/resources/api/mappings/mapping-search-repositories-based-on-topics-awesome-O8W78.json @@ -8,7 +8,7 @@ }, "queryParameters" : { "q" : { - "equalTo" : "topic:awesome org:stephenc" + "equalTo" : "topic:awesome org:stephenc fork:true" } } }, @@ -35,4 +35,4 @@ "X-GitHub-Request-Id" : "C1F9:C8CD:5FFC805:7325FB9:5E340656" } } -} \ No newline at end of file +} diff --git a/src/test/resources/api/mappings/mapping-search-repositories-based-on-topics-nope-O8W78.json b/src/test/resources/api/mappings/mapping-search-repositories-based-on-topics-nope-O8W78.json index 3e013c85e..b97182d7b 100644 --- a/src/test/resources/api/mappings/mapping-search-repositories-based-on-topics-nope-O8W78.json +++ b/src/test/resources/api/mappings/mapping-search-repositories-based-on-topics-nope-O8W78.json @@ -8,7 +8,7 @@ }, "queryParameters" : { "q" : { - "equalTo" : "topic:nope org:stephenc" + "equalTo" : "topic:nope org:stephenc fork:true" } } }, @@ -35,4 +35,4 @@ "X-GitHub-Request-Id" : "C1F9:C8CD:5FFC805:7325FB9:5E340656" } } -} \ No newline at end of file +}