Skip to content
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

[JENKINS-68809] Topic repository listing should include forks by default #580

Merged
merged 4 commits into from
Jun 30, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -986,9 +986,15 @@ public void visitSources(SCMSourceObserver observer) throws IOException, Interru
String.format("Looking up repositories of myself %s", repoOwner)));
final Iterable<GHRepository> 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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -1247,17 +1259,14 @@ public void visitSources(SCMSourceObserver observer) throws IOException, Interru
}
}

private Iterable<GHRepository> getGhRepositoriesBasedOnTopics(
final TaskListener listener, final GitHub github, final List<String> topics) {
listener
.getLogger()
.println(
GitHubConsoleNote.create(
System.currentTimeMillis(),
String.format("Looking up repositories for topics: '%s'", topics)));
private Iterable<GHRepository> 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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating this :)

I had put a pull request into github api a while ago

hub4j/github-api#1355

if (!context.isExcludeForkedRepositories()) {
ghRepositorySearchBuilder.q("fork:true");
}
return ghRepositorySearchBuilder.list().withPageSize(100);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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": "[email protected]: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
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
},
Expand All @@ -35,4 +35,4 @@
"X-GitHub-Request-Id" : "C1F9:C8CD:5FFC805:7325FB9:5E340656"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"queryParameters" : {
"q" : {
"equalTo" : "topic:awesome org:stephenc"
"equalTo" : "topic:awesome org:stephenc fork:true"
}
}
},
Expand All @@ -35,4 +35,4 @@
"X-GitHub-Request-Id" : "C1F9:C8CD:5FFC805:7325FB9:5E340656"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"queryParameters" : {
"q" : {
"equalTo" : "topic:nope org:stephenc"
"equalTo" : "topic:nope org:stephenc fork:true"
}
}
},
Expand All @@ -35,4 +35,4 @@
"X-GitHub-Request-Id" : "C1F9:C8CD:5FFC805:7325FB9:5E340656"
}
}
}
}