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-72894 - Adding support to more than 1000 repositories #773

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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 @@ -1019,7 +1019,7 @@
String.format(
"Looking up repositories for topics: '%s'",
gitHubSCMNavigatorContext.getTopics())));
repositories = searchRepositories(github, gitHubSCMNavigatorContext);
repositories = searchRepositories(github, gitHubSCMNavigatorContext, myself);
} else {
repositories = myself.listRepositories(100);
}
Expand Down Expand Up @@ -1114,138 +1114,162 @@
String.format(
"Looking up repositories for topics: '%s'",
gitHubSCMNavigatorContext.getTopics())));
repositories = searchRepositories(github, gitHubSCMNavigatorContext);
repositories = searchRepositories(github, gitHubSCMNavigatorContext, org);
} else {
repositories = org.listRepositories(100);
}
for (GHRepository repo : repositories) {
if (repo.isArchived() && gitHubSCMNavigatorContext.isExcludeArchivedRepositories()) {
// exclude archived repositories
witness.record(repo.getName(), false);
listener.getLogger()
.println(GitHubConsoleNote.create(
System.currentTimeMillis(),
String.format(
"Skipping repository %s because it is archived", repo.getName())));
} else if (!gitHubSCMNavigatorContext.getTopics().isEmpty()
&& !repo.listTopics().containsAll(gitHubSCMNavigatorContext.getTopics())) {
// exclude repositories which are missing one or more of the specified topics
witness.record(repo.getName(), false);
listener.getLogger()
.println(GitHubConsoleNote.create(
System.currentTimeMillis(),
String.format(
"Skipping repository %s because it is missing one or more of the following topics: '%s'",
repo.getName(), gitHubSCMNavigatorContext.getTopics())));

} else if (!repo.isPrivate() && gitHubSCMNavigatorContext.isExcludePublicRepositories()) {
witness.record(repo.getName(), false);
listener.getLogger()
.println(GitHubConsoleNote.create(
System.currentTimeMillis(),
String.format(
"Skipping repository %s because it is public", repo.getName())));

} else if (repo.isPrivate() && gitHubSCMNavigatorContext.isExcludePrivateRepositories()) {
witness.record(repo.getName(), false);
listener.getLogger()
.println(GitHubConsoleNote.create(
System.currentTimeMillis(),
String.format(
"Skipping repository %s because it is private", repo.getName())));

} else if (gitHubSCMNavigatorContext.isExcludeForkedRepositories()
&& repo.getSource() != null) {
witness.record(repo.getName(), false);
listener.getLogger()
.println(GitHubConsoleNote.create(
System.currentTimeMillis(),
String.format(
"Skipping repository %s because it is a fork", repo.getName())));
} else if (request.process(repo.getName(), sourceFactory, null, witness)) {
listener.getLogger()
.println(GitHubConsoleNote.create(
System.currentTimeMillis(),
String.format(
"%d repositories were processed (query completed)",
witness.getCount())));
}
}
listener.getLogger()
.println(GitHubConsoleNote.create(
System.currentTimeMillis(),
String.format("%d repositories were processed", witness.getCount())));
return;
}

GHUser user = null;
try {
user = github.getUser(repoOwner);
} catch (RateLimitExceededException rle) {
throw new AbortException(rle.getMessage());
} catch (FileNotFoundException fnf) {
// the user may not exist... ok to ignore
}
if (user != null && repoOwner.equalsIgnoreCase(user.getLogin())) {
listener.getLogger().format("Looking up repositories of user %s%n%n", repoOwner);
for (GHRepository repo : user.listRepositories(100)) {
if (repo.isArchived() && gitHubSCMNavigatorContext.isExcludeArchivedRepositories()) {
witness.record(repo.getName(), false);
listener.getLogger()
.println(GitHubConsoleNote.create(
System.currentTimeMillis(),
String.format(
"Skipping repository %s because it is archived", repo.getName())));

} else if (!gitHubSCMNavigatorContext.getTopics().isEmpty()
&& !repo.listTopics().containsAll(gitHubSCMNavigatorContext.getTopics())) {
// exclude repositories which are missing one or more of the specified topics
witness.record(repo.getName(), false);
listener.getLogger()
.println(GitHubConsoleNote.create(
System.currentTimeMillis(),
String.format(
"Skipping repository %s because it is missing one or more of the following topics: '%s'",
repo.getName(), gitHubSCMNavigatorContext.getTopics())));
} else if (gitHubSCMNavigatorContext.isExcludeForkedRepositories()
&& repo.getSource() != null) {
witness.record(repo.getName(), false);
listener.getLogger()
.println(GitHubConsoleNote.create(
System.currentTimeMillis(),
String.format(
"Skipping repository %s because it is a fork", repo.getName())));
} else if (request.process(repo.getName(), sourceFactory, null, witness)) {
listener.getLogger()
.println(GitHubConsoleNote.create(
System.currentTimeMillis(),
String.format(
"%d repositories were processed (query completed)",
witness.getCount())));
}
}
listener.getLogger()
.println(GitHubConsoleNote.create(
System.currentTimeMillis(),
String.format("%d repositories were processed", witness.getCount())));
return;
}

throw new AbortException(
repoOwner + " does not correspond to a known GitHub User Account or Organization");
}
} finally {
Connector.release(github);
}
}

private Iterable<GHRepository> searchRepositories(final GitHub github, final GitHubSCMNavigatorContext context) {
private Iterable<GHRepository> searchRepositories(
final GitHub github, final GitHubSCMNavigatorContext context, final GHOrganization org) {
final GHRepositorySearchBuilder ghRepositorySearchBuilder = github.searchRepositories();
context.getTopics().forEach(ghRepositorySearchBuilder::topic);
ghRepositorySearchBuilder.org(getRepoOwner());
if (!context.isExcludeForkedRepositories()) {
ghRepositorySearchBuilder.q("fork:true");
}

// Only the first 1000 search results are available
if (ghRepositorySearchBuilder.list().getTotalCount() > 1000) {
return org.listRepositories(100);
Copy link
Member

Choose a reason for hiding this comment

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

Looks to me like this is ignoring all the search settings above.
There is also redundant code in these two new methods that can be reduced.

Copy link
Author

Choose a reason for hiding this comment

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

Hello, the query above only returns the first 1000 records. This is a github limitation.
The query also returns the number of existing records and this value is used to make a decision.

}

return ghRepositorySearchBuilder.list().withPageSize(100);

Check warning on line 1256 in src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMNavigator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 1117-1256 are not covered by tests
}

private Iterable<GHRepository> searchRepositories(
final GitHub github, final GitHubSCMNavigatorContext context, final GHMyself myself) {
final GHRepositorySearchBuilder ghRepositorySearchBuilder = github.searchRepositories();
context.getTopics().forEach(ghRepositorySearchBuilder::topic);
ghRepositorySearchBuilder.org(getRepoOwner());
if (!context.isExcludeForkedRepositories()) {
ghRepositorySearchBuilder.q("fork:true");
}

// Only the first 1000 search results are available
if (ghRepositorySearchBuilder.list().getTotalCount() > 1000) {

Check warning on line 1269 in src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMNavigator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 1269 is only partially covered, one branch is missing
return myself.listRepositories(100);

Check warning on line 1270 in src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMNavigator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 1270 is not covered by tests
}

return ghRepositorySearchBuilder.list().withPageSize(100);
}

Expand Down