Skip to content

Instantly share code, notes, and snippets.

@vinijmoura
Created February 16, 2022 22:51
Show Gist options
  • Save vinijmoura/90c870ba66442d072460812ec5d81f4a to your computer and use it in GitHub Desktop.
Save vinijmoura/90c870ba66442d072460812ec5d81f4a to your computer and use it in GitHub Desktop.
Param
(
[string]$PAT,
[string]$Organization,
[string]$Connstr
)
$SQLQuery = "TRUNCATE TABLE RepositoriesBranchesAheadBehind"
Invoke-Sqlcmd -query $SQLQuery -ConnectionString $Connstr
$UriOrganization = "https://api.github.com/orgs/$($organization)"
$UriRepos = "https://api.github.com/repos/$($organization)"
$base64Token = [System.Convert]::ToBase64String([char[]]$PAT)
$headers = @{Authorization = 'Basic {0}' -f $base64Token};
$uriRepositories = "$($UriOrganization)/repos"
$RepositoriesResult = Invoke-RestMethod -Headers $headers -Uri $uriRepositories
foreach ($repo in $RepositoriesResult)
{
$urlBranchesRepo = $repo.branches_url.Replace('{/branch}','')
$BranchesRepoResult = Invoke-RestMethod -Headers $headers -Uri $urlBranchesRepo
foreach ($branchRepo in $BranchesRepoResult)
{
$uriCompare = "$($UriRepos)/$($repo.name)/compare/$($repo.default_branch)...$($branchRepo.name)"
$ComparesResult = Invoke-RestMethod -Headers $headers -Uri $uriCompare
$SQLQuery = "INSERT INTO RepositoriesBranchesAheadBehind (
RepositoryId,
RepositoryName,
RepositoryBranchName,
RepositoryBranchAheadCount,
RepositoryBranchBehindCount
)
VALUES(
'$($repo.id)',
'$($repo.name)',
'$($branchRepo.name)',
$($ComparesResult.ahead_by),
$($ComparesResult.behind_by)
)"
Invoke-Sqlcmd -query $SQLQuery -ConnectionString $Connstr
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment