Merge pull request #1504 from PowerShell/maertend-patch-2
Created readme.md, rest folder, and added the up to date script files
This commit is contained in:
commit
f8364d9d66
3 changed files with 79 additions and 0 deletions
20
demos/rest/curlDemo.txt
Normal file
20
demos/rest/curlDemo.txt
Normal file
|
@ -0,0 +1,20 @@
|
|||
# EQ Linux
|
||||
|
||||
# get the json from the repo api and assign it to the txt file
|
||||
|
||||
# This grads the json file from the API
|
||||
curl -u <insert GitHub PAT token> <insert GitHub repo URL> > outputtest.txt
|
||||
|
||||
# Reformats the json block to proper json (removes the newlines)
|
||||
string=$(cat temp.txt)
|
||||
|
||||
# Replace the "private" value to false
|
||||
find='"private": true'
|
||||
replace='"private": false'
|
||||
string2=${string/$find/$replace}
|
||||
|
||||
# push the json block into a txt file
|
||||
echo $string2 > output.txt
|
||||
|
||||
# Post the updated JSON data to the repo
|
||||
curl -u <insert GitHub PAT token> <insert GitHub repo URL> --data @output.txt
|
13
demos/rest/readme.md
Normal file
13
demos/rest/readme.md
Normal file
|
@ -0,0 +1,13 @@
|
|||
This demo shows interacting with the Github API via Invoke-RestMethod.
|
||||
NOTE: A repo URL must be specified in these scripts and a Github PAT token with access to the repo must be generated and specified
|
||||
|
||||
rest.ps1:
|
||||
Invoke-RestMethod is used to get the json of a repo as a PowerShell object,
|
||||
the object is then manipulated and the "private" parameter is changed to 'false'.
|
||||
The object is converted back to json formating and Posted back to the repo API
|
||||
|
||||
The benefit of PS is shown at the end of the script with PS objects.
|
||||
Enabling users to get info on multiple repos and then sort that data as objects.
|
||||
|
||||
curlDemo.txt:
|
||||
This shos the equavilent bash commmands to change the private status of a Github repo
|
46
demos/rest/rest.ps1
Normal file
46
demos/rest/rest.ps1
Normal file
|
@ -0,0 +1,46 @@
|
|||
# NOTE: This demo is still in progress and needs validation in Linux
|
||||
# ------------------------------------
|
||||
|
||||
#region Setup the credentials for use in HTTP header
|
||||
$user = '<insert GitHub PAT token>'
|
||||
$pass= ""
|
||||
$pair = "${user}:${pass}"
|
||||
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
|
||||
$base64 = [System.Convert]::ToBase64String($bytes)
|
||||
$basicAuthValue = "Basic $base64"
|
||||
$headers = @{ Authorization = $basicAuthValue }
|
||||
#endregion
|
||||
|
||||
# Changing the status of a Private GitHub repository to Public
|
||||
|
||||
# URL to PowerShell Github Repo
|
||||
$PowerShellGithubUri = '<insert GitHub repo URL>'
|
||||
|
||||
# Get the blob from the Github API as a PS object
|
||||
$JsonBlock = Invoke-RestMethod -Uri $PowerShellGithubUri -Headers $headers
|
||||
|
||||
# Explore the object (Notice that it is a private repo)
|
||||
$JsonBlock
|
||||
|
||||
# Given it is an object, you can explore and interact with it
|
||||
# Change the private value to false
|
||||
$JsonBlock.private = 'false'
|
||||
|
||||
# Convert the object back to a json
|
||||
$Json = ConvertTo-Json $JsonBlock
|
||||
|
||||
# Post the updated json block back to the GitHub
|
||||
Invoke-RestMethod -Uri $PowerShellGithubUri -Headers $headers -Method Post -Body $Json
|
||||
|
||||
|
||||
# --------------
|
||||
|
||||
# We can also use the PS objects to sort the different repos on github
|
||||
|
||||
# If we grab the json from the PowerShell github
|
||||
Invoke-RestMethod https://api.github.com/users/powershell/repos | sv repoData
|
||||
|
||||
# We can sort it based on the number of forks each repo has
|
||||
$repoData | Sort-Object -Property forks_count -Descending | ft -f id,name,stargazers_count,forks_count
|
||||
|
||||
$repoData | Sort-Object -Property stargazers_count -Descending | ft -f id,name,stargazers_count,forks_count
|
Loading…
Reference in a new issue