Skip to content

Commit

Permalink
Update load-used-actions.ps1 (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
rajbos authored Sep 11, 2023
1 parent 52f280c commit 30d0ad2
Show file tree
Hide file tree
Showing 14 changed files with 435 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"ghcr.io/devcontainers/features/github-cli:1": {}
},

"postCreateCommand": "sudo chsh vscode -s \"$(which pwsh)\"",
"postCreateCommand": "sudo chsh vscode -s \"$(which pwsh)\"; ./.devcontainer/postCreateCommand.ps1",

// Configure tool-specific properties.
"customizations": {
Expand Down
5 changes: 5 additions & 0 deletions .devcontainer/postCreateCommand.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!pwsh

# install the needed pwsh modules
Install-Module Pester
Install-Module powershell-yaml
6 changes: 4 additions & 2 deletions .github/workflows/publishing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ jobs:
with:
PAT: ${{ secrets.GITHUB_TOKEN }}
id: load-actions

- shell: pwsh
run: |
Write-Host "Found actions [${{ steps.load-actions.outputs.actions }}]"
$content = ${{ steps.load-actions.outputs.actions }}
Write-Host "Found actions [${{ steps.load-actions.outputs.actions-file }}]"
$content = ${{ steps.load-actions.outputs.actions-file }}
New-Item -Path 'actions.json' -Value $content -Force | Out-Null
$actions = $content | ConvertFrom-Json
if ($actions.Length -le 0) {
Expand Down Expand Up @@ -67,6 +68,7 @@ jobs:
Release ${{ github.ref }} is available now
# todo: figure out how this works with an action :-)
# does not work, as you cannot set the flag to publish the action to the marketplace
#- uses: actions/publish-release-asset@v2
#with:
#upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
Expand Down
62 changes: 29 additions & 33 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@ on:
permissions: read-all

jobs:
run-pester-tests:
runs-on: ubuntu-latest
name: Run Pester tests
steps:
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0

- name: Execute tests
shell: pwsh
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# execute Pester tests
# install powershell-yaml as it is not on the runners by default
$moduleName = "powershell-yaml"
Install-Module -Name $moduleName -Force -Scope CurrentUser -AllowClobber
# execute tests
$testResults = Invoke-Pester
if ($testResults.FailedCount -gt 0) {
throw "Tests failed"
}
else {
Write-Host "Tests passed"
}
load-all-used-actions:
runs-on: ubuntu-latest
name: Test on current organization
Expand All @@ -21,23 +47,8 @@ jobs:
PAT: ${{ secrets.GITHUB_TOKEN }}
id: load-actions

- shell: pwsh
run: |
Write-Host "Found actions [${{ steps.load-actions.outputs.actions }}]"
$content = ${{ steps.load-actions.outputs.actions }}
New-Item -Path 'actions.json' -Value $content -Force | Out-Null
$actions = $content | ConvertFrom-Json
if ($actions.Length -le 0) {
Set-Content -Value "No actions found" -Path $env:GITHUB_STEP_SUMMARY
throw "No actions found"
}
else {
Write-Host "Found [$($actions.Length)] actions"
Set-Content -Value "Found [$($actions.Length)] actions" -Path $env:GITHUB_STEP_SUMMARY
}
- shell: pwsh
name: check the output file location to contain the expected content
name: Check the output file location to contain the expected content
run: |
# check the output file location to contain the expected content
Write-Host "Got actions file location here [${{ steps.load-actions.outputs.actions-file }}]"
Expand All @@ -64,7 +75,7 @@ jobs:
runs-on: ubuntu-latest
name: Test on different organization
env:
organization: rajbos-actions
organization: rajbos-actions-demo
steps:
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0

Expand All @@ -75,23 +86,8 @@ jobs:
organization: ${{ env.organization }}
id: load-actions

- shell: pwsh
run: |
Write-Host "Found actions [${{ steps.load-actions.outputs.actions }}]"
$content = ${{ steps.load-actions.outputs.actions }}
New-Item -Path 'actions.json' -Value $content -Force | Out-Null
$actions = $content | ConvertFrom-Json
if ($actions.Length -le 0) {
Set-Content -Value "No actions found" -Path $env:GITHUB_STEP_SUMMARY
throw "No actions found"
}
else {
Write-Host "Found [$($actions.Length)] actions"
Set-Content -Value "Found [$($actions.Length)] actions" -Path $env:GITHUB_STEP_SUMMARY
}
- shell: pwsh
name: check the output file location to contain the expected content
name: Check the output file location to contain the expected content
run: |
# check the output file location to contain the expected content
Write-Host "Got actions file location here [${{ steps.load-actions.outputs.actions-file }}]"
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ jobs:
PAT: ${{ secrets.GITHUB_TOKEN }} # use an Access Token with correct permissions to view private repos if you need to
- shell: pwsh
name: Store json file
run: echo ${{ steps.load-actions.outputs.actions }} > 'actions.json'
name: Show json file
run: cat ${{ steps.load-actions.outputs.actions-file }}
- name: Upload result file as artefact
uses: actions/upload-artifact@v2
with:
name: actions
name: actions-file
path: actions.json
```

Expand All @@ -57,7 +57,7 @@ jobs:
|PAT|The Personal Access Token to use for the API calls.|

## Outputs
actions: a compressed json string with all the actions used in the workflows in the organization. The json is in the format:
actions-file: path to file containing compressed json string with all the actions used in the workflows in the organization. The json is in the format:
``` json
[
"actionLink": "actions/checkout",
Expand Down
9 changes: 8 additions & 1 deletion Src/PowerShell/entrypoint.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ function main {
throw
}

$actions = (.\load-used-actions.ps1 -orgName $organization -PAT $PAT)
echo $pwd
ls

# pull in the methods from load-actions:
. $PSScriptRoot\load-used-actions.ps1 -orgName $organization -PAT $PAT

# Get all actions
$actions = LoadAllActionsFromConfiguration

# write the file outside of the container so we can pick it up
Write-Host "Found [$($actions.Count)] actions "
Expand Down
52 changes: 46 additions & 6 deletions Src/PowerShell/github-calls.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

function Get-Headers {
param (
[string] $userName,
Expand Down Expand Up @@ -311,13 +310,54 @@ function FindAllRepos {
function GetRawFile {
param (
[string] $url,
[string] $PAT
[string] $PAT,
[string] $userName
)

Write-Host "Loading file content from url [$($url.Replace($PAT, "****")))]"

if ($null -eq $PAT -or $PAT.Length -eq 0) {
Write-Warning "Cannot handle empty PAT"
return ""
}

if ($null -eq $userName -or $userName.Length -eq 0) {
Write-Warning "Cannot handle empty userName"
return ""
}

Write-Host "GetRawFile: $url"
if ($null -eq $url) {
Write-Warning "Cannot handle empty url"
return ""
}
$index = $url.IndexOf("?token=")
$logUrl = ""
if ($index -gt 0) {
$logUrl = $url.Substring(0, $index)
}
else {
$logUrl = $url
}

$Headers = Get-Headers -userName $userName -PAT $PAT
$result = Invoke-WebRequest -Uri $url -Headers $Headers -Method Get -ErrorAction Stop | Select-Object -Expand Content
$requestResult = ""
try {
$requestResult = Invoke-WebRequest -Uri $url -Headers $Headers -Method Get -ErrorAction Stop
}
catch {
Write-Warning "Error loading file content response from url [$($logUrl)]"
Write-Warning "Error: [$_]"
return ""
}

try {
$result = $requestResult | Select-Object -Expand Content
}
catch {
Write-Warning "Error converting file content from url [$($logUrl)]"
Write-Warning "Error: [$_]"
Write-Warning "Content: [$requestResult]"
return ""
}

return $result
}
}
Loading

0 comments on commit 30d0ad2

Please sign in to comment.