Skip to content

Commit b9cc165

Browse files
authored
Merge pull request devops-actions#148 from devops-actions/test
Fix issue with parsing yaml
2 parents c788876 + 684dfd2 commit b9cc165

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

Src/PowerShell/entrypoint.ps1

+7-7
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ function main {
4343
}
4444

4545
if ($null -eq $PAT -or "" -eq $PAT) {
46-
Write-Error "No value given for input PAT: Use at least [GITHUB_TOKEN]"
47-
throw
46+
$PAT = $env:GITHUB_TOKEN
47+
if ($null -eq $PAT -or "" -eq $PAT) {
48+
Write-Error "No value given for input PAT: Use at least [GITHUB_TOKEN]"
49+
throw
50+
}
4851
}
4952

50-
echo $pwd
51-
ls
52-
5353
# pull in the methods from load-actions:
5454
. $PSScriptRoot\load-used-actions.ps1 -orgName $organization -PAT $PAT
5555

@@ -61,15 +61,15 @@ function main {
6161
}
6262

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

6767
# store the json in a file and write the path to the output variable
6868
$fileName = "used-actions.json"
6969
$filePath = "$($env:GITHUB_WORKSPACE)/$fileName"
7070

7171
if ($null -ne $env:GITHUB_WORKSPACE -and "" -ne $env:GITHUB_WORKSPACE) {
72-
Write-Host "Writing actions to file in workspace: [$($env:GITHUB_WORKSPACE)]"
72+
Write-Host "Writing actions to file in workspace: [$($env:GITHUB_WORKSPACE)] to filePath [$filePath]"
7373
Set-Content -Value "$jsonObject" -Path "$filePath"
7474
}
7575
else {

Src/PowerShell/github-calls.ps1

+17-8
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,20 @@ function CallWebRequest {
5757
# convert the response json content
5858
$info = ($result.Content | ConvertFrom-Json)
5959

60-
Write-Debug " Paging links: $($result.Headers["Link"])"
60+
61+
$LinkHeader = $result.Headers["Link"]
62+
Write-Debug " Paging links: $($LinkHeader)"
6163
# Test for paging links and try to enumerate all pages
62-
if ($null -ne $result.Headers["Link"]) {
64+
if ($null -ne $LinkHeader) {
6365
#Write-Warning "Paging link detected:"
64-
foreach ($page in $result.Headers["Link"].Split(", ")) {
66+
foreach ($page in $LinkHeader.Split(",")) {
6567

6668
#Write-Host "Found page: [$page]"
6769
#Write-Host "rel next found at: [$($page.Split(";")[1])"
6870

69-
if ($page.Split("; ")[1] -eq 'rel="next"') {
71+
if ($page.Split(";")[1].Trim() -eq 'rel="next"') {
7072
#Write-Host "Next page is at [$page]"
71-
$almostUrl = $page.Split(";")[0]
73+
$almostUrl = $page.Split(";")[0].Trim()
7274
#Write-Host "Almost: $almostUrl"
7375
$linkUrl = $almostUrl.Substring(1, $almostUrl.Length - 2)
7476
Write-Host "Handling pagination link with next page at: $linkUrl"
@@ -349,14 +351,21 @@ function GetRawFile {
349351
}
350352

351353
try {
352-
$result = $requestResult | Select-Object -Expand Content
354+
$result = $requestResult.Content
355+
$output = ""
356+
# remove any empty lines or tabs in the result to prevent issues with parsing yaml
357+
foreach ($line in $result.Split([Environment]::NewLine)) {
358+
$trimmedLine = $line.Trim().Replace("`t", "").Replace(" ", "")
359+
if ($trimmedLine.Length -gt 0) {
360+
$output += $line + [Environment]::NewLine
361+
}
362+
}
363+
return $output
353364
}
354365
catch {
355366
Write-Warning "Error converting file content from url [$($logUrl)]"
356367
Write-Warning "Error: [$_]"
357368
Write-Warning "Content: [$requestResult]"
358369
return ""
359370
}
360-
361-
return $result
362371
}

Src/PowerShell/load-used-actions.ps1

+2-1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ function GetAllUsedActionsFromRepo {
131131
foreach ($workflowFile in $workflowFiles) {
132132
try {
133133
if ($null -ne $workflowFile.download_url -and $workflowFile.download_url.Length -gt 0 -and $workflowFile.download_url.Split("?")[0].EndsWith(".yml")) {
134+
Write-Host "Loading workflow file: [$($workflowFile.name)]"
134135
$workflow = GetRawFile -url $workflowFile.download_url -PAT $PAT -userName $userName
135136
$actions = GetActionsFromWorkflow -workflow $workflow -workflowFileName $workflowFile.name -repo $repo
136137

@@ -252,7 +253,7 @@ function LoadAllActionsFromConfiguration() {
252253

253254
$summarizeActions = SummarizeActionsUsed -actions $actionsFound
254255

255-
Write-Host "Found [$($actionsFound.Count)] actions used in workflows with [$($summarizeActions.Count) unique actions]"
256+
Write-Host "Found [$($actionsFound.Count)] actions used in [$($repos.Count)] repos by workflows with [$($summarizeActions.Count) unique actions]"
256257

257258
# write the actions to disk
258259
$fileName = "summarized-actions.json"

0 commit comments

Comments
 (0)