Skip to content

Commit afda93b

Browse files
segraefAz-Tech Platform
andauthored
PowerShell function to clone or update GitHub repositories (#18)
* Add Clone-GitHubRepos function for cloning and updating GitHub repositories * Enhance Clone-GitHubRepos function with detailed documentation and parameter validation * Replace Clone-GitHubRepos function with Get-GitHubRepos for improved repository management --------- Co-authored-by: Az-Tech Platform <az-techH@team.telstra.com>
1 parent 5760923 commit afda93b

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

.github/workflows/linter.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ jobs:
1616
VALIDATE_ALL_CODEBASE: false
1717
VALIDATE_MARKDOWN: false
1818
DEFAULT_BRANCH: main
19-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
19+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
function Get-GitHubRepos {
2+
3+
<#
4+
.SYNOPSIS
5+
Clones or updates GitHub repositories for a specified organization.
6+
7+
.DESCRIPTION
8+
This script clones or updates GitHub repositories for a specified organization into a specified destination folder.
9+
10+
.PARAMETER destinationFolder
11+
The folder where the repositories will be cloned or updated.
12+
13+
.PARAMETER org
14+
The GitHub organization name.
15+
16+
.PARAMETER repos
17+
The list of repositories to clone or update.
18+
19+
.INPUTS
20+
None
21+
22+
.OUTPUTS
23+
None
24+
25+
.NOTES
26+
Version: 1.0
27+
Author: Sebastian Graef
28+
Creation Date: 22-03-2025
29+
Purpose/Change: Initial script development
30+
31+
.EXAMPLE
32+
Get-GitHubRepos -destinationFolder "Git/Folder1" -org "Azure" -repos @("repo1", "repo2")
33+
34+
.EXAMPLE
35+
$tfrepos = gh repo list azure -L 5000 --json name --jq '.[].name' | Select-String -Pattern "terraform-azurerm-avm"
36+
Get-GitHubRepos -repos $tfrepos
37+
#>
38+
39+
#region Parameters
40+
41+
[CmdletBinding()]
42+
param
43+
(
44+
[Parameter()]
45+
[string]$destinationFolder,
46+
[Parameter()]
47+
[string]$org,
48+
[Parameter()]
49+
[string[]]$repos
50+
)
51+
52+
#endregion
53+
54+
Write-Output "Found $($repos.Count) repositories."
55+
$confirmation = Read-Host "Do you want to proceed with processing these repositories? (y/n)"
56+
if ($confirmation -ne 'y') {
57+
return
58+
}
59+
60+
foreach ($repo in $repos) {
61+
$repoPath = Join-Path -Path $destinationFolder -ChildPath "$org/$repo"
62+
If (!(Test-Path -Path $repoPath)) {
63+
New-Item -ItemType Directory -Path $repoPath -Force
64+
Set-Location -Path $repoPath
65+
Write-Output "Cloning repository $repo into $repoPath."
66+
git clone "https://github.com/$org/$repo.git"
67+
} else {
68+
Write-Output "Directory $repoPath already exists. Updating only."
69+
if ((Get-ChildItem -Path $repoPath).Count -eq 0) {
70+
Write-Output "Cloning repository $repo into $repoPath."
71+
git clone "https://github.com/$org/$repo.git"
72+
} else {
73+
Write-Output "Pulling latest changes for $repo."
74+
Set-Location -Path $repoPath
75+
git checkout main
76+
git pull
77+
}
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)