Skip to content

Commit 2d6474b

Browse files
committed
script and readme
1 parent 951d57f commit 2d6474b

File tree

2 files changed

+171
-1
lines changed

2 files changed

+171
-1
lines changed

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,34 @@
11
# github-issue-to-branch
2-
A script that uses the Github CLI `gh` to automate creation of new branches using an issue number.
2+
3+
A script that uses the Github CLI `gh` to automatically create and name new branches using an issue number.
4+
5+
## Usage
6+
7+
```bash
8+
ghib <issue_number1> [<issue_number2> ...] [postfix]
9+
```
10+
11+
## Example
12+
13+
```bash
14+
# Assume the there in issue 123 with the title "implement feature x".
15+
# A git branch named `123-implement-feature-x` will be created.
16+
ghib 123
17+
18+
# You can also specify multiple issues
19+
ghib 123 124 125
20+
21+
# And you can add a postfix to the branch name
22+
ghib 123 testing
23+
# The above command will create a branch named `123-implement-feature-x-testing`
24+
```
25+
26+
## Installation
27+
28+
```bash
29+
# make the script executable
30+
chmod +x ghib.sh
31+
32+
# automatically add the script to an alias in your shell
33+
./ghib.sh --setup
34+
```

ghib.sh

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#!/bin/bash
2+
3+
# Check if a command exists
4+
command_exists() {
5+
command -v "$1" >/dev/null 2>&1
6+
}
7+
8+
# Check if required commands exist
9+
check_dependencies() {
10+
local deps=("gh" "jq" "iconv" "git")
11+
local missing_deps=()
12+
13+
for dep in "${deps[@]}"; do
14+
command_exists "$dep" || missing_deps+=("$dep")
15+
done
16+
17+
if [ ${#missing_deps[@]} -gt 0 ]; then
18+
echo "Error: The following required commands are missing:"
19+
for dep in "${missing_deps[@]}"; do
20+
echo " $dep"
21+
done
22+
echo "Please install them and try again."
23+
exit 1
24+
fi
25+
}
26+
27+
# Function to slugify a string
28+
slugify() {
29+
echo "$1" | iconv -t ascii//TRANSLIT | sed -E 's/[^a-zA-Z0-9]+/-/g' | sed -E 's/^-*//;s/-*$//;s/-+/-/g' | tr A-Z a-z
30+
}
31+
32+
# Function to create a new git branch
33+
create_branch() {
34+
branch_name="$1"
35+
git checkout -b "$branch_name"
36+
}
37+
38+
# Function to add alias to .bashrc or .zshrc
39+
add_alias_to_rc() {
40+
local rc_file alias_name script_path
41+
script_path="$(realpath "$0")" # Get the full path of the script
42+
43+
if [ -f "$HOME/.zshrc" ]; then
44+
rc_file="$HOME/.zshrc"
45+
elif [ -f "$HOME/.bashrc" ]; then
46+
rc_file="$HOME/.bashrc"
47+
else
48+
echo "Error: Neither .bashrc nor .zshrc found in your home directory."
49+
exit 1
50+
fi
51+
52+
# Prompt the user for a custom alias
53+
read -rp "Enter your preferred alias (default is 'ghib'): " alias_name
54+
alias_name="${alias_name:-ghib}"
55+
56+
# Prompt the user
57+
read -p "Do you want to add the alias '$alias_name' to your shell configuration file? (y/n): " confirm
58+
if [ "$confirm" == "y" ] || [ "$confirm" == "Y" ] || [ -z "$confirm" ]; then
59+
echo "alias $alias_name='$script_path'" >> "$rc_file"
60+
echo "Alias added. You can now use '$alias_name' command to run this script."
61+
echo "Please run 'source $rc_file' to activate the changes or open a new terminal session."
62+
elif [ "$confirm" == "n" ] || [ "$confirm" == "N" ]; then
63+
echo "Alias not added. You can manually add the alias '$alias_name' to your shell configuration file if needed."
64+
else
65+
echo "Invalid input. Alias not added. You can manually add the alias '$alias_name' to your shell configuration file if needed."
66+
fi
67+
}
68+
69+
# Error message function
70+
error() {
71+
echo "Error: $1"
72+
exit 1
73+
}
74+
75+
# Check if required commands exist
76+
check_dependencies
77+
78+
# Check if --setup flag is provided
79+
if [ "$1" == "--setup" ]; then
80+
add_alias_to_rc "$2"
81+
exit 0
82+
fi
83+
84+
# Check if at least one argument is provided
85+
[ $# -lt 1 ] && error "Usage: ghib [--setup [alias]] <issue_number1> [<issue_number2> ...] [postfix]"
86+
87+
# Extract postfix (if provided)
88+
postfix=""
89+
if [[ ! $@ =~ ^[0-9]+$ ]]; then
90+
postfix="${@: -1}"
91+
if [[ "$postfix" =~ ^[0-9]+$ ]]; then
92+
unset 'ARGV[$#]'
93+
postfix=""
94+
fi
95+
fi
96+
97+
# Extract issue numbers from arguments and sort them in ascending order
98+
sorted_issue_numbers=($(for arg; do [[ "$arg" =~ ^[0-9]+$ ]] && echo "$arg"; done | sort -n))
99+
100+
# Check if there are any issue numbers provided
101+
[ ${#sorted_issue_numbers[@]} -eq 0 ] && error "No valid issue numbers provided."
102+
103+
# Retrieve the names of the GitHub issues
104+
issue_names=""
105+
for issue_number in "${sorted_issue_numbers[@]}"; do
106+
issue_name=$(gh issue view "$issue_number" --json title 2>/dev/null | jq -r '.title')
107+
[ -z "$issue_name" ] && echo "Error: Issue $issue_number not found." && continue
108+
if [ -z "$issue_names" ]; then
109+
issue_names="$issue_name"
110+
else
111+
issue_names+=" and $issue_name"
112+
fi
113+
done
114+
115+
# Slugify and lowercase the branch name
116+
slugified_branch=$(slugify "${sorted_issue_numbers[*]}")-$(slugify "${issue_names[*]}")
117+
118+
# Add postfix if provided
119+
if [ -n "$postfix" ]; then
120+
slugified_branch+=" $postfix"
121+
fi
122+
123+
slugified_branch=$(slugify "$slugified_branch")
124+
125+
# Remove trailing hyphen if there's no postfix
126+
slugified_branch=${slugified_branch%-}
127+
128+
# Show the proposed branch name to the user
129+
echo "Proposed branch name: $slugified_branch"
130+
131+
# Prompt the user for confirmation
132+
read -rp "Do you want to create this branch? (y/n): " confirm
133+
[[ "$confirm" != [Yy] ]] && exit 0
134+
135+
# Create a new git branch
136+
create_branch "$slugified_branch"
137+
138+
echo "Created branch '$slugified_branch'"

0 commit comments

Comments
 (0)