Skip to content
Open
2 changes: 1 addition & 1 deletion .github/workflows/TestWorkflowTemplate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:
# Runs a set of commands using the runners shell
- name: Run a multi-line script
run: |
./script.ps1 ${{github.token}}
./script.ps1 ${{ secrets.GITHUB_TOKEN }}
79 changes: 60 additions & 19 deletions script.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,66 @@ function add-csv {
}
}

function main {
$fileDirectory = ".\Deployments"
add-csv
# $parse_results = New-Object System.Collections.ArrayList;

# Use a foreach to loop through all the files in a directory.
# This method allows us to easily track the file name so we can report
# our findings by file.
# get current path and then add file name to it
foreach($file in Get-ChildItem $fileDirectory)
{
# Processing code goes here
$filePath = $fileDirectory + "\" + $file;
$sha = (Get-Content -Path $file | ConvertFrom-Json).sha

Write-Output $sha

"{0},{1}" -f $filePath,$sha | add-content -path $outfile
function main {
param (
$token
)

$Header = @{
"authorization" = "Bearer $token"
}

$branchResponse = Invoke-RestMethod https://api.github.com/repos/aaroncorreya/SmartDeploymentPOC/branches -Headers $header

Write-Output $branchResponse
Write-Output $branchResponse.GetType().Name

$mainSha = $branchResponse | ForEach-Object -Process {if ($_.name -eq "main") {$_.commit.sha}}

Write-Output $mainSha

$treeUrl = "https://api.github.com/repos/aaroncorreya/SmartDeploymentPOC/git/trees/" + $mainSha + "?recursive=true"
Write-Output $treeUrl
$treeResponse = Invoke-RestMethod $treeUrl -Headers $header

Write-Output $treeResponse.GetType().Name
Write-Output $treeResponse.sha

#Create hashtable
$csvTable = @{}
#Add all json files to the hashtable
$treeResponse.tree | ForEach-Object -Process {if ($_.path.Substring($_.path.Length-5) -eq ".json") {$csvTable.Add($_.path, $_.sha)}}

Write-Output "testing dictionary"
Write-Output $csvTable

#Enumerate through table and add to csv file
add-csv
$csvTable.GetEnumerator() | foreach {
"{0},{1}" -f $_.Key, $_.Value | add-content -path $outfile
}

$createFileUrl = "https://api.github.com/repos/aaroncorreya/SmartDeploymentPOC/contents/trackingTable.csv"
$content = Get-Content -Path "./trackingTable.csv" | Out-String
Write-Output $content

$encodedBytes = [System.Text.Encoding]::UTF8.GetBytes($content)
$encodedContent = [System.Convert]::ToBase64String($encodedBytes)

$body = @{
message = "trackingTable.csv created."
content = $encodedContent
branch = "api-calls"
}

$Parameters = @{
Method = "PUT"
Uri = $createFileUrl
Headers = $Header
Body = $body | ConvertTo-Json
}
#Commit csv file
Invoke-RestMethod @Parameters
}

main
main -token $args[0]
4 changes: 4 additions & 0 deletions trackingTable.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"FILE_NAME","SHA"
,
Deployments/testFile2.json,d3e7e677ac8853b8ab6eada1c8fb0cd0a482f272
Deployments/testFile1.json,8330f68c71cb50f3eae48ea1bd328690be1a751b