Skip to content

Commit 6d8471b

Browse files
🩹 [Patch]: Standardize DateTime and TimeSpan property naming convention (#467)
This pull request introduces several changes to improve code clarity, consistency, and functionality across multiple files. The most significant updates involve renaming properties for better readability, adding new functionality for handling expiration times, and modifying constructors and formatting files to align with these changes. ### Property Renaming for Consistency * Renamed `TokenExpirationDate` to `TokenExpiresAt` in `[InstallationGitHubContext]` and `[UserGitHubContext]`. Updated all related assignments and references accordingly. * Renamed `RefreshTokenExpirationDate` to `RefreshTokenExpiresAt` in `[UserGitHubContext]`. Updated references in constructors and methods. * Renamed `Reset` to `ResetsAt` in `GithubRateLimitResource` and updated its constructor to calculate `ResetsAt` using Unix epoch time. ### Handling Expiration Times * Added a `ExpiresIn` script property to the `[GitHubArtifact]` type to calculate remaining time until expiration dynamically. * Updated logic in `Test-GitHubAccessTokenRefreshRequired` and `Update-GitHubUserAccessToken` to use `TokenExpiresAt` and `RefreshTokenExpiresAt` for expiration checks. ### Formatting and Output Enhancements * Updated formats for `[GitHubContext]` and `[GitHubRateLimitResource]` to reflect renamed properties (`TokenExpiresAt`, `ResetsAt`) and added new labels (`TokenExpiresIn`, `ResetsIn`). * Improved list formatting in `Get-GitHubRateLimit` for better readability and updated the output type to `GitHubRateLimitResource`. [[1]](diffhunk://#diff-fb1d1f2f0c6408f7e57b571e58424a515ce78bf35281ef778d2924a674dce150L11-R19) [[2]](diffhunk://#diff-fb1d1f2f0c6408f7e57b571e58424a515ce78bf35281ef778d2924a674dce150R29-R31) ### Constructor Updates: * Simplified constructors in `GithubRateLimitResource.ps1` by removing the hashtable-based initialization and using `PSCustomObject` directly. * Updated constructors in `Connect-GitHubAccount.ps1` and `Connect-GitHubApp.ps1` to use the new property names and expiration handling. [[1]](diffhunk://#diff-12918e90451cdedb78571b9a67ac0313331a25175cebb606b7108b7bf06af092L222-R227) [[2]](diffhunk://#diff-7d1951ca779d73ee11b11db4ade6f33f8ea5fcf052324a72d2c8e83304eaa045L142-R142) ### Build and Test Configuration: * Added `Skip` directives for various test and build steps in `.github/PSModule.yml` to streamline CI/CD workflows. --------- Co-authored-by: Marius Storhaug <[email protected]>
1 parent 26ca2a5 commit 6d8471b

18 files changed

+236
-91
lines changed

‎.github/PSModule.yml‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
11
Test:
2+
SourceCode:
3+
Windows:
4+
Skip: true
5+
MacOS:
6+
Skip: true
7+
PSModule:
8+
Windows:
9+
Skip: true
10+
MacOS:
11+
Skip: true
12+
Module:
13+
Windows:
14+
Skip: true
15+
MacOS:
16+
Skip: true
17+
TestResults:
18+
Skip: true
219
CodeCoverage:
20+
Skip: true
321
PercentTarget: 50
22+
Build:
23+
Docs:
24+
Skip: true

‎src/classes/public/Context/GitHubContext/InstallationGitHubContext.ps1‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# The token expiration date.
66
# 2024-01-01-00:00:00
7-
[System.Nullable[datetime]] $TokenExpirationDate
7+
[System.Nullable[datetime]] $TokenExpiresAt
88

99
# The installation ID.
1010
[System.Nullable[uint64]] $InstallationID
@@ -43,7 +43,7 @@
4343
$this.HttpVersion = $Object.HttpVersion
4444
$this.PerPage = $Object.PerPage
4545
$this.ClientID = $Object.ClientID
46-
$this.TokenExpirationDate = $Object.TokenExpirationDate
46+
$this.TokenExpiresAt = $Object.TokenExpiresAt
4747
$this.InstallationID = $Object.InstallationID
4848
$this.Permissions = $Object.Permissions
4949
$this.Events = $Object.Events

‎src/classes/public/Context/GitHubContext/UserGitHubContext.ps1‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313

1414
# The token expiration date.
1515
# 2024-01-01-00:00:00
16-
[System.Nullable[datetime]] $TokenExpirationDate
16+
[System.Nullable[datetime]] $TokenExpiresAt
1717

1818
# The refresh token.
1919
[securestring] $RefreshToken
2020

2121
# The refresh token expiration date.
2222
# 2024-01-01-00:00:00
23-
[System.Nullable[datetime]] $RefreshTokenExpirationDate
23+
[System.Nullable[datetime]] $RefreshTokenExpiresAt
2424

2525
UserGitHubContext() {}
2626

@@ -46,8 +46,8 @@
4646
$this.AuthClientID = $Object.AuthClientID
4747
$this.DeviceFlowType = $Object.DeviceFlowType
4848
$this.Scope = $Object.Scope
49-
$this.TokenExpirationDate = $Object.TokenExpirationDate
49+
$this.TokenExpiresAt = $Object.TokenExpiresAt
5050
$this.RefreshToken = $Object.RefreshToken
51-
$this.RefreshTokenExpirationDate = $Object.RefreshTokenExpirationDate
51+
$this.RefreshTokenExpiresAt = $Object.RefreshTokenExpiresAt
5252
}
5353
}

‎src/classes/public/RateLimit/GithubRateLimitResource.ps1‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,17 @@
1212
[UInt64] $Remaining
1313

1414
# The time when the rate limit will reset.
15-
[DateTime] $Reset
15+
[DateTime] $ResetsAt
1616

1717
# Simple parameterless constructor
1818
GitHubRateLimitResource() {}
1919

20-
# Constructor that initializes the class from a hashtable
21-
GitHubRateLimitResource([hashtable]$Properties) {
22-
foreach ($Property in $Properties.Keys) {
23-
$this.$Property = $Properties.$Property
24-
}
25-
}
26-
2720
# Constructor that initializes the class from a PSCustomObject
28-
GitHubRateLimitResource([PSCustomObject]$Object) {
29-
$Object.PSObject.Properties | ForEach-Object {
30-
$this.($_.Name) = $_.Value
31-
}
21+
GitHubRateLimitResource([pscustomobject]$Object) {
22+
$this.Name = $Object.name
23+
$this.Limit = $Object.limit
24+
$this.Used = $Object.used
25+
$this.Remaining = $Object.remaining
26+
$this.ResetsAt = [DateTime]::UnixEpoch.AddSeconds($Object.reset).ToLocalTime()
3227
}
3328
}

‎src/formats/GitHubContext.Format.ps1xml‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
<Label>TokenType</Label>
2525
</TableColumnHeader>
2626
<TableColumnHeader>
27-
<Label>TokenExpirationDate</Label>
27+
<Label>TokenExpiresAt</Label>
2828
</TableColumnHeader>
2929
<TableColumnHeader>
30-
<Label>Remaining</Label>
30+
<Label>TokenExpiresIn</Label>
3131
</TableColumnHeader>
3232
</TableHeaders>
3333
<TableRowEntries>
@@ -50,7 +50,7 @@
5050
<PropertyName>TokenType</PropertyName>
5151
</TableColumnItem>
5252
<TableColumnItem>
53-
<PropertyName>TokenExpirationDate</PropertyName>
53+
<PropertyName>TokenExpiresAt</PropertyName>
5454
</TableColumnItem>
5555
<TableColumnItem>
5656
<ScriptBlock>
@@ -61,7 +61,8 @@
6161
if ($_.Remaining -lt 0) {
6262
$text = "Expired"
6363
} else {
64-
$text = "$($_.Remaining.Hours)h $($_.Remaining.Minutes)m $($_.Remaining.Seconds)s"
64+
$text = "$($_.Remaining.Hours)h $($_.Remaining.Minutes)m
65+
$($_.Remaining.Seconds)s"
6566
}
6667

6768
if ($Host.UI.SupportsVirtualTerminal -and
@@ -266,7 +267,10 @@
266267
<PropertyName>InstallationID</PropertyName>
267268
</ListItem>
268269
<ListItem>
269-
<PropertyName>TokenExpirationDate</PropertyName>
270+
<PropertyName>TokenExpiresAt</PropertyName>
271+
</ListItem>
272+
<ListItem>
273+
<PropertyName>TokenExpiresIn</PropertyName>
270274
</ListItem>
271275
<ListItem>
272276
<PropertyName>InstallationType</PropertyName>
@@ -340,10 +344,16 @@
340344
<PropertyName>Scope</PropertyName>
341345
</ListItem>
342346
<ListItem>
343-
<PropertyName>TokenExpirationDate</PropertyName>
347+
<PropertyName>TokenExpiresAt</PropertyName>
348+
</ListItem>
349+
<ListItem>
350+
<PropertyName>TokenExpiresIn</PropertyName>
351+
</ListItem>
352+
<ListItem>
353+
<PropertyName>RefreshTokenExpiresAt</PropertyName>
344354
</ListItem>
345355
<ListItem>
346-
<PropertyName>RefreshTokenExpirationDate</PropertyName>
356+
<PropertyName>RefreshTokenExpiresIn</PropertyName>
347357
</ListItem>
348358
<ListItem>
349359
<PropertyName>ApiBaseUri</PropertyName>

‎src/formats/GitHubRateLimitResource.Format.ps1xml‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
<Label>Remaining</Label>
2222
</TableColumnHeader>
2323
<TableColumnHeader>
24-
<Label>Reset</Label>
24+
<Label>ResetsAt</Label>
25+
</TableColumnHeader>
26+
<TableColumnHeader>
27+
<Label>ResetsIn</Label>
2528
</TableColumnHeader>
2629
</TableHeaders>
2730
<TableRowEntries>
@@ -40,7 +43,10 @@
4043
<PropertyName>Remaining</PropertyName>
4144
</TableColumnItem>
4245
<TableColumnItem>
43-
<PropertyName>Reset</PropertyName>
46+
<PropertyName>ResetsAt</PropertyName>
47+
</TableColumnItem>
48+
<TableColumnItem>
49+
<PropertyName>ResetsIn</PropertyName>
4450
</TableColumnItem>
4551
</TableColumnItems>
4652
</TableRowEntry>

‎src/functions/private/Auth/DeviceFlow/Test-GitHubAccessTokenRefreshRequired.ps1‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
}
2727

2828
process {
29-
$tokenExpirationDate = $Context.TokenExpirationDate
30-
$remainingDuration = [datetime]$tokenExpirationDate - [datetime]::Now
31-
$updateToken = $remainingDuration.TotalHours -lt $script:GitHub.Config.AccessTokenGracePeriodInHours
29+
$updateToken = ($Context.TokenExpiresAt - [datetime]::Now).TotalHours -lt $script:GitHub.Config.AccessTokenGracePeriodInHours
3230
$updateToken
3331
}
3432

‎src/functions/private/Auth/DeviceFlow/Update-GitHubUserAccessToken.ps1‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
if ($updateToken) {
6161
try {
62-
$refreshTokenValidity = [datetime]($Context.RefreshTokenExpirationDate) - [datetime]::Now
62+
$refreshTokenValidity = [datetime]($Context.RefreshTokenExpiresAt) - [datetime]::Now
6363
$refreshTokenIsValid = $refreshTokenValidity.TotalSeconds -gt 0
6464
if ($refreshTokenIsValid) {
6565
if (-not $Silent) {
@@ -72,10 +72,10 @@
7272
$tokenResponse = Invoke-GitHubDeviceFlowLogin -ClientID $Context.AuthClientID -HostName $Context.HostName
7373
}
7474
$Context.Token = ConvertTo-SecureString -AsPlainText $tokenResponse.access_token
75-
$Context.TokenExpirationDate = (Get-Date).AddSeconds($tokenResponse.expires_in)
75+
$Context.TokenExpiresAt = ([DateTime]::Now).AddSeconds($tokenResponse.expires_in)
7676
$Context.TokenType = $tokenResponse.access_token -replace $script:GitHub.TokenPrefixPattern
7777
$Context.RefreshToken = ConvertTo-SecureString -AsPlainText $tokenResponse.refresh_token
78-
$Context.RefreshTokenExpirationDate = (Get-Date).AddSeconds($tokenResponse.refresh_token_expires_in)
78+
$Context.RefreshTokenExpiresAt = ([DateTime]::Now).AddSeconds($tokenResponse.refresh_token_expires_in)
7979

8080
if ($PSCmdlet.ShouldProcess('Access token', 'Update/refresh')) {
8181
Set-Context -Context $Context -Vault $script:GitHub.ContextVault

‎src/functions/private/Webhooks/Get-GitHubAppWebhookDeliveryToRedeliver.ps1‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
}
3535

3636
process {
37-
$checkPoint = (Get-Date).AddHours($TimeSpan)
37+
$checkPoint = ([DateTime]::Now).AddHours($TimeSpan)
3838
Get-GitHubAppWebhookDeliveryByList -Context $Context -PerPage $PerPage | Where-Object { $_.DeliveredAt -gt $checkPoint } |
3939
Group-Object -Property GUID | Where-Object { $_.Group.Status -notcontains 'OK' } | ForEach-Object {
4040
$refObject = $_.Group | Sort-Object -Property DeliveredAt

‎src/functions/public/Auth/Connect-GitHubAccount.ps1‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,14 @@
218218
switch ($Mode) {
219219
'GitHubApp' {
220220
$context += @{
221-
Token = ConvertTo-SecureString -AsPlainText $tokenResponse.access_token
222-
TokenExpirationDate = (Get-Date).AddSeconds($tokenResponse.expires_in)
223-
TokenType = $tokenResponse.access_token -replace $script:GitHub.TokenPrefixPattern
224-
AuthClientID = $authClientID
225-
DeviceFlowType = $Mode
226-
RefreshToken = ConvertTo-SecureString -AsPlainText $tokenResponse.refresh_token
227-
RefreshTokenExpirationDate = (Get-Date).AddSeconds($tokenResponse.refresh_token_expires_in)
228-
Scope = $tokenResponse.scope
221+
Token = ConvertTo-SecureString -AsPlainText $tokenResponse.access_token
222+
TokenExpiresAt = ([DateTime]::Now).AddSeconds($tokenResponse.expires_in)
223+
TokenType = $tokenResponse.access_token -replace $script:GitHub.TokenPrefixPattern
224+
AuthClientID = $authClientID
225+
DeviceFlowType = $Mode
226+
RefreshToken = ConvertTo-SecureString -AsPlainText $tokenResponse.refresh_token
227+
RefreshTokenExpiresAt = ([DateTime]::Now).AddSeconds($tokenResponse.refresh_token_expires_in)
228+
Scope = $tokenResponse.scope
229229
}
230230
}
231231
'OAuthApp' {

0 commit comments

Comments
 (0)