-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelete-GitHubRepos.ps1
67 lines (58 loc) · 2.36 KB
/
Delete-GitHubRepos.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
# GitHub Repository Deletion Tool
# Author: Jerome Wolff
# Purpose: Deletes all GitHub repositories in an organization with a specific prefix.
# Define parameters
param (
[string]$GitHubToken, # Fine-grained personal access token
[string]$OrganizationName, # GitHub organization name
[string]$RepoPrefix # Prefix for repositories to delete
)
# Check for required inputs
if (-not $GitHubToken -or -not $OrganizationName) {
Write-Error "GitHubToken and OrganizationName are required parameters."
exit 1
}
# GitHub API endpoint
$GitHubApiUrl = "https://api.github.com"
# Function to get repositories with a specific prefix
function Get-RepositoriesWithPrefix {
param (
[string]$Organization,
[string]$Prefix
)
$headers = @{ Authorization = "Bearer $GitHubToken" }
$url = "$GitHubApiUrl/orgs/$Organization/repos?per_page=100"
$repositories = @()
do {
$response = Invoke-RestMethod -Uri $url -Headers $headers -Method Get -ErrorAction Stop
$repositories += $response | Where-Object { $_.name -like "$Prefix*" }
$url = ($response | Get-Member -Name "Link" -ErrorAction Ignore).Definition -match 'rel="next"' ? $matches[1] : $null
} while ($url)
return $repositories
}
# Function to delete a repository
function Delete-Repository {
param (
[string]$RepoFullName
)
$headers = @{ Authorization = "Bearer $GitHubToken" }
$url = "$GitHubApiUrl/repos/$RepoFullName"
try {
Invoke-RestMethod -Uri $url -Headers $headers -Method Delete -ErrorAction Stop
Write-Host "Deleted repository: $RepoFullName" -ForegroundColor Green
} catch {
Write-Error "Failed to delete repository: $RepoFullName. $_"
}
}
# Main script
Write-Host "Fetching repositories with prefix '$RepoPrefix' in organization '$OrganizationName'..." -ForegroundColor Yellow
$repositories = Get-RepositoriesWithPrefix -Organization $OrganizationName -Prefix $RepoPrefix
if ($repositories.Count -eq 0) {
Write-Host "No repositories found with prefix '$RepoPrefix' in organization '$OrganizationName'." -ForegroundColor Cyan
exit 0
}
Write-Host "Found $($repositories.Count) repositories to delete." -ForegroundColor Yellow
foreach ($repo in $repositories) {
Delete-Repository -RepoFullName $repo.full_name
}
Write-Host "Deletion process completed." -ForegroundColor Green