Created
June 30, 2021 17:38
-
-
Save vinijmoura/5c11a39e4d3ac04c00020bb96c868e2c 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]$Organization, | |
[string]$Connstr | |
) | |
$SQLQuery = "TRUNCATE TABLE ReposAheadBehind" | |
Invoke-Sqlcmd -query $SQLQuery -ConnectionString $Connstr | |
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) } | |
$UriOrganization = "https://dev.azure.com/$($Organization)/" | |
$uriProject = $UriOrganization + "_apis/projects?`$top=500" | |
$ProjectsResult = Invoke-RestMethod -Uri $uriProject -Method get -Headers $AzureDevOpsAuthenicationHeader | |
Foreach ($project in $ProjectsResult.value) | |
{ | |
$uriRepositories = $UriOrganization + "$($project.id)/_apis/git/repositories?api-version=6.1-preview.1" | |
$RepositoriesResult = Invoke-RestMethod -Uri $uriRepositories -Method get -Headers $AzureDevOpsAuthenicationHeader | |
Foreach ($repo in $RepositoriesResult.value) | |
{ | |
if ($repo.size) | |
{ | |
$uriRepositoryStats = $UriOrganization + "$($project.id)/_apis/git/repositories/$($repo.id)/stats/branches?api-version=6.1-preview.1" | |
$RepositoryStatsResult = Invoke-RestMethod -Uri $uriRepositoryStats -Method get -Headers $AzureDevOpsAuthenicationHeader | |
Foreach ($repostats in $RepositoryStatsResult.value) | |
{ | |
$SQLQuery = "INSERT INTO ReposAheadBehind ( | |
TeamProjectName, | |
RepositoryId, | |
RepositoryName, | |
RepositoryBranchName, | |
RepositoryBranchAheadCount, | |
RepositoryBranchBehindCount, | |
RepositoryBranchIsBaseVersion | |
) | |
VALUES( | |
'$($project.name)', | |
'$($repo.id)', | |
'$($repo.name)', | |
'$($repostats.name)', | |
$($repostats.aheadCount), | |
$($repostats.behindCount), | |
'$($repostats.isBaseVersion)' | |
)" | |
Invoke-Sqlcmd -query $SQLQuery -ConnectionString $Connstr | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment