-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Includes an auto_branch_prefix property #2
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,13 @@ | |
|
||
MAX_RESULT = 5 | ||
|
||
# Mapping of ticket types to branch prefixes | ||
TICKET_TYPE_TO_PREFIX = { | ||
"Story": "feature", | ||
"Task": "feature", | ||
"Bug": "bugfix", | ||
"Refactoring": "refactor" | ||
} | ||
|
||
def load_branches(): | ||
instance = os.environ.get("JIRA_INSTANCE") | ||
|
@@ -32,28 +39,42 @@ def load_branches(): | |
|
||
for issue in response['issues']: | ||
formatted = issue['key'] + " " + issue['fields']['summary'] | ||
formatted_branches.append(re.sub(r"[^a-zA-Z0-9]+", ' ', formatted)) | ||
formatted_summary = re.sub(r"[^a-zA-Z0-9]+", ' ', formatted) | ||
formatted_branches.append({ | ||
'summary': formatted_summary, | ||
'issuetype': issue['fields']['issuetype'] | ||
}) | ||
return formatted_branches[:MAX_RESULT] | ||
|
||
|
||
def main(prefix: Annotated[str, typer.Option(help="Prefix that is being used for the new branch.")] = "feature", | ||
no_prefix: Annotated[bool, typer.Option("--no-prefix", help="Will not use a prefix")] = False): | ||
no_prefix: Annotated[bool, typer.Option("--no-prefix", help="Will not use a prefix")] = False, | ||
auto_branch_prefix: Annotated[bool, typer.Option("--auto-branch-prefix", help="Automatically determine branch prefix based on ticket type")] = False): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you do https://github.com/freenowtech/git-jira/pull/2/files#r1477907772, I think you can drop this (=> only map if the variable is defined). |
||
""" | ||
CLI to switch to git branches based on one's JIRA tickets. | ||
CLI to switch to git branches based on one's JIRA tickets. | ||
|
||
If --prefix is used, it will add a specific prefix to the branch (e.g. feature -> "feature/") | ||
--no-prefix will omit the default "feature/" prefix. | ||
""" | ||
If --prefix is used, it will add a specific prefix to the branch (e.g. feature -> "feature/") | ||
--no-prefix will omit the default "feature/" prefix. | ||
--auto_branch_prefix will enable the ticket type to set the prefix: feature, bugfix, or refactor | ||
""" | ||
tasks = load_branches() | ||
terminal_menu = TerminalMenu(tasks) | ||
formatted_tasks = [f"{task['summary']}" for task in tasks] | ||
terminal_menu = TerminalMenu(formatted_tasks) | ||
menu_entry_index = terminal_menu.show() | ||
selected_task = tasks[menu_entry_index] | ||
prefix = None if no_prefix else prefix | ||
formatted_branch = format_branch(selected_task, prefix) | ||
print(f"Switching to branch: {formatted_branch}") | ||
process = subprocess.Popen(['git', 'switch', '-c', formatted_branch], | ||
stdout=subprocess.PIPE) | ||
process.communicate() | ||
if menu_entry_index is not None: | ||
selected_task = tasks[menu_entry_index] | ||
prefix = None if no_prefix else prefix | ||
if auto_branch_prefix: | ||
auto_prefix = TICKET_TYPE_TO_PREFIX.get(selected_task['issuetype']['name'], None) | ||
print(f"Ticket Type: {selected_task['issuetype']}") | ||
formatted_branch = format_branch(selected_task['summary'], auto_prefix) | ||
else: | ||
formatted_branch = format_branch(selected_task['summary'], prefix) | ||
print(f"Switching to branch: {formatted_branch}") | ||
process = subprocess.Popen(['git', 'switch', '-c', formatted_branch], | ||
stdout=subprocess.PIPE) | ||
process.communicate() | ||
else: | ||
print("No menu entry selected. Exiting.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! :) |
||
|
||
|
||
def format_branch(selected_task: str, prefix: Optional[str]): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd honestly move this to an environment variable, so everyone can define the mapping themselves (more extendable).
JIRA_TYPE_MAPPING="Story=feature,Task=feature"
https://github.com/sloria/environs can read these maps.