mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-17 04:36:39 +00:00
Update rest demo (#1854)
This commit is contained in:
1 parent
1509c1f311
commit
338035be93
3 files changed
+48
-77
No files matched your search
@@ -1,20 +0,0 @@
|
|||||||
# 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
|
|
||||||
+5
-11
@@ -1,13 +1,7 @@
|
|||||||
This demo shows interacting with the Github API via Invoke-RestMethod.
|
## Rest demo
|
||||||
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
|
|
||||||
|
This demo shows interacting with the Github API via Invoke-WebRequest.
|
||||||
|
|
||||||
rest.ps1:
|
rest.ps1:
|
||||||
Invoke-RestMethod is used to get the json of a repo as a PowerShell object,
|
Invoke-WebRequest and ConvertFrom-Json are used to get the issues of a repo.
|
||||||
the object is then manipulated and the "private" parameter is changed to 'false'.
|
The issues are processed as objects to find the most commented on issues.
|
||||||
The object is converted back to json formating and Posted back to the repo API
|
|
||||||
|
|
||||||
The benefit of PowerShell 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 shows the equavilent bash commmands to change the private status of a Github repo
|
|
||||||
+43
-46
@@ -1,46 +1,43 @@
|
|||||||
# NOTE: This demo is still in progress and needs validation in Linux
|
|
||||||
# ------------------------------------
|
#-----------------
|
||||||
|
|
||||||
#region Setup the credentials for use in HTTP header
|
function Get-Issues
|
||||||
$user = '<insert GitHub PAT token>'
|
{
|
||||||
$pass= ""
|
param([string]$UserName,
|
||||||
$pair = "${user}:${pass}"
|
[string]$Repo,
|
||||||
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
|
[ValidateRange(1,100)][int]$PerPage = 100)
|
||||||
$base64 = [System.Convert]::ToBase64String($bytes)
|
|
||||||
$basicAuthValue = "Basic $base64"
|
$body = @{
|
||||||
$headers = @{ Authorization = $basicAuthValue }
|
per_page = $PerPage
|
||||||
#endregion
|
}
|
||||||
|
|
||||||
# Changing the status of a Private GitHub repository to Public
|
$uri = "https://api.github.com/repos/$UserName/$Repo/issues"
|
||||||
|
while ($uri)
|
||||||
# URL to PowerShell Github Repo
|
{
|
||||||
$PowerShellGithubUri = '<insert GitHub repo URL>'
|
$response = Invoke-WebRequest -Uri $uri -Body $body
|
||||||
|
$response.Content | ConvertFrom-Json | Write-Output
|
||||||
# Get the blob from the Github API as a PS object
|
|
||||||
$JsonBlock = Invoke-RestMethod -Uri $PowerShellGithubUri -Headers $headers
|
$uri = $null
|
||||||
|
foreach ($link in $response.Headers.Link -split ',')
|
||||||
# Explore the object (Notice that it is a private repo)
|
{
|
||||||
$JsonBlock
|
if ($link -match '\s*<(.*)>;\s+rel="next"')
|
||||||
|
{
|
||||||
# Given it is an object, you can explore and interact with it
|
$uri = $matches[1]
|
||||||
# Change the private value to false
|
}
|
||||||
$JsonBlock.private = 'false'
|
}
|
||||||
|
}
|
||||||
# Convert the object back to a json
|
}
|
||||||
$Json = ConvertTo-Json $JsonBlock
|
|
||||||
|
$issues = Get-Issues -UserName lzybkr -Repo PSReadline
|
||||||
# Post the updated json block back to the GitHub
|
|
||||||
Invoke-RestMethod -Uri $PowerShellGithubUri -Headers $headers -Method Post -Body $Json
|
$issues.Count
|
||||||
|
|
||||||
|
$issues | Sort-Object -Descending comments | Select-Object -First 15 | ft number,comments,title
|
||||||
# --------------
|
|
||||||
|
foreach ($issue in $issues)
|
||||||
# We can also use the PS objects to sort the different repos on github
|
{
|
||||||
|
if ($issue.labels.name -contains 'bug' -and $issue.labels.name -contains 'vi mode')
|
||||||
# If we grab the json from the PowerShell github
|
{
|
||||||
Invoke-RestMethod https://api.github.com/users/powershell/repos | sv repoData
|
"{0} is a vi mode bug" -f $issue.url
|
||||||
|
}
|
||||||
# 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
|
|
||||||
Reference in new issue
Block a user