Skip to content

Commit 30d0ad2

Browse files
authoredSep 11, 2023
Update load-used-actions.ps1 (#69)
1 parent 52f280c commit 30d0ad2

14 files changed

+435
-106
lines changed
 

‎.devcontainer/devcontainer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"ghcr.io/devcontainers/features/github-cli:1": {}
1515
},
1616

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

1919
// Configure tool-specific properties.
2020
"customizations": {

‎.devcontainer/postCreateCommand.ps1

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!pwsh
2+
3+
# install the needed pwsh modules
4+
Install-Module Pester
5+
Install-Module powershell-yaml

‎.github/workflows/publishing.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ jobs:
2121
with:
2222
PAT: ${{ secrets.GITHUB_TOKEN }}
2323
id: load-actions
24+
2425
- shell: pwsh
2526
run: |
26-
Write-Host "Found actions [${{ steps.load-actions.outputs.actions }}]"
27-
$content = ${{ steps.load-actions.outputs.actions }}
27+
Write-Host "Found actions [${{ steps.load-actions.outputs.actions-file }}]"
28+
$content = ${{ steps.load-actions.outputs.actions-file }}
2829
New-Item -Path 'actions.json' -Value $content -Force | Out-Null
2930
$actions = $content | ConvertFrom-Json
3031
if ($actions.Length -le 0) {
@@ -67,6 +68,7 @@ jobs:
6768
Release ${{ github.ref }} is available now
6869
6970
# todo: figure out how this works with an action :-)
71+
# does not work, as you cannot set the flag to publish the action to the marketplace
7072
#- uses: actions/publish-release-asset@v2
7173
#with:
7274
#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

‎.github/workflows/testing.yml

+29-33
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,32 @@ on:
99
permissions: read-all
1010

1111
jobs:
12+
run-pester-tests:
13+
runs-on: ubuntu-latest
14+
name: Run Pester tests
15+
steps:
16+
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
17+
18+
- name: Execute tests
19+
shell: pwsh
20+
env:
21+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
22+
run: |
23+
# execute Pester tests
24+
25+
# install powershell-yaml as it is not on the runners by default
26+
$moduleName = "powershell-yaml"
27+
Install-Module -Name $moduleName -Force -Scope CurrentUser -AllowClobber
28+
29+
# execute tests
30+
$testResults = Invoke-Pester
31+
if ($testResults.FailedCount -gt 0) {
32+
throw "Tests failed"
33+
}
34+
else {
35+
Write-Host "Tests passed"
36+
}
37+
1238
load-all-used-actions:
1339
runs-on: ubuntu-latest
1440
name: Test on current organization
@@ -21,23 +47,8 @@ jobs:
2147
PAT: ${{ secrets.GITHUB_TOKEN }}
2248
id: load-actions
2349

24-
- shell: pwsh
25-
run: |
26-
Write-Host "Found actions [${{ steps.load-actions.outputs.actions }}]"
27-
$content = ${{ steps.load-actions.outputs.actions }}
28-
New-Item -Path 'actions.json' -Value $content -Force | Out-Null
29-
$actions = $content | ConvertFrom-Json
30-
if ($actions.Length -le 0) {
31-
Set-Content -Value "No actions found" -Path $env:GITHUB_STEP_SUMMARY
32-
throw "No actions found"
33-
}
34-
else {
35-
Write-Host "Found [$($actions.Length)] actions"
36-
Set-Content -Value "Found [$($actions.Length)] actions" -Path $env:GITHUB_STEP_SUMMARY
37-
}
38-
3950
- shell: pwsh
40-
name: check the output file location to contain the expected content
51+
name: Check the output file location to contain the expected content
4152
run: |
4253
# check the output file location to contain the expected content
4354
Write-Host "Got actions file location here [${{ steps.load-actions.outputs.actions-file }}]"
@@ -64,7 +75,7 @@ jobs:
6475
runs-on: ubuntu-latest
6576
name: Test on different organization
6677
env:
67-
organization: rajbos-actions
78+
organization: rajbos-actions-demo
6879
steps:
6980
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
7081

@@ -75,23 +86,8 @@ jobs:
7586
organization: ${{ env.organization }}
7687
id: load-actions
7788

78-
- shell: pwsh
79-
run: |
80-
Write-Host "Found actions [${{ steps.load-actions.outputs.actions }}]"
81-
$content = ${{ steps.load-actions.outputs.actions }}
82-
New-Item -Path 'actions.json' -Value $content -Force | Out-Null
83-
$actions = $content | ConvertFrom-Json
84-
if ($actions.Length -le 0) {
85-
Set-Content -Value "No actions found" -Path $env:GITHUB_STEP_SUMMARY
86-
throw "No actions found"
87-
}
88-
else {
89-
Write-Host "Found [$($actions.Length)] actions"
90-
Set-Content -Value "Found [$($actions.Length)] actions" -Path $env:GITHUB_STEP_SUMMARY
91-
}
92-
9389
- shell: pwsh
94-
name: check the output file location to contain the expected content
90+
name: Check the output file location to contain the expected content
9591
run: |
9692
# check the output file location to contain the expected content
9793
Write-Host "Got actions file location here [${{ steps.load-actions.outputs.actions-file }}]"

‎README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ jobs:
4040
PAT: ${{ secrets.GITHUB_TOKEN }} # use an Access Token with correct permissions to view private repos if you need to
4141
4242
- shell: pwsh
43-
name: Store json file
44-
run: echo ${{ steps.load-actions.outputs.actions }} > 'actions.json'
43+
name: Show json file
44+
run: cat ${{ steps.load-actions.outputs.actions-file }}
4545
4646
- name: Upload result file as artefact
4747
uses: actions/upload-artifact@v2
4848
with:
49-
name: actions
49+
name: actions-file
5050
path: actions.json
5151
```
5252

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

5959
## Outputs
60-
actions: a compressed json string with all the actions used in the workflows in the organization. The json is in the format:
60+
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:
6161
``` json
6262
[
6363
"actionLink": "actions/checkout",

‎Src/PowerShell/entrypoint.ps1

+8-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@ function main {
4747
throw
4848
}
4949

50-
$actions = (.\load-used-actions.ps1 -orgName $organization -PAT $PAT)
50+
echo $pwd
51+
ls
52+
53+
# pull in the methods from load-actions:
54+
. $PSScriptRoot\load-used-actions.ps1 -orgName $organization -PAT $PAT
55+
56+
# Get all actions
57+
$actions = LoadAllActionsFromConfiguration
5158

5259
# write the file outside of the container so we can pick it up
5360
Write-Host "Found [$($actions.Count)] actions "

‎Src/PowerShell/github-calls.ps1

+46-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
function Get-Headers {
32
param (
43
[string] $userName,
@@ -311,13 +310,54 @@ function FindAllRepos {
311310
function GetRawFile {
312311
param (
313312
[string] $url,
314-
[string] $PAT
313+
[string] $PAT,
314+
[string] $userName
315315
)
316316

317-
Write-Host "Loading file content from url [$($url.Replace($PAT, "****")))]"
318-
317+
if ($null -eq $PAT -or $PAT.Length -eq 0) {
318+
Write-Warning "Cannot handle empty PAT"
319+
return ""
320+
}
321+
322+
if ($null -eq $userName -or $userName.Length -eq 0) {
323+
Write-Warning "Cannot handle empty userName"
324+
return ""
325+
}
326+
327+
Write-Host "GetRawFile: $url"
328+
if ($null -eq $url) {
329+
Write-Warning "Cannot handle empty url"
330+
return ""
331+
}
332+
$index = $url.IndexOf("?token=")
333+
$logUrl = ""
334+
if ($index -gt 0) {
335+
$logUrl = $url.Substring(0, $index)
336+
}
337+
else {
338+
$logUrl = $url
339+
}
340+
319341
$Headers = Get-Headers -userName $userName -PAT $PAT
320-
$result = Invoke-WebRequest -Uri $url -Headers $Headers -Method Get -ErrorAction Stop | Select-Object -Expand Content
342+
$requestResult = ""
343+
try {
344+
$requestResult = Invoke-WebRequest -Uri $url -Headers $Headers -Method Get -ErrorAction Stop
345+
}
346+
catch {
347+
Write-Warning "Error loading file content response from url [$($logUrl)]"
348+
Write-Warning "Error: [$_]"
349+
return ""
350+
}
351+
352+
try {
353+
$result = $requestResult | Select-Object -Expand Content
354+
}
355+
catch {
356+
Write-Warning "Error converting file content from url [$($logUrl)]"
357+
Write-Warning "Error: [$_]"
358+
Write-Warning "Content: [$requestResult]"
359+
return ""
360+
}
321361

322362
return $result
323-
}
363+
}

0 commit comments

Comments
 (0)
Please sign in to comment.