Created
September 5, 2022 01:00
-
-
Save vinijmoura/ac311299e9164079f68ab4f5d8d8f161 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param | |
( | |
[string]$PAT, | |
[string]$UserGitHub, | |
[string]$Connstr | |
) | |
Get-Date | |
$SQLQuery="TRUNCATE TABLE RepositoriesStargazers" | |
Invoke-Sqlcmd -query $SQLQuery -ConnectionString $Connstr | |
$UriUser="https://api.github.com/users/$($UserGitHub)" | |
$UriRepos="https://api.github.com/repos/$($UserGitHub)" | |
$base64Token=[System.Convert]::ToBase64String([char[]]$PAT) | |
$headers=@{Authorization='Basic {0}' -f $base64Token;Accept='application/vnd.github.v3.star+json'} | |
#$headers=@{Authorization='Basic {0}' -f $base64Token} | |
$Repos = @() | |
$pageRepos=1 | |
do | |
{ | |
$uriRepositories="$($UriUser)/repos?page=$($pageRepos)" | |
$RepositoriesResult=Invoke-RestMethod -Headers $headers -Uri $uriRepositories | |
$Repos+=$RepositoriesResult | |
$pageRepos++ | |
} while ($RepositoriesResult.Count -gt 0) | |
foreach ($repo in $Repos) | |
{ | |
$pageStargazers=1 | |
do | |
{ | |
$uriStargazers="$($UriRepos)/$($repo.name)/stargazers?page=$($pageStargazers)" | |
$StargazersRepoResult=Invoke-RestMethod -Headers $headers -Uri $uriStargazers | |
foreach ($stargazer in $StargazersRepoResult) | |
{ | |
$SQLQuery = "INSERT INTO RepositoriesStargazers ( | |
RepositoryName, | |
StargazerLogin, | |
StargazerAvatarUrl, | |
StargazerCreatedDate | |
) | |
VALUES( | |
'$($repo.name)', | |
'$($stargazer.user.login)', | |
'$($stargazer.user.avatar_url)', | |
'$($stargazer.starred_at)' | |
)" | |
Invoke-Sqlcmd -query $SQLQuery -ConnectionString $Connstr | |
} | |
$pageStargazers++ | |
} while ($StargazersRepoResult.Count -gt 0) | |
} | |
Get-Date |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment