diff --git a/.github/workflows/TestWorkflowTemplate.yml b/.github/workflows/TestWorkflowTemplate.yml index 3afab60..9684270 100644 --- a/.github/workflows/TestWorkflowTemplate.yml +++ b/.github/workflows/TestWorkflowTemplate.yml @@ -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 }} diff --git a/script.ps1 b/script.ps1 index 9bcfdd2..4dbb9e9 100644 --- a/script.ps1 +++ b/script.ps1 @@ -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 \ No newline at end of file +main -token $args[0] diff --git a/trackingTable.csv b/trackingTable.csv new file mode 100644 index 0000000..5e36315 --- /dev/null +++ b/trackingTable.csv @@ -0,0 +1,4 @@ +"FILE_NAME","SHA" +, +Deployments/testFile2.json,d3e7e677ac8853b8ab6eada1c8fb0cd0a482f272 +Deployments/testFile1.json,8330f68c71cb50f3eae48ea1bd328690be1a751b