From 62a08a9dfd165b8fae33e0c2305dae67e08da26f Mon Sep 17 00:00:00 2001 From: Rob Bos Date: Tue, 5 Sep 2023 09:02:38 +0000 Subject: [PATCH] Add support for finding reusable workflows --- .env | 4 ++-- .gitignore | 5 +++++ Src/PowerShell/entrypoint.ps1 | 14 ++++++++++++-- Src/PowerShell/load-used-actions.ps1 | 24 ++++++++++++------------ 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/.env b/.env index 2d04993..8f923f3 100644 --- a/.env +++ b/.env @@ -1,3 +1,3 @@ GITHUB_ACTOR=debugging -GITHUB_WORKSPACE= -GITHUB_OUTPUT= \ No newline at end of file +GITHUB_WORKSPACE=github_workspace +GITHUB_OUTPUT=github_output_file \ No newline at end of file diff --git a/.gitignore b/.gitignore index 4bae860..514cbb1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,6 @@ Src/PowerShell/summarized-actions.json +Src/PowerShell/github_output +Src/PowerShell/used-actions.json +Src/PowerShell/github_output_file +Src/PowerShell/entrypoint.ps1 +Src/PowerShell/github_workspace/used-actions.json diff --git a/Src/PowerShell/entrypoint.ps1 b/Src/PowerShell/entrypoint.ps1 index 8beeb7d..8286d2b 100644 --- a/Src/PowerShell/entrypoint.ps1 +++ b/Src/PowerShell/entrypoint.ps1 @@ -49,8 +49,18 @@ function main { # store the json in a file and write the path to the output variable $fileName = "used-actions.json" $filePath = "$($env:GITHUB_WORKSPACE)/$fileName" - - Set-Content -Value "$jsonObject" -Path "$filePath" + + if ($null -ne $env:GITHUB_WORKSPACE -and "" -ne $env:GITHUB_WORKSPACE) { + Write-Host "Writing actions to file in workspace: [$($env:GITHUB_WORKSPACE)]" + Set-Content -Value "$jsonObject" -Path "$filePath" + } + else { + Write-Host "Writing actions to file in current folder: [$($pwd)]" + $filePath = "./used-actions.json" + Set-Content -Value "$jsonObject" -Path "$filePath" + } + + # write the name of the file to the output folder Set-Content -Value "actions-file=$fileName" -Path $env:GITHUB_OUTPUT Write-Host "Stored actions in the actions output. Use $${{ steps..outputs.actions }} in next action to load the json" Write-Host "Stored actions file in the actions output. Use $${{ steps..outputs.actions-file }} in next action to load the file from the $$GITHUB_WORKSPACE folder" diff --git a/Src/PowerShell/load-used-actions.ps1 b/Src/PowerShell/load-used-actions.ps1 index a316278..fba92d0 100644 --- a/Src/PowerShell/load-used-actions.ps1 +++ b/Src/PowerShell/load-used-actions.ps1 @@ -130,9 +130,9 @@ function SummarizeActionsUsed { $summarized = @() foreach ($action in $actions) { - $found = $summarized | Where-Object { $_.actionLink -eq $action.actionLink } # todo: summarize with reusable workflows as well + $found = $summarized | Where-Object { $_.actionLink -eq $action.actionLink -And $_.type -eq $action.type } if ($null -ne $found) { - # action already found, add this info to it + # item already found, add this info to it $newInfo = [PSCustomObject]@{ repo = $action.repo workflowFileName = $action.workflowFileName @@ -142,8 +142,9 @@ function SummarizeActionsUsed { $found.count++ } else { - # new action, create a new object + # new item, create a new object $newItem = [PSCustomObject]@{ + type = $action.type actionLink = $action.actionLink count = 1 workflows = @( @@ -151,7 +152,7 @@ function SummarizeActionsUsed { repo = $action.repo workflowFileName = $action.workflowFileName } - ) + ) } $summarized += $newItem } @@ -170,7 +171,7 @@ function LoadAllUsedActionsFromRepos { # create hastable $actions = @() - $i=0 + #$i=0 foreach ($repo in $repos) { if ($null -ne $repo -And $repo.full_name.Length -gt 0) { Write-Host "Loading actions from repo: [$($repo.full_name)]" @@ -180,13 +181,12 @@ function LoadAllUsedActionsFromRepos { # comment out code below to stop after a certain number of repos to prevent issues with # rate limiting on the load file count (that is not workin correctly) - - $i++ - if ($i -eq 2) { - # break on second result: - Write-Host "Breaking after [$i] repos" - return $actions - } + # $i++ + # if ($i -eq 2) { + # # break on second result: + # Write-Host "Breaking after [$i] repos" + # return $actions + # } } }