-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAzureDevOps-PullRequestStats.ps1
More file actions
65 lines (53 loc) · 2.68 KB
/
AzureDevOps-PullRequestStats.ps1
File metadata and controls
65 lines (53 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# AzureDevOps-PullRequestStats.ps1
# This script provides a weekly breakdown of pull request stats across all projects and repositories within an Azure DevOps organization.
#Your organization name here
$organization = ""
#Replace with your Personal Access Token (NB : the colon is required at the beginning of the token before the pat itself, so please always include the : in front of the pat)
$accessToken = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":your-pat-token"))
# Parameters for time frame and pull request status
$startDate = "2023-01-01" # Default start date
$endDate = "2024-01-31" # Default end date
$pullRequestStatus = "all" # Default status
$Headers = @{
"Authorization" = $accessToken
"Content-Type" = "application/json"
}
Function Group-PullRequestsByWeek {
param (
[Parameter(Mandatory = $true)]
[array]$pullRequests
)
$groupedByWeek = @{}
foreach ($pr in $pullRequests) {
$creationDate = [DateTime]::Parse($pr.creationDate)
$weekOfYear = Get-Date -Year $creationDate.Year -Month $creationDate.Month -Day $creationDate.Day -UFormat "%V"
$yearWeek = "$($creationDate.Year)-W$weekOfYear"
$groupedByWeek[$yearWeek] += 1
}
return $groupedByWeek
}
$projectApiUrl = "https://dev.azure.com/$organization/_apis/projects?api-version=7.1-preview.1"
$projectsResponse = Invoke-RestMethod -Uri $projectApiUrl -Headers $Headers
$projects = $projectsResponse.value
foreach ($project in $projects) {
$projectId = $project.id
$repoApiUrl = "https://dev.azure.com/$organization/$projectId/_apis/git/repositories?api-version=7.1-preview.1"
$reposResponse = Invoke-RestMethod -Uri $repoApiUrl -Headers $Headers
$repos = $reposResponse.value
foreach ($repo in $repos) {
$repoId = $repo.id
$pullRequestsApiUrl = "https://dev.azure.com/$organization/$projectId/_apis/git/pullrequests?searchCriteria.repositoryId=$repoId&searchCriteria.status=$pullRequestStatus&searchCriteria.minTime=$startDate&searchCriteria.maxTime=$endDate&api-version=7.1-preview.1"
$pullRequestsResponse = Invoke-RestMethod -Uri $pullRequestsApiUrl -Headers $Headers
$pullRequests = $pullRequestsResponse.value
if ($pullRequests.Count -gt 0) {
$groupedByWeek = Group-PullRequestsByWeek $pullRequests
Write-Host "Project: $($project.name) - Repo: $($repo.name)"
foreach ($week in $groupedByWeek.Keys | Sort-Object) {
Write-Host "$week : $($groupedByWeek[$week])"
}
}
else {
Write-Host "Project: $($project.name) - Repo: $($repo.name) has no pull requests matching the criteria."
}
}
}