From eccd5efa237a2d8e1da187b7b57e6c037d4507da Mon Sep 17 00:00:00 2001 From: Aaron Correya Date: Fri, 12 Nov 2021 16:53:03 -0800 Subject: [PATCH 01/11] Created rough draft of script with API functionality --- script.ps1 | 54 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/script.ps1 b/script.ps1 index 9bcfdd2..26616c8 100644 --- a/script.ps1 +++ b/script.ps1 @@ -1,5 +1,9 @@ $global:outfile="trackingTable.csv" +# Param ( +# [Parameter(Mandatory=$true)][string]$GithubToken +# ) + function add-csv { if (!(Test-Path $outfile)) { $newcsv = {} | Select "FILE_NAME","SHA" | Export-Csv $outfile @@ -8,25 +12,45 @@ function add-csv { } } -function main { - $fileDirectory = ".\Deployments" - add-csv - # $parse_results = New-Object System.Collections.ArrayList; +function main { + $token = "ghu_3EHLFRevI3QYc80494jfsilN6tQ4rI1vJUOL" + $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}} - # 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 $mainSha - Write-Output $sha + $treeUrl = "https://api.github.com/repos/aaroncorreya/SmartDeploymentPOC/git/trees/" + $mainSha + "?recursive=true" + Write-Output $treeUrl + $treeResponse = Invoke-RestMethod $treeUrl -Headers $header - "{0},{1}" -f $filePath,$sha | add-content -path $outfile + 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/repos/aaroncorreya/SmartDeploymentPOC/contents/trackingTable.csv" + $createResponse = Invoke-RestMethod $createFileUrl -Headers $header + Write-Output $createResponse } main \ No newline at end of file From 50d0f7f79c3b92212fd4b348d2e3211817ba7337 Mon Sep 17 00:00:00 2001 From: Aaron Correya Date: Fri, 12 Nov 2021 16:57:41 -0800 Subject: [PATCH 02/11] Updated script to create trackingTable.csv --- script.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script.ps1 b/script.ps1 index 26616c8..70a05c3 100644 --- a/script.ps1 +++ b/script.ps1 @@ -13,7 +13,7 @@ function add-csv { } function main { - $token = "ghu_3EHLFRevI3QYc80494jfsilN6tQ4rI1vJUOL" + $token = "ghu_osnzVOCUp7amtAfRtiymbviXs8uAW330byzu" $Header = @{ "authorization" = "Bearer $token" } From 43bd45c82ba45d29420410a3824171f0b4e074c9 Mon Sep 17 00:00:00 2001 From: Aaron Correya Date: Fri, 12 Nov 2021 17:11:13 -0800 Subject: [PATCH 03/11] Added token parameter to workflow and script --- .github/workflows/TestWorkflowTemplate.yml | 2 +- script.ps1 | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) 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 70a05c3..0c75afa 100644 --- a/script.ps1 +++ b/script.ps1 @@ -12,8 +12,13 @@ function add-csv { } } + + function main { - $token = "ghu_osnzVOCUp7amtAfRtiymbviXs8uAW330byzu" + param ( + $token + ) + $Header = @{ "authorization" = "Bearer $token" } @@ -53,4 +58,4 @@ function main { Write-Output $createResponse } -main \ No newline at end of file +main -token $args[0] From eb1775ca5489cd0e58020a478c7e64d29663447b Mon Sep 17 00:00:00 2001 From: Aaron Correya Date: Fri, 12 Nov 2021 17:12:21 -0800 Subject: [PATCH 04/11] Committed out creation for now. --- script.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/script.ps1 b/script.ps1 index 0c75afa..50eda55 100644 --- a/script.ps1 +++ b/script.ps1 @@ -53,9 +53,9 @@ function main { "{0},{1}" -f $_.Key, $_.Value | add-content -path $outfile } - $createFileUrl = "https://api.github.com/repos/aaroncorreya/SmartDeploymentPOC/repos/aaroncorreya/SmartDeploymentPOC/contents/trackingTable.csv" - $createResponse = Invoke-RestMethod $createFileUrl -Headers $header - Write-Output $createResponse + # $createFileUrl = "https://api.github.com/repos/aaroncorreya/SmartDeploymentPOC/repos/aaroncorreya/SmartDeploymentPOC/contents/trackingTable.csv" + # $createResponse = Invoke-RestMethod $createFileUrl -Headers $header + # Write-Output $createResponse } main -token $args[0] From f0cfd9fcf40015fcb15e70ddc369fc126c20d9aa Mon Sep 17 00:00:00 2001 From: Aaron Correya Date: Mon, 15 Nov 2021 10:57:02 -0800 Subject: [PATCH 05/11] Added creation/update csv file code. --- script.ps1 | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/script.ps1 b/script.ps1 index 50eda55..8da5cd3 100644 --- a/script.ps1 +++ b/script.ps1 @@ -53,9 +53,17 @@ function main { "{0},{1}" -f $_.Key, $_.Value | add-content -path $outfile } - # $createFileUrl = "https://api.github.com/repos/aaroncorreya/SmartDeploymentPOC/repos/aaroncorreya/SmartDeploymentPOC/contents/trackingTable.csv" - # $createResponse = Invoke-RestMethod $createFileUrl -Headers $header - # Write-Output $createResponse + $createFileUrl = "https://api.github.com/repos/aaroncorreya/SmartDeploymentPOC/repos/aaroncorreya/SmartDeploymentPOC/contents/trackingTable.csv" + $content = Get-Content -Path "./trackingTable.csv" | Out-String + Write-Output $content + $Parameters = @{ + Method = "PUT" + Uri = $createFileUrl + Headers = $Header + Message = "trackingTable.csv created." + Content = $content + } + Invoke-RestMethod @Parameters } main -token $args[0] From 33d75737ddbcd2d328b5aa1aed159ea3d7b6a314 Mon Sep 17 00:00:00 2001 From: Aaron Correya <34196924+aaroncorreya@users.noreply.github.com> Date: Mon, 15 Nov 2021 12:08:59 -0800 Subject: [PATCH 06/11] trackingTable.csv created. --- trackingTable.csv | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 trackingTable.csv diff --git a/trackingTable.csv b/trackingTable.csv new file mode 100644 index 0000000..887b728 --- /dev/null +++ b/trackingTable.csv @@ -0,0 +1,11 @@ +#TYPE Selected.System.Management.Automation.ScriptBlock +"FILE_NAME","SHA" +, +Deployments/testFile1.json,8330f68c71cb50f3eae48ea1bd328690be1a751b +Deployments/testFile2.json,d3e7e677ac8853b8ab6eada1c8fb0cd0a482f272 +Deployments/testFile1.json,8330f68c71cb50f3eae48ea1bd328690be1a751b +Deployments/testFile2.json,d3e7e677ac8853b8ab6eada1c8fb0cd0a482f272 +Deployments/testFile1.json,8330f68c71cb50f3eae48ea1bd328690be1a751b +Deployments/testFile2.json,d3e7e677ac8853b8ab6eada1c8fb0cd0a482f272 +Deployments/testFile1.json,8330f68c71cb50f3eae48ea1bd328690be1a751b +Deployments/testFile2.json,d3e7e677ac8853b8ab6eada1c8fb0cd0a482f272 From d86586761632c039e4c8e892b84e8bece592c4fa Mon Sep 17 00:00:00 2001 From: Aaron Correya <34196924+aaroncorreya@users.noreply.github.com> Date: Mon, 15 Nov 2021 15:36:04 -0800 Subject: [PATCH 07/11] Delete trackingTable.csv --- trackingTable.csv | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 trackingTable.csv diff --git a/trackingTable.csv b/trackingTable.csv deleted file mode 100644 index 887b728..0000000 --- a/trackingTable.csv +++ /dev/null @@ -1,11 +0,0 @@ -#TYPE Selected.System.Management.Automation.ScriptBlock -"FILE_NAME","SHA" -, -Deployments/testFile1.json,8330f68c71cb50f3eae48ea1bd328690be1a751b -Deployments/testFile2.json,d3e7e677ac8853b8ab6eada1c8fb0cd0a482f272 -Deployments/testFile1.json,8330f68c71cb50f3eae48ea1bd328690be1a751b -Deployments/testFile2.json,d3e7e677ac8853b8ab6eada1c8fb0cd0a482f272 -Deployments/testFile1.json,8330f68c71cb50f3eae48ea1bd328690be1a751b -Deployments/testFile2.json,d3e7e677ac8853b8ab6eada1c8fb0cd0a482f272 -Deployments/testFile1.json,8330f68c71cb50f3eae48ea1bd328690be1a751b -Deployments/testFile2.json,d3e7e677ac8853b8ab6eada1c8fb0cd0a482f272 From e9e2cbba9ea1ed1d7f05bc6616eddad0a475c725 Mon Sep 17 00:00:00 2001 From: Aaron Correya Date: Mon, 15 Nov 2021 15:36:30 -0800 Subject: [PATCH 08/11] Finished csv creation functionality --- script.ps1 | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/script.ps1 b/script.ps1 index 8da5cd3..4dbb9e9 100644 --- a/script.ps1 +++ b/script.ps1 @@ -1,9 +1,5 @@ $global:outfile="trackingTable.csv" -# Param ( -# [Parameter(Mandatory=$true)][string]$GithubToken -# ) - function add-csv { if (!(Test-Path $outfile)) { $newcsv = {} | Select "FILE_NAME","SHA" | Export-Csv $outfile @@ -12,8 +8,6 @@ function add-csv { } } - - function main { param ( $token @@ -53,16 +47,26 @@ function main { "{0},{1}" -f $_.Key, $_.Value | add-content -path $outfile } - $createFileUrl = "https://api.github.com/repos/aaroncorreya/SmartDeploymentPOC/repos/aaroncorreya/SmartDeploymentPOC/contents/trackingTable.csv" + $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 - Message = "trackingTable.csv created." - Content = $content + Body = $body | ConvertTo-Json } + #Commit csv file Invoke-RestMethod @Parameters } From 45c9638ec3f3a3c83e4f0c1357ec5f8d90a9a208 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 15 Nov 2021 23:37:09 +0000 Subject: [PATCH 09/11] trackingTable.csv created. --- trackingTable.csv | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 trackingTable.csv 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 From 25460a20de77073fd313ab272619bc806f2d3835 Mon Sep 17 00:00:00 2001 From: Aaron Correya <34196924+aaroncorreya@users.noreply.github.com> Date: Wed, 15 Dec 2021 14:22:39 -0800 Subject: [PATCH 10/11] Delete trackingTable.csv --- trackingTable.csv | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 trackingTable.csv diff --git a/trackingTable.csv b/trackingTable.csv deleted file mode 100644 index 5e36315..0000000 --- a/trackingTable.csv +++ /dev/null @@ -1,4 +0,0 @@ -"FILE_NAME","SHA" -, -Deployments/testFile2.json,d3e7e677ac8853b8ab6eada1c8fb0cd0a482f272 -Deployments/testFile1.json,8330f68c71cb50f3eae48ea1bd328690be1a751b From 73d7d9580e3e35b35ae348d570c24b55e02feb8e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 15 Dec 2021 22:23:06 +0000 Subject: [PATCH 11/11] trackingTable.csv created. --- trackingTable.csv | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 trackingTable.csv 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