Skip to content

Instantly share code, notes, and snippets.

@juandspy
Last active March 20, 2024 09:10
Show Gist options
  • Save juandspy/af902f8c97865ad87515849c7fd5b33e to your computer and use it in GitHub Desktop.
Save juandspy/af902f8c97865ad87515849c7fd5b33e to your computer and use it in GitHub Desktop.
Utility functions for dealing with multiple git repositories

Utility functions for dealing with multiple git repositories

Adding a file to multiple repos

Edit any variable in the shell script. Then run it this way:

./add-file-to-repos.sh 2>&1 | tee out.log

and find the URLs to the PRs created using grep:

grep -E 'https:\/\/github\.com\/RedHatInsights\/.*\/pull\/.*' out.log

You can also use ./check-pr-checks.sh to generate a CSV with the status of each PR.

Commenting on multiple PRs

You can use ./comment-on-prs.sh. Modify the search query and comment.

Add collaborator to the repos

Use ./add-admin-repos.sh

REPOS=(
"insights-results-aggregator"
"insights-behavioral-spec"
"insights-results-aggregator-utils"
"insights-results-aggregator-mock"
"dvo-extractor"
"insights-content-template-renderer"
"insights-operator-gathering-conditions-service"
"insights-results-aggregator-exporter"
"ccx-notification-writer"
"ccx-notification-service"
"insights-results-aggregator-cleaner"
"insights-ccx-messaging"
"insights-results-smart-proxy"
"insights-content-service"
)
OWNERORG="RedHatInsights"
USERNAME="InsightsDroid"
PERMISSION="write"
# For each repo, create a table with the permissions
for repo in "${REPOS[@]}"
do
permission=$(gh api "repos/$OWNERORG/$repo/collaborators/$USERNAME/permission" | jq '.permission' -r)
echo "$USERNAME has $permission permission in $OWNERORG/$repo"
if [ "$permission" != "write" ]; then
out=$(gh api \
--method=PUT \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
repos/$OWNERORG/$repo/collaborators/$USERNAME \
-f permission=$PERMISSION | jq '.message' -r)
if [ "$out" == "Not Found" ]; then
echo "You don't have enough permissions to add $USERNAME to $repo"
else
echo "$USERNAME now has write permissions in $OWNERORG/$repo"
echo $out
fi
fi
echo "---------------------"
done
#!/bin/bash -e
REPOS=(
"dvo-extractor"
"insights-ccx-messaging"
"insights-results-aggregator"
"insights-behavioral-spec"
"insights-results-aggregator-utils"
"insights-results-aggregator-mock"
"insights-content-template-renderer"
"insights-operator-gathering-conditions-service"
"insights-results-aggregator-exporter"
"ccx-notification-writer"
"ccx-notification-service"
"insights-results-aggregator-cleaner"
"insights-results-smart-proxy"
"insights-content-service"
)
CLONED_REPOS_FOLDER="/Users/jdiazsua/Documents/Projects"
OWNERORG="RedHatInsights"
FORKOWNER="juandspy"
CURR_DIR=$(pwd)
FILE_TO_UPLOAD="dependabot-automerge.yml"
FOLDER_IN_THE_REPO=".github/workflows"
JIRA_TASK="CCXDEV-12378"
REASON="to automerge the dependabot pull requests"
for repo in "${REPOS[@]}"
do
repo_path="$CLONED_REPOS_FOLDER/$repo"
if [ -d "$repo_path" ]; then
echo "$repo_path exists"
else
echo "$repo_path doesn't exist. Cloning it"
(
cd $CLONED_REPOS_FOLDER
git clone git@github.com:$OWNERORG/$repo.git
)
fi
(
cd "$repo_path"
default_branch=$(git remote show origin | grep "HEAD branch" | sed 's/.*: //')
git checkout "$default_branch"
git pull origin "$default_branch"
if [ -f "$FOLDER_IN_THE_REPO/$FILE_TO_UPLOAD" ]; then
echo "[WARNING] Skipping $repo because file exists"
continue
fi
if ! git remote -v | grep -q "fork"; then
echo "[WARNING] Fork not found. Forking the repo..."
gh repo set-default https://github.com/$OWNERORG/$repo
gh repo fork
git remote add fork git@github.com:$FORKOWNER/$repo
fi
git branch -D "$JIRA_TASK" || true
git checkout -b "$JIRA_TASK"
mkdir -p "$FOLDER_IN_THE_REPO"
cp "$CURR_DIR/$FILE_TO_UPLOAD" "$FOLDER_IN_THE_REPO/$FILE_TO_UPLOAD"
git add "$FOLDER_IN_THE_REPO/$FILE_TO_UPLOAD"
git commit -m "[${JIRA_TASK}] Add $FILE_TO_UPLOAD $REASON"
git push fork "${JIRA_TASK}" -f
gh pr create --fill --head "$FORKOWNER:$JIRA_TASK" || true
)
echo "-------------"
done
PR_URLS=(
"https://github.com/RedHatInsights/dvo-extractor/pull/50"
"https://github.com/RedHatInsights/insights-results-aggregator/pull/1993"
"https://github.com/RedHatInsights/insights-behavioral-spec/pull/592"
"https://github.com/RedHatInsights/insights-results-aggregator-utils/pull/382"
"https://github.com/RedHatInsights/insights-results-aggregator-mock/pull/494"
"https://github.com/RedHatInsights/insights-content-template-renderer/pull/74"
"https://github.com/RedHatInsights/insights-operator-gathering-conditions-service/pull/215"
"https://github.com/RedHatInsights/insights-results-aggregator-exporter/pull/266"
"https://github.com/RedHatInsights/ccx-notification-writer/pull/441"
"https://github.com/RedHatInsights/ccx-notification-service/pull/721"
"https://github.com/RedHatInsights/insights-results-aggregator-cleaner/pull/303"
"https://github.com/RedHatInsights/insights-results-smart-proxy/pull/1253"
"https://github.com/RedHatInsights/insights-content-service/pull/578"
)
echo "PR url,checks status"
for pr_url in "${PR_URLS[@]}"
do
GH_PAGER=cat gh pr checks $pr_url 2>&1 > /dev/null
if [ $? -eq 0 ]; then
echo "$pr_url,PASS"
else
echo "$pr_url,FAIL"
fi
done
#!/bin/bash -ex
REPOS=(
"insights-results-aggregator"
"insights-behavioral-spec"
"insights-results-aggregator-utils"
"insights-results-aggregator-mock"
"dvo-extractor"
"insights-content-template-renderer"
"insights-operator-gathering-conditions-service"
"insights-results-aggregator-exporter"
"ccx-notification-writer"
"ccx-notification-service"
"insights-results-aggregator-cleaner"
"insights-ccx-messaging"
"insights-results-smart-proxy"
"insights-content-service"
)
OWNERORG="RedHatInsights"
AUTHOR="app/dependabot"
SEARCH_QUERY="author:$AUTHOR"
COMMENT="@dependabot recreate"
for repo in "${REPOS[@]}"
do
export GH_PAGER=cat
pr_list=$(gh pr list --search $SEARCH_QUERY --repo https://github.com/$OWNERORG/$repo --json url)
urls=$(echo $pr_list | jq -r '.[] | .url')
for url in $urls
do
gh pr comment --body="$COMMENT" $url
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment