Skip to content
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
128 changes: 100 additions & 28 deletions plugins/DNNCommunity.DNNDocs.Plugins/Providers/GitHubApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,148 @@ namespace DNNCommunity.DNNDocs.Plugins.Providers
{
public class GitHubApi
{
private readonly string token;
private readonly string? token;
private readonly GitHubClient client;

private const int RateLimitThreshold = 1000; // Starts throtling if less than x calls remaining for the next hour.
private const int MaxDelayMs = 15000; // Maximum delay of 15 seconds.
private const int RateLimitThreshold = 100; // Start warning if less than this many calls remain for the hour.
private const int MaxDelayMs = 5000; // Maximum progressive-backoff delay of 5 seconds.
private const double ExponentialBase = 1.5; // Exponential base for progressive backoff.

public GitHubApi()
{
DotNetEnv.Env.Load(); // Loads .env file if it exists, into environment varialbes.
DotNetEnv.Env.Load(); // Loads .env file if it exists, into environment variables.
client = new GitHubClient(new ProductHeaderValue("DNNDocsPlugin"));

// Optionally pull from env if not passed

var skip = Environment.GetEnvironmentVariable("SKIP_CONTRIBUTORS") == "true";
if (!skip)
{
this.token = Environment.GetEnvironmentVariable("GithubToken") ?? string.Empty;
// Prefer a PAT (ACCESS_TOKEN) for higher rate limits; fall back to GithubToken (GITHUB_TOKEN in CI).
this.token = Environment.GetEnvironmentVariable("ACCESS_TOKEN");
if (string.IsNullOrEmpty(this.token))
{
this.token = Environment.GetEnvironmentVariable("GithubToken");
}
}

if (!string.IsNullOrEmpty(this.token))
{
client.Credentials = new Credentials(this.token);
}

var rateLimits = client.RateLimit.GetRateLimits().GetAwaiter().GetResult();
Console.WriteLine($"Remaining: {rateLimits.Resources.Core.Remaining}, Resets at: {rateLimits.Resources.Core.Reset}");
try
{
var rateLimits = client.RateLimit.GetRateLimits().GetAwaiter().GetResult();
Console.WriteLine($"[RepoStats] GitHub API remaining: {rateLimits.Resources.Core.Remaining}/{rateLimits.Resources.Core.Limit}, resets at: {rateLimits.Resources.Core.Reset}");
}
catch (Exception ex)
{
Console.WriteLine($"[RepoStats] WARNING: Could not retrieve GitHub rate limit info: {ex.Message}");
}
}

public async Task<IReadOnlyList<Contributor>> GetContributorsAsync()
{
if (string.IsNullOrEmpty(this.token))
{
Console.WriteLine("[RepoStats] No GitHub token configured — skipping contributor stats.");
return new List<Contributor>();
}

if (!await this.ThrottleIfNeeded())
{
return new List<Contributor>();
}

await this.ThrottleIfNeeded();
return await client.Repository.Statistics.GetContributors("DNNCommunity", "DNNDocs");
try
{
return await client.Repository.Statistics.GetContributors("DNNCommunity", "DNNDocs");
}
catch (RateLimitExceededException ex)
{
Console.WriteLine($"[RepoStats] WARNING: GitHub API rate limit exceeded while fetching contributors. Skipping stats. Reset at: {ex.Reset}");
return new List<Contributor>();
}
catch (Exception ex)
{
Console.WriteLine($"[RepoStats] WARNING: Failed to fetch contributors: {ex.Message}. Skipping stats.");
return new List<Contributor>();
}
}

public async Task<IReadOnlyList<GitHubCommit>> GetCommitsAsync(string path = "")
{
if (string.IsNullOrEmpty(this.token))
{
Console.WriteLine("[RepoStats] No GitHub token configured — skipping commit stats.");
return new List<GitHubCommit>();
}

if (!await this.ThrottleIfNeeded())
{
return new List<GitHubCommit>();
}

var request = new CommitRequest();
if (!string.IsNullOrEmpty(path))
request.Path = path;
try
{
var request = new CommitRequest();
if (!string.IsNullOrEmpty(path))
request.Path = path;

await this.ThrottleIfNeeded();
return await client.Repository.Commit.GetAll("DNNCommunity", "DNNDocs", request);
return await client.Repository.Commit.GetAll("DNNCommunity", "DNNDocs", request);
}
catch (RateLimitExceededException ex)
{
Console.WriteLine($"[RepoStats] WARNING: GitHub API rate limit exceeded while fetching commits. Skipping stats. Reset at: {ex.Reset}");
return new List<GitHubCommit>();
}
catch (Exception ex)
{
Console.WriteLine($"[RepoStats] WARNING: Failed to fetch commits: {ex.Message}. Skipping stats.");
return new List<GitHubCommit>();
}
}

private async Task ThrottleIfNeeded()
/// <summary>
/// Checks the current rate limit. Returns <c>true</c> if it is safe to proceed with an API call,
/// or <c>false</c> if the rate limit is exhausted and callers should skip their request.
/// When nearing (but not at) the limit, a short progressive backoff delay is applied.
/// </summary>
private async Task<bool> ThrottleIfNeeded()
{
var rateLimits = await client.RateLimit.GetRateLimits();
var remaining = rateLimits.Resources.Core.Remaining;
var limit = rateLimits.Resources.Core.Limit;
var reset = rateLimits.Resources.Core.Reset;
try
{
var rateLimits = await client.RateLimit.GetRateLimits();
var remaining = rateLimits.Resources.Core.Remaining;
var limit = rateLimits.Resources.Core.Limit;
var reset = rateLimits.Resources.Core.Reset;

if (remaining == 0)
{
var waitSeconds = (int)(reset - DateTimeOffset.UtcNow).TotalSeconds + 5;
Console.WriteLine($"[RepoStats] WARNING: GitHub API rate limit fully exhausted (0/{limit}). Resets at {reset} UTC (~{waitSeconds}s). Skipping stats to avoid blocking the build.");
return false;
}

if (remaining < RateLimitThreshold)
{
int delayFactor = RateLimitThreshold - remaining;
int delayMs = (int)Math.Pow(ExponentialBase, delayFactor);
delayMs = Math.Min(delayMs, MaxDelayMs);
if (remaining < RateLimitThreshold)
{
int delayFactor = RateLimitThreshold - remaining;
int delayMs = (int)Math.Min(Math.Pow(ExponentialBase, delayFactor), MaxDelayMs);

Console.WriteLine($"[RepoStats] Nearing GitHub API rate limit, applying backoff of {delayMs} ms. Remaining: {remaining}/{limit}, resets at {reset}.");
await Task.Delay(delayMs);
}

Console.WriteLine($"[Throttle] Nearing rate limit, delaying {delayMs} ms... GitHub API remaining: {remaining}/{limit}, resets at {reset}");
await Task.Delay(delayMs);
return true;
}
catch (RateLimitExceededException ex)
{
Console.WriteLine($"[RepoStats] WARNING: GitHub API rate limit exceeded during throttle check. Skipping stats. Reset at: {ex.Reset}");
return false;
}
catch (Exception ex)
{
Console.WriteLine($"[RepoStats] WARNING: Could not check rate limit ({ex.Message}). Proceeding cautiously.");
return true;
}
}
}
Expand Down
27 changes: 20 additions & 7 deletions plugins/DNNCommunity.DNNDocs.Plugins/RepoStatsBuildStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ public void Build(FileModel model, IHostService host)
{
}

// IDocumentBuildStep.Postbuild is a synchronous interface contract; bridge to the async implementation.
public void Postbuild(ImmutableList<FileModel> models, IHostService host)
{
=> PostbuildAsync(models, host).GetAwaiter().GetResult();

private async Task PostbuildAsync(ImmutableList<FileModel> models, IHostService host)
{
if (models == null || models.Count == 0)
{
Console.WriteLine("[PLUGIN] No models to process.");
Expand All @@ -36,15 +39,25 @@ public void Postbuild(ImmutableList<FileModel> models, IHostService host)
Console.WriteLine($"[PLUGIN] {nameof(RepoStatsBuildStep)} Processing Repo Stats for {models.Count()} models");

var gitHubApi = new GitHubApi();
var contributors = gitHubApi.GetContributorsAsync().GetAwaiter().GetResult();
Console.WriteLine($"Found {contributors.Count} contributors");

var commits = gitHubApi.GetCommitsAsync().GetAwaiter().GetResult();
List<Octokit.Contributor> contributors;
List<GitHubCommit> commits;

// Order the commits by date
commits = commits.OrderByDescending(c => c.Commit.Author.Date).ToList();
try
{
contributors = (await gitHubApi.GetContributorsAsync()).ToList();
Console.WriteLine($"[RepoStats] Found {contributors.Count} contributors");

Console.WriteLine($"Found {commits.Count} commits");
commits = (await gitHubApi.GetCommitsAsync()).ToList();
// Order the commits by date
commits = commits.OrderByDescending(c => c.Commit.Author.Date).ToList();
Console.WriteLine($"[RepoStats] Found {commits.Count} commits");
}
catch (Exception ex)
{
Console.WriteLine($"[RepoStats] WARNING: Failed to retrieve GitHub stats ({ex.Message}). Skipping contributor data for this build.");
return;
}

if (contributors.Any() && commits.Any())
{
Expand Down
Loading