Skip to content

Commit

Permalink
Load reusable workflows next to the actions in use (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
rajbos authored Sep 5, 2023
1 parent e04d7a9 commit 104493b
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GITHUB_ACTOR=debugging
GITHUB_WORKSPACE=github_workspace
GITHUB_OUTPUT=github_output_file
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +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
47 changes: 42 additions & 5 deletions Src/PowerShell/entrypoint.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ function Get-LocationInfo {
}
}

function Import-EnvironmentVariables {
# load the environment variables from the .env file in the root of the repo:
Get-Content "../../.env" | ForEach-Object {
$name, $value = $_.split('=')
# if name already exists, do not overwrite it:
if ($false -eq (Test-Path env:$name)) {
if ($null -ne $value -and "" -ne $value) {
Write-Host "Setting environment variable [$name] to [$value] from the .env file"
Set-Content env:\$name $value
}
}
else {
Write-Host "Environment variable [$name] was already set. Value is [$($env:name)]"
}
}
}

function main {

if ($null -eq $organization -or "" -eq $organization) {
Expand All @@ -25,15 +42,25 @@ function main {

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

# wite the file outside of the container so we can pick it up
# write the file outside of the container so we can pick it up
Write-Host "Found [$($actions.Count)] actions "
$jsonObject = ($actions | ConvertTo-Json -Depth 10 -Compress)

# 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.<step id>.outputs.actions }} in next action to load the json"
Write-Host "Stored actions file in the actions output. Use $${{ steps.<step id>.outputs.actions-file }} in next action to load the file from the $$GITHUB_WORKSPACE folder"
Expand All @@ -42,20 +69,30 @@ function main {
Add-Content -Value "actions='$jsonObject'" -Path $env:GITHUB_OUTPUT
}

try {
$currentLocation = Get-Location
try {
# always run in the correct location, where our scripts are located:
Set-Location $PSScriptRoot
Import-EnvironmentVariables

# call main script:
main

Write-Host "Going back to location before the run: [$currentLocation]"
Set-Location $currentLocation

# return the container with the exit code = Ok:
exit 0
}
catch {
# return the container with the last exit code:
$exitError = $_
Write-Error "Error loading the actions:"
Write-Error $_
Write-Error $exitError

Write-Host "Going back to location before the run: [$currentLocation]"
Set-Location $currentLocation

# return the container with an erroneous exit code:
exit 1
}
17 changes: 16 additions & 1 deletion Src/PowerShell/generic.ps1
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
$moduleName = "powershell-yaml"

# install a yaml parsing module (already done in the container image)
if($env:computername -ne "ROB-XPS9700") {
Write-Host "PSHOME: [$pshome]"

# check if module is installed locally:
$module = Get-Module -name $moduleName
if ($null -eq $module) {
Write-Host "Module [$moduleName] not found, installing it"
Install-Module -Name $moduleName -Force -Scope CurrentUser -AllowClobber
}
else {
Write-Host "Module [$moduleName] found, skipping installation"
}

# add back the root folder to the modules path because GitHub runner seems to overwite it
$env:PSModulePath += ":/root/.local/share/powershell/Modules"
# first check if module path already has this value:
if ($false -eq ($env:PSModulePath -like "/root/.local/share/powershell/Modules")) {
$env:PSModulePath += ":/root/.local/share/powershell/Modules"
}

Write-Host "PSModulePath:"
foreach ($path in $env:PSModulePath -split ':') {
Write-Host "- [$path]"
}

try {
Write-Host "Importing module for the yaml parsing"
Import-Module powershell-yaml -Force
Expand Down
2 changes: 1 addition & 1 deletion Src/PowerShell/github-calls.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ function GetRawFile {
[string] $PAT
)

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

$Headers = Get-Headers -userName $userName -PAT $PAT
$result = Invoke-WebRequest -Uri $url -Headers $Headers -Method Get -ErrorAction Stop | Select-Object -Expand Content
Expand Down
49 changes: 35 additions & 14 deletions Src/PowerShell/load-used-actions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,36 @@ function GetActionsFromWorkflow {
foreach ($job in $parsedYaml["jobs"].GetEnumerator()) {
Write-Host " Job found: [$($job.Key)]"
$steps=$job.Value.Item("steps")
foreach ($step in $steps) {
$uses=$step.Item("uses")
if ($null -ne $steps) {
foreach ($step in $steps) {
$uses=$step.Item("uses")
if ($null -ne $uses) {
Write-Host " Found action used: [$uses]"
$actionLink = $uses.Split("@")[0]

$data = [PSCustomObject]@{
actionLink = $actionLink
workflowFileName = $workflowFileName
repo = $repo
type = "action"
}

$actions += $data
}
}
}
else {
# check for reusable workflow
$uses = $job.Value.Item("uses")
if ($null -ne $uses) {
Write-Host " Found action used: [$uses]"
Write-Host " Found reusable workflow used: [$uses]"
$actionLink = $uses.Split("@")[0]

$data = [PSCustomObject]@{
actionLink = $actionLink
workflowFileName = $workflowFileName
repo = $repo
type = "reusable workflow"
}

$actions += $data
Expand Down Expand Up @@ -93,7 +113,7 @@ function GetAllUsedActionsFromRepo {
}
catch {
Write-Warning "Error handling this workflow file:"
Write-Host $workflowFile | ConvertFrom-Json -Depth 10
Write-Host $workflowFile.Replace($PAT, "****") | ConvertFrom-Json -Depth 10
Write-Warning "----------------------------------"
Write-Host "Error: [$_]"
Write-Warning "----------------------------------"
Expand All @@ -110,9 +130,9 @@ function SummarizeActionsUsed {

$summarized = @()
foreach ($action in $actions) {
$found = $summarized | Where-Object { $_.actionLink -eq $action.actionLink }
$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
Expand All @@ -122,16 +142,17 @@ 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 = @(
[PSCustomObject]@{
repo = $action.repo
workflowFileName = $action.workflowFileName
}
)
)
}
$summarized += $newItem
}
Expand All @@ -150,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)]"
Expand All @@ -160,12 +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 out on second result:
# $i++
# if ($i -eq 2) {
# # break on second result:
# Write-Host "Breaking after [$i] repos"
# return $actions
#}
# }
}
}

Expand Down

0 comments on commit 104493b

Please sign in to comment.