Last active
August 9, 2023 01:21
-
-
Save jadeallencook/6a544e23d84bb1c4ae21d7e1b34be882 to your computer and use it in GitHub Desktop.
Shell script to standardize pull request descriptions.
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
#!/bin/bash | |
# 1. Install hub on your machine (https://hub.github.com) | |
# 2. Add this file to your project and update your package.json | |
# "scripts": { | |
# "pull-request": "sh ./path/pull-request.sh" | |
# } | |
# 3. Run "npm run pull-request" to open a pull request | |
REMOTE=$(echo `git remote get-url origin` | sed -e 's/.*:\(.*\)\/.*/\1/') | |
choices=() | |
description="" | |
# colors used within application | |
CYAN=$(tput setaf 6) | |
GREEN=$(tput setaf 2) | |
YELLOW=$(tput setaf 3) | |
RED=$(tput setaf 1) | |
RESET=$(tput sgr0) | |
# all the questions and options | |
questions=( | |
"Enter pull request title:" | |
"What is the purpose of this pull request:" | |
"The origin or ticket link:" | |
"Did you add any new packages:Yes No" | |
"Are there any risks to be aware of:" | |
"These changes follow developer guidelines:Yes No" | |
) | |
# display question and options | |
display_question() { | |
local question="$1" | |
local options="$2" | |
echo "${CYAN}${question}${RESET}" | |
if [ -n "$options" ]; then | |
IFS=' ' read -ra options_array <<< "$options" | |
for ((i = 0; i < ${#options_array[@]}; i++)); do | |
echo "${GREEN}$((i + 1)). ${options_array[i]}${RESET}" | |
done | |
fi | |
} | |
# gets user input for each question | |
get_user_choice() { | |
local num_options="$1" | |
local choice | |
while true; do | |
read -rp "${YELLOW}Enter your choice (1-$num_options): ${RESET}" choice | |
if [[ $choice =~ ^[1-$num_options]$ ]]; then | |
break | |
else | |
echo "${RED}Invalid choice. Please enter a number between 1 and $num_options.${RESET}" | |
fi | |
done | |
echo "$choice" | |
} | |
# loop over all questions and get user input | |
for question_with_options in "${questions[@]}"; do | |
question="${question_with_options%%:*}" | |
options="${question_with_options#*:}" | |
if [ -n "$options" ]; then | |
display_question "$question" "$options" | |
num_options=${#options_array[@]} | |
while true; do | |
choice=$(get_user_choice "$num_options") | |
selected_option="${options_array[$((choice - 1))]}" | |
if [ -n "$selected_option" ]; then | |
break | |
fi | |
done | |
choices+=("$selected_option") | |
else | |
read -rp "${CYAN}$question:${RESET} " user_response | |
choices+=("$user_response") | |
fi | |
echo "" | |
done | |
# generate pull request description | |
for ((i = 0; i < ${#questions[@]}; i++)); do | |
question="${questions[i]%%:*}" | |
if [ $i == 0 ]; then | |
description+="${choices[i]}" | |
else | |
description+=$'\n' | |
description+="**${question}:**" | |
description+=$'\n' | |
description+="${choices[i]}" | |
fi | |
description+=$'\n' | |
done | |
# open the pull request using description | |
if [ -n "$1" ]; then | |
hub pull-request -o -b $REMOTE:"$1" -m "${description}" | |
else | |
hub pull-request -o -b $REMOTE:develop -m "${description}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment