Skip to content

Instantly share code, notes, and snippets.

@cderv
Last active December 6, 2024 15:27
Show Gist options
  • Save cderv/eac2e630a4bde09dfbe0bd6d213140aa to your computer and use it in GitHub Desktop.
Save cderv/eac2e630a4bde09dfbe0bd6d213140aa to your computer and use it in GitHub Desktop.
Update all repos from an organisation on github
# Get organization name from parent directory
$orgName = Split-Path -Path $PSScriptRoot -Leaf
# Prompt user for confirmation
$confirmation = Read-Host -Prompt "Do you want to update repositories for organization '$orgName'? (y/n)"
if ($confirmation -ne 'y') {
Write-Output "Operation cancelled by user"
exit 0
}
# Validate organization name
if ([string]::IsNullOrWhiteSpace($orgName)) {
Write-Error "Could not determine organization name from parent directory"
exit 1
}
# Basic format validation (alphanumeric with hyphens and underscores allowed)
if ($orgName -notmatch '^[a-zA-Z0-9-_]+$') {
Write-Error "Invalid organization name format: $orgName"
exit 1
}
# Check if organization exists on GitHub
try {
# First check if github.com is reachable
if (-not (Test-Connection -ComputerName "github.com" -Count 1 -Quiet)) {
Write-Error "Cannot reach github.com"
exit 1
}
# Try to access the organization page
Invoke-WebRequest -Uri "https://github.com/$orgName" -Method HEAD -ErrorAction Stop | Out-Null
} catch {
if ($_.Exception.Response.StatusCode.value__ -eq 404) {
Write-Error "GitHub organization '$orgName' does not exist"
} else {
Write-Error "Error checking organization: $_"
}
exit 1
}
$repos = gh repo list $orgName --no-archived --source --json owner,name,defaultBranchRef -q '.[] | {org: .owner.login, repo: .name, branch: .defaultBranchRef.name}' | ConvertFrom-Json
foreach($row in $repos) {
Write-Output "Looking for $($row.repo) folder"
if (Test-Path -Path $row.repo) {
git -C "$($row.repo)" checkout $row.branch
git -C "$($row.repo)" pull origin $row.branch
} else {
gh repo clone "$($row.org)/$($row.repo)"
}
}
Write-Output "All repos updated!"
#!/bin/bash
repos=$(gh repo list 'quarto-journals' --no-archived --source --json owner,name,defaultBranchRef -q '.[] | {org: .owner.login, repo: .name, branch: .defaultBranchRef.name}')
echo $repos
for row in $(echo "${repos}"); do
repo=$(echo ${row} | jq -r '.repo')
branch=$(echo ${row} | jq -r '.branch')
org=$(echo ${row} | jq -r '.org')
echo "Looking for ${repo} folder"
if [ -d "$repo" ]; then
git -C "$repo" checkout $branch
git -C "$repo" pull origin $branch
else
gh repo clone "${org}/${repo}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment