diff --git a/PowerShell/Snippets/Add-PullRequests.ps1 b/PowerShell/Snippets/Add-PullRequests.ps1 new file mode 100644 index 0000000..17abe5f --- /dev/null +++ b/PowerShell/Snippets/Add-PullRequests.ps1 @@ -0,0 +1,26 @@ +function Add-PullRequests { + + [CmdletBinding()] + Param( + [Parameter()] + [string]$org = "x00", + [Parameter()] + [string]$project = "Modules", + [Parameter()] + [string]$token = "token", + [Parameter()] + [string]$branchName = "users/segraef/provider-upgrade" + ) + + az extension add --name azure-devops + + Write-Output $token | az devops login --organization https://dev.azure.com/$org + az devops configure --defaults organization=https://dev.azure.com/$org project=$project + $repos = az repos list | ConvertFrom-Json + + # Create Pull Request on all repos + foreach ($repo in $repos) { + Write-Output $repo.name + az repos pr create --repository $repo.name --source-branch $branchName --open --output table + } +} diff --git a/PowerShell/Snippets/Clone-Repos.ps1 b/PowerShell/Snippets/Clone-Repos.ps1 index 0045530..ff16701 100644 --- a/PowerShell/Snippets/Clone-Repos.ps1 +++ b/PowerShell/Snippets/Clone-Repos.ps1 @@ -2,38 +2,36 @@ Param( [string]$destinationFolder = ".", [string]$org = "123", [array]$projects = ( - "Modules" -) + "Modules") -# Make sure you have the Azure CLI installed (az extension add --name azure-devops) and logged in via az login or az devops login -# If you face /_apis authentication issues make sure to login via az login --allow-no-subscriptions + # Make sure you have the Azure CLI installed (az extension add --name azure-devops) and logged in via az login or az devops login + # If you face /_apis authentication issues make sure to login via az login --allow-no-subscriptions -az devops configure --defaults organization=https://dev.azure.com/$org -# $projects = az devops project list --organization=https://dev.azure.com/$org | ConvertFrom-Json + az devops configure --defaults organization=https://dev.azure.com/$org + # $projects = az devops project list --organization=https://dev.azure.com/$org | ConvertFrom-Json -foreach ($project in $projects) { - $repos = az repos list --project $project | ConvertFrom-Json - foreach ($repo in $repos) { - Write-Output "Repository [$($repo.name)] in project [$($project)]" - If(!(test-path -PathType container $destinationFolder)) - { + foreach ($project in $projects) { + $repos = az repos list --project $project | ConvertFrom-Json + foreach ($repo in $repos) { + Write-Output "Repository [$($repo.name)] in project [$($project)]" + If (!(test-path -PathType container $destinationFolder)) { New-Item -ItemType Directory -Path $destinationFolder } - git clone $($repo.remoteUrl) $destinationFolder/$($project)/$($repo.name) + git clone $($repo.remoteUrl) $destinationFolder/$($project)/$($repo.name) + } } -} -# clone repos in github + # clone repos in github -$destinationFolder = "." + $destinationFolder = "." -$tfrepos = gh repo list azure -L 5000 --json name --jq '.[].name' | Select-String -Pattern "terraform-azurerm-avm" -$org = 'Azure' + $tfrepos = gh repo list azure -L 5000 --json name --jq '.[].name' | Select-String -Pattern "terraform-azurerm-avm" + $org = 'Azure' -foreach ($repo in $tfrepos) { - If (!(test-path -PathType container $destinationFolder/$($org)/$($repo))) { - New-Item -ItemType Directory -Path $destinationFolder/$($org)/$($repo) - } - Write-Output "https://github.com/$org/$repo.git into $destinationFolder/$($org)/$($repo)" - git clone "https://github.com/$org/$repo.git" $destinationFolder/$($org)/$($repo) -} + foreach ($repo in $tfrepos) { + If (!(test-path -PathType container $destinationFolder/$($org)/$($repo))) { + New-Item -ItemType Directory -Path $destinationFolder/$($org)/$($repo) + } + Write-Output "https://github.com/$org/$repo.git into $destinationFolder/$($org)/$($repo)" + git clone "https://github.com/$org/$repo.git" $destinationFolder/$($org)/$($repo) + } diff --git a/PowerShell/Snippets/CloneUpdate-Repos.ps1 b/PowerShell/Snippets/CloneUpdate-Repos.ps1 deleted file mode 100644 index 41689f4..0000000 --- a/PowerShell/Snippets/CloneUpdate-Repos.ps1 +++ /dev/null @@ -1,65 +0,0 @@ -# Define the list of Azure DevOps project names -$organization = "yourOrg" -$destinationFolder = "/Users/user/$organization" -$pat = "" -$VerbosePreference = 'Continue' - -# Function to get all projects in the organization -function Get-AdoProjects { - $uri = "https://dev.azure.com/$organization/_apis/projects?api-version=6.0" - $response = Invoke-RestMethod -Uri $uri -Headers @{Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat")) } - return $response.value -} - -# Function to get repositories for a given project -function Get-AdoRepositories($project) { - $uri = "https://dev.azure.com/$organization/$project/_apis/git/repositories?api-version=6.0" - $uri = $uri -replace " ", "%20" - Write-Verbose $uri - $response = Invoke-RestMethod -Uri $uri -Headers @{Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat")) } - return $response.value -} - -# Function to clone or update repositories -function CloneOrUpdateRepo($repo, $projectFolder) { - $repoName = $repo.name - $repoUrl = $repo.remoteUrl - $repoFolder = "$projectFolder/$repoName" - - if (-not (Test-Path -Path $repoFolder)) { - Write-Verbose "Cloning $($repo.name)" - git clone $repoUrl $repoFolder - } else { - Write-Verbose "Pulling/Refreshing $($repo.name)" - Set-Location -Path $repoFolder - git checkout main - git pull - Set-Location -Path $projectFolder - } -} - -# Main script -Write-Verbose "Getting projects ..." -$projects = Get-AdoProjects -Write-Verbose "Found $($projects.Count) projects: $($projects.name)" -foreach ($project in $projects) { - $projectFolder = "$destinationFolder/$($project.name)" - if (-not (Test-Path -Path $projectFolder)) { - Write-Verbose "Creating folder $projectFolder" - New-Item -ItemType Directory -Path $projectFolder - } - - Write-Verbose "Getting repos for $($project.name) ..." - $repos = Get-AdoRepositories -project $project.name - Write-Verbose "Found $($repos.Count) repos: $($repos.name)" - # ask to proceed - Read-Host "Press Enter to continue" - foreach ($repo in $repos) { - $response = Read-Host "Do you want to clone/update the repo $($repo.name)? (y/n)" - if ($response -eq 'y') { - CloneOrUpdateRepo -repo $repo -projectFolder $projectFolder - } else { - Write-Verbose "Skipping $($repo.name)" - } - } -} diff --git a/PowerShell/Snippets/Create-PullRequests.ps1 b/PowerShell/Snippets/Create-PullRequests.ps1 deleted file mode 100644 index 30010e0..0000000 --- a/PowerShell/Snippets/Create-PullRequests.ps1 +++ /dev/null @@ -1,21 +0,0 @@ -Param( - [string]$org = "x00", - [string]$project = "ESLZ Modules", - [string]$token = "token", - [string]$branchName = "users/segraef/provider-upgrade" -) - -az extension add --name azure-devops - -echo $token | az devops login --organization https://dev.azure.com/$org -az devops configure --defaults organization=https://dev.azure.com/$org project=$project -$repos = az repos list | ConvertFrom-Json - -$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token))) - -# Create Pull Request on all repos -foreach($repo in $repos){ - Write-Host $repo.name - # az repos pr create --repository $repo.name --source-branch $branchName --open --output table -} - diff --git a/PowerShell/Update-AdoRepos.ps1 b/PowerShell/Update-AdoRepos.ps1 new file mode 100644 index 0000000..aa95d1c --- /dev/null +++ b/PowerShell/Update-AdoRepos.ps1 @@ -0,0 +1,110 @@ +<# +.SYNOPSIS + Script to clone or update Azure DevOps repositories for all projects in an organization. + +.DESCRIPTION + This script clones or updates Azure DevOps repositories for all projects in a specified organization into a specified destination folder. + +.PARAMETER organization + The Azure DevOps organization name. + +.PARAMETER destinationFolder + The folder where the repositories will be cloned or updated. + +.PARAMETER pat + The personal access token for authentication. + +.INPUTS + None + +.OUTPUTS + None + +.NOTES + Version: 1.0 + Author: Sebastian Graef + Creation Date: 22-03-2025 + Purpose/Change: Initial script development + +.EXAMPLE + CloneUpdate-AdoRepos.ps1 -organization "yourOrg" -destinationFolder "C:\Repos" -pat "yourPAT" +#> + +function Update-AdoRepos { + + [CmdletBinding(SupportsShouldProcess)] + param + ( + [Parameter()] + [string]$organization, + [Parameter()] + [string]$destinationFolder, + [Parameter()] + [string]$pat + ) + + # Function to get all projects in the organization + function Get-AdoProjects($organization,$pat) { + $uri = "https://dev.azure.com/$organization/_apis/projects?api-version=6.0" + $response = Invoke-RestMethod -Uri $uri -Headers @{Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat")) } + return $response.value + } + + # Function to get repositories for a given project + function Get-AdoRepositories($organization,$pat,$project) { + $uri = "https://dev.azure.com/$organization/$project/_apis/git/repositories?api-version=6.0" + $uri = $uri -replace " ", "%20" + Write-Output $uri + $response = Invoke-RestMethod -Uri $uri -Headers @{Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat")) } + return $response.value + } + + # Function to clone or update repositories + function CloneOrUpdateRepo($repo, $projectFolder) { + $repoName = $repo.name + $repoUrl = $repo.remoteUrl + $repoFolder = "$projectFolder/$repoName" + + if (-not (Test-Path -Path $repoFolder)) { + if ($PSCmdlet.ShouldProcess("Cloning $($repo.name)")) { + Write-Output "Cloning $($repo.name)" + git clone $repoUrl $repoFolder + } + } else { + if ($PSCmdlet.ShouldProcess("Pulling/Refreshing $($repo.name)")) { + Write-Output "Pulling/Refreshing $($repo.name)" + Set-Location -Path $repoFolder + git checkout main + git pull + Set-Location -Path $projectFolder + } + } + } + + # Main script + Write-Output "Getting projects ..." + $projects = Get-AdoProjects -organization $organization -pat $pat + Write-Output "Found $($projects.Count) projects: $($projects.name)" + foreach ($project in $projects) { + $projectFolder = "$destinationFolder/$($project.name)" + if (-not (Test-Path -Path $projectFolder)) { + if ($PSCmdlet.ShouldProcess("Creating folder $projectFolder")) { + Write-Output "Creating folder $projectFolder" + New-Item -ItemType Directory -Path $projectFolder + } + } + + Write-Output "Getting repos for $($project.name) ..." + $repos = Get-AdoRepositories -organization $organization -pat $pat -project $project.name + Write-Output "Found $($repos.Count) repos: $($repos.name)" + Read-Host "Press Enter to continue" + foreach ($repo in $repos) { + $response = Read-Host "Do you want to clone/update the repo $($repo.name)? (y/n)" + if ($response -eq 'y') { + CloneOrUpdateRepo -repo $repo -projectFolder $projectFolder + } else { + Write-Output "Skipping $($repo.name)" + } + } + } +} diff --git a/PowerShell/Update-GitHubRepos.ps1 b/PowerShell/Update-GitHubRepos.ps1 new file mode 100644 index 0000000..c82c8c4 --- /dev/null +++ b/PowerShell/Update-GitHubRepos.ps1 @@ -0,0 +1,82 @@ +<# + .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 organization + 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" -organization "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 +#> + +function Update-GitHubRepos { + + [CmdletBinding(SupportsShouldProcess)] + param + ( + [Parameter()] + [string]$destinationFolder, + [Parameter()] + [string]$organization, + [Parameter()] + [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 "$organization/$repo" + If (!(Test-Path -Path $repoPath)) { + if ($PSCmdlet.ShouldProcess("Cloning repository $repo into $repoPath")) { + New-Item -ItemType Directory -Path $repoPath -Force + Set-Location -Path $repoPath + Write-Output "Cloning repository $repo into $repoPath." + git clone "https://github.com/$organization/$repo.git" + } + } else { + Write-Output "Directory $repoPath already exists. Updating only." + if ((Get-ChildItem -Path $repoPath).Count -eq 0) { + if ($PSCmdlet.ShouldProcess("Cloning repository $repo into $repoPath")) { + Write-Output "Cloning repository $repo into $repoPath." + git clone "https://github.com/$organization/$repo.git" + } + } else { + if ($PSCmdlet.ShouldProcess("Pulling latest changes for $repo")) { + Write-Output "Pulling latest changes for $repo." + Set-Location -Path $repoPath + git checkout main + git pull + } + } + } + } +} diff --git a/PowerShell/Update-Repos.ps1 b/PowerShell/Update-Repos.ps1 new file mode 100644 index 0000000..2674489 --- /dev/null +++ b/PowerShell/Update-Repos.ps1 @@ -0,0 +1,256 @@ +<# + .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 organization + 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" -organization "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 +#> + +function Update-GitHubRepos { + + [CmdletBinding(SupportsShouldProcess)] + param + ( + [Parameter()] + [string]$destinationFolder, + [Parameter()] + [string]$organization, + [Parameter()] + [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 "$organization/$repo" + If (!(Test-Path -Path $repoPath)) { + if ($PSCmdlet.ShouldProcess("Cloning repository $repo into $repoPath")) { + New-Item -ItemType Directory -Path $repoPath -Force + Set-Location -Path $repoPath + Write-Output "Cloning repository $repo into $repoPath." + git clone "https://github.com/$organization/$repo.git" + } + } else { + Write-Output "Directory $repoPath already exists. Updating only." + if ((Get-ChildItem -Path $repoPath).Count -eq 0) { + if ($PSCmdlet.ShouldProcess("Cloning repository $repo into $repoPath")) { + Write-Output "Cloning repository $repo into $repoPath." + git clone "https://github.com/$organization/$repo.git" + } + } else { + if ($PSCmdlet.ShouldProcess("Pulling latest changes for $repo")) { + Write-Output "Pulling latest changes for $repo." + Set-Location -Path $repoPath + git checkout main + git pull + } + } + } + } +} + + +<# +.SYNOPSIS + Script to clone or update Azure DevOps repositories for all projects in an organization. + +.DESCRIPTION + This script clones or updates Azure DevOps repositories for all projects in a specified organization into a specified destination folder. + +.PARAMETER organization + The Azure DevOps organization name. + +.PARAMETER destinationFolder + The folder where the repositories will be cloned or updated. + +.PARAMETER pat + The personal access token for authentication. + +.INPUTS + None + +.OUTPUTS + None + +.NOTES + Version: 1.0 + Author: Sebastian Graef + Creation Date: 22-03-2025 + Purpose/Change: Initial script development + +.EXAMPLE + CloneUpdate-AdoRepos.ps1 -organization "yourOrg" -destinationFolder "C:\Repos" -pat "yourPAT" +#> + +function Update-AdoRepos { + + [CmdletBinding(SupportsShouldProcess)] + param + ( + [Parameter()] + [string]$organization, + [Parameter()] + [string]$destinationFolder, + [Parameter()] + [string]$pat + ) + + # Function to get all projects in the organization + function Get-AdoProjects($organization,$pat) { + $uri = "https://dev.azure.com/$organization/_apis/projects?api-version=6.0" + $response = Invoke-RestMethod -Uri $uri -Headers @{Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat")) } + return $response.value + } + + # Function to get repositories for a given project + function Get-AdoRepositories($organization,$pat,$project) { + $uri = "https://dev.azure.com/$organization/$project/_apis/git/repositories?api-version=6.0" + $uri = $uri -replace " ", "%20" + Write-Output $uri + $response = Invoke-RestMethod -Uri $uri -Headers @{Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat")) } + return $response.value + } + + # Function to clone or update repositories + function CloneOrUpdateRepo($repo, $projectFolder) { + $repoName = $repo.name + $repoUrl = $repo.remoteUrl + $repoFolder = "$projectFolder/$repoName" + + if (-not (Test-Path -Path $repoFolder)) { + if ($PSCmdlet.ShouldProcess("Cloning $($repo.name)")) { + Write-Output "Cloning $($repo.name)" + git clone $repoUrl $repoFolder + } + } else { + if ($PSCmdlet.ShouldProcess("Pulling/Refreshing $($repo.name)")) { + Write-Output "Pulling/Refreshing $($repo.name)" + Set-Location -Path $repoFolder + git checkout main + git pull + Set-Location -Path $projectFolder + } + } + } + + # Main script + Write-Output "Getting projects ..." + $projects = Get-AdoProjects -organization $organization -pat $pat + Write-Output "Found $($projects.Count) projects: $($projects.name)" + foreach ($project in $projects) { + $projectFolder = "$destinationFolder/$($project.name)" + if (-not (Test-Path -Path $projectFolder)) { + if ($PSCmdlet.ShouldProcess("Creating folder $projectFolder")) { + Write-Output "Creating folder $projectFolder" + New-Item -ItemType Directory -Path $projectFolder + } + } + + Write-Output "Getting repos for $($project.name) ..." + $repos = Get-AdoRepositories -organization $organization -pat $pat -project $project.name + Write-Output "Found $($repos.Count) repos: $($repos.name)" + Read-Host "Press Enter to continue" + foreach ($repo in $repos) { + $response = Read-Host "Do you want to clone/update the repo $($repo.name)? (y/n)" + if ($response -eq 'y') { + CloneOrUpdateRepo -repo $repo -projectFolder $projectFolder + } else { + Write-Output "Skipping $($repo.name)" + } + } + } +} + +<# +.SYNOPSIS + Script to clone or update repositories for a specified organization. + +.DESCRIPTION + This script clones or updates repositories for a specified organization into a specified destination folder. It supports both GitHub and Azure DevOps repositories. + +.PARAMETER destinationFolder + The folder where the repositories will be cloned or updated. + +.PARAMETER organization + The organization name (GitHub or Azure DevOps). + +.PARAMETER repos + The list of GitHub repositories to clone or update. + +.PARAMETER pat + The personal access token for Azure DevOps authentication. + +.INPUTS + None + +.OUTPUTS + None + +.NOTES + Version: 1.0 + Author: Sebastian Graef + Creation Date: 22-03-2025 + Purpose/Change: Initial script development + +.EXAMPLE + Update-Repos -destinationFolder "C:\Repos" -organization "yourOrg" -repos @("repo1", "repo2") + +.EXAMPLE + Update-Repos -destinationFolder "C:\Repos" -organization "yourOrg" -pat "yourPAT" +#> + +function Update-Repos { + + [CmdletBinding(SupportsShouldProcess)] + param + ( + [Parameter()] + [string]$destinationFolder, + [Parameter()] + [string]$organization, + [Parameter()] + [string[]]$repos, + [Parameter()] + [string]$pat + ) + + if ($pat) { + Write-Output "- Updating Azure DevOps repositories" + Update-AdoRepos -organization $organization -destinationFolder $destinationFolder -pat $pat + } else { + Write-Output "- Updating GitHub repositories" + Update-GitHubRepos -destinationFolder $destinationFolder -organization $organization -repos $repos + } +}