Skip to content

Instantly share code, notes, and snippets.

@roeniss
Last active January 23, 2024 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roeniss/c8f27a41088b3cee600b77aa38a44a40 to your computer and use it in GitHub Desktop.
Save roeniss/c8f27a41088b3cee600b77aa38a44a40 to your computer and use it in GitHub Desktop.
Script for adding same file to all repositories in a organization as a pull request (works in 2024)
#!/usr/bin/env bash
#
# How it works
# - clone all repositories using `gh-cli`
# - copy the prepared file to one repo among them
# - git add, git commit, git push, gh pr create
# - Repeat above two steps for other repos
#
# Step 0. Prepare empty folder and copy the target file into the folder
mkdir -p tmp_for_all_pr
cd tmp_for_all_pr
copy ../pull_request_template.md ./pull_request_template.md
# Step 1. Clone all repositories
ORG_NAME="my-org"
MAX_COUNT=500
# No forked repo, no archived repo
repos=$(gh repo list $ORG_NAME --limit $MAX_COUNT --source --no-archived --json url --jq ".[].url")
for repo in $repos
do
git clone $repo
done
# Step 2. Add file and make a pull_request to each repositories
for dir in $(ls -d */); do
dir=${dir%*/}
dir=${dir##*/}
mkdir -p $dir/.github # make sure the folder exist
cp pull_request_template.md $dir/.github/pull_request_template.md # copy the file to target folder
cd $dir # *splash*
git pull
git checkout -b feat/pull-request-template
git add .github/pull_request_template.md
git commit -m "feat: Apply pull request template"
git push --set-upstream origin feat/pull-request-template
# I don't specify the base branch because not every repo has the same default branch name
# Without --base parameter, Each pr heads to the base branch of its repo
gh pr create --title "feat: Apply pull request template" --body ""
cd .. # *hsalps*
done
@roeniss
Copy link
Author

roeniss commented Jan 23, 2024

By the way, check if the org has a repo named .github. You probably want to exclude it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment