-
Notifications
You must be signed in to change notification settings - Fork 0
/
V2 Sonar6 - billing script, getProjectAdmins.ps1
118 lines (84 loc) · 3.26 KB
/
V2 Sonar6 - billing script, getProjectAdmins.ps1
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<#
Powershell script for obtain projects admins from sonarqube
#>
####### CONFIG #######
$debug = "0";
#Authentication
#prod sonar
$baseUrl = "SONARBASEURL"
$Username = "AUTHTOKEN"
#URLS
$Headers = @{ Authorization = "Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $Username,$Password))) }
$urlProjectSearch="/api/projects/search?ps=499&qualifiers=TRK"
$urlPRojectsPermissions="/api/permissions/users?projectKey="
<#
LOAD Projects with last analysis date, key, name
#>
function getProjects(){
$projUrl = "$baseUrl$urlProjectSearch"
if($debug -eq "1"){
Write-Host "projects URL: $projURL"
}
$allProjList = New-Object System.Collections.ArrayList
#pagination
$loadedCount = 0
$expectedCount = 0
$currPage =1
do{
$urlPaging=$projUrl+"&p="+$currPage
if($debug -eq "1"){
Write-Host $urlPaging
}
$obj = Invoke-WebRequest -Uri $urlPaging -Headers $Headers
$json = $obj.Content | ConvertFrom-Json
$expectedCount=$json.paging.total
$json.components | foreach {
$project = New-Object System.Object
$project | Add-Member -type NoteProperty -name name -value $_.name
$project | Add-Member -type NoteProperty -name key -value $_.key
$project | Add-Member -type NoteProperty -name id -value $_.id
$project | Add-Member -type NoteProperty -name lastAnalysis -value $analysisDate
$adminUsers = New-Object System.Collections.ArrayList
$adminUsers = getProjectPermissions($project)
$project | Add-Member -type NoteProperty -name adminUsers -value $adminUsers
$allProjList.Add($project) > $null
}
$loadedCount = $loadedCount +1
$currPage=$currPage+1
}while($loadedCount -lt $expectedCount )
return $allProjList
}
<#
Load project permissions and filter per admins
#>
function getProjectPermissions($project){
$returnList = New-Object System.Collections.ArrayList
$projPermUrl = "$baseUrl$urlPRojectsPermissions" + $project.key
if($debug -eq "1"){
Write-Host $projPermUrl
}
$obj = Invoke-WebRequest -Uri $projPermUrl -Headers $Headers
$json = $obj.Content | ConvertFrom-Json
$json = $json.users
foreach ($jsonItem in $json) {
#Write-Host $jsonItem.name
$permissions = $jsonItem.permissions
foreach ($permissionItem in $permissions){
#Write-Host $permissionItem
if($permissionItem -eq "admin"){
$returnList.Add($jsonItem.name + "-" + $jsonItem.email) >$null
}
}
}
return $returnList
}
[System.Collections.ArrayList] $projList = getProjects
foreach ($project in $projList){
$admins = ""
foreach ($admin in $project.adminUsers){
$admins = $admin + ";" + $admins
}
$record = $project.name + ";" +$admins
Write-Host $record
Add-Content -Path "adminSonarUsers.txt" -Value $record
}