-
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?
Conversation
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.
Pull Request Overview
This pull request standardizes GitHub Personal Access Token (PAT) resolution across all CLI commands by refactoring the inconsistent handling between command-line arguments and environment variables. The primary goal is to ensure CLI-provided tokens take precedence without requiring environment variables to be set.
Key changes include:
- Unified token resolution strategy with CLI args taking precedence over environment variables
- Consistent error handling with
throwIfNotFound=false
for environment variable resolution - Centralized validation in factory classes with descriptive error messages
Reviewed Changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.
Show a summary per file
File | Description |
---|---|
src/gei/Factories/SecretScanningAlertServiceFactory.cs |
Added consistent token resolution with fallback and validation logic |
src/gei/Factories/CodeScanningAlertServiceFactory.cs |
Added consistent token resolution with fallback and validation logic |
src/gei/Commands/MigrateSecretAlerts/MigrateSecretAlertsCommand.cs |
Removed redundant environment variable resolution from command handler |
src/gei/Commands/MigrateRepo/MigrateRepoCommandHandler.cs |
Updated to use throwIfNotFound=false for environment variable resolution |
src/gei/Commands/MigrateOrg/MigrateOrgCommandHandler.cs |
Updated to use throwIfNotFound=false for environment variable resolution |
src/gei/Commands/MigrateCodeScanningAlerts/MigrateCodeScanningAlertsCommand.cs |
Removed redundant environment variable resolution from command handler |
src/bbs2gh/Commands/MigrateRepo/MigrateRepoCommandHandler.cs |
Updated to use throwIfNotFound=false for environment variable resolution |
src/ado2gh/Commands/MigrateRepo/MigrateRepoCommandHandler.cs |
Updated to use throwIfNotFound=false for environment variable resolution |
src/ado2gh/Commands/IntegrateBoards/IntegrateBoardsCommandHandler.cs |
Updated to use throwIfNotFound=false for environment variable resolution |
src/Octoshift/Factories/GithubApiFactory.cs |
Updated to use throwIfNotFound=false for environment variable resolution |
RELEASENOTES.md |
Added user-friendly description of the fix |
? _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 comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The null
parameter being passed as the second argument appears to be for uploadsUrl
. Consider using a named parameter or constant to make the intent clearer, such as uploadsUrl: null
or defining a constant for the default uploads URL.
: _sourceGithubApiFactory.Create(sourceApi, null, sourceToken); | |
: _sourceGithubApiFactory.Create(sourceApi, uploadsUrl: null, sourceToken); |
Copilot uses AI. Check for mistakes.
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The null
parameter being passed as the second argument appears to be for uploadsUrl
. Consider using a named parameter or constant to make the intent clearer, such as uploadsUrl: null
or defining a constant for the default uploads URL.
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.
? _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 comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The null
parameter being passed as the second argument appears to be for uploadsUrl
. Consider using a named parameter or constant to make the intent clearer, such as uploadsUrl: null
or defining a constant for the default uploads URL.
: _sourceGithubApiFactory.Create(sourceApi, null, sourceToken); | |
: _sourceGithubApiFactory.Create(sourceApi, uploadsUrl: null, sourceToken); |
Copilot uses AI. Check for mistakes.
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The null
parameter being passed as the second argument appears to be for uploadsUrl
. Consider using a named parameter or constant to make the intent clearer, such as uploadsUrl: null
or defining a constant for the default uploads URL.
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.
Unit Test Results 1 files 1 suites 20s ⏱️ Results for commit 5a71721. |
@@ -72,7 +72,7 @@ public async Task Handle(MigrateOrgCommandArgs args) | |||
} | |||
|
|||
private string GetSourceToken(MigrateOrgCommandArgs args) => |
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.
@@ -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 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 target PAT we can't continue with this command.
@@ -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 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.
This pull request improves how GitHub Personal Access Tokens (PATs) are resolved throughout the codebase.
The issue was identified in the #1409. After investigating it turned out there were inconsistencies on how we resolve PATs CLI provided and ENV provided ones.
The following issues were identified:
This PR tries to consolidate to one solution and uses the same pattern everywhere. The order is as follows:
Improvements from user perspective:
Code perspective:
ThirdPartyNotices.txt
(if applicable)