Skip to content

🩹 Color token expiration time based on validity in authentication contexts #431

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

Closed
wants to merge 4 commits into from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 21, 2025

This change implements color-coded display of token expiration times in the GitHub authentication system to provide better visual feedback about token validity.

Changes Made

Modified Update-GitHubUserAccessToken.ps1 to color the token expiration time text based on remaining hours:

  • Green if above 4 hours remaining
  • Yellow if between 1 and 4 hours remaining
  • Orange (DarkYellow) if between 0 and 1 hour remaining
  • Red if expired (negative time)

Implementation Details

The changes are minimal and surgical:

  1. Added color determination logic after calculating $accessTokenValidityText
  2. Updated two Write-Host statement blocks to display colored time text:
    • Valid token display with green checkmark (✓)
    • Warning display with yellow warning symbol (⚠)
  3. Split Write-Host statements to separately color just the time portion while preserving existing message formatting

Before/After Example

Before:

✓ Access token is still valid for 02:30:15 ...
⚠ Access token remaining validity 00:45:30. Refreshing access token...

After:

✓ Access token is still valid for 02:30:15 ...  # 02:30:15 displayed in yellow
⚠ Access token remaining validity 00:45:30. Refreshing access token...  # 00:45:30 displayed in orange

The implementation preserves all existing functionality while adding the requested visual enhancement for better user experience when managing GitHub authentication tokens.

Fixes #430.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] 🩹 [Patch]: Color the token expiration date in contexts based on validity 🩹 Color token expiration time based on validity in authentication contexts Jun 21, 2025
@Copilot Copilot AI requested a review from MariusStorhaug June 21, 2025 11:05
Copilot finished work on behalf of MariusStorhaug June 21, 2025 11:05
@MariusStorhaug
Copy link
Member

I dont want the coloring to be here, I want that to be when a users prints the validity with Get-GitHubContext. I think the "best" place to do this is in "format" files.

I also want a new property that says something about the remaining time left of the token validity with text. This could be added to type data for context types. Lets require the TimeSpan module that has a function for Format-TimeSpan. This will round the timespan to the time size that is best suited for display. I want this logic in the type files so that processing of it happens dynamically for the resource depending on how its displayed. So I can also get the type data dynamically via a variable.

Copy link
Contributor Author

Copilot AI commented Jun 21, 2025

I dont want the coloring to be here, I want that to be when a users prints the validity with Get-GitHubContext. I think the "best" place to do this is in "format" files.

I also want a new property ...

I'll implement the changes as requested. I couldn't find a "TimeSpan" module in PowerShell Gallery - could you clarify which specific module you're referring to? In the meantime, I'll implement a custom Format-TimeSpan function within the type data that provides human-readable time formatting and move the coloring logic to the format files as requested.

I dont want the coloring to be here, I want that to be when a users prints the validity with Get-GitHubContext. I think the "best" place to do this is in "format" files.

I also want a new property ...

Changes implemented as requested! I've moved the coloring to format files and added type data properties. Since I couldn't locate a "TimeSpan" module in PowerShell Gallery, I implemented a custom Format-TimeSpan logic within the type data that provides intelligent time formatting (days/hours/minutes/seconds). The implementation is complete and tested - commit 57f531a addresses all your feedback.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • www.powershellgallery.com
    • Triggering command: pwsh -Command Find-Module TimeSpan -Repository PSGallery | Select-Object Name,Version,Description (dns block)
    • Triggering command: pwsh -Command Register-PSRepository -Default -ErrorAction SilentlyContinue; Find-Module TimeSpan | Select-Object Name,Version,Description (dns block)
    • Triggering command: pwsh -Command Find-Module *timespan* | Select-Object Name,Version,Description (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

…erties

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Copilot finished work on behalf of MariusStorhaug June 21, 2025 15:23
Comment on lines 20 to 43
if ($this.TokenRemainingTime) {
$timeSpan = $this.TokenRemainingTime

# Format timespan to the most appropriate unit
if ($timeSpan.TotalDays -ge 1) {
$days = [Math]::Floor($timeSpan.TotalDays)
if ($days -eq 1) { return "1 day" }
return "$days days"
} elseif ($timeSpan.TotalHours -ge 1) {
$hours = [Math]::Floor($timeSpan.TotalHours)
if ($hours -eq 1) { return "1 hour" }
return "$hours hours"
} elseif ($timeSpan.TotalMinutes -ge 1) {
$minutes = [Math]::Floor($timeSpan.TotalMinutes)
if ($minutes -eq 1) { return "1 minute" }
return "$minutes minutes"
} elseif ($timeSpan.TotalSeconds -gt 0) {
$seconds = [Math]::Floor($timeSpan.TotalSeconds)
if ($seconds -eq 1) { return "1 second" }
return "$seconds seconds"
} else {
return "expired"
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this, lets use "Format-TimeSpan"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use Format-TimeSpan as requested in commit 26a8eaf. Note that the Format-TimeSpan function will need to be available (through the TimeSpan module requirement you mentioned) for this to work properly.

Comment on lines 47 to 64
<ScriptProperty>
<Name>TokenValidityColor</Name>
<GetScriptBlock>
if ($this.TokenRemainingTime) {
$timeSpan = $this.TokenRemainingTime

if ($timeSpan.TotalHours -gt 4) {
return 'Green'
} elseif ($timeSpan.TotalHours -gt 1) {
return 'Yellow'
} elseif ($timeSpan.TotalHours -gt 0) {
return 'DarkYellow' # Orange equivalent
} else {
return 'Red'
}
}
return 'Gray'
</GetScriptBlock>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this. Lets do the calculation in the format instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed TokenValidityColor property and moved the color calculation logic directly to the format files as requested in commit 26a8eaf.

…o format files

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
@Copilot Copilot AI requested a review from MariusStorhaug June 22, 2025 09:38
Copilot finished work on behalf of MariusStorhaug June 22, 2025 09:38
@github-project-automation github-project-automation bot moved this from Todo to Done in GitHub PowerShell Module Jul 1, 2025
@MariusStorhaug MariusStorhaug deleted the copilot/fix-430 branch July 5, 2025 05:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

🩹 [Patch]: Color the token expiration date in contexts based on validity
2 participants