Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Allow user to set calls per minute in the RateLimiter #127

Open
wants to merge 2 commits 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*.iml
.idea
bin
target
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ by the settings configuration property in parentheses.
* Name of repository
* `repositoryOwner`
* Owner of repository
* `callsPerMinute` (`github.global.callsPerMinute`)
* Number of calls per minute to allow in the rate limiter. Default is 20, which allows 20 calls/min. Setting this to a value like 1000 will allow 1000 calls/minute to github. Useful if you are using GitHub Enterprise or other self hosted solution where the limit isn't capped at a fixed rate like the public GitHub.

*Note:* `repositoryOwner` property and `repositoryName` are optional and will be
inferred from the following properties if not specified
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import org.apache.maven.settings.Proxy;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.eclipse.egit.github.core.RepositoryId;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
import org.apache.maven.settings.crypto.SettingsDecrypter;
import org.apache.maven.settings.crypto.SettingsDecryptionResult;
Expand All @@ -42,14 +40,16 @@
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.ContextException;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
import org.eclipse.egit.github.core.RepositoryId;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.client.IGitHubConstants;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.List;
import org.eclipse.egit.github.core.client.IGitHubConstants;

/**
* Base GitHub Mojo class to be extended.
Expand Down Expand Up @@ -134,30 +134,36 @@ protected void info(String message, Throwable throwable) {
log.info(message, throwable);
}

/**
* Create client
*
* @param host
* @param userName
* @param password
* @param oauth2Token
* @param serverId
* @param settings
* @param session
* @return client
* @throws MojoExecutionException
*/
protected GitHubClient createClient(String host, String userName,
String password, String oauth2Token, String serverId,
Settings settings, MavenSession session)
throws MojoExecutionException {
GitHubClient client;
if (!StringUtils.isEmpty(host)) {
if (isDebug())
debug("Using custom host: " + host);
client = createClient(host);
} else
client = createClient();
/**
* Create client
*
* @param host
* @param userName
* @param password
* @param oauth2Token
* @param serverId
* @param settings
* @param session
* @return client
* @throws MojoExecutionException
*/
protected GitHubClient createClient(String host, String userName,
String password, String oauth2Token, String serverId,
Settings settings, MavenSession session) throws MojoExecutionException {
return createClient(host, userName, password, oauth2Token, serverId, settings, session, 20.0);
}

protected GitHubClient createClient(String host, String userName,
String password, String oauth2Token, String serverId,
Settings settings, MavenSession session, double callsPerMinute)
throws MojoExecutionException {
GitHubClient client;
if (!StringUtils.isEmpty(host)) {
if (isDebug())
debug("Using custom host: " + host);
client = createClient(host, callsPerMinute);
} else
client = createClient(callsPerMinute);

{
Proxy proxy = getProxy( settings, serverId, host );
Expand Down Expand Up @@ -203,39 +209,48 @@ protected GitHubClient createClient(String host, String userName,
"No authentication credentials configured");
}

/**
* Create client
* <p>
* Subclasses can override to do any custom client configuration
*
* @param hostname
* @return non-null client
* @throws MojoExecutionException
*/
protected GitHubClient createClient(String hostname)
throws MojoExecutionException {
if (!hostname.contains("://"))
return new RateLimitedGitHubClient(hostname);
try {
URL hostUrl = new URL(hostname);
return new RateLimitedGitHubClient(hostUrl.getHost(), hostUrl.getPort(),
hostUrl.getProtocol());
} catch (MalformedURLException e) {
throw new MojoExecutionException("Could not parse host URL "
+ hostname, e);
}
}
/**
* Create client
* <p>
* Subclasses can override to do any custom client configuration
*
* @param hostname
* @return non-null client
* @throws MojoExecutionException
*/
protected GitHubClient createClient(String hostname)
throws MojoExecutionException {
return createClient(hostname, 20.0);
}

/**
* Create client
* <p>
* Subclasses can override to do any custom client configuration
*
* @return non-null client
*/
protected GitHubClient createClient() {
return new RateLimitedGitHubClient();
}
protected GitHubClient createClient(String hostname, double callsPerMinute)
throws MojoExecutionException {
if (!hostname.contains("://"))
return new RateLimitedGitHubClient(hostname, callsPerMinute);
try {
URL hostUrl = new URL(hostname);
return new RateLimitedGitHubClient(hostUrl.getHost(), hostUrl.getPort(),
hostUrl.getProtocol(), callsPerMinute);
} catch (MalformedURLException e) {
throw new MojoExecutionException("Could not parse host URL "
+ hostname, e);
}
}

/**
* Create client
* <p>
* Subclasses can override to do any custom client configuration
*
* @return non-null client
*/
protected GitHubClient createClient() {
return createClient(20.0);
}

protected GitHubClient createClient(double callsPerMinute) {
return new RateLimitedGitHubClient(callsPerMinute);
}

/**
* Configure credentials from configured username/password combination
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ public class RateLimitedGitHubClient extends GitHubClientEgit {
* AS per https://github.com/octokit/octokit.net/issues/638#issuecomment-67795998,
* it seems that GitHub only allow 20 API calls per 1-minute period
*/
private RateLimiter rateLimiter = RateLimiter.create(20.0/60.0);
private RateLimiter rateLimiter;

public RateLimitedGitHubClient() {
public RateLimitedGitHubClient(double callsPerMinute) {
super();
rateLimiter = RateLimiter.create(callsPerMinute/60.0);
}

public RateLimitedGitHubClient(String hostname) {
public RateLimitedGitHubClient(String hostname, double callsPerMinute) {
super(hostname);
rateLimiter = RateLimiter.create(callsPerMinute/60.0);
}

public RateLimitedGitHubClient(String hostname, int port, String scheme) {
public RateLimitedGitHubClient(String hostname, int port, String scheme, double callsPerMinute) {
super(hostname, port, scheme);
rateLimiter = RateLimiter.create(callsPerMinute/60.0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,17 @@ private class TestMojo extends GitHubProjectMojo {

private final AtomicReference<String> token = new AtomicReference<String>();

protected GitHubClient createClient() {
try
{
DefaultPlexusContainer container = new DefaultPlexusContainer();
Context context = container.getContext();
context.put( PlexusConstants.PLEXUS_KEY, container );
super.contextualize(context );
}
catch ( PlexusContainerException pce )
{
pce.printStackTrace( System.err );
}
catch ( ContextException ce )
{
ce.printStackTrace( System.err );
}
protected GitHubClient createClient(double callsPerMinute) {
try {
DefaultPlexusContainer container = new DefaultPlexusContainer();
Context context = container.getContext();
context.put(PlexusConstants.PLEXUS_KEY, container);
super.contextualize(context);
} catch (PlexusContainerException pce) {
pce.printStackTrace(System.err);
} catch (ContextException ce) {
ce.printStackTrace(System.err);
}

return new GitHubClient() {
public GitHubClient setCredentials(String user, String password) {
Expand All @@ -85,17 +80,29 @@ public GitHubClient setOAuth2Token(String token) {
return super.setOAuth2Token(token);
}

};
}
};
}

@Override
public GitHubClient createClient(String host, String userName,
String password, String oauth2Token, String serverId,
Settings settings, MavenSession session)
throws MojoExecutionException {
return super.createClient(host, userName, password, oauth2Token,
serverId, settings, session);
}
protected GitHubClient createClient() {
return createClient(20.0);
}

public GitHubClient createClient(String host, String userName,
String password, String oauth2Token, String serverId,
Settings settings, MavenSession session, double callsPerMinute)
throws MojoExecutionException {
return super.createClient(host, userName, password, oauth2Token,
serverId, settings, session, callsPerMinute);
}

@Override
public GitHubClient createClient(String host, String userName,
String password, String oauth2Token, String serverId,
Settings settings, MavenSession session)
throws MojoExecutionException {
return this.createClient(host, userName, password, oauth2Token,
serverId, settings, session, 20.0);
}

public void execute() throws MojoExecutionException,
MojoFailureException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,22 @@ private class TestMojo extends GitHubProjectMojo {

private final AtomicReference<String> host = new AtomicReference<String>();

protected GitHubClient createClient() {
protected GitHubClient createClient(double callsPerMinute) {
host.set(null);
return super.createClient();
return super.createClient(20.0);
}

protected GitHubClient createClient(String hostname)
throws MojoExecutionException {
protected GitHubClient createClient(String hostname, double callsPerMinute) throws MojoExecutionException {
host.set(hostname);
return super.createClient(hostname);
return super.createClient(hostname, callsPerMinute);
}

public GitHubClient createClient(String host, String userName,
String password, String oauth2Token, String serverId,
Settings settings, MavenSession session)
Settings settings, MavenSession session, double callsPerMinute)
throws MojoExecutionException {
return super.createClient(host, userName, password, oauth2Token,
serverId, settings, session);
serverId, settings, session, callsPerMinute);
}

public void execute() throws MojoExecutionException,
Expand Down
2 changes: 1 addition & 1 deletion github-site-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<description>Maven plugin that commits files to a branch in a GitHub repository</description>

<properties>
<last.released.version>0.9</last.released.version>
<last.released.version>0.12</last.released.version>
</properties>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ public class SiteMojo extends GitHubProjectMojo {
*/
private boolean skip;

/**
* The number of calls per minute to allow to github. Default to 20.
*
* @parameter expression="${github.site.callsPerMinute}"
* default-value="20.0"
*/
private double callsPerMinute;

/**
* Create blob
*
Expand Down Expand Up @@ -328,7 +336,7 @@ public void execute() throws MojoExecutionException {
Arrays.toString(paths)));

DataService service = new DataService(createClient(host, userName,
password, oauth2Token, server, settings, session));
password, oauth2Token, server, settings, session, callsPerMinute));

// Write blobs and build tree entries
List<TreeEntry> entries = new ArrayList<TreeEntry>(paths.length);
Expand Down