Skip to content

Instantly share code, notes, and snippets.

@phenixita
Created October 24, 2019 10:42
Show Gist options
  • Save phenixita/0838935dfdd44acebfb4060132785e3e to your computer and use it in GitHub Desktop.
Save phenixita/0838935dfdd44acebfb4060132785e3e to your computer and use it in GitHub Desktop.
Powershell Scripts to gather some stats from Azure DevOps projects
function GetUrl() {
param(
[string]$orgUrl,
[hashtable]$header,
[string]$AreaId
)
# Build the URL for calling the org-level Resource Areas REST API for the RM APIs
$orgResourceAreasUrl = [string]::Format("{0}/_apis/resourceAreas/{1}?api-preview=5.0-preview.1", $orgUrl, $AreaId)
# Do a GET on this URL (this returns an object with a "locationUrl" field)
$results = Invoke-RestMethod -Uri $orgResourceAreasUrl -Headers $header
# The "locationUrl" field reflects the correct base URL for RM REST API calls
if ("null" -eq $results -or $results.count -gt 0) {
$areaUrl = $orgUrl
}
else {
$areaUrl = $results.locationUrl
}
return $areaUrl
}
$orgUrl = "https://dev.azure.com/xxxx"
$personalToken = ""
Write-Host "Initialize authentication context" -ForegroundColor Yellow
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
$header = @{authorization = "Basic $token" }
Write-Host "Lista dei progetti" -ForegroundColor Yellow
$coreAreaId = "79134c72-4a58-4b42-976c-04e7115f32bf"
$tfsBaseUrl = GetUrl -orgUrl $orgUrl -header $header -AreaId $coreAreaId
$projectsUrl = "$($tfsBaseUrl)_apis/projects?api-version=5.0"
$projects = Invoke-RestMethod -Uri $projectsUrl -Method Get -ContentType "application/json" -Headers $header
$projects.value | ForEach-Object {
Write-Host $_.name -ForegroundColor White
}
$globalSumBuildRun = 0;
Write-Host "Contegggio Build run per progetto" -ForegroundColor Yellow
$projects.value | ForEach-Object {
$project = $_.name
$tfsBaseUrl = GetUrl -orgUrl $orgUrl -header $header
$relDefUrl = "$tfsBaseUrl/$project/_apis/build/builds?api-version=5.1"
$result = Invoke-RestMethod $relDefUrl -Method Get -ContentType "application/json" -Headers $header
$relDefs = $result.value
if ($relDefs.count -gt 0) {
$globalSumBuildRun += $relDefs.Count
Write-Host "$project $($relDefs.count) build founds" -ForegroundColor White
}
}
Write-Host "Conteggio globale build: " $globalSumBuildRun -ForegroundColor White
$globalSumGitCount = 0;
$globalCommitCount = 0;
Write-Host "Contegggio dei repo git per progetto e del numero di commit" -ForegroundColor Yellow
$projects.value | ForEach-Object {
$project = $_.name
$relDefUrl = "$tfsBaseUrl/$project/_apis/git/repositories?api-version=5.1"
$result = Invoke-RestMethod $relDefUrl -Method Get -ContentType "application/json" -Headers $header
$relDefs = $result.value
if ($relDefs.count -gt 0) {
$globalSumGitCount += $relDefs.Count
Write-Host "$project $($relDefs.count) git repos found" -ForegroundColor White
}
$result.value | ForEach-Object {
$repositoryId = $_.id
$relUrlForGitCommits = "$tfsBaseUrl/$project/_apis/git/repositories/$repositoryId/commits?api-version=5.1"
$gitCommitsResult = Invoke-RestMethod $relUrlForGitCommits -Method Get -ContentType 'application/json' -Headers $header
$gitCommits = $gitCommitsResult.value.Length
if ($gitCommits -gt 0) {
$globalCommitCount += $gitCommits
}
}
}
Write-Host "Conteggio git repo totali: " $globalSumGitCount -ForegroundColor White
Write-Host "Conteggio git commit totali " $globalCommitCount -ForegroundColor White
$globalSumTestRuns = 0;
Write-Host "Contegggio Test runs progetto" -ForegroundColor Yellow
$projects.value | ForEach-Object {
$project = $_.name
$tfsBaseUrl = GetUrl -orgUrl $orgUrl -header $header
$relDefUrl = "$tfsBaseUrl/$project/_apis/test/runs?api-version=5.1"
$result = Invoke-RestMethod $relDefUrl -Method Get -ContentType "application/json" -Headers $header
$relDefs = $result.value
if ($relDefs.count -gt 0) {
$globalSumTestRuns += $relDefs.Count
Write-Host "$project $($relDefs.count) test runs founds" -ForegroundColor White
}
}
Write-Host "Conteggio test run totali " $globalSumTestRuns -ForegroundColor White
# $relDefUrl = "https://vssps.dev.azure.com/ader-devops/_apis/graph/users?api-version=5.1"
# $result = Invoke-RestMethod $relDefUrl -Method Get -ContentType "application/json" -Headers $header
# $relDefs = $result.value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment