Skip to content

Instantly share code, notes, and snippets.

@thakichowdhury
Created February 18, 2021 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thakichowdhury/3e7615d1f0b3dd69a68a0187c4b92950 to your computer and use it in GitHub Desktop.
Save thakichowdhury/3e7615d1f0b3dd69a68a0187c4b92950 to your computer and use it in GitHub Desktop.
A script to help create git branches from ticket information in the format `issue_type/issue_key/issue-title-formatted-with-dashes`
#!/usr/bin/env python3
import re
import subprocess
from typing import List
## ------------ FUNCTIONS ------------ ##
def create_branch_from_ticket(issue_type: str, issue_key: str, issue_title: str) -> str:
# format issue_title to replace spaces with - and remove any non-alphanumeric chars
issue_title_formatted: str = re.sub('[^a-zA-Z0-9 ]', '', issue_title)
issue_title_formatted = re.sub(' ', '-', issue_title_formatted)
# concatentate the values into the final branch
branch_name: str = f'{issue_type}/{issue_key}/{issue_title_formatted}'
print(f'Checking out {branch_name}')
# create and checkout a new branch
subprocess.run(['git', 'checkout', '-b', branch_name])
## ------------ OPERATIONS ------------ ##
# list all the possible issue types
issue_types: List[str] = ['feature', 'bug', 'hotfix', 'documentation', 'devops']
print('Enter issue type index, e.g. 1, 2')
# print an enumerated list of issue types
for i in range(len(issue_types)):
issue: str = issue_types[i]
print(f'({i + 1}) {issue}')
# get the index of the issue type from user and assign the mapped value to issue_type
issue_type_index: int = int(input())
issue_type: str = issue_types[issue_type_index - 1]
print('\n')
# get the issue key from user input and make it all uppercase
print('Enter issue key, e.g. mob-27, wb-15')
issue_key: str = input().upper()
print('\n')
# get the issue title from user input and make it all lowercase
print('Enter issue title, e.g. Create deployment documentation')
issue_title: str = input().lower()
print('\n')
if __name__ == '__main__':
create_branch_from_ticket(issue_type, issue_key, issue_title)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment