Skip to content

Commit 3df50dd

Browse files
committed
Init
1 parent 369fc3a commit 3df50dd

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# OrgWideGitFileChanger
2-
A bash-based tool to read over all GitHub Org's repos, clone them all, and make changes to each one in sequence. Linked to Medium article:
2+
3+
A bash-based tool to read over all GitHub Org's repos, clone them all, and make changes to each one in sequence. Linked to Medium article: https://kymidd.medium.com/lets-do-devops-update-files-in-hundreds-of-github-repos-5262dfe5f529

org_wide_git_changer.sh

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/bin/bash
2+
3+
# Summary
4+
# This tool is intended to loop over every repo within an Org or User space, and make changes to the repo.
5+
# It's useful for when you need to make a change to every repo in an org
6+
7+
# Auth Requirements
8+
# Make sure to export your github token. If SSO is enabled in your Org, you will need to authorize your token for SSO within the Org
9+
# export GITHUB_TOKEN='ghp_xxxx'
10+
11+
# check to make sure GITHUB_TOKEN is set
12+
if [ -z "$GITHUB_TOKEN" ]; then
13+
echo '$GITHUB_TOKEN is not set, please set it and try again'
14+
exit 0
15+
fi
16+
17+
# Set vars
18+
gh_org=your-org-name-here # Your GitHub Organization (or your username, if that's where your repos are)
19+
20+
# PR information - please customize this information
21+
pr_body="This PR makes some automated changes to the repo."
22+
pr_title="🤖 Making some changes 🤖"
23+
branch_name='Branch-Name-Here'
24+
commit_message='Commit message here'
25+
26+
# Should we use admin privileges to merge PR.
27+
# If true, admin privileges will be used to merge the PR. You must have admin privileges to use this option.
28+
# If false, the PR will not be automatically merged. The URL will be written to the log, and you must merge them manually
29+
auto_merge_pr=false
30+
31+
# Get the names of all repos in the org
32+
# This method is limited to 1k repos, if you have more than 1k repos, use this method: https://medium.com/@kymidd/lets-do-devops-github-api-paginated-calls-more-than-1k-repos-3ff0cc92cc50
33+
org_repos=$(gh repo list --no-archived $gh_org -L 1000 --json name --jq '.[].name')
34+
35+
# Iterate over all repos, make changes
36+
while IFS=$'\n' read -r gh_repo; do
37+
38+
# Clone the repo, will fail if the repo folder already exists
39+
git clone git@github.com:$gh_org/${gh_repo}.git
40+
41+
# Change directories into the repo
42+
cd $gh_repo
43+
44+
###
45+
### Make your changes here
46+
### Add or delete any files you need to this location
47+
### For example, modify any file, or copy over existing files
48+
###
49+
50+
# Read the REST info on the repo to get the repo's default branch
51+
# Set that default branch as the base branch for the PR
52+
base_branch=$(curl -s \
53+
-H "Accept: application/vnd.github+json" \
54+
-H "Authorization: Bearer $GITHUB_TOKEN" \
55+
https://api.github.com/repos/$gh_org/$gh_repo | jq -r '.default_branch')
56+
57+
# Git add with '.' target identifies all changes
58+
# Using the '-vvv' verbose flag to get the output of the git add command, which we will use to determine if there are changes
59+
git_add=$(git add -vvv .)
60+
61+
# If there are no changes, the PR will not be created
62+
# Note that even modified files will show up as 'add' in the git add output
63+
if [[ $(echo "$git_add" | grep -E 'add|remove') ]]; then
64+
65+
# Changes were made, checkout a branch and make a PR
66+
git checkout -b $branch_name
67+
git commit -m $commit_message
68+
ggpush
69+
created_pr_url=$(gh pr create -b "$pr_body" -t "$pr_title" -B "$base_branch" --fill)
70+
71+
# If auto_merge_pr is true, merge the PR
72+
if [ "$auto_merge_pr" = true ]; then
73+
gh pr merge --admin -d -m $created_pr_url
74+
else
75+
echo "PR created, please merge: $created_pr_url"
76+
fi
77+
78+
# Sleep 2 seconds to avoid rate limiting
79+
sleep 2
80+
81+
fi
82+
83+
# Reset location
84+
cd ..
85+
86+
done <<< "$org_repos"
87+
88+
# Finish
89+
echo "################"
90+
echo "Done!"
91+
echo "################"
92+
exit 0

0 commit comments

Comments
 (0)