-
Notifications
You must be signed in to change notification settings - Fork 119
Refactor the inconsistent access token resolution between ENV and CLI args #1410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,7 +151,7 @@ public async Task Handle(MigrateRepoCommandArgs args) | |
|
||
var sourceRepoUrl = GetSourceRepoUrl(args); | ||
var sourceToken = GetSourceToken(args); | ||
var targetToken = args.GithubTargetPat ?? _environmentVariableProvider.TargetGithubPersonalAccessToken(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we want to throw here? If we can't find a target PAT we can't continue with this command. |
||
var targetToken = args.GithubTargetPat ?? _environmentVariableProvider.TargetGithubPersonalAccessToken(throwIfNotFound: false); | ||
|
||
string migrationId; | ||
|
||
|
@@ -212,7 +212,7 @@ public async Task Handle(MigrateRepoCommandArgs args) | |
_log.LogInformation($"Migration log available at {migrationLogUrl} or by running `gh {CliContext.RootCommand} download-logs --github-target-org {args.GithubTargetOrg} --target-repo {args.TargetRepo}`"); | ||
} | ||
|
||
private string GetSourceToken(MigrateRepoCommandArgs args) => args.GithubSourcePat ?? _environmentVariableProvider.SourceGithubPersonalAccessToken(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we want to throw here? If we can't find a source PAT we can't continue with this command. |
||
private string GetSourceToken(MigrateRepoCommandArgs args) => args.GithubSourcePat ?? _environmentVariableProvider.SourceGithubPersonalAccessToken(throwIfNotFound: false); | ||
|
||
private string GetSourceRepoUrl(MigrateRepoCommandArgs args) => GetGithubRepoUrl(args.GithubSourceOrg, args.SourceRepo, args.GhesApiUrl.HasValue() ? ExtractGhesBaseUrl(args.GhesApiUrl) : null); | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -25,13 +25,27 @@ public CodeScanningAlertServiceFactory( | |||||
public virtual CodeScanningAlertService | ||||||
Create(string sourceApi, string sourceToken, string targetApi, string targetToken, bool sourceApiNoSsl = false) | ||||||
{ | ||||||
sourceToken ??= _environmentVariableProvider.SourceGithubPersonalAccessToken(); | ||||||
targetToken ??= _environmentVariableProvider.TargetGithubPersonalAccessToken(); | ||||||
// Only resolve from environment if tokens are explicitly null | ||||||
// Use consistent throwIfNotFound=false to prevent exceptions when CLI args are preferred | ||||||
sourceToken ??= _environmentVariableProvider.SourceGithubPersonalAccessToken(false); | ||||||
|
||||||
targetToken ??= _environmentVariableProvider.TargetGithubPersonalAccessToken(false); | ||||||
|
||||||
// Validate that we have tokens after all resolution attempts | ||||||
if (string.IsNullOrWhiteSpace(sourceToken)) | ||||||
{ | ||||||
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."); | ||||||
} | ||||||
|
||||||
if (string.IsNullOrWhiteSpace(targetToken)) | ||||||
{ | ||||||
throw new OctoshiftCliException("Target GitHub Personal Access Token is required. Provide it via --github-target-pat argument or GH_PAT environment variable."); | ||||||
} | ||||||
|
||||||
var sourceGithubApi = sourceApiNoSsl | ||||||
? _sourceGithubApiFactory.CreateClientNoSsl(sourceApi, sourceToken) | ||||||
: _sourceGithubApiFactory.Create(sourceApi, sourceToken); | ||||||
? _sourceGithubApiFactory.CreateClientNoSsl(sourceApi, null, sourceToken) | ||||||
: _sourceGithubApiFactory.Create(sourceApi, null, sourceToken); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] The
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
|
||||||
return new(sourceGithubApi, _targetGithubApiFactory.Create(targetApi, targetToken), _octoLogger); | ||||||
return new(sourceGithubApi, _targetGithubApiFactory.Create(targetApi, null, targetToken), _octoLogger); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] The
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -25,13 +25,27 @@ public SecretScanningAlertServiceFactory( | |||||
public virtual SecretScanningAlertService | ||||||
Create(string sourceApi, string sourceToken, string targetApi, string targetToken, bool sourceApiNoSsl = false) | ||||||
{ | ||||||
sourceToken ??= _environmentVariableProvider.SourceGithubPersonalAccessToken(); | ||||||
targetToken ??= _environmentVariableProvider.TargetGithubPersonalAccessToken(); | ||||||
// Only resolve from environment if tokens are explicitly null | ||||||
// Use consistent throwIfNotFound=false to prevent exceptions when CLI args are preferred | ||||||
sourceToken ??= _environmentVariableProvider.SourceGithubPersonalAccessToken(false); | ||||||
|
||||||
targetToken ??= _environmentVariableProvider.TargetGithubPersonalAccessToken(false); | ||||||
|
||||||
// Validate that we have tokens after all resolution attempts | ||||||
if (string.IsNullOrWhiteSpace(sourceToken)) | ||||||
{ | ||||||
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."); | ||||||
} | ||||||
|
||||||
if (string.IsNullOrWhiteSpace(targetToken)) | ||||||
{ | ||||||
throw new OctoshiftCliException("Target GitHub Personal Access Token is required. Provide it via --github-target-pat argument or GH_PAT environment variable."); | ||||||
} | ||||||
|
||||||
var sourceGithubApi = sourceApiNoSsl | ||||||
? _sourceGithubApiFactory.CreateClientNoSsl(sourceApi, sourceToken) | ||||||
: _sourceGithubApiFactory.Create(sourceApi, sourceToken); | ||||||
? _sourceGithubApiFactory.CreateClientNoSsl(sourceApi, null, sourceToken) | ||||||
: _sourceGithubApiFactory.Create(sourceApi, null, sourceToken); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] The
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
|
||||||
return new(sourceGithubApi, _targetGithubApiFactory.Create(targetApi, targetToken), _octoLogger); | ||||||
return new(sourceGithubApi, _targetGithubApiFactory.Create(targetApi, null, targetToken), _octoLogger); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] The Note: See the diff below for a potential fix: @@ -43,9 +43,9 @@
}
var sourceGithubApi = sourceApiNoSsl
- ? _sourceGithubApiFactory.CreateClientNoSsl(sourceApi, null, sourceToken)
- : _sourceGithubApiFactory.Create(sourceApi, null, sourceToken);
+ ? _sourceGithubApiFactory.CreateClientNoSsl(sourceApi, uploadsUrl: null, sourceToken)
+ : _sourceGithubApiFactory.Create(sourceApi, uploadsUrl: null, sourceToken);
- return new(sourceGithubApi, _targetGithubApiFactory.Create(targetApi, null, targetToken), _octoLogger);
+ return new(sourceGithubApi, _targetGithubApiFactory.Create(targetApi, uploadsUrl: null, targetToken), _octoLogger);
}
}
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we want to throw here? If we can't find a source PAT we can't continue with this command.