Skip to content

Commit c5288e5

Browse files
segraefAz-Tech Platform
andauthored
Add Update-GitHubRepos script and refactor Create-PullRequests parame… (#19)
* Add Update-GitHubRepos script and refactor Create-PullRequests parameters * Refactor Update-AdoRepos and Update-GitHubRepos scripts to support ShouldProcess for safer execution * Refactor Create-PullRequests and Update-AdoRepos scripts to improve parameter handling and output methods * Refactor Create-PullRequests script to use CmdletBinding and support ShouldProcess for safer execution * Replace Create-PullRequests script with Add-PullRequests script for improved functionality --------- Co-authored-by: Az-Tech Platform <az-techH@team.telstra.com>
1 parent afda93b commit c5288e5

8 files changed

Lines changed: 497 additions & 191 deletions
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function Add-PullRequests {
2+
3+
[CmdletBinding()]
4+
Param(
5+
[Parameter()]
6+
[string]$org = "x00",
7+
[Parameter()]
8+
[string]$project = "Modules",
9+
[Parameter()]
10+
[string]$token = "token",
11+
[Parameter()]
12+
[string]$branchName = "users/segraef/provider-upgrade"
13+
)
14+
15+
az extension add --name azure-devops
16+
17+
Write-Output $token | az devops login --organization https://dev.azure.com/$org
18+
az devops configure --defaults organization=https://dev.azure.com/$org project=$project
19+
$repos = az repos list | ConvertFrom-Json
20+
21+
# Create Pull Request on all repos
22+
foreach ($repo in $repos) {
23+
Write-Output $repo.name
24+
az repos pr create --repository $repo.name --source-branch $branchName --open --output table
25+
}
26+
}

PowerShell/Snippets/Clone-Repos.ps1

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,36 @@ Param(
22
[string]$destinationFolder = ".",
33
[string]$org = "123",
44
[array]$projects = (
5-
"Modules"
6-
)
5+
"Modules")
76

8-
# Make sure you have the Azure CLI installed (az extension add --name azure-devops) and logged in via az login or az devops login
9-
# If you face /_apis authentication issues make sure to login via az login --allow-no-subscriptions
7+
# Make sure you have the Azure CLI installed (az extension add --name azure-devops) and logged in via az login or az devops login
8+
# If you face /_apis authentication issues make sure to login via az login --allow-no-subscriptions
109

11-
az devops configure --defaults organization=https://dev.azure.com/$org
12-
# $projects = az devops project list --organization=https://dev.azure.com/$org | ConvertFrom-Json
10+
az devops configure --defaults organization=https://dev.azure.com/$org
11+
# $projects = az devops project list --organization=https://dev.azure.com/$org | ConvertFrom-Json
1312

14-
foreach ($project in $projects) {
15-
$repos = az repos list --project $project | ConvertFrom-Json
16-
foreach ($repo in $repos) {
17-
Write-Output "Repository [$($repo.name)] in project [$($project)]"
18-
If(!(test-path -PathType container $destinationFolder))
19-
{
13+
foreach ($project in $projects) {
14+
$repos = az repos list --project $project | ConvertFrom-Json
15+
foreach ($repo in $repos) {
16+
Write-Output "Repository [$($repo.name)] in project [$($project)]"
17+
If (!(test-path -PathType container $destinationFolder)) {
2018
New-Item -ItemType Directory -Path $destinationFolder
2119
}
22-
git clone $($repo.remoteUrl) $destinationFolder/$($project)/$($repo.name)
20+
git clone $($repo.remoteUrl) $destinationFolder/$($project)/$($repo.name)
21+
}
2322
}
24-
}
2523

26-
# clone repos in github
24+
# clone repos in github
2725

28-
$destinationFolder = "."
26+
$destinationFolder = "."
2927

30-
$tfrepos = gh repo list azure -L 5000 --json name --jq '.[].name' | Select-String -Pattern "terraform-azurerm-avm"
31-
$org = 'Azure'
28+
$tfrepos = gh repo list azure -L 5000 --json name --jq '.[].name' | Select-String -Pattern "terraform-azurerm-avm"
29+
$org = 'Azure'
3230

33-
foreach ($repo in $tfrepos) {
34-
If (!(test-path -PathType container $destinationFolder/$($org)/$($repo))) {
35-
New-Item -ItemType Directory -Path $destinationFolder/$($org)/$($repo)
36-
}
37-
Write-Output "https://github.com/$org/$repo.git into $destinationFolder/$($org)/$($repo)"
38-
git clone "https://github.com/$org/$repo.git" $destinationFolder/$($org)/$($repo)
39-
}
31+
foreach ($repo in $tfrepos) {
32+
If (!(test-path -PathType container $destinationFolder/$($org)/$($repo))) {
33+
New-Item -ItemType Directory -Path $destinationFolder/$($org)/$($repo)
34+
}
35+
Write-Output "https://github.com/$org/$repo.git into $destinationFolder/$($org)/$($repo)"
36+
git clone "https://github.com/$org/$repo.git" $destinationFolder/$($org)/$($repo)
37+
}

PowerShell/Snippets/CloneUpdate-Repos.ps1

Lines changed: 0 additions & 65 deletions
This file was deleted.

PowerShell/Snippets/Create-PullRequests.ps1

Lines changed: 0 additions & 21 deletions
This file was deleted.

PowerShell/Snippets/Get-GitHubRepos.ps1

Lines changed: 0 additions & 80 deletions
This file was deleted.

PowerShell/Update-AdoRepos.ps1

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<#
2+
.SYNOPSIS
3+
Script to clone or update Azure DevOps repositories for all projects in an organization.
4+
5+
.DESCRIPTION
6+
This script clones or updates Azure DevOps repositories for all projects in a specified organization into a specified destination folder.
7+
8+
.PARAMETER organization
9+
The Azure DevOps organization name.
10+
11+
.PARAMETER destinationFolder
12+
The folder where the repositories will be cloned or updated.
13+
14+
.PARAMETER pat
15+
The personal access token for authentication.
16+
17+
.INPUTS
18+
None
19+
20+
.OUTPUTS
21+
None
22+
23+
.NOTES
24+
Version: 1.0
25+
Author: Sebastian Graef
26+
Creation Date: 22-03-2025
27+
Purpose/Change: Initial script development
28+
29+
.EXAMPLE
30+
CloneUpdate-AdoRepos.ps1 -organization "yourOrg" -destinationFolder "C:\Repos" -pat "yourPAT"
31+
#>
32+
33+
function Update-AdoRepos {
34+
35+
[CmdletBinding(SupportsShouldProcess)]
36+
param
37+
(
38+
[Parameter()]
39+
[string]$organization,
40+
[Parameter()]
41+
[string]$destinationFolder,
42+
[Parameter()]
43+
[string]$pat
44+
)
45+
46+
# Function to get all projects in the organization
47+
function Get-AdoProjects($organization,$pat) {
48+
$uri = "https://dev.azure.com/$organization/_apis/projects?api-version=6.0"
49+
$response = Invoke-RestMethod -Uri $uri -Headers @{Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat")) }
50+
return $response.value
51+
}
52+
53+
# Function to get repositories for a given project
54+
function Get-AdoRepositories($organization,$pat,$project) {
55+
$uri = "https://dev.azure.com/$organization/$project/_apis/git/repositories?api-version=6.0"
56+
$uri = $uri -replace " ", "%20"
57+
Write-Output $uri
58+
$response = Invoke-RestMethod -Uri $uri -Headers @{Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat")) }
59+
return $response.value
60+
}
61+
62+
# Function to clone or update repositories
63+
function CloneOrUpdateRepo($repo, $projectFolder) {
64+
$repoName = $repo.name
65+
$repoUrl = $repo.remoteUrl
66+
$repoFolder = "$projectFolder/$repoName"
67+
68+
if (-not (Test-Path -Path $repoFolder)) {
69+
if ($PSCmdlet.ShouldProcess("Cloning $($repo.name)")) {
70+
Write-Output "Cloning $($repo.name)"
71+
git clone $repoUrl $repoFolder
72+
}
73+
} else {
74+
if ($PSCmdlet.ShouldProcess("Pulling/Refreshing $($repo.name)")) {
75+
Write-Output "Pulling/Refreshing $($repo.name)"
76+
Set-Location -Path $repoFolder
77+
git checkout main
78+
git pull
79+
Set-Location -Path $projectFolder
80+
}
81+
}
82+
}
83+
84+
# Main script
85+
Write-Output "Getting projects ..."
86+
$projects = Get-AdoProjects -organization $organization -pat $pat
87+
Write-Output "Found $($projects.Count) projects: $($projects.name)"
88+
foreach ($project in $projects) {
89+
$projectFolder = "$destinationFolder/$($project.name)"
90+
if (-not (Test-Path -Path $projectFolder)) {
91+
if ($PSCmdlet.ShouldProcess("Creating folder $projectFolder")) {
92+
Write-Output "Creating folder $projectFolder"
93+
New-Item -ItemType Directory -Path $projectFolder
94+
}
95+
}
96+
97+
Write-Output "Getting repos for $($project.name) ..."
98+
$repos = Get-AdoRepositories -organization $organization -pat $pat -project $project.name
99+
Write-Output "Found $($repos.Count) repos: $($repos.name)"
100+
Read-Host "Press Enter to continue"
101+
foreach ($repo in $repos) {
102+
$response = Read-Host "Do you want to clone/update the repo $($repo.name)? (y/n)"
103+
if ($response -eq 'y') {
104+
CloneOrUpdateRepo -repo $repo -projectFolder $projectFolder
105+
} else {
106+
Write-Output "Skipping $($repo.name)"
107+
}
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)