-
Notifications
You must be signed in to change notification settings - Fork 1
/
Teams-repoaccess.ps1
95 lines (57 loc) · 3.01 KB
/
Teams-repoaccess.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
Write-Output " _____ _______ "
Write-Output " | __ \ |__ __| /\ "
Write-Output " | |__) |___ _ __ ___ | | ___ __ _ _ __ ___ ___ / \ ___ ___ ___ ___ ___ "
Write-Output " | _ // _ \ '_ \ / _ \ | |/ _ \/ _ | '_ ` _ \/ __| / /\ \ / __/ __/ _ \/ __/ __| "
Write-Output " | | \ \ __/ |_) | (_) | | | __/ (_| | | | | | \__ \ / ____ \ (_| (_| __/\__ \__ \ "
Write-Output " |_| \_\___| .__/ \___/ |_|\___|\__,_|_| |_| |_|___/ /_/ \_\___\___\___||___/___/ "
Write-Output " | | "
Write-Output " |_| "
Write-Host
$PATToken = Read-Host -Prompt 'GitHub Token' -AsSecureString
$Organization = Read-Host -Prompt 'GitHub Organization'
$ExcelSourceDir = Read-Host -Prompt 'Excel Source'
$WorkSheetName = Read-Host -Prompt 'Specify the worksheet name'
$UserToken =[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($PATToken))
$head = @{
Authorization = 'Bearer ' + $UserToken
Accept ="application/vnd.github.nebula-preview+json"
}
$excel = New-Object -com Excel.Application
$wbook = $excel.workbooks.open($ExcelSourceDir)
$worksheet = $wbook.Worksheets.Item($WorkSheetName)
$maxrows = ($worksheet.UsedRange.Rows).Count
$wbobject = New-Object -TypeName psobject
$wbobject | Add-Member -MemberType NoteProperty -Name Team -Value $null
$wbobject | Add-Member -MemberType NoteProperty -Name Repository -Value $null
$wbobject | Add-Member -MemberType NoteProperty -Name Permissions -Value $null
for ($i = 2; $i -le $maxrows; $i++)
{
$wbobject.Team = $worksheet.Cells.item($i,1).Text.ToLower();
$wbobject.Repository = $worksheet.Cells.item($i,2).Text.ToLower();
$wbobject.Permissions = $worksheet.Cells.item($i,3).Text.ToLower();
$repo = $wbobject.Repository
$team = $wbobject.Team
$repopermission = $wbobject.Permissions
$repoparams=@{
permission=$repopermission
}
$newteamname = $team -replace ' ','-'
$body=$repoparams | ConvertTo-Json
$teamsaccesstoreposrequest=@{
Uri = "https://api.github.com/orgs/$Organization/teams/$newteamname/repos/$Organization/$repo"
Method = "PUT"
body = $body
ContentType = "application/json"
Headers = $head
}
try {
$gitObject= Invoke-RestMethod @teamsaccesstoreposrequest
$repo_newname= $repo.ToUpper()
$team_newname= $team.ToUpper()
Write-Host
Write-Host "$team_newname Team has been given $repopermission permissions to $repo_newname"
} catch{
Write-Host "Error:" $_.Exception.Response.StatusDescription -ForegroundColor Red
}
}