Skip to content

Commit 7a93be3

Browse files
committed
Refactor ArchiveUploader to use EnvironmentVariableProvider
ArchiveUploader no longer reads the env variable directly. This eliminates the race condition in the tests
1 parent fa462b5 commit 7a93be3

File tree

7 files changed

+84
-66
lines changed

7 files changed

+84
-66
lines changed

src/Octoshift/Factories/GithubApiFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ GithubApi ISourceGithubApiFactory.Create(string apiUrl, string sourcePersonalAcc
3030
apiUrl ??= DEFAULT_API_URL;
3131
sourcePersonalAccessToken ??= _environmentVariableProvider.SourceGithubPersonalAccessToken();
3232
var githubClient = new GithubClient(_octoLogger, _clientFactory.CreateClient("Default"), _versionProvider, _retryPolicy, _dateTimeProvider, sourcePersonalAccessToken);
33-
var multipartUploader = new ArchiveUploader(githubClient, _octoLogger, _retryPolicy);
33+
var multipartUploader = new ArchiveUploader(githubClient, _octoLogger, _retryPolicy, _environmentVariableProvider);
3434
return new GithubApi(githubClient, apiUrl, _retryPolicy, multipartUploader);
3535
}
3636

@@ -39,7 +39,7 @@ GithubApi ISourceGithubApiFactory.CreateClientNoSsl(string apiUrl, string source
3939
apiUrl ??= DEFAULT_API_URL;
4040
sourcePersonalAccessToken ??= _environmentVariableProvider.SourceGithubPersonalAccessToken();
4141
var githubClient = new GithubClient(_octoLogger, _clientFactory.CreateClient("NoSSL"), _versionProvider, _retryPolicy, _dateTimeProvider, sourcePersonalAccessToken);
42-
var multipartUploader = new ArchiveUploader(githubClient, _octoLogger, _retryPolicy);
42+
var multipartUploader = new ArchiveUploader(githubClient, _octoLogger, _retryPolicy, _environmentVariableProvider);
4343
return new GithubApi(githubClient, apiUrl, _retryPolicy, multipartUploader);
4444
}
4545

@@ -48,7 +48,7 @@ GithubApi ITargetGithubApiFactory.Create(string apiUrl, string targetPersonalAcc
4848
apiUrl ??= DEFAULT_API_URL;
4949
targetPersonalAccessToken ??= _environmentVariableProvider.TargetGithubPersonalAccessToken();
5050
var githubClient = new GithubClient(_octoLogger, _clientFactory.CreateClient("Default"), _versionProvider, _retryPolicy, _dateTimeProvider, targetPersonalAccessToken);
51-
var multipartUploader = new ArchiveUploader(githubClient, _octoLogger, _retryPolicy);
51+
var multipartUploader = new ArchiveUploader(githubClient, _octoLogger, _retryPolicy, _environmentVariableProvider);
5252
return new GithubApi(githubClient, apiUrl, _retryPolicy, multipartUploader);
5353
}
5454
}

src/Octoshift/Services/ArchiveUploader.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@ namespace OctoshiftCLI.Services;
1212

1313
public class ArchiveUploader
1414
{
15+
private const int MIN_MULTIPART_BYTES = 5 * 1024 * 1024; // 5 MiB minimum size for multipart upload. Don't allow overrides smaller than this.
16+
1517
private readonly GithubClient _client;
1618
private readonly OctoLogger _log;
19+
private readonly EnvironmentVariableProvider _environmentVariableProvider;
1720
internal int _streamSizeLimit = 100 * 1024 * 1024; // 100 MiB
18-
private const int MIN_MULTIPART_BYTES = 5 * 1024 * 1024; // 5 MiB minimum size for multipart upload. Don't allow overrides smaller than this.
1921
private readonly RetryPolicy _retryPolicy;
2022

2123
private const string BASE_URL = "https://uploads.github.com";
2224

23-
public ArchiveUploader(GithubClient client, OctoLogger log, RetryPolicy retryPolicy)
25+
public ArchiveUploader(GithubClient client, OctoLogger log, RetryPolicy retryPolicy, EnvironmentVariableProvider environmentVariableProvider)
2426
{
2527
_client = client;
2628
_log = log;
2729
_retryPolicy = retryPolicy;
30+
_environmentVariableProvider = environmentVariableProvider;
2831

2932
SetStreamSizeLimitFromEnvironment();
3033
}
@@ -166,7 +169,7 @@ private Uri GetNextUrl(IEnumerable<KeyValuePair<string, IEnumerable<string>>> he
166169

167170
private void SetStreamSizeLimitFromEnvironment()
168171
{
169-
var envValue = Environment.GetEnvironmentVariable("GITHUB_OWNED_STORAGE_MULTIPART_BYTES");
172+
var envValue = _environmentVariableProvider.GithubOwnedStorageMultipartBytes();
170173
if (!int.TryParse(envValue, out var limit) || limit <= 0)
171174
{
172175
return;

src/Octoshift/Services/EnvironmentVariableProvider.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class EnvironmentVariableProvider
1818
private const string SMB_PASSWORD = "SMB_PASSWORD";
1919
private const string GEI_SKIP_STATUS_CHECK = "GEI_SKIP_STATUS_CHECK";
2020
private const string GEI_SKIP_VERSION_CHECK = "GEI_SKIP_VERSION_CHECK";
21+
private const string GITHUB_OWNED_STORAGE_MULTIPART_BYTES = "GITHUB_OWNED_STORAGE_MULTIPART_BYTES";
2122

2223
private readonly OctoLogger _logger;
2324

@@ -65,6 +66,9 @@ public virtual string SkipStatusCheck(bool throwIfNotFound = false) =>
6566
public virtual string SkipVersionCheck(bool throwIfNotFound = false) =>
6667
GetValue(GEI_SKIP_VERSION_CHECK, throwIfNotFound);
6768

69+
public virtual string GithubOwnedStorageMultipartBytes(bool throwIfNotFound = false) =>
70+
GetValue(GITHUB_OWNED_STORAGE_MULTIPART_BYTES, throwIfNotFound);
71+
6872
private string GetValue(string name, bool throwIfNotFound)
6973
{
7074
var value = Environment.GetEnvironmentVariable(name);

src/OctoshiftCLI.IntegrationTests/BbsToGithub.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public BbsToGithub(ITestOutputHelper output)
6060
_targetGithubHttpClient = new HttpClient();
6161
_targetGithubClient = new GithubClient(_logger, _targetGithubHttpClient, new VersionChecker(_versionClient, _logger), new RetryPolicy(_logger), new DateTimeProvider(), targetGithubToken);
6262
var retryPolicy = new RetryPolicy(_logger);
63-
_archiveUploader = new ArchiveUploader(_targetGithubClient, _logger, retryPolicy);
63+
var environmentVariableProvider = new EnvironmentVariableProvider(_logger);
64+
_archiveUploader = new ArchiveUploader(_targetGithubClient, _logger, retryPolicy, environmentVariableProvider);
6465
_targetGithubApi = new GithubApi(_targetGithubClient, "https://api.github.com", new RetryPolicy(_logger), _archiveUploader);
6566

6667
_blobServiceClient = new BlobServiceClient(_azureStorageConnectionString);

src/OctoshiftCLI.IntegrationTests/GhesToGithub.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ public GhesToGithub(ITestOutputHelper output)
4848

4949
_versionClient = new HttpClient();
5050
var retryPolicy = new RetryPolicy(logger);
51-
_archiveUploader = new ArchiveUploader(_targetGithubClient, logger, retryPolicy);
51+
var environmentVariableProvider = new EnvironmentVariableProvider(logger);
5252

5353
_sourceGithubHttpClient = new HttpClient();
5454
_sourceGithubClient = new GithubClient(logger, _sourceGithubHttpClient, new VersionChecker(_versionClient, logger), new RetryPolicy(logger), new DateTimeProvider(), sourceGithubToken);
55+
_archiveUploader = new ArchiveUploader(_sourceGithubClient, logger, retryPolicy, environmentVariableProvider);
5556
_sourceGithubApi = new GithubApi(_sourceGithubClient, GHES_API_URL, new RetryPolicy(logger), _archiveUploader);
5657

5758
_targetGithubHttpClient = new HttpClient();

0 commit comments

Comments
 (0)