Forked from thakichowdhury/create-branch-from-ticket.py
Created
February 26, 2021 20:43
-
-
Save MichaelDimitras/8ad19aba0046104de6230ab611a88fa8 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`
This file contains 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
#!/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