Created
March 15, 2024 21:45
-
-
Save slaughtr/d4d540cf7ef553dc1c335c1f1d69f2a3 to your computer and use it in GitHub Desktop.
prcache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Another ChatGPT win, I only had to make one change | |
# This just builds a JSON-based cache (in your home dir, update the cacheFile if you don't like that) based on a git branch name, commit message, and pull request body | |
# Using those, it will create a new branch, add a commit, and (using the gh CLI) create a PR with the cached body | |
# This allows for however many cached values you need | |
# Pretty much only useful for large-scale small-change PR rollouts, like small GHA updates etc | |
prcache() { | |
cacheFile="$HOME/.git_pr_cache.json" | |
# Initialize the cache file with an empty array if it doesn't exist | |
if [[ ! -f $cacheFile ]]; then | |
echo "[]" > "$cacheFile" | |
fi | |
entries=$(jq '. | length' $cacheFile) | |
if [[ "$entries" -gt 0 ]]; then | |
echo "Cached entries found:" | |
# Generate a TSV format with indices and use `column` to create a table (adjusted for macOS) | |
jq -r 'to_entries | map("\(.key)\tBranch Name: \(.value.branchName)\tCommit Message: \(.value.commitMessage)\tPR Body: \(.value.prBody | gsub("\n"; " "))") | .[]' $cacheFile | column -t -s $'\t' | |
read "selection?Enter the index of the entry you wish to use (or 'n' for a new entry): " | |
if [[ $selection =~ ^[0-9]+$ ]]; then | |
branchName=$(jq -r ".[$selection].branchName" $cacheFile) | |
commitMessage=$(jq -r ".[$selection].commitMessage" $cacheFile) | |
prBody=$(jq -r ".[$selection].prBody" $cacheFile) | |
else | |
echo "Creating a new entry..." | |
fi | |
fi | |
if [[ -z $branchName ]]; then | |
echo "Enter the branch name:" | |
read branchName | |
echo "Enter the commit message:" | |
read commitMessage | |
echo "Enter the PR body (Ctrl-D to finish):" | |
prBody=$(cat) | |
fi | |
if [[ $selection =~ ^[0-9]+$ ]]; then | |
echo "Not adding new entry" | |
else | |
# Add the new entry to the cache | |
jq --arg bn "$branchName" --arg cm "$commitMessage" --arg pb "$prBody" \ | |
'. += [{"branchName":$bn, "commitMessage":$cm, "prBody":$pb}]' $cacheFile > tmp.$$.json && mv tmp.$$.json $cacheFile | |
fi | |
# Git operations | |
git checkout -b "$branchName" | |
git add . | |
git commit -m "$commitMessage" | |
gh pr create --title "$commitMessage" --body "$prBody" | |
echo "PR created successfully with branch '$branchName'." | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment