How to: List user Repositories and respective Stargazers on GitHub

Vinicius Moura
2 min readSep 5, 2022

--

This script and report extract all user repositories and stargazers on GitHub in the timeline

An original script is available on my GitHub repository. See below this script:

Let’s go understand each command used.

  1. PowerShell script will receive the following parameters:
  • $PAT = Personal Access token to connect on GitHub user account;
  • $UserGitHub = GitHub user name;
  • $Connstr = connection string to Azure SQL Database stores the report information. To create this report, it’s necessary to create a Azure SQL Server and Database previously and run a script below:

2. List repositories for a user = use this REST API to list public repositories for the specified user

$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'}
$Repos = @()
$pageRepos=1
do
{
$uriRepositories="$($UriUser)/repos?page=$($pageRepos)"
$RepositoriesResult=Invoke-RestMethod -Headers $headers -Uri $uriRepositories
$Repos+=$RepositoriesResult
$pageRepos++
} while ($RepositoriesResult.Count -gt 0)

3. List stargazers = use this REST API to list the people that have starred the repository

$pageStargazers=1    
do
{
$uriStargazers="$($UriRepos)/$($repo.name)/stargazers? page=$($pageStargazers)"
$StargazersRepoResult=Invoke-RestMethod -Headers $headers -Uri $uriStargazers
foreach ($stargazer in $StargazersRepoResult)
{
$stargazer.user.login
}
$pageStargazers++
} while ($StargazersRepoResult.Count -gt 0)

4. After extracting all repositories, and stargazers, this information is stored in a table in Azure SQL.

5. After inserting information into a table, I connected this database on Power BI:

  • Repository Name (1) = Filter report using Repository Name field;
  • Stargazer Dates (Range) (2) = Filter report using stargazers date range;
  • Repositories, stargazer date, and avatar (3) = List all information about stargazer occurrences, respective date, and stargazer on each user repository;
  • Number of Stargazers per repository (4): a chart that shows the number of stargazers per repository;
  • Number of stargazers per Year/Month (evolution) (5): a chart that shows the evolution of stargazers in the timeline.

--

--