From 5c15098b5c08bfad2e983949cbe974f7511aeb9d Mon Sep 17 00:00:00 2001 From: Az-Tech Platform Date: Sat, 22 Mar 2025 09:47:28 +1000 Subject: [PATCH 1/2] Add Clone-GitHubRepos function for cloning and updating GitHub repositories --- PowerShell/Snippets/Clone-GitHubRepos.ps1 | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 PowerShell/Snippets/Clone-GitHubRepos.ps1 diff --git a/PowerShell/Snippets/Clone-GitHubRepos.ps1 b/PowerShell/Snippets/Clone-GitHubRepos.ps1 new file mode 100644 index 0000000..7235d39 --- /dev/null +++ b/PowerShell/Snippets/Clone-GitHubRepos.ps1 @@ -0,0 +1,39 @@ +function Clone-GitHubRepos { + [CmdletBinding()] + Param( + [string]$destinationFolder = "/Users/segraef/Git/GitHub", + [string]$org = 'Azure', + [string[]]$repos + ) + + 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 + } + } + } +} + +# Example usage: +$tfrepos = gh repo list azure -L 5000 --json name --jq '.[].name' | Select-String -Pattern "terraform-azurerm-avm" +Clone-GitHubRepos -repos $tfrepos From 1325382cdc2119dcbae0a507a3f6fec4e2ec0b7d Mon Sep 17 00:00:00 2001 From: Az-Tech Platform Date: Sat, 22 Mar 2025 10:03:46 +1000 Subject: [PATCH 2/2] Enhance Clone-GitHubRepos function with detailed documentation and parameter validation --- PowerShell/Snippets/Clone-GitHubRepos.ps1 | 55 ++++++++++++++++++++--- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/PowerShell/Snippets/Clone-GitHubRepos.ps1 b/PowerShell/Snippets/Clone-GitHubRepos.ps1 index 7235d39..4a1115e 100644 --- a/PowerShell/Snippets/Clone-GitHubRepos.ps1 +++ b/PowerShell/Snippets/Clone-GitHubRepos.ps1 @@ -1,11 +1,56 @@ function Clone-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 + Clone-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" + Clone-GitHubRepos -repos $tfrepos +#> + + #region Parameters + [CmdletBinding()] - Param( - [string]$destinationFolder = "/Users/segraef/Git/GitHub", - [string]$org = 'Azure', + 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') { @@ -33,7 +78,3 @@ function Clone-GitHubRepos { } } } - -# Example usage: -$tfrepos = gh repo list azure -L 5000 --json name --jq '.[].name' | Select-String -Pattern "terraform-azurerm-avm" -Clone-GitHubRepos -repos $tfrepos