-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGet-GitHubRepos.ps1
More file actions
80 lines (65 loc) · 2.17 KB
/
Get-GitHubRepos.ps1
File metadata and controls
80 lines (65 loc) · 2.17 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
function Get-GitHubRepos {
<#
.SYNOPSIS
Clones or updates GitHub repositories for a specified organization.
.DESCRIPTION
This script clones or updates GitHub repositories for a specified organization into a specified destination folder.
.PARAMETER destinationFolder
The folder where the repositories will be cloned or updated.
.PARAMETER org
The GitHub organization name.
.PARAMETER repos
The list of repositories to clone or update.
.INPUTS
None
.OUTPUTS
None
.NOTES
Version: 1.0
Author: Sebastian Graef
Creation Date: 22-03-2025
Purpose/Change: Initial script development
.EXAMPLE
Get-GitHubRepos -destinationFolder "Git/Folder1" -org "Azure" -repos @("repo1", "repo2")
.EXAMPLE
$tfrepos = gh repo list azure -L 5000 --json name --jq '.[].name' | Select-String -Pattern "terraform-azurerm-avm"
Get-GitHubRepos -repos $tfrepos
#>
#region Parameters
[CmdletBinding()]
param
(
[Parameter()]
[string]$destinationFolder,
[Parameter()]
[string]$org,
[Parameter()]
[string[]]$repos
)
#endregion
Write-Output "Found $($repos.Count) repositories."
$confirmation = Read-Host "Do you want to proceed with processing these repositories? (y/n)"
if ($confirmation -ne 'y') {
return
}
foreach ($repo in $repos) {
$repoPath = Join-Path -Path $destinationFolder -ChildPath "$org/$repo"
If (!(Test-Path -Path $repoPath)) {
New-Item -ItemType Directory -Path $repoPath -Force
Set-Location -Path $repoPath
Write-Output "Cloning repository $repo into $repoPath."
git clone "https://github.com/$org/$repo.git"
} else {
Write-Output "Directory $repoPath already exists. Updating only."
if ((Get-ChildItem -Path $repoPath).Count -eq 0) {
Write-Output "Cloning repository $repo into $repoPath."
git clone "https://github.com/$org/$repo.git"
} else {
Write-Output "Pulling latest changes for $repo."
Set-Location -Path $repoPath
git checkout main
git pull
}
}
}
}