Skip to content

Commit 478e8d9

Browse files
committed
Merge branch 'pr1410' into brianaj/external-pr-1410
2 parents d141fb1 + 5a71721 commit 478e8d9

File tree

11 files changed

+51
-28
lines changed

11 files changed

+51
-28
lines changed

RELEASENOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11

2+
- Fixed issue where CLI commands required GH_PAT environment variable even when GitHub tokens were provided via command-line arguments (--github-source-pat, --github-target-pat). All migration commands now work correctly with CLI-only token authentication.

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
@@ -229,7 +229,7 @@ private async Task<string> CreateMigrationSource(MigrateRepoCommandArgs args)
229229
{
230230
_log.LogInformation("Creating Migration Source...");
231231

232-
args.GithubPat ??= _environmentVariableProvider.TargetGithubPersonalAccessToken();
232+
args.GithubPat ??= _environmentVariableProvider.TargetGithubPersonalAccessToken(throwIfNotFound: false);
233233
var githubOrgId = await _githubApi.GetOrganizationId(args.GithubOrg);
234234

235235
try
@@ -252,7 +252,7 @@ private async Task ImportArchive(MigrateRepoCommandArgs args, string migrationSo
252252

253253
var bbsRepoUrl = GetBbsRepoUrl(args);
254254

255-
args.GithubPat ??= _environmentVariableProvider.TargetGithubPersonalAccessToken();
255+
args.GithubPat ??= _environmentVariableProvider.TargetGithubPersonalAccessToken(throwIfNotFound: false);
256256
var githubOrgId = await _githubApi.GetOrganizationId(args.GithubOrg);
257257

258258
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
@@ -153,7 +153,7 @@ public async Task Handle(MigrateRepoCommandArgs args)
153153

154154
var sourceRepoUrl = GetSourceRepoUrl(args);
155155
var sourceToken = GetSourceToken(args);
156-
var targetToken = args.GithubTargetPat ?? _environmentVariableProvider.TargetGithubPersonalAccessToken();
156+
var targetToken = args.GithubTargetPat ?? _environmentVariableProvider.TargetGithubPersonalAccessToken(throwIfNotFound: false);
157157

158158
string migrationId;
159159

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

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

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

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
}

0 commit comments

Comments
 (0)