How to: List user Repositories and respective Forks on GitHub
2 min readSep 2, 2022
This script and report extract all user repositories and forks 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.
- 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 that stores the report information. To create this report, it’s necessary to create previously a Azure SQL Server and Database 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} $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 forks = use this REST API to list forks for specific repository
$Forks=@()
foreach ($repo in $Repos)
{
$pageForks=1
do
{
$uriForks="$($UriRepos)/$($repo.name)/forks?page=$($pageForks)"
$ForksRepoResult=Invoke-RestMethod -Headers $headers -Uri
$uriForks
$Forks+=$ForksRepoResult
$pageForks++
} while ($ForksRepoResult.Count -gt 0)
}
4. After extracting all repositories, and forks, 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;
- Fork Dates (Range) (2) = Filter report using Forks date range;
- Repositories and fork date (3) = List all information about fork occurrences and respective dates on each user repository;
- Number of forks per repository (4): a chart that shows the number of forks per repository;
- Number of forks per month/year (5): a chart that shows the number of forks in the timeline;