Skip to content

Commit

Permalink
Use GITHUB_TOKEN to limit GitHub API rate-limiting (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
BMurri authored Feb 18, 2025
1 parent b1032c3 commit c91e6e0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/build-push-acr/AcrBuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static IArchive GetGitHubArchive(BuildType buildType, string @ref, Micros
? new Microsoft.Kiota.Abstractions.Authentication.AnonymousAuthenticationProvider()
: new GitHub.Octokit.Client.Authentication.TokenAuthProvider(tokenProvider))
.WithUserAgent("microsoft-ga4gh-tes", string.Empty)
.WithRequestTimeout(TimeSpan.FromSeconds(100))
.WithRequestTimeout(TimeSpan.FromHours(1.5))
//.WithBaseUrl("https://api.github.com")
.Build()),
"microsoft",
Expand Down
23 changes: 23 additions & 0 deletions src/build-push-acr/GitHubArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO.Compression;
using System.Net;
using CommonUtilities;
using Microsoft.Kiota.Abstractions.Authentication;

namespace BuildPushAcr
{
Expand All @@ -24,6 +25,28 @@ public sealed class GitHubArchive(GitHub.GitHubClient client, string owner, stri
private string? srcRoot;
private string? root;

public static IAccessTokenProvider? GetAccessTokenProvider()
{
var pat = Environment.GetEnvironmentVariable("GITHUB_TOKEN");

if (string.IsNullOrWhiteSpace(pat))
{
return default;
}

return new AccessTokenProvider(pat);
}

private class AccessTokenProvider(string pat) : IAccessTokenProvider
{
private readonly string pat = pat;

AllowedHostsValidator IAccessTokenProvider.AllowedHostsValidator { get; } = new(["api.github.com"]);

Task<string> IAccessTokenProvider.GetAuthorizationTokenAsync(Uri uri, Dictionary<string, object>? additionalAuthenticationContext, CancellationToken cancellationToken)
=> Task.FromResult(pat);
}

async ValueTask<Version> IArchive.GetTagAsync(CancellationToken cancellationToken)
{
List<GitHub.Models.Tag> tags = [];
Expand Down
4 changes: 3 additions & 1 deletion src/build-push-acr/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ IArchive GetLocalGitArchive(System.CommandLine.Invocation.InvocationContext cont
=> AcrBuild.GetLocalGitArchiveAsync(context.ParseResult.GetValueForOption(directoryOption)!);

IArchive GetGitHubArchive(System.CommandLine.Invocation.InvocationContext context)
=> AcrBuild.GetGitHubArchive(context.ParseResult.GetValueForOption(buildTypeOption), context.ParseResult.GetValueForOption(tagOption)!.ToString(3));
=> AcrBuild.GetGitHubArchive(context.ParseResult.GetValueForOption(buildTypeOption),
context.ParseResult.GetValueForOption(tagOption)!.ToString(3),
GitHubArchive.GetAccessTokenProvider());

githubCommand.SetHandler(context => Handler(context, GetGitHubArchive));
localCommand.SetHandler(context => Handler(context, GetLocalGitArchive));
Expand Down
2 changes: 1 addition & 1 deletion src/deploy-tes-on-azure/Deployer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1335,7 +1335,7 @@ private async Task BuildPushAcrAsync(Dictionary<string, string> settings, string

if (string.IsNullOrWhiteSpace(configuration.SolutionDir))
{
tar = AcrBuild.GetGitHubArchive(BuildType.Tes, string.IsNullOrWhiteSpace(configuration.GitHubCommit) ? new Version(targetVersion).ToString(3) : configuration.GitHubCommit);
tar = AcrBuild.GetGitHubArchive(BuildType.Tes, string.IsNullOrWhiteSpace(configuration.GitHubCommit) ? new Version(targetVersion).ToString(3) : configuration.GitHubCommit, GitHubArchive.GetAccessTokenProvider());
tarDisposable = tar as IAsyncDisposable;
}
else
Expand Down
15 changes: 15 additions & 0 deletions src/deploy-tes-on-azure/deploy-tes-on-azure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,19 @@
<ProjectReference Include="..\build-push-acr\build-push-acr.csproj" />
</ItemGroup>

<!-- Not needed in NET SDK 9 -->
<Target Name="RemoveSingleFileHostsFromDependencies" BeforeTargets="_ComputeCopyToPublishDirectoryItems">
<PropertyGroup>
<SingleFileHostIntermediatePath Condition=" '$(SingleFileHostIntermediatePath)' == '' ">$(AppHostIntermediatePath)</SingleFileHostIntermediatePath>
</PropertyGroup>

<ItemGroup>
<__ItemsToRemove Remove="@(__ItemsToRemove)" />
<__ItemsToRemove Condition=" '%(Filename)' == '$(_DotNetSingleFileHostExecutableNameWithoutExtension)' And '%(FullPath)' != '$(SingleFileHostIntermediatePath)' "
Include="@(_SourceItemsToCopyToPublishDirectoryAlways)" />
<_SourceItemsToCopyToPublishDirectoryAlways Remove="@(__ItemsToRemove)" />
<__ItemsToRemove Remove="@(__ItemsToRemove)" />
</ItemGroup>
</Target>

</Project>

0 comments on commit c91e6e0

Please sign in to comment.