Skip to content

Commit

Permalink
token cache
Browse files Browse the repository at this point in the history
  • Loading branch information
freddydk committed Nov 19, 2024
1 parent d7d440b commit cd03ea6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Actions/Github-Helper.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
$script:escchars = @(' ','!','\"','#','$','%','\u0026','\u0027','(',')','*','+',',','-','.','/','0','1','2','3','4','5','6','7','8','9',':',';','\u003c','=','\u003e','?','@','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','[','\\',']','^','_',[char]96,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','{','|','}','~')
$script:realTokenCache = @{
"token" = ''
"repository" = ''
"realToken" = ''
"expires" = [datetime]::Now
}

function MaskValue {
Param(
Expand Down Expand Up @@ -599,6 +605,11 @@ function GetRealToken {
# not a json token
return $token
}
elseif ($script:realTokenCache.token -eq $token -and $script:realTokenCache.repository -eq $repository -and $script:realTokenCache.expires -gt [datetime]::Now.AddMinutes(10)) {
# Same token request and cached token won't expire in 10 minutes
Write-Host "return cached token"
return $script:realTokenCache.realToken
}
else {
try {
$json = $token | ConvertFrom-Json
Expand All @@ -616,6 +627,12 @@ function GetRealToken {
Write-Host "Get Token Response $($appInfo.access_tokens_url)"
$tokenResponse = Invoke-RestMethod -Method POST -UseBasicParsing -Headers $headers -Uri $appInfo.access_tokens_url
Write-Host "return token"
$script:realTokenCache = @{
"token" = $token
"repository" = $repository
"realToken" = $tokenResponse.token
"expires" = [datetime]::Now.AddSeconds($tokenResponse.expires_in)
}
return $tokenResponse.token
}
catch {
Expand Down
3 changes: 3 additions & 0 deletions e2eTests/scenarios/ReferenceDocumentation/runtest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ WaitWorkflow -repository $repository -runid $run.id
Test-ArtifactsFromRun -runid $run.id -folder 'artifacts' -expectedArtifacts @{"Apps"=1;"TestApps"=0;"Dependencies"=0;"github-pages"=1} -repoVersion '1.0' -appVersion '1.0'

# Set GitHub Pages in repository to GitHub Actions
SetTokenAndRepository -github:$github -githubOwner $githubOwner -token $token -repository $repository
gh api --method POST /repos/$repository/pages -f build_type=workflow | Out-Null

# Add setting to deploy to GitHub Pages
Expand All @@ -92,6 +93,7 @@ CommitAndPush -commitMessage 'DeployToGitHubPages'
RunDeployReferenceDocumentation -repository $repository -wait | Out-Null

# Get Pages URL and read the content
SetTokenAndRepository -github:$github -githubOwner $githubOwner -token $token -repository $repository
$pagesInfo = gh api /repos/$repository/pages | ConvertFrom-Json
$html = (Invoke-WebRequest -Uri $pagesInfo.html_url -UseBasicParsing).Content
$html | Should -belike "*Documentation for $repository*"
Expand All @@ -108,6 +110,7 @@ CommitAndPush -commitMessage 'Continuous Deployment of ALDoc'
WaitAllWorkflows -repository $repository -noError

# Get Pages URL and read the content
SetTokenAndRepository -github:$github -githubOwner $githubOwner -token $token -repository $repository
$pagesInfo = gh api /repos/$repository/pages | ConvertFrom-Json
$html = (Invoke-WebRequest -Uri $pagesInfo.html_url -UseBasicParsing).Content
$html | Should -belike "*Documentazione per $repository*"
Expand Down

0 comments on commit cd03ea6

Please sign in to comment.