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

Specify a prefix for limiting the list of Gerrit Projects #391

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -54,8 +54,9 @@ public class GerritProjectListUpdater extends Thread implements ConnectionListen
/**
* The command for fetching projects.
*/
public static final String GERRIT_LS_PROJECTS = "gerrit ls-projects";
public static final String GERRIT_LS_PROJECTS = "gerrit ls-projects --prefix ";
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: The --prefix option is not documented on the ls-projects command for 2.14, 2.15 or 2.16.

Copy link
Member Author

Choose a reason for hiding this comment

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

True, this needs to be fixed in Gerrit though.

With regards to the support in Gerrit < 2.13, needs to be documented in this plugin and checked as soon as the version is queried.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've posted the documentation for review https://gerrit-review.googlesource.com/c/gerrit/+/212012

private static final int MAX_WAIT_TIME = 64;
private final String projectPrefix;

private AtomicBoolean connected = new AtomicBoolean(false);
private boolean shutdown = false;
Expand All @@ -66,11 +67,13 @@ public class GerritProjectListUpdater extends Thread implements ConnectionListen
/**
* Default constructor.
* @param serverName the name of the Gerrit server.
* @param projectPrefix of the projects to list from the Gerrit server.
*/
public GerritProjectListUpdater(String serverName) {
public GerritProjectListUpdater(String serverName, String projectPrefix) {
this.setName(this.getClass().getName() + " for " + serverName + " Thread");
this.setDaemon(true);
this.serverName = serverName;
this.projectPrefix = projectPrefix;
addThisAsListener();
}

Expand Down Expand Up @@ -239,7 +242,8 @@ private void tryLoadProjectList() {
activeConfig.getGerritProxy(),
activeConfig.getGerritAuthentication()
);
List<String> projects = readProjects(sshConnection.executeCommandReader(GERRIT_LS_PROJECTS));
List<String> projects = readProjects(sshConnection
.executeCommandReader(GERRIT_LS_PROJECTS + projectPrefix));
Copy link
Member

Choose a reason for hiding this comment

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

Should we assume that htis works if projectPrefix is an empty string?
In that case I guess it won't work when it's null

if (projects.size() > 0) {
setGerritProjects(projects);
logger.info("Project list from {} contains {} entries", serverName, projects.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public class GerritServer implements Describable<GerritServer>, Action {
private static final int RESPONSE_INTERVAL_MS = 1000;
private static final int RESPONSE_TIMEOUT_S = 10;
private String name;
private String projectPrefix;
@Deprecated
private transient boolean pseudoMode;
private boolean noConnectionOnStartup;
Expand Down Expand Up @@ -251,6 +252,26 @@ public String getName() {
return name;
}

/**
* Get the prefix for fetching all project names.
*
* @return projects prefix.
*/
@Exported
public String getProjectPrefix() {
return projectPrefix;
}

/**
* Set the prefix for fetching all project names.
*
* @param projectPrefix prefix of the projects to list from the Gerrit Server.
*/
@Exported
public void setProjectPrefix(String projectPrefix) {
Copy link
Contributor

@dpursehouse dpursehouse Jan 28, 2019

Choose a reason for hiding this comment

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

Maybe I'm missing something, but where does this get called?

If it's not called from anywhere, the projectPrefix is uninitialized which results in the full list being returned, i.e. the same behavior as before.

Copy link
Member Author

Choose a reason for hiding this comment

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

For backward compatibility, it will work as before.

Need to find a place to hook into it, do you have any suggestions? I’m not very familiar with this codebase :-(

Copy link
Member

Choose a reason for hiding this comment

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

Yes this uses an outdated way of doing Jenkins form submission. See public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws ServletException... in the decriptor.

this.projectPrefix = projectPrefix;
}

/**
* Get hostname of the server.
*
Expand Down Expand Up @@ -445,7 +466,7 @@ public void start() {
initializeConnectionListener();

projectListUpdater =
new GerritProjectListUpdater(name);
new GerritProjectListUpdater(name, projectPrefix);
projectListUpdater.start();

missedEventsPlaybackManager.checkIfEventsLogPluginSupported();
Expand Down