Skip to content

Commit ec1b1dc

Browse files
committed
refactor cli and env access token implementation
1 parent 64836a8 commit ec1b1dc

File tree

10 files changed

+50
-28
lines changed

10 files changed

+50
-28
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 uploadsUrl, strin
3030
{
3131
apiUrl ??= DEFAULT_API_URL;
3232
uploadsUrl ??= DEFAULT_UPLOADS_URL;
33-
sourcePersonalAccessToken ??= _environmentVariableProvider.SourceGithubPersonalAccessToken();
33+
sourcePersonalAccessToken ??= _environmentVariableProvider.SourceGithubPersonalAccessToken(throwIfNotFound: false);
3434
var githubClient = new GithubClient(_octoLogger, _clientFactory.CreateClient("Default"), _versionProvider, _retryPolicy, _dateTimeProvider, sourcePersonalAccessToken);
3535
var multipartUploader = new ArchiveUploader(githubClient, uploadsUrl, _octoLogger, _retryPolicy);
3636
return new GithubApi(githubClient, apiUrl, _retryPolicy, multipartUploader);
@@ -40,7 +40,7 @@ GithubApi ISourceGithubApiFactory.CreateClientNoSsl(string apiUrl, string upload
4040
{
4141
apiUrl ??= DEFAULT_API_URL;
4242
uploadsUrl ??= DEFAULT_UPLOADS_URL;
43-
sourcePersonalAccessToken ??= _environmentVariableProvider.SourceGithubPersonalAccessToken();
43+
sourcePersonalAccessToken ??= _environmentVariableProvider.SourceGithubPersonalAccessToken(throwIfNotFound: false);
4444
var githubClient = new GithubClient(_octoLogger, _clientFactory.CreateClient("NoSSL"), _versionProvider, _retryPolicy, _dateTimeProvider, sourcePersonalAccessToken);
4545
var multipartUploader = new ArchiveUploader(githubClient, uploadsUrl, _octoLogger, _retryPolicy);
4646
return new GithubApi(githubClient, apiUrl, _retryPolicy, multipartUploader);
@@ -50,7 +50,7 @@ GithubApi ITargetGithubApiFactory.Create(string apiUrl, string uploadsUrl, strin
5050
{
5151
apiUrl ??= DEFAULT_API_URL;
5252
uploadsUrl ??= DEFAULT_UPLOADS_URL;
53-
targetPersonalAccessToken ??= _environmentVariableProvider.TargetGithubPersonalAccessToken();
53+
targetPersonalAccessToken ??= _environmentVariableProvider.TargetGithubPersonalAccessToken(throwIfNotFound: false);
5454
var githubClient = new GithubClient(_octoLogger, _clientFactory.CreateClient("Default"), _versionProvider, _retryPolicy, _dateTimeProvider, targetPersonalAccessToken);
5555
var multipartUploader = new ArchiveUploader(githubClient, uploadsUrl, _octoLogger, _retryPolicy);
5656
return new GithubApi(githubClient, apiUrl, _retryPolicy, multipartUploader);

src/ado2gh/Commands/IntegrateBoards/IntegrateBoardsCommandHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public async Task Handle(IntegrateBoardsCommandArgs args)
2929

3030
_log.LogInformation("Integrating Azure Boards...");
3131

32-
args.GithubPat ??= _environmentVariableProvider.TargetGithubPersonalAccessToken();
32+
args.GithubPat ??= _environmentVariableProvider.TargetGithubPersonalAccessToken(throwIfNotFound: false);
3333

3434
var adoTeamProjectId = await _adoApi.GetTeamProjectId(args.AdoOrg, args.AdoTeamProject);
3535
var githubHandle = await _adoApi.GetGithubHandle(args.AdoOrg, args.AdoTeamProject, args.GithubPat);

src/ado2gh/Commands/MigrateRepo/MigrateRepoCommandHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public async Task Handle(MigrateRepoCommandArgs args)
3030

3131
_log.LogInformation("Migrating Repo...");
3232

33-
args.GithubPat ??= _environmentVariableProvider.TargetGithubPersonalAccessToken();
33+
args.GithubPat ??= _environmentVariableProvider.TargetGithubPersonalAccessToken(throwIfNotFound: false);
3434

3535
var adoRepoUrl = GetAdoRepoUrl(args.AdoOrg, args.AdoTeamProject, args.AdoRepo, args.AdoServerUrl);
3636

src/bbs2gh/Commands/MigrateRepo/MigrateRepoCommandHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private async Task<string> CreateMigrationSource(MigrateRepoCommandArgs args)
227227
{
228228
_log.LogInformation("Creating Migration Source...");
229229

230-
args.GithubPat ??= _environmentVariableProvider.TargetGithubPersonalAccessToken();
230+
args.GithubPat ??= _environmentVariableProvider.TargetGithubPersonalAccessToken(throwIfNotFound: false);
231231
var githubOrgId = await _githubApi.GetOrganizationId(args.GithubOrg);
232232

233233
try
@@ -250,7 +250,7 @@ private async Task ImportArchive(MigrateRepoCommandArgs args, string migrationSo
250250

251251
var bbsRepoUrl = GetBbsRepoUrl(args);
252252

253-
args.GithubPat ??= _environmentVariableProvider.TargetGithubPersonalAccessToken();
253+
args.GithubPat ??= _environmentVariableProvider.TargetGithubPersonalAccessToken(throwIfNotFound: false);
254254
var githubOrgId = await _githubApi.GetOrganizationId(args.GithubOrg);
255255

256256
string migrationId;

src/gei/Commands/MigrateCodeScanningAlerts/MigrateCodeScanningAlertsCommand.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ public override MigrateCodeScanningAlertsCommandHandler BuildHandler(MigrateCode
7878
throw new ArgumentNullException(nameof(sp));
7979
}
8080

81-
var environmentVariableProvider = sp.GetRequiredService<EnvironmentVariableProvider>();
82-
args.GithubSourcePat ??= environmentVariableProvider.SourceGithubPersonalAccessToken(false);
83-
args.GithubTargetPat ??= environmentVariableProvider.TargetGithubPersonalAccessToken();
84-
81+
// The factory handles environment variable resolution
8582
if (args.GithubSourcePat.IsNullOrWhiteSpace())
8683
{
8784
args.GithubSourcePat = args.GithubTargetPat;

src/gei/Commands/MigrateOrg/MigrateOrgCommandHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public async Task Handle(MigrateOrgCommandArgs args)
7272
}
7373

7474
private string GetSourceToken(MigrateOrgCommandArgs args) =>
75-
args.GithubSourcePat ?? _environmentVariableProvider.SourceGithubPersonalAccessToken();
75+
args.GithubSourcePat ?? _environmentVariableProvider.SourceGithubPersonalAccessToken(throwIfNotFound: false);
7676

7777
private string GetGithubOrgUrl(string org, string baseUrl) => $"{baseUrl ?? DEFAULT_GITHUB_BASE_URL}/{org.EscapeDataString()}";
7878
}

src/gei/Commands/MigrateRepo/MigrateRepoCommandHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public async Task Handle(MigrateRepoCommandArgs args)
151151

152152
var sourceRepoUrl = GetSourceRepoUrl(args);
153153
var sourceToken = GetSourceToken(args);
154-
var targetToken = args.GithubTargetPat ?? _environmentVariableProvider.TargetGithubPersonalAccessToken();
154+
var targetToken = args.GithubTargetPat ?? _environmentVariableProvider.TargetGithubPersonalAccessToken(throwIfNotFound: false);
155155

156156
string migrationId;
157157

@@ -212,7 +212,7 @@ public async Task Handle(MigrateRepoCommandArgs args)
212212
_log.LogInformation($"Migration log available at {migrationLogUrl} or by running `gh {CliContext.RootCommand} download-logs --github-target-org {args.GithubTargetOrg} --target-repo {args.TargetRepo}`");
213213
}
214214

215-
private string GetSourceToken(MigrateRepoCommandArgs args) => args.GithubSourcePat ?? _environmentVariableProvider.SourceGithubPersonalAccessToken();
215+
private string GetSourceToken(MigrateRepoCommandArgs args) => args.GithubSourcePat ?? _environmentVariableProvider.SourceGithubPersonalAccessToken(throwIfNotFound: false);
216216

217217
private string GetSourceRepoUrl(MigrateRepoCommandArgs args) => GetGithubRepoUrl(args.GithubSourceOrg, args.SourceRepo, args.GhesApiUrl.HasValue() ? ExtractGhesBaseUrl(args.GhesApiUrl) : null);
218218

src/gei/Commands/MigrateSecretAlerts/MigrateSecretAlertsCommand.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ public override MigrateSecretAlertsCommandHandler BuildHandler(MigrateSecretAler
7878
throw new ArgumentNullException(nameof(sp));
7979
}
8080

81-
var environmentVariableProvider = sp.GetRequiredService<EnvironmentVariableProvider>();
82-
args.GithubSourcePat ??= environmentVariableProvider.SourceGithubPersonalAccessToken(false);
83-
args.GithubTargetPat ??= environmentVariableProvider.TargetGithubPersonalAccessToken();
84-
81+
// The factory handles environment variable resolution
8582
if (args.GithubSourcePat.IsNullOrWhiteSpace())
8683
{
8784
args.GithubSourcePat = args.GithubTargetPat;

src/gei/Factories/CodeScanningAlertServiceFactory.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,27 @@ public CodeScanningAlertServiceFactory(
2525
public virtual CodeScanningAlertService
2626
Create(string sourceApi, string sourceToken, string targetApi, string targetToken, bool sourceApiNoSsl = false)
2727
{
28-
sourceToken ??= _environmentVariableProvider.SourceGithubPersonalAccessToken();
29-
targetToken ??= _environmentVariableProvider.TargetGithubPersonalAccessToken();
28+
// Only resolve from environment if tokens are explicitly null
29+
// Use consistent throwIfNotFound=false to prevent exceptions when CLI args are preferred
30+
sourceToken ??= _environmentVariableProvider.SourceGithubPersonalAccessToken(false);
31+
32+
targetToken ??= _environmentVariableProvider.TargetGithubPersonalAccessToken(false);
33+
34+
// Validate that we have tokens after all resolution attempts
35+
if (string.IsNullOrWhiteSpace(sourceToken))
36+
{
37+
throw new OctoshiftCliException("Source GitHub Personal Access Token is required. Provide it via --github-source-pat argument or GH_SOURCE_PAT/GH_PAT environment variable.");
38+
}
39+
40+
if (string.IsNullOrWhiteSpace(targetToken))
41+
{
42+
throw new OctoshiftCliException("Target GitHub Personal Access Token is required. Provide it via --github-target-pat argument or GH_PAT environment variable.");
43+
}
3044

3145
var sourceGithubApi = sourceApiNoSsl
32-
? _sourceGithubApiFactory.CreateClientNoSsl(sourceApi, sourceToken)
33-
: _sourceGithubApiFactory.Create(sourceApi, sourceToken);
46+
? _sourceGithubApiFactory.CreateClientNoSsl(sourceApi, null, sourceToken)
47+
: _sourceGithubApiFactory.Create(sourceApi, null, sourceToken);
3448

35-
return new(sourceGithubApi, _targetGithubApiFactory.Create(targetApi, targetToken), _octoLogger);
49+
return new(sourceGithubApi, _targetGithubApiFactory.Create(targetApi, null, targetToken), _octoLogger);
3650
}
3751
}

src/gei/Factories/SecretScanningAlertServiceFactory.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,27 @@ public SecretScanningAlertServiceFactory(
2525
public virtual SecretScanningAlertService
2626
Create(string sourceApi, string sourceToken, string targetApi, string targetToken, bool sourceApiNoSsl = false)
2727
{
28-
sourceToken ??= _environmentVariableProvider.SourceGithubPersonalAccessToken();
29-
targetToken ??= _environmentVariableProvider.TargetGithubPersonalAccessToken();
28+
// Only resolve from environment if tokens are explicitly null
29+
// Use consistent throwIfNotFound=false to prevent exceptions when CLI args are preferred
30+
sourceToken ??= _environmentVariableProvider.SourceGithubPersonalAccessToken(false);
31+
32+
targetToken ??= _environmentVariableProvider.TargetGithubPersonalAccessToken(false);
33+
34+
// Validate that we have tokens after all resolution attempts
35+
if (string.IsNullOrWhiteSpace(sourceToken))
36+
{
37+
throw new OctoshiftCliException("Source GitHub Personal Access Token is required. Provide it via --github-source-pat argument or GH_SOURCE_PAT/GH_PAT environment variable.");
38+
}
39+
40+
if (string.IsNullOrWhiteSpace(targetToken))
41+
{
42+
throw new OctoshiftCliException("Target GitHub Personal Access Token is required. Provide it via --github-target-pat argument or GH_PAT environment variable.");
43+
}
3044

3145
var sourceGithubApi = sourceApiNoSsl
32-
? _sourceGithubApiFactory.CreateClientNoSsl(sourceApi, sourceToken)
33-
: _sourceGithubApiFactory.Create(sourceApi, sourceToken);
46+
? _sourceGithubApiFactory.CreateClientNoSsl(sourceApi, null, sourceToken)
47+
: _sourceGithubApiFactory.Create(sourceApi, null, sourceToken);
3448

35-
return new(sourceGithubApi, _targetGithubApiFactory.Create(targetApi, targetToken), _octoLogger);
49+
return new(sourceGithubApi, _targetGithubApiFactory.Create(targetApi, null, targetToken), _octoLogger);
3650
}
3751
}

0 commit comments

Comments
 (0)